LazyUserFolder.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. namespace OC\Files\Node;
  9. use OCP\Constants;
  10. use OCP\Files\File;
  11. use OCP\Files\FileInfo;
  12. use OCP\Files\Folder;
  13. use OCP\Files\IRootFolder;
  14. use OCP\Files\Mount\IMountManager;
  15. use OCP\Files\NotFoundException;
  16. use OCP\IUser;
  17. use Psr\Log\LoggerInterface;
  18. class LazyUserFolder extends LazyFolder {
  19. private IUser $user;
  20. private string $path;
  21. private IMountManager $mountManager;
  22. public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
  23. $this->user = $user;
  24. $this->mountManager = $mountManager;
  25. $this->path = '/' . $user->getUID() . '/files';
  26. parent::__construct($rootFolder, function () use ($user): Folder {
  27. try {
  28. $node = $this->getRootFolder()->get($this->path);
  29. if ($node instanceof File) {
  30. $e = new \RuntimeException();
  31. \OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [
  32. 'exception' => $e,
  33. ]);
  34. throw $e;
  35. }
  36. return $node;
  37. } catch (NotFoundException $e) {
  38. if (!$this->getRootFolder()->nodeExists('/' . $user->getUID())) {
  39. $this->getRootFolder()->newFolder('/' . $user->getUID());
  40. }
  41. return $this->getRootFolder()->newFolder($this->path);
  42. }
  43. }, [
  44. 'path' => $this->path,
  45. // Sharing user root folder is not allowed
  46. 'permissions' => Constants::PERMISSION_ALL ^ Constants::PERMISSION_SHARE,
  47. 'type' => FileInfo::TYPE_FOLDER,
  48. 'mimetype' => FileInfo::MIMETYPE_FOLDER,
  49. ]);
  50. }
  51. public function getMountPoint() {
  52. if ($this->folder !== null) {
  53. return $this->folder->getMountPoint();
  54. }
  55. $mountPoint = $this->mountManager->find('/' . $this->user->getUID());
  56. if (is_null($mountPoint)) {
  57. throw new \Exception('No mountpoint for user folder');
  58. }
  59. return $mountPoint;
  60. }
  61. }