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
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Jan 22, 2025
1 parent 52ab1dd commit 105a6c6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 56 deletions.
5 changes: 4 additions & 1 deletion lib/Sabre/Album/AlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -30,7 +31,8 @@ public function __construct(
protected AlbumWithFiles $album,
protected IRootFolder $rootFolder,
protected string $userId,
protected UserConfigService $userConfigService
protected UserConfigService $userConfigService,
protected LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -96,6 +98,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 Down
32 changes: 15 additions & 17 deletions lib/Sabre/Album/AlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,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 @@ -32,17 +27,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 @@ -91,7 +82,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
9 changes: 4 additions & 5 deletions lib/Sabre/Album/SharedAlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@
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;

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

$this->userManager = $userManager;
}

/**
Expand Down
28 changes: 17 additions & 11 deletions lib/Sabre/Album/SharedAlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,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 @@ -66,7 +64,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->userManager,
$this->logger,
);
}, $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 @@ -18,6 +18,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 @@ -33,6 +34,7 @@ public function __construct(
private IUserManager $userManager,
private IGroupManager $groupManager,
private UserConfigService $userConfigService,
private LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -69,9 +71,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 @@ -84,8 +86,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
32 changes: 15 additions & 17 deletions lib/Sabre/PublicRootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,24 @@
use OCP\Files\IRootFolder;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAVACL\AbstractPrincipalCollection;
use Sabre\DAVACL\PrincipalBackend;

class PublicRootCollection extends AbstractPrincipalCollection {
private const BRUTEFORCE_ACTION = 'publicphotos_webdav_auth';
private AlbumMapper $albumMapper;
private IRootFolder $rootFolder;
private UserConfigService $userConfigService;
private IRequest $request;
private IThrottler $throttler;

public function __construct(
AlbumMapper $albumMapper,
IRootFolder $rootFolder,
private AlbumMapper $albumMapper,
private IRootFolder $rootFolder,
PrincipalBackend\BackendInterface $principalBackend,
UserConfigService $userConfigService,
IRequest $request,
IThrottler $throttler
private UserConfigService $userConfigService,
private IRequest $request,
private IThrottler $throttler,
private LoggerInterface $logger,
) {
parent::__construct($principalBackend, 'principals/token');

$this->albumMapper = $albumMapper;
$this->rootFolder = $rootFolder;
$this->userConfigService = $userConfigService;
$this->request = $request;
$this->throttler = $throttler;
}

public function getName(): string {
Expand Down Expand Up @@ -79,6 +70,13 @@ public function getChild($name) {
throw new NotFound('Unable to find public album');
}

return new PublicAlbumRoot($this->albumMapper, $albums[0], $this->rootFolder, $albums[0]->getAlbum()->getUserId(), $this->userConfigService);
return new PublicAlbumRoot(
$this->albumMapper,
$albums[0],
$this->rootFolder,
$albums[0]->getAlbum()->getUserId(),
$this->userConfigService,
$this->logger,
);
}
}
4 changes: 3 additions & 1 deletion lib/Sabre/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAVACL\AbstractPrincipalCollection;
use Sabre\DAVACL\PrincipalBackend;

Expand All @@ -30,6 +31,7 @@ public function __construct(
private IUserManager $userManager,
private IGroupManager $groupManager,
private UserConfigService $userConfigService,
private LoggerInterface $logger,
) {
parent::__construct($principalBackend, 'principals/users');
}
Expand All @@ -49,7 +51,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->albumMapper, $this->placeMapper, $this->reverseGeoCoderService, $name, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService);
return new PhotosHome($principalInfo, $this->albumMapper, $this->placeMapper, $this->reverseGeoCoderService, $name, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger);
}

public function getName(): string {
Expand Down

0 comments on commit 105a6c6

Please sign in to comment.