Skip to content

Commit 6dcca51

Browse files
committed
feat: Add log when file could not be created in an album
1 parent 3940bb9 commit 6dcca51

7 files changed

Lines changed: 100 additions & 76 deletions

File tree

lib/Sabre/Album/AlbumRoot.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,29 @@
3232
use OCP\Files\InvalidDirectoryException;
3333
use OCP\Files\IRootFolder;
3434
use OCP\Files\NotFoundException;
35+
use Psr\Log\LoggerInterface;
3536
use Sabre\DAV\Exception\Conflict;
3637
use Sabre\DAV\Exception\Forbidden;
3738
use Sabre\DAV\Exception\NotFound;
3839
use Sabre\DAV\ICollection;
3940
use Sabre\DAV\ICopyTarget;
4041
use Sabre\DAV\INode;
4142

42-
class AlbumRoot implements ICollection, ICopyTarget {
43+
class AlbumRoot implements ICollection, ICopyTarget
44+
{
4345
protected AlbumMapper $albumMapper;
4446
protected AlbumWithFiles $album;
4547
protected IRootFolder $rootFolder;
4648
protected string $userId;
49+
private UserConfigService $userConfigService;
4750

4851
public function __construct(
4952
AlbumMapper $albumMapper,
5053
AlbumWithFiles $album,
5154
IRootFolder $rootFolder,
5255
string $userId,
53-
UserConfigService $userConfigService
56+
UserConfigService $userConfigService,
57+
protected LoggerInterface $logger,
5458
) {
5559
$this->albumMapper = $albumMapper;
5660
$this->album = $album;
@@ -66,7 +70,8 @@ public function delete() {
6670
$this->albumMapper->delete($this->album->getAlbum()->getId());
6771
}
6872

69-
public function getName(): string {
73+
public function getName(): string
74+
{
7075
return basename($this->album->getAlbum()->getTitle());
7176
}
7277

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

135-
public function getChildren(): array {
141+
public function getChildren(): array
142+
{
136143
return array_map(function (AlbumFile $file) {
137144
return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId));
138145
}, $this->album->getFiles());
139146
}
140147

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

150-
public function childExists($name): bool {
158+
public function childExists($name): bool
159+
{
151160
try {
152161
$this->getChild($name);
153162
return true;
@@ -156,11 +165,13 @@ public function childExists($name): bool {
156165
}
157166
}
158167

159-
public function getLastModified(): int {
168+
public function getLastModified(): int
169+
{
160170
return 0;
161171
}
162172

163-
public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
173+
public function copyInto($targetName, $sourcePath, INode $sourceNode): bool
174+
{
164175
if (!$sourceNode instanceof File) {
165176
throw new Forbidden("The source is not a file");
166177
}
@@ -175,7 +186,8 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
175186
return $this->addFile($sourceId, $ownerUID);
176187
}
177188

178-
protected function addFile(int $sourceId, string $ownerUID): bool {
189+
protected function addFile(int $sourceId, string $ownerUID): bool
190+
{
179191
if (in_array($sourceId, $this->album->getFileIds())) {
180192
throw new Conflict("File $sourceId is already in the folder");
181193
}
@@ -188,11 +200,13 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
188200
return false;
189201
}
190202

191-
public function getAlbum(): AlbumWithFiles {
203+
public function getAlbum(): AlbumWithFiles
204+
{
192205
return $this->album;
193206
}
194207

195-
public function getDateRange(): array {
208+
public function getDateRange(): array
209+
{
196210
$earliestDate = null;
197211
$latestDate = null;
198212

@@ -231,7 +245,8 @@ public function getCover() {
231245
/**
232246
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
233247
*/
234-
public function getCollaborators(): array {
248+
public function getCollaborators(): array
249+
{
235250
return array_map(
236251
fn (array $collaborator) => [ 'nc:collaborator' => $collaborator ],
237252
$this->albumMapper->getCollaborators($this->album->getAlbum()->getId()),
@@ -242,7 +257,8 @@ public function getCollaborators(): array {
242257
* @param array{'id': string, 'type': int} $collaborators
243258
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
244259
*/
245-
public function setCollaborators($collaborators): array {
260+
public function setCollaborators($collaborators): array
261+
{
246262
$this->albumMapper->setCollaborators($this->getAlbum()->getAlbum()->getId(), $collaborators);
247263
return $this->getCollaborators();
248264
}

lib/Sabre/Album/AlbumsHome.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@
2828
use OCA\Photos\Album\AlbumWithFiles;
2929
use OCA\Photos\Service\UserConfigService;
3030
use OCP\Files\IRootFolder;
31+
use Psr\Log\LoggerInterface;
3132
use Sabre\DAV\Exception\Forbidden;
3233
use Sabre\DAV\Exception\NotFound;
3334
use Sabre\DAV\ICollection;
3435

3536
class AlbumsHome implements ICollection {
36-
protected AlbumMapper $albumMapper;
37-
protected array $principalInfo;
38-
protected string $userId;
39-
protected IRootFolder $rootFolder;
40-
protected UserConfigService $userConfigService;
41-
4237
public const NAME = 'albums';
4338

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

4944
public function __construct(
50-
array $principalInfo,
51-
AlbumMapper $albumMapper,
52-
string $userId,
53-
IRootFolder $rootFolder,
54-
UserConfigService $userConfigService
45+
protected array $principalInfo,
46+
protected AlbumMapper $albumMapper,
47+
protected string $userId,
48+
protected IRootFolder $rootFolder,
49+
protected UserConfigService $userConfigService,
50+
protected LoggerInterface $logger,
5551
) {
56-
$this->principalInfo = $principalInfo;
57-
$this->albumMapper = $albumMapper;
58-
$this->userId = $userId;
59-
$this->rootFolder = $rootFolder;
60-
$this->userConfigService = $userConfigService;
6152
}
6253

6354
/**
@@ -106,7 +97,14 @@ public function getChildren(): array {
10697
if ($this->children === null) {
10798
$albumInfos = $this->albumMapper->getForUser($this->userId);
10899
$this->children = array_map(function (AlbumInfo $albumInfo) {
109-
return new AlbumRoot($this->albumMapper, new AlbumWithFiles($albumInfo, $this->albumMapper), $this->rootFolder, $this->userId, $this->userConfigService);
100+
return new AlbumRoot(
101+
$this->albumMapper,
102+
new AlbumWithFiles($albumInfo, $this->albumMapper),
103+
$this->rootFolder,
104+
$this->userId,
105+
$this->userConfigService,
106+
$this->logger,
107+
);
110108
}, $albumInfos);
111109
}
112110

lib/Sabre/Album/SharedAlbumRoot.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,48 @@
2727
use OCA\Photos\Album\AlbumWithFiles;
2828
use OCA\Photos\Service\UserConfigService;
2929
use OCP\Files\IRootFolder;
30-
use OCP\IUserManager;
30+
use Psr\Log\LoggerInterface;
3131
use Sabre\DAV\Exception\Conflict;
3232
use Sabre\DAV\Exception\Forbidden;
3333

34-
class SharedAlbumRoot extends AlbumRoot {
35-
private IUserManager $userManager;
36-
34+
class SharedAlbumRoot extends AlbumRoot
35+
{
3736
public function __construct(
3837
AlbumMapper $albumMapper,
3938
AlbumWithFiles $album,
4039
IRootFolder $rootFolder,
4140
string $userId,
4241
UserConfigService $userConfigService,
43-
IUserManager $userManager
42+
LoggerInterface $logger,
4443
) {
4544
parent::__construct(
4645
$albumMapper,
4746
$album,
4847
$rootFolder,
4948
$userId,
5049
$userConfigService,
51-
$userManager
50+
$logger
5251
);
53-
54-
$this->userManager = $userManager;
5552
}
5653

5754
/**
5855
* @return void
5956
*/
60-
public function delete() {
57+
public function delete()
58+
{
6159
$this->albumMapper->deleteUserFromAlbumCollaboratorsList($this->userId, $this->album->getAlbum()->getId());
6260
}
6361

6462
/**
6563
* @return void
6664
*/
67-
public function setName($name) {
65+
public function setName($name)
66+
{
6867
throw new Forbidden('Not allowed to rename a shared album');
6968
}
7069

71-
protected function addFile(int $sourceId, string $ownerUID): bool {
70+
protected function addFile(int $sourceId, string $ownerUID): bool
71+
{
7272
if (in_array($sourceId, $this->album->getFileIds())) {
7373
throw new Conflict("File $sourceId is already in the folder");
7474
}
@@ -84,7 +84,8 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
8484
/**
8585
* Return only the owner, and do not reveal other collaborators.
8686
*/
87-
public function getCollaborators(): array {
87+
public function getCollaborators(): array
88+
{
8889
return [[
8990
'nc:collaborator' => [
9091
'id' => $this->album->getAlbum()->getUserId(),
@@ -94,7 +95,8 @@ public function getCollaborators(): array {
9495
]];
9596
}
9697

97-
public function setCollaborators($collaborators): array {
98+
public function setCollaborators($collaborators): array
99+
{
98100
throw new Forbidden('Not allowed to collaborators to a public album');
99101
}
100102
}

lib/Sabre/Album/SharedAlbumsHome.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,31 @@
2929
use OCP\Files\IRootFolder;
3030
use OCP\IGroupManager;
3131
use OCP\IUserManager;
32+
use Psr\Log\LoggerInterface;
3233
use Sabre\DAV\Exception\Forbidden;
3334

3435
class SharedAlbumsHome extends AlbumsHome {
35-
private IUserManager $userManager;
36-
private IGroupManager $groupManager;
37-
3836
public const NAME = 'sharedalbums';
3937

4038
public function __construct(
4139
array $principalInfo,
4240
AlbumMapper $albumMapper,
4341
string $userId,
4442
IRootFolder $rootFolder,
45-
IUserManager $userManager,
46-
IGroupManager $groupManager,
47-
UserConfigService $userConfigService
43+
private IUserManager $userManager,
44+
private IGroupManager $groupManager,
45+
UserConfigService $userConfigService,
46+
LoggerInterface $logger,
47+
4848
) {
4949
parent::__construct(
5050
$principalInfo,
5151
$albumMapper,
5252
$userId,
5353
$rootFolder,
54-
$userConfigService
54+
$userConfigService,
55+
$logger,
5556
);
56-
57-
$this->userManager = $userManager;
58-
$this->groupManager = $groupManager;
5957
}
6058

6159
/**
@@ -81,7 +79,15 @@ public function getChildren(): array {
8179
}
8280

8381
$this->children = array_map(function (AlbumWithFiles $album) {
84-
return new SharedAlbumRoot($this->albumMapper, $album, $this->rootFolder, $this->userId, $this->userConfigService, $this->userManager);
82+
return new SharedAlbumRoot(
83+
$this->albumMapper,
84+
$album,
85+
$this->rootFolder,
86+
$this->userId,
87+
$this->userConfigService,
88+
$this->logger,
89+
$this->userManager,
90+
);
8591
}, $albums);
8692
}
8793

lib/Sabre/PhotosHome.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\Files\IRootFolder;
3434
use OCP\IGroupManager;
3535
use OCP\IUserManager;
36+
use Psr\Log\LoggerInterface;
3637
use Sabre\DAV\Exception\Forbidden;
3738
use Sabre\DAV\Exception\NotFound;
3839
use Sabre\DAV\ICollection;
@@ -48,6 +49,7 @@ public function __construct(
4849
private IUserManager $userManager,
4950
private IGroupManager $groupManager,
5051
private UserConfigService $userConfigService,
52+
private LoggerInterface $logger,
5153
) {
5254
}
5355

@@ -84,9 +86,9 @@ public function createDirectory($name) {
8486
public function getChild($name) {
8587
switch ($name) {
8688
case AlbumsHome::NAME:
87-
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService);
89+
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger);
8890
case SharedAlbumsHome::NAME:
89-
return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService);
91+
return new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger);
9092
case PlacesHome::NAME:
9193
return new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper);
9294
}
@@ -99,8 +101,8 @@ public function getChild($name) {
99101
*/
100102
public function getChildren(): array {
101103
return [
102-
new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService),
103-
new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService),
104+
new AlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userConfigService, $this->logger),
105+
new SharedAlbumsHome($this->principalInfo, $this->albumMapper, $this->userId, $this->rootFolder, $this->userManager, $this->groupManager, $this->userConfigService, $this->logger),
104106
new PlacesHome($this->userId, $this->rootFolder, $this->reverseGeoCoderService, $this->placeMapper),
105107
];
106108
}

0 commit comments

Comments
 (0)