1
0

LazyUserFolder.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Files\FileInfo;
  24. use OCP\Constants;
  25. use OCP\Files\IRootFolder;
  26. use OCP\Files\Mount\IMountManager;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\Folder;
  29. use OCP\Files\File;
  30. use OCP\IUser;
  31. use Psr\Log\LoggerInterface;
  32. class LazyUserFolder extends LazyFolder {
  33. private IRootFolder $root;
  34. private IUser $user;
  35. private string $path;
  36. private IMountManager $mountManager;
  37. public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
  38. $this->root = $rootFolder;
  39. $this->user = $user;
  40. $this->mountManager = $mountManager;
  41. $this->path = '/' . $user->getUID() . '/files';
  42. parent::__construct(function () use ($user): Folder {
  43. try {
  44. $node = $this->root->get($this->path);
  45. if ($node instanceof File) {
  46. $e = new \RuntimeException();
  47. \OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [
  48. 'exception' => $e,
  49. ]);
  50. throw $e;
  51. }
  52. return $node;
  53. } catch (NotFoundException $e) {
  54. if (!$this->root->nodeExists('/' . $user->getUID())) {
  55. $this->root->newFolder('/' . $user->getUID());
  56. }
  57. return $this->root->newFolder($this->path);
  58. }
  59. }, [
  60. 'path' => $this->path,
  61. 'permissions' => Constants::PERMISSION_ALL,
  62. 'type' => FileInfo::TYPE_FOLDER,
  63. 'mimetype' => FileInfo::MIMETYPE_FOLDER,
  64. ]);
  65. }
  66. public function get($path) {
  67. return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
  68. }
  69. /**
  70. * @param int $id
  71. * @return \OCP\Files\Node[]
  72. */
  73. public function getById($id) {
  74. return $this->root->getByIdInPath((int)$id, $this->getPath());
  75. }
  76. public function getMountPoint() {
  77. if ($this->folder !== null) {
  78. return $this->folder->getMountPoint();
  79. }
  80. $mountPoint = $this->mountManager->find('/' . $this->user->getUID());
  81. if (is_null($mountPoint)) {
  82. throw new \Exception("No mountpoint for user folder");
  83. }
  84. return $mountPoint;
  85. }
  86. }