Skip to content

Commit

Permalink
feat: Add log when file could not be created in an album
Browse files Browse the repository at this point in the history
  • Loading branch information
artonge committed Jan 29, 2025
1 parent 1abf419 commit 5db4a51
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 76 deletions.
42 changes: 29 additions & 13 deletions lib/Sabre/Album/AlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,29 @@
use OCP\Files\InvalidDirectoryException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Conflict;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\ICollection;
use Sabre\DAV\ICopyTarget;
use Sabre\DAV\INode;

class AlbumRoot implements ICollection, ICopyTarget {
class AlbumRoot implements ICollection, ICopyTarget
{
protected AlbumMapper $albumMapper;
protected AlbumWithFiles $album;
protected IRootFolder $rootFolder;
protected string $userId;
private UserConfigService $userConfigService;

public function __construct(
AlbumMapper $albumMapper,
AlbumWithFiles $album,
IRootFolder $rootFolder,
string $userId,
UserConfigService $userConfigService
UserConfigService $userConfigService,
protected LoggerInterface $logger,
) {
$this->albumMapper = $albumMapper;
$this->album = $album;
Expand All @@ -66,7 +70,8 @@ public function delete() {
$this->albumMapper->delete($this->album->getAlbum()->getId());
}

public function getName(): string {
public function getName(): string
{
return basename($this->album->getAlbum()->getTitle());
}

Expand Down Expand Up @@ -121,6 +126,7 @@ public function createFile($name, $data = null) {
\header('OC-FileId: ' . $node->getId());
return '"' . $node->getEtag() . '"';
} catch (\Exception $e) {
$this->logger->error('Could not create file', ['exception' => $e]);
throw new Forbidden('Could not create file');
}
}
Expand All @@ -132,13 +138,15 @@ public function createDirectory($name) {
throw new Forbidden('Not allowed to create directories in this folder');
}

public function getChildren(): array {
public function getChildren(): array
{
return array_map(function (AlbumFile $file) {
return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId));
}, $this->album->getFiles());
}

public function getChild($name): AlbumPhoto {
public function getChild($name): AlbumPhoto
{
foreach ($this->album->getFiles() as $file) {
if ($file->getFileId() . "-" . $file->getName() === $name) {
return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId));
Expand All @@ -147,7 +155,8 @@ public function getChild($name): AlbumPhoto {
throw new NotFound("$name not found");
}

public function childExists($name): bool {
public function childExists($name): bool
{
try {
$this->getChild($name);
return true;
Expand All @@ -156,11 +165,13 @@ public function childExists($name): bool {
}
}

public function getLastModified(): int {
public function getLastModified(): int
{
return 0;
}

public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
public function copyInto($targetName, $sourcePath, INode $sourceNode): bool
{
if (!$sourceNode instanceof File) {
throw new Forbidden("The source is not a file");
}
Expand All @@ -175,7 +186,8 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
return $this->addFile($sourceId, $ownerUID);
}

protected function addFile(int $sourceId, string $ownerUID): bool {
protected function addFile(int $sourceId, string $ownerUID): bool
{
if (in_array($sourceId, $this->album->getFileIds())) {
throw new Conflict("File $sourceId is already in the folder");
}
Expand All @@ -188,11 +200,13 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
return false;
}

public function getAlbum(): AlbumWithFiles {
public function getAlbum(): AlbumWithFiles
{
return $this->album;
}

public function getDateRange(): array {
public function getDateRange(): array
{
$earliestDate = null;
$latestDate = null;

Expand Down Expand Up @@ -231,7 +245,8 @@ public function getCover() {
/**
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
*/
public function getCollaborators(): array {
public function getCollaborators(): array
{
return array_map(
fn (array $collaborator) => [ 'nc:collaborator' => $collaborator ],
$this->albumMapper->getCollaborators($this->album->getAlbum()->getId()),
Expand All @@ -242,7 +257,8 @@ public function getCollaborators(): array {
* @param array{'id': string, 'type': int} $collaborators
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
*/
public function setCollaborators($collaborators): array {
public function setCollaborators($collaborators): array
{
$this->albumMapper->setCollaborators($this->getAlbum()->getAlbum()->getId(), $collaborators);
return $this->getCollaborators();
}
Expand Down
32 changes: 15 additions & 17 deletions lib/Sabre/Album/AlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@
use OCA\Photos\Album\AlbumWithFiles;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\ICollection;

class AlbumsHome implements ICollection {
protected AlbumMapper $albumMapper;
protected array $principalInfo;
protected string $userId;
protected IRootFolder $rootFolder;
protected UserConfigService $userConfigService;

public const NAME = 'albums';

/**
Expand All @@ -47,17 +42,13 @@ class AlbumsHome implements ICollection {
protected ?array $children = null;

public function __construct(
array $principalInfo,
AlbumMapper $albumMapper,
string $userId,
IRootFolder $rootFolder,
UserConfigService $userConfigService
protected array $principalInfo,
protected AlbumMapper $albumMapper,
protected string $userId,
protected IRootFolder $rootFolder,
protected UserConfigService $userConfigService,
protected LoggerInterface $logger,
) {
$this->principalInfo = $principalInfo;
$this->albumMapper = $albumMapper;
$this->userId = $userId;
$this->rootFolder = $rootFolder;
$this->userConfigService = $userConfigService;
}

/**
Expand Down Expand Up @@ -106,7 +97,14 @@ public function getChildren(): array {
if ($this->children === null) {
$albumInfos = $this->albumMapper->getForUser($this->userId);
$this->children = array_map(function (AlbumInfo $albumInfo) {
return new AlbumRoot($this->albumMapper, new AlbumWithFiles($albumInfo, $this->albumMapper), $this->rootFolder, $this->userId, $this->userConfigService);
return new AlbumRoot(
$this->albumMapper,
new AlbumWithFiles($albumInfo, $this->albumMapper),
$this->rootFolder,
$this->userId,
$this->userConfigService,
$this->logger,
);
}, $albumInfos);
}

Expand Down
28 changes: 15 additions & 13 deletions lib/Sabre/Album/SharedAlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,48 @@
use OCA\Photos\Album\AlbumWithFiles;
use OCA\Photos\Service\UserConfigService;
use OCP\Files\IRootFolder;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Conflict;
use Sabre\DAV\Exception\Forbidden;

class SharedAlbumRoot extends AlbumRoot {
private IUserManager $userManager;

class SharedAlbumRoot extends AlbumRoot
{
public function __construct(
AlbumMapper $albumMapper,
AlbumWithFiles $album,
IRootFolder $rootFolder,
string $userId,
UserConfigService $userConfigService,
IUserManager $userManager
LoggerInterface $logger,
) {
parent::__construct(
$albumMapper,
$album,
$rootFolder,
$userId,
$userConfigService,
$userManager
$logger
);

$this->userManager = $userManager;
}

/**
* @return void
*/
public function delete() {
public function delete()
{
$this->albumMapper->deleteUserFromAlbumCollaboratorsList($this->userId, $this->album->getAlbum()->getId());
}

/**
* @return void
*/
public function setName($name) {
public function setName($name)
{
throw new Forbidden('Not allowed to rename a shared album');
}

protected function addFile(int $sourceId, string $ownerUID): bool {
protected function addFile(int $sourceId, string $ownerUID): bool
{
if (in_array($sourceId, $this->album->getFileIds())) {
throw new Conflict("File $sourceId is already in the folder");
}
Expand All @@ -84,7 +84,8 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
/**
* Return only the owner, and do not reveal other collaborators.
*/
public function getCollaborators(): array {
public function getCollaborators(): array
{
return [[
'nc:collaborator' => [
'id' => $this->album->getAlbum()->getUserId(),
Expand All @@ -94,7 +95,8 @@ public function getCollaborators(): array {
]];
}

public function setCollaborators($collaborators): array {
public function setCollaborators($collaborators): array
{
throw new Forbidden('Not allowed to collaborators to a public album');
}
}
28 changes: 17 additions & 11 deletions lib/Sabre/Album/SharedAlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,31 @@
use OCP\Files\IRootFolder;
use OCP\IGroupManager;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;

class SharedAlbumsHome extends AlbumsHome {
private IUserManager $userManager;
private IGroupManager $groupManager;

public const NAME = 'sharedalbums';

public function __construct(
array $principalInfo,
AlbumMapper $albumMapper,
string $userId,
IRootFolder $rootFolder,
IUserManager $userManager,
IGroupManager $groupManager,
UserConfigService $userConfigService
private IUserManager $userManager,
private IGroupManager $groupManager,
UserConfigService $userConfigService,
LoggerInterface $logger,

) {
parent::__construct(
$principalInfo,
$albumMapper,
$userId,
$rootFolder,
$userConfigService
$userConfigService,
$logger,
);

$this->userManager = $userManager;
$this->groupManager = $groupManager;
}

/**
Expand All @@ -81,7 +79,15 @@ public function getChildren(): array {
}

$this->children = array_map(function (AlbumWithFiles $album) {
return new SharedAlbumRoot($this->albumMapper, $album, $this->rootFolder, $this->userId, $this->userConfigService, $this->userManager);
return new SharedAlbumRoot(
$this->albumMapper,
$album,
$this->rootFolder,
$this->userId,
$this->userConfigService,
$this->logger,
$this->userManager,
);
}, $albums);
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Sabre/PhotosHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\Files\IRootFolder;
use OCP\IGroupManager;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\ICollection;
Expand All @@ -48,6 +49,7 @@ public function __construct(
private IUserManager $userManager,
private IGroupManager $groupManager,
private UserConfigService $userConfigService,
private LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -84,9 +86,9 @@ public function createDirectory($name) {
public function getChild($name) {
switch ($name) {
case AlbumsHome::NAME:
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService);
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger);
case SharedAlbumsHome::NAME:
return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService);
return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger);
case PlacesHome::NAME:
return new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper);
}
Expand All @@ -99,8 +101,8 @@ public function getChild($name) {
*/
public function getChildren(): array {
return [
new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService),
new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService),
new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger),
new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger),
new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper),
];
}
Expand Down
Loading

0 comments on commit 5db4a51

Please sign in to comment.