diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index d9ba4c3e2a6cb..57187ff35dedc 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -149,7 +149,10 @@ public function __construct() { $uploadCollection = new Upload\RootCollection( $userPrincipalBackend, 'principals/users', - \OC::$server->query(CleanupService::class)); + \OC::$server->get(CleanupService::class), + $rootFolder, + $userSession, + ); $uploadCollection->disableListing = $disableListing; $avatarCollection = new Avatars\RootCollection($userPrincipalBackend, 'principals/users'); diff --git a/apps/dav/lib/Upload/RootCollection.php b/apps/dav/lib/Upload/RootCollection.php index 1adab40671b8d..9ea2592702b24 100644 --- a/apps/dav/lib/Upload/RootCollection.php +++ b/apps/dav/lib/Upload/RootCollection.php @@ -9,26 +9,28 @@ */ namespace OCA\DAV\Upload; +use OCP\Files\IRootFolder; +use OCP\IUserSession; use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\PrincipalBackend; class RootCollection extends AbstractPrincipalCollection { - /** @var CleanupService */ - private $cleanupService; - - public function __construct(PrincipalBackend\BackendInterface $principalBackend, + public function __construct( + PrincipalBackend\BackendInterface $principalBackend, string $principalPrefix, - CleanupService $cleanupService) { + private CleanupService $cleanupService, + private IRootFolder $rootFolder, + private IUserSession $userSession, + ) { parent::__construct($principalBackend, $principalPrefix); - $this->cleanupService = $cleanupService; } /** * @inheritdoc */ public function getChildForPrincipal(array $principalInfo): UploadHome { - return new UploadHome($principalInfo, $this->cleanupService); + return new UploadHome($principalInfo, $this->cleanupService, $this->rootFolder, $this->userSession); } /** diff --git a/apps/dav/lib/Upload/UploadHome.php b/apps/dav/lib/Upload/UploadHome.php index 3e7e3c6c986a8..a6551d4d079fe 100644 --- a/apps/dav/lib/Upload/UploadHome.php +++ b/apps/dav/lib/Upload/UploadHome.php @@ -7,21 +7,24 @@ */ namespace OCA\DAV\Upload; -use OC\Files\Filesystem; use OC\Files\View; use OCA\DAV\Connector\Sabre\Directory; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IUserSession; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\ICollection; class UploadHome implements ICollection { - /** @var array */ - private $principalInfo; - /** @var CleanupService */ - private $cleanupService; - - public function __construct(array $principalInfo, CleanupService $cleanupService) { - $this->principalInfo = $principalInfo; - $this->cleanupService = $cleanupService; + private ?Folder $uploadFolder = null; + + public function __construct( + private readonly array $principalInfo, + private readonly CleanupService $cleanupService, + private readonly IRootFolder $rootFolder, + private readonly IUserSession $userSession, + ) { } public function createFile($name, $data = null) { @@ -66,28 +69,33 @@ public function getLastModified() { return $this->impl()->getLastModified(); } - /** - * @return Directory - */ - private function impl() { - $view = $this->getView(); - $rootInfo = $view->getFileInfo(''); - return new Directory($view, $rootInfo); + private function getUploadFolder(): Folder { + if ($this->uploadFolder === null) { + $user = $this->userSession->getUser(); + if (!$user) { + throw new Forbidden('Not logged in'); + } + $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() { - $rootView = new View(); - $user = \OC::$server->getUserSession()->getUser(); - Filesystem::initMountPoints($user->getUID()); - if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { - $rootView->mkdir('/' . $user->getUID() . '/uploads'); - } - return new View('/' . $user->getUID() . '/uploads'); + private function impl(): Directory { + $folder = $this->getUploadFolder(); + $view = new View($folder->getPath()); + return new Directory($view, $folder); } private function getStorage() { - $view = $this->getView(); - $storage = $view->getFileInfo('')->getStorage(); - return $storage; + return $this->getUploadFolder()->getStorage(); } }