Skip to content

Commit 98af649

Browse files
committed
fix: rework UploadFolder implementation
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 373107b commit 98af649

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

apps/dav/lib/RootCollection.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ public function __construct() {
157157
$uploadCollection = new Upload\RootCollection(
158158
$userPrincipalBackend,
159159
'principals/users',
160-
Server::get(CleanupService::class));
160+
Server::get(CleanupService::class),
161+
$rootFolder,
162+
$userSession,
163+
);
161164
$uploadCollection->disableListing = $disableListing;
162165

163166
$avatarCollection = new Avatars\RootCollection($userPrincipalBackend, 'principals/users');

apps/dav/lib/Upload/RootCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace OCA\DAV\Upload;
1111

12+
use OCP\Files\IRootFolder;
13+
use OCP\IUserSession;
1214
use Sabre\DAVACL\AbstractPrincipalCollection;
1315
use Sabre\DAVACL\PrincipalBackend;
1416

@@ -18,6 +20,8 @@ public function __construct(
1820
PrincipalBackend\BackendInterface $principalBackend,
1921
string $principalPrefix,
2022
private CleanupService $cleanupService,
23+
private IRootFolder $rootFolder,
24+
private IUserSession $userSession,
2125
) {
2226
parent::__construct($principalBackend, $principalPrefix);
2327
}
@@ -26,7 +30,7 @@ public function __construct(
2630
* @inheritdoc
2731
*/
2832
public function getChildForPrincipal(array $principalInfo): UploadHome {
29-
return new UploadHome($principalInfo, $this->cleanupService);
33+
return new UploadHome($principalInfo, $this->cleanupService, $this->rootFolder, $this->userSession);
3034
}
3135

3236
/**

apps/dav/lib/Upload/UploadHome.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
*/
88
namespace OCA\DAV\Upload;
99

10-
use OC\Files\Filesystem;
1110
use OC\Files\View;
1211
use OCA\DAV\Connector\Sabre\Directory;
12+
use OCP\Files\Folder;
13+
use OCP\Files\IRootFolder;
14+
use OCP\Files\NotFoundException;
1315
use OCP\IUserSession;
14-
use OCP\Server;
1516
use Sabre\DAV\Exception\Forbidden;
1617
use Sabre\DAV\ICollection;
1718

1819
class UploadHome implements ICollection {
20+
private ?Folder $uploadFolder = null;
21+
1922
public function __construct(
20-
private array $principalInfo,
21-
private CleanupService $cleanupService,
23+
private readonly array $principalInfo,
24+
private readonly CleanupService $cleanupService,
25+
private readonly IRootFolder $rootFolder,
26+
private readonly IUserSession $userSession,
2227
) {
2328
}
2429

@@ -64,28 +69,33 @@ public function getLastModified() {
6469
return $this->impl()->getLastModified();
6570
}
6671

67-
/**
68-
* @return Directory
69-
*/
70-
private function impl() {
71-
$view = $this->getView();
72-
$rootInfo = $view->getFileInfo('');
73-
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;
7490
}
7591

76-
private function getView() {
77-
$rootView = new View();
78-
$user = Server::get(IUserSession::class)->getUser();
79-
Filesystem::initMountPoints($user->getUID());
80-
if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
81-
$rootView->mkdir('/' . $user->getUID() . '/uploads');
82-
}
83-
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);
8496
}
8597

8698
private function getStorage() {
87-
$view = $this->getView();
88-
$storage = $view->getFileInfo('')->getStorage();
89-
return $storage;
99+
return $this->getUploadFolder()->getStorage();
90100
}
91101
}

0 commit comments

Comments
 (0)