fix: rework UploadFolder implementation

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/51010/head
Robin Appelman 2 months ago
parent 373107b6e4
commit 98af649ccc
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
  1. 5
      apps/dav/lib/RootCollection.php
  2. 6
      apps/dav/lib/Upload/RootCollection.php
  3. 54
      apps/dav/lib/Upload/UploadHome.php

@ -157,7 +157,10 @@ class RootCollection extends SimpleCollection {
$uploadCollection = new Upload\RootCollection( $uploadCollection = new Upload\RootCollection(
$userPrincipalBackend, $userPrincipalBackend,
'principals/users', 'principals/users',
Server::get(CleanupService::class)); Server::get(CleanupService::class),
$rootFolder,
$userSession,
);
$uploadCollection->disableListing = $disableListing; $uploadCollection->disableListing = $disableListing;
$avatarCollection = new Avatars\RootCollection($userPrincipalBackend, 'principals/users'); $avatarCollection = new Avatars\RootCollection($userPrincipalBackend, 'principals/users');

@ -9,6 +9,8 @@ declare(strict_types=1);
*/ */
namespace OCA\DAV\Upload; namespace OCA\DAV\Upload;
use OCP\Files\IRootFolder;
use OCP\IUserSession;
use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\AbstractPrincipalCollection;
use Sabre\DAVACL\PrincipalBackend; use Sabre\DAVACL\PrincipalBackend;
@ -18,6 +20,8 @@ class RootCollection extends AbstractPrincipalCollection {
PrincipalBackend\BackendInterface $principalBackend, PrincipalBackend\BackendInterface $principalBackend,
string $principalPrefix, string $principalPrefix,
private CleanupService $cleanupService, private CleanupService $cleanupService,
private IRootFolder $rootFolder,
private IUserSession $userSession,
) { ) {
parent::__construct($principalBackend, $principalPrefix); parent::__construct($principalBackend, $principalPrefix);
} }
@ -26,7 +30,7 @@ class RootCollection extends AbstractPrincipalCollection {
* @inheritdoc * @inheritdoc
*/ */
public function getChildForPrincipal(array $principalInfo): UploadHome { public function getChildForPrincipal(array $principalInfo): UploadHome {
return new UploadHome($principalInfo, $this->cleanupService); return new UploadHome($principalInfo, $this->cleanupService, $this->rootFolder, $this->userSession);
} }
/** /**

@ -7,18 +7,23 @@
*/ */
namespace OCA\DAV\Upload; namespace OCA\DAV\Upload;
use OC\Files\Filesystem;
use OC\Files\View; use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\Directory;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Server;
use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\ICollection; use Sabre\DAV\ICollection;
class UploadHome implements ICollection { class UploadHome implements ICollection {
private ?Folder $uploadFolder = null;
public function __construct( public function __construct(
private array $principalInfo, private readonly array $principalInfo,
private CleanupService $cleanupService, private readonly CleanupService $cleanupService,
private readonly IRootFolder $rootFolder,
private readonly IUserSession $userSession,
) { ) {
} }
@ -64,28 +69,33 @@ class UploadHome implements ICollection {
return $this->impl()->getLastModified(); return $this->impl()->getLastModified();
} }
/** private function getUploadFolder(): Folder {
* @return Directory if ($this->uploadFolder === null) {
*/ $user = $this->userSession->getUser();
private function impl() { if (!$user) {
$view = $this->getView(); throw new Forbidden('Not logged in');
$rootInfo = $view->getFileInfo(''); }
return new Directory($view, $rootInfo); $path = '/' . $user->getUID() . '/uploads';
try {
$folder = $this->rootFolder->get($path);
if (!$folder instanceof Folder) {
throw new \Exception('Upload folder is a file');
}
$this->uploadFolder = $folder;
} catch (NotFoundException $e) {
$this->uploadFolder = $this->rootFolder->newFolder($path);
}
}
return $this->uploadFolder;
} }
private function getView() { private function impl(): Directory {
$rootView = new View(); $folder = $this->getUploadFolder();
$user = Server::get(IUserSession::class)->getUser(); $view = new View($folder->getPath());
Filesystem::initMountPoints($user->getUID()); return new Directory($view, $folder);
if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
$rootView->mkdir('/' . $user->getUID() . '/uploads');
}
return new View('/' . $user->getUID() . '/uploads');
} }
private function getStorage() { private function getStorage() {
$view = $this->getView(); return $this->getUploadFolder()->getStorage();
$storage = $view->getFileInfo('')->getStorage();
return $storage;
} }
} }

Loading…
Cancel
Save