diff options
author | Louis Chemineau <louis@chmn.me> | 2022-09-28 11:01:12 +0200 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2022-10-10 12:23:52 +0200 |
commit | a2890b03e795184863e4252fd535fa6037b86235 (patch) | |
tree | 88bae5eae2c006c0d31daf01e499847338b405d0 /lib | |
parent | dc44603b7c33e502049b47b9b10c251c758235ef (diff) |
Refactor getting album storage folder
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Sabre/Album/AlbumRoot.php | 15 | ||||
-rw-r--r-- | lib/Sabre/Album/PublicAlbumRoot.php | 37 | ||||
-rw-r--r-- | lib/Service/UserConfigService.php | 10 |
3 files changed, 19 insertions, 43 deletions
diff --git a/lib/Sabre/Album/AlbumRoot.php b/lib/Sabre/Album/AlbumRoot.php index db1f1775..f63f8823 100644 --- a/lib/Sabre/Album/AlbumRoot.php +++ b/lib/Sabre/Album/AlbumRoot.php @@ -80,6 +80,12 @@ class AlbumRoot implements ICollection, ICopyTarget { $this->albumMapper->rename($this->album->getAlbum()->getId(), $name); } + protected function getPhotosLocationInfo() { + $photosLocation = $this->userConfigService->getUserConfig('photosLocation'); + $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); + return [$photosLocation, $userFolder]; + } + /** * We cannot create files in an Album * We add the file to the default Photos folder and then link it there. @@ -90,10 +96,15 @@ class AlbumRoot implements ICollection, ICopyTarget { */ public function createFile($name, $data = null) { try { - // userConfigService->getUserConfig handle the path creation if missing - $photosLocation = $this->userConfigService->getUserConfig('photosLocation'); + [$photosLocation, $userFolder] = $this->getPhotosLocationInfo(); + + // If the folder does not exists, create it. + if (!$userFolder->nodeExists($photosLocation)) { + return $userFolder->newFolder($photosLocation); + } $photosFolder = $this->userFolder->get($photosLocation); + if (!($photosFolder instanceof Folder)) { throw new Conflict('The destination exists and is not a folder'); } diff --git a/lib/Sabre/Album/PublicAlbumRoot.php b/lib/Sabre/Album/PublicAlbumRoot.php index 19cff6bb..ab4e6fc4 100644 --- a/lib/Sabre/Album/PublicAlbumRoot.php +++ b/lib/Sabre/Album/PublicAlbumRoot.php @@ -47,38 +47,11 @@ class PublicAlbumRoot extends AlbumRoot { throw new Forbidden('Not allowed to copy into a public album'); } - /** - * We cannot create files in an Album - * We add the file to the default Photos folder and then link it there. - * - * @param string $name - * @param null|resource|string $data - * @return null - */ - public function createFile($name, $data = null) { - // TODO: implement public album upload - throw new Forbidden('Not allowed to create a file in a public album'); - - try { - $albumOwner = $this->album->getAlbum()->getUserId(); - $photosLocation = $this->userConfigService->getConfigForUser($albumOwner, 'photosLocation'); - $photosFolder = $this->rootFolder->getUserFolder($albumOwner)->get($photosLocation); - if (!($photosFolder instanceof Folder)) { - throw new Conflict('The destination exists and is not a folder'); - } - - // Check for conflict and rename the file accordingly - $newName = \basename(\OC_Helper::buildNotExistingFileName($photosLocation, $name)); - - $node = $photosFolder->newFile($newName, $data); - $this->addFile($node->getId(), $node->getOwner()->getUID()); - // Cheating with header because we are using fileID-fileName - // https://github.com/nextcloud/server/blob/af29b978078ffd9169a9bd9146feccbb7974c900/apps/dav/lib/Connector/Sabre/FilesPlugin.php#L564-L585 - \header('OC-FileId: ' . $node->getId()); - return '"' . $node->getEtag() . '"'; - } catch (\Exception $e) { - throw new Forbidden('Could not create file'); - } + protected function getPhotosLocationInfo() { + $albumOwner = $this->album->getAlbum()->getUserId(); + $photosLocation = $this->userConfigService->getConfigForUser($albumOwner, 'photosLocation'); + $userFolder = $this->rootFolder->getUserFolder($albumOwner); + return [$photosLocation, $userFolder]; } protected function addFile(int $sourceId, string $ownerUID): bool { diff --git a/lib/Service/UserConfigService.php b/lib/Service/UserConfigService.php index d25a492e..e087cb77 100644 --- a/lib/Service/UserConfigService.php +++ b/lib/Service/UserConfigService.php @@ -60,18 +60,10 @@ class UserConfigService { if (!in_array($key, array_keys(self::DEFAULT_CONFIGS))) { throw new Exception('Unknown user config key'); } + $default = self::DEFAULT_CONFIGS[$key]; $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $default); - // If the config is a path, make sure it exists - if (str_starts_with($default, '/')) { - $userFolder = $this->rootFolder->getUserFolder($userId); - // If the folder does not exists, create it - if (!$userFolder->nodeExists($value)) { - $userFolder->newFolder($value); - } - } - return $value; } } |