|
7 | 7 | */ |
8 | 8 | namespace OCA\DAV\Upload; |
9 | 9 |
|
10 | | -use OC\Files\Filesystem; |
11 | 10 | use OC\Files\View; |
12 | 11 | use OCA\DAV\Connector\Sabre\Directory; |
| 12 | +use OCP\Files\Folder; |
| 13 | +use OCP\Files\IRootFolder; |
| 14 | +use OCP\Files\NotFoundException; |
| 15 | +use OCP\IUserSession; |
13 | 16 | use Sabre\DAV\Exception\Forbidden; |
14 | 17 | use Sabre\DAV\ICollection; |
15 | 18 |
|
16 | 19 | class UploadHome implements ICollection { |
| 20 | + private ?Folder $uploadFolder = null; |
| 21 | + |
17 | 22 | public function __construct( |
18 | | - private array $principalInfo, |
19 | | - private CleanupService $cleanupService, |
| 23 | + private readonly array $principalInfo, |
| 24 | + private readonly CleanupService $cleanupService, |
| 25 | + private readonly IRootFolder $rootFolder, |
| 26 | + private readonly IUserSession $userSession, |
20 | 27 | ) { |
21 | 28 | } |
22 | 29 |
|
@@ -62,28 +69,33 @@ public function getLastModified() { |
62 | 69 | return $this->impl()->getLastModified(); |
63 | 70 | } |
64 | 71 |
|
65 | | - /** |
66 | | - * @return Directory |
67 | | - */ |
68 | | - private function impl() { |
69 | | - $view = $this->getView(); |
70 | | - $rootInfo = $view->getFileInfo(''); |
71 | | - return new Directory($view, $rootInfo); |
| 72 | + private function getUploadFolder(): Folder { |
| 73 | + if ($this->uploadFolder === null) { |
| 74 | + $user = $this->userSession->getUser(); |
| 75 | + if (!$user) { |
| 76 | + throw new Forbidden('Not logged in'); |
| 77 | + } |
| 78 | + $path = '/' . $user->getUID() . '/uploads'; |
| 79 | + try { |
| 80 | + $folder = $this->rootFolder->get($path); |
| 81 | + if (!$folder instanceof Folder) { |
| 82 | + throw new \Exception('Upload folder is a file'); |
| 83 | + } |
| 84 | + $this->uploadFolder = $folder; |
| 85 | + } catch (NotFoundException $e) { |
| 86 | + $this->uploadFolder = $this->rootFolder->newFolder($path); |
| 87 | + } |
| 88 | + } |
| 89 | + return $this->uploadFolder; |
72 | 90 | } |
73 | 91 |
|
74 | | - private function getView() { |
75 | | - $rootView = new View(); |
76 | | - $user = \OC::$server->getUserSession()->getUser(); |
77 | | - Filesystem::initMountPoints($user->getUID()); |
78 | | - if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { |
79 | | - $rootView->mkdir('/' . $user->getUID() . '/uploads'); |
80 | | - } |
81 | | - return new View('/' . $user->getUID() . '/uploads'); |
| 92 | + private function impl(): Directory { |
| 93 | + $folder = $this->getUploadFolder(); |
| 94 | + $view = new View($folder->getPath()); |
| 95 | + return new Directory($view, $folder); |
82 | 96 | } |
83 | 97 |
|
84 | 98 | private function getStorage() { |
85 | | - $view = $this->getView(); |
86 | | - $storage = $view->getFileInfo('')->getStorage(); |
87 | | - return $storage; |
| 99 | + return $this->getUploadFolder()->getStorage(); |
88 | 100 | } |
89 | 101 | } |
0 commit comments