Skip to content

Commit

Permalink
Merge pull request #1225 from nextcloud/feat/upload-albums
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Sep 13, 2022
2 parents de90112 + 8c4bc0d commit dd48e49
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
57 changes: 46 additions & 11 deletions lib/Sabre/Album/AlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Photos\Album\AlbumFile;
use OCA\Photos\Album\AlbumMapper;
use OCA\Photos\Album\AlbumWithFiles;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IUser;
Expand All @@ -40,16 +41,22 @@
class AlbumRoot implements ICollection, ICopyTarget {
private AlbumMapper $albumMapper;
private AlbumWithFiles $album;
private IRootFolder $rootFolder;
private Folder $userFolder;
private IUser $user;

public function __construct(AlbumMapper $albumMapper, AlbumWithFiles $album, IRootFolder $rootFolder, Folder $userFolder, IUser $user) {
private UserConfigService $userConfigService;

public function __construct(AlbumMapper $albumMapper,
AlbumWithFiles $album,
IRootFolder $rootFolder,
Folder $userFolder,
IUser $user,
UserConfigService $userConfigService) {
$this->albumMapper = $albumMapper;
$this->album = $album;
$this->rootFolder = $rootFolder;
$this->userFolder = $userFolder;
$this->user = $user;
$this->userConfigService = $userConfigService;
}

/**
Expand All @@ -70,8 +77,29 @@ public function setName($name) {
$this->albumMapper->rename($this->album->getAlbum()->getId(), $name);
}

/**
* We cannot create files in an Album
* We add the file to the default Photos folder and then link it there.
*
* @param [type] $name
* @param [type] $data
* @return void
*/
public function createFile($name, $data = null) {
throw new Forbidden('Not allowed to create files in this folder, copy files into this folder instead');
try {
// userConfigService->getUserConfig handle the path creation if missing
$photosLocation = $this->userConfigService->getUserConfig('photosLocation');

$photosFolder = $this->userFolder->get($photosLocation);
if (!($photosFolder instanceof Folder)) {
throw new Conflict('The destination exists and is not a folder');
}

$node = $photosFolder->newFile($name, $data);
return $this->addFile($node->getId(), $node->getOwner()->getUID());
} catch (\Exception $e) {
throw new Forbidden('Could not create file');
}
}

/**
Expand Down Expand Up @@ -113,17 +141,24 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
$uid = $this->user->getUID();
if ($sourceNode instanceof File) {
$sourceId = $sourceNode->getId();
if (in_array($sourceId, $this->album->getFileIds())) {
throw new Conflict("File $sourceId is already in the folder");
}
if ($sourceNode->getFileInfo()->getOwner()->getUID() === $uid) {
$this->albumMapper->addFile($this->album->getAlbum()->getId(), $sourceId);
return true;
}
$ownerUID = $sourceNode->getFileInfo()->getOwner()->getUID();
return $this->addFile($sourceId, $ownerUID);
}
throw new \Exception("Can't add file to album, only files from $uid can be added");
}

private function addFile(int $sourceId, string $ownerUID): bool {
$uid = $this->user->getUID();
if (in_array($sourceId, $this->album->getFileIds())) {
throw new Conflict("File $sourceId is already in the folder");
}
if ($ownerUID === $uid) {
$this->albumMapper->addFile($this->album->getAlbum()->getId(), $sourceId);
return true;
}
return false;
}

public function getAlbum(): AlbumWithFiles {
return $this->album;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Sabre/Album/AlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Photos\Album\AlbumMapper;
use OCA\Photos\Album\AlbumWithFiles;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IUser;
Expand All @@ -38,6 +39,7 @@ class AlbumsHome implements ICollection {
private IUser $user;
private IRootFolder $rootFolder;
private Folder $userFolder;
private UserConfigService $userConfigService;

/**
* @var AlbumRoot[]
Expand All @@ -48,13 +50,14 @@ public function __construct(
array $principalInfo,
AlbumMapper $albumMapper,
IUser $user,
IRootFolder $rootFolder
) {
IRootFolder $rootFolder,
UserConfigService $userConfigService) {
$this->principalInfo = $principalInfo;
$this->albumMapper = $albumMapper;
$this->user = $user;
$this->rootFolder = $rootFolder;
$this->userFolder = $rootFolder->getUserFolder($user->getUID());
$this->userConfigService = $userConfigService;
}

/**
Expand Down Expand Up @@ -104,7 +107,7 @@ public function getChildren(): array {
if ($this->children === null) {
$folders = $this->albumMapper->getForUserWithFiles($this->user->getUID());
$this->children = array_map(function (AlbumWithFiles $folder) {
return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user);
return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService);
}, $folders);
}

Expand Down
13 changes: 7 additions & 6 deletions lib/Sabre/PhotosHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use OCA\Photos\Album\AlbumMapper;
use OCA\Photos\Sabre\Album\AlbumsHome;
use OCP\Files\Folder;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\IRootFolder;
use OCP\IUser;
use Sabre\DAV\Exception\Forbidden;
Expand All @@ -37,19 +37,20 @@ class PhotosHome implements ICollection {
private array $principalInfo;
private IUser $user;
private IRootFolder $rootFolder;
private Folder $userFolder;
private UserConfigService $userConfigService;

public function __construct(
array $principalInfo,
AlbumMapper $albumMapper,
IUser $user,
IRootFolder $rootFolder
IRootFolder $rootFolder,
UserConfigService $userConfigService
) {
$this->principalInfo = $principalInfo;
$this->albumMapper = $albumMapper;
$this->user = $user;
$this->rootFolder = $rootFolder;
$this->userFolder = $rootFolder->getUserFolder($user->getUID());
$this->userConfigService = $userConfigService;
}

/**
Expand Down Expand Up @@ -84,7 +85,7 @@ public function createDirectory($name) {

public function getChild($name) {
if ($name === 'albums') {
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder);
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder, $this->userConfigService);
}

throw new NotFound();
Expand All @@ -94,7 +95,7 @@ public function getChild($name) {
* @return AlbumsHome[]
*/
public function getChildren(): array {
return [new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder)];
return [new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder, $this->userConfigService)];
}

public function childExists($name): bool {
Expand Down
9 changes: 6 additions & 3 deletions lib/Sabre/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Photos\Sabre;

use OCA\Photos\Album\AlbumMapper;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\IRootFolder;
use OCP\IUserSession;
use Sabre\DAVACL\AbstractPrincipalCollection;
Expand All @@ -33,18 +34,20 @@ class RootCollection extends AbstractPrincipalCollection {
private AlbumMapper $folderMapper;
private IUserSession $userSession;
private IRootFolder $rootFolder;
private UserConfigService $userConfigService;

public function __construct(
AlbumMapper $folderMapper,
IUserSession $userSession,
IRootFolder $rootFolder,
PrincipalBackend\BackendInterface $principalBackend
) {
PrincipalBackend\BackendInterface $principalBackend,
UserConfigService $userConfigService) {
parent::__construct($principalBackend, 'principals/users');

$this->folderMapper = $folderMapper;
$this->userSession = $userSession;
$this->rootFolder = $rootFolder;
$this->userConfigService = $userConfigService;
}

/**
Expand All @@ -62,7 +65,7 @@ public function getChildForPrincipal(array $principalInfo): PhotosHome {
if (is_null($user) || $name !== $user->getUID()) {
throw new \Sabre\DAV\Exception\Forbidden();
}
return new PhotosHome($principalInfo, $this->folderMapper, $user, $this->rootFolder);
return new PhotosHome($principalInfo, $this->folderMapper, $user, $this->rootFolder, $this->userConfigService);
}

public function getName(): string {
Expand Down

0 comments on commit dd48e49

Please sign in to comment.