Refactor getting album storage folder

Signed-off-by: Louis Chemineau <louis@chmn.me>
This commit is contained in:
Louis Chemineau 2022-09-28 11:01:12 +02:00
parent dc44603b7c
commit a2890b03e7
3 changed files with 19 additions and 43 deletions

View File

@ -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');
}

View File

@ -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 {

View File

@ -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;
}
}