Skip to content

Commit 24ed04b

Browse files
committed
fix: rework UploadFolder implementation
Signed-off-by: Robin Appelman <robin@icewind.nl> [skip ci]
1 parent 6a8bccc commit 24ed04b

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

apps/dav/lib/RootCollection.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ public function __construct() {
172172
$uploadCollection = new Upload\RootCollection(
173173
$userPrincipalBackend,
174174
'principals/users',
175-
\OC::$server->query(CleanupService::class));
175+
\OC::$server->query(CleanupService::class),
176+
$rootFolder,
177+
$userSession,
178+
);
176179
$uploadCollection->disableListing = $disableListing;
177180

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

apps/dav/lib/Upload/RootCollection.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@
2626
*/
2727
namespace OCA\DAV\Upload;
2828

29+
use OCP\Files\IRootFolder;
30+
use OCP\IUserSession;
2931
use Sabre\DAVACL\AbstractPrincipalCollection;
3032
use Sabre\DAVACL\PrincipalBackend;
3133

3234
class RootCollection extends AbstractPrincipalCollection {
33-
34-
/** @var CleanupService */
35-
private $cleanupService;
36-
3735
public function __construct(PrincipalBackend\BackendInterface $principalBackend,
3836
string $principalPrefix,
39-
CleanupService $cleanupService) {
37+
private CleanupService $cleanupService,
38+
private IRootFolder $rootFolder,
39+
private IUserSession $userSession,
40+
) {
4041
parent::__construct($principalBackend, $principalPrefix);
41-
$this->cleanupService = $cleanupService;
4242
}
4343

4444
/**
4545
* @inheritdoc
4646
*/
4747
public function getChildForPrincipal(array $principalInfo): UploadHome {
48-
return new UploadHome($principalInfo, $this->cleanupService);
48+
return new UploadHome($principalInfo, $this->cleanupService, $this->rootFolder, $this->userSession);
4949
}
5050

5151
/**

apps/dav/lib/Upload/UploadHome.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@
2525
*/
2626
namespace OCA\DAV\Upload;
2727

28-
use OC\Files\Filesystem;
2928
use OC\Files\View;
3029
use OCA\DAV\Connector\Sabre\Directory;
30+
use OCP\Files\Folder;
31+
use OCP\Files\IRootFolder;
32+
use OCP\Files\NotFoundException;
33+
use OCP\IUserSession;
3134
use Sabre\DAV\Exception\Forbidden;
3235
use Sabre\DAV\ICollection;
3336

3437
class UploadHome implements ICollection {
35-
/** @var array */
36-
private $principalInfo;
37-
/** @var CleanupService */
38-
private $cleanupService;
39-
40-
public function __construct(array $principalInfo, CleanupService $cleanupService) {
41-
$this->principalInfo = $principalInfo;
42-
$this->cleanupService = $cleanupService;
38+
private ?Folder $uploadFolder = null;
39+
40+
public function __construct(
41+
private array $principalInfo,
42+
private CleanupService $cleanupService,
43+
private IRootFolder $rootFolder,
44+
private IUserSession $userSession,
45+
) {
4346
}
4447

4548
public function createFile($name, $data = null) {
@@ -84,28 +87,33 @@ public function getLastModified() {
8487
return $this->impl()->getLastModified();
8588
}
8689

87-
/**
88-
* @return Directory
89-
*/
90-
private function impl() {
91-
$view = $this->getView();
92-
$rootInfo = $view->getFileInfo('');
93-
return new Directory($view, $rootInfo);
90+
private function getUploadFolder(): Folder {
91+
if ($this->uploadFolder === null) {
92+
$user = $this->userSession->getUser();
93+
if (!$user) {
94+
throw new Forbidden('Not logged in');
95+
}
96+
$path = '/' . $user->getUID() . '/uploads';
97+
try {
98+
$folder = $this->rootFolder->get($path);
99+
if (!$folder instanceof Folder) {
100+
throw new \Exception('Upload folder is a file');
101+
}
102+
$this->uploadFolder = $folder;
103+
} catch (NotFoundException $e) {
104+
$this->uploadFolder = $this->rootFolder->newFolder($path);
105+
}
106+
}
107+
return $this->uploadFolder;
94108
}
95109

96-
private function getView() {
97-
$rootView = new View();
98-
$user = \OC::$server->getUserSession()->getUser();
99-
Filesystem::initMountPoints($user->getUID());
100-
if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
101-
$rootView->mkdir('/' . $user->getUID() . '/uploads');
102-
}
103-
return new View('/' . $user->getUID() . '/uploads');
110+
private function impl(): Directory {
111+
$folder = $this->getUploadFolder();
112+
$view = new View($folder->getPath());
113+
return new Directory($view, $folder);
104114
}
105115

106116
private function getStorage() {
107-
$view = $this->getView();
108-
$storage = $view->getFileInfo('')->getStorage();
109-
return $storage;
117+
return $this->getUploadFolder()->getStorage();
110118
}
111119
}

0 commit comments

Comments
 (0)