LazyUserFolder.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\Files\Node;
  23. use OCP\Constants;
  24. use OCP\Files\File;
  25. use OCP\Files\FileInfo;
  26. use OCP\Files\Folder;
  27. use OCP\Files\IRootFolder;
  28. use OCP\Files\Mount\IMountManager;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IUser;
  31. use Psr\Log\LoggerInterface;
  32. class LazyUserFolder extends LazyFolder {
  33. private IUser $user;
  34. private string $path;
  35. private IMountManager $mountManager;
  36. public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
  37. $this->user = $user;
  38. $this->mountManager = $mountManager;
  39. $this->path = '/' . $user->getUID() . '/files';
  40. parent::__construct($rootFolder, function () use ($user): Folder {
  41. try {
  42. $node = $this->getRootFolder()->get($this->path);
  43. if ($node instanceof File) {
  44. $e = new \RuntimeException();
  45. \OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [
  46. 'exception' => $e,
  47. ]);
  48. throw $e;
  49. }
  50. return $node;
  51. } catch (NotFoundException $e) {
  52. if (!$this->getRootFolder()->nodeExists('/' . $user->getUID())) {
  53. $this->getRootFolder()->newFolder('/' . $user->getUID());
  54. }
  55. return $this->getRootFolder()->newFolder($this->path);
  56. }
  57. }, [
  58. 'path' => $this->path,
  59. // Sharing user root folder is not allowed
  60. 'permissions' => Constants::PERMISSION_ALL ^ Constants::PERMISSION_SHARE,
  61. 'type' => FileInfo::TYPE_FOLDER,
  62. 'mimetype' => FileInfo::MIMETYPE_FOLDER,
  63. ]);
  64. }
  65. public function getMountPoint() {
  66. if ($this->folder !== null) {
  67. return $this->folder->getMountPoint();
  68. }
  69. $mountPoint = $this->mountManager->find('/' . $this->user->getUID());
  70. if (is_null($mountPoint)) {
  71. throw new \Exception("No mountpoint for user folder");
  72. }
  73. return $mountPoint;
  74. }
  75. }