Skip to content

Commit 3b2c87b

Browse files
icewind1991AndyScherzinger
authored andcommitted
fix: rework UploadFolder implementation
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 2f3ceec commit 3b2c87b

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

apps/dav/lib/RootCollection.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public function __construct() {
149149
$uploadCollection = new Upload\RootCollection(
150150
$userPrincipalBackend,
151151
'principals/users',
152-
\OC::$server->query(CleanupService::class));
152+
\OC::$server->query(CleanupService::class),
153+
$rootFolder,
154+
$userSession,
155+
);
153156
$uploadCollection->disableListing = $disableListing;
154157

155158
$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: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +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;
15+
use OCP\IUserSession;
1316
use Sabre\DAV\Exception\Forbidden;
1417
use Sabre\DAV\ICollection;
1518

1619
class UploadHome implements ICollection {
20+
private ?Folder $uploadFolder = null;
21+
1722
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,
2027
) {
2128
}
2229

@@ -62,28 +69,33 @@ public function getLastModified() {
6269
return $this->impl()->getLastModified();
6370
}
6471

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;
7290
}
7391

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);
8296
}
8397

8498
private function getStorage() {
85-
$view = $this->getView();
86-
$storage = $view->getFileInfo('')->getStorage();
87-
return $storage;
99+
return $this->getUploadFolder()->getStorage();
88100
}
89101
}

0 commit comments

Comments
 (0)