Skip to content

Commit fc94bb4

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

7 files changed

+112
-82
lines changed

lib/Sabre/Album/AlbumRoot.php

+41-19
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;
@@ -62,22 +66,26 @@ public function __construct(
6266
/**
6367
* @return void
6468
*/
65-
public function delete() {
69+
public function delete()
70+
{
6671
$this->albumMapper->delete($this->album->getAlbum()->getId());
6772
}
6873

69-
public function getName(): string {
74+
public function getName(): string
75+
{
7076
return basename($this->album->getAlbum()->getTitle());
7177
}
7278

7379
/**
7480
* @return void
7581
*/
76-
public function setName($name) {
82+
public function setName($name)
83+
{
7784
$this->albumMapper->rename($this->album->getAlbum()->getId(), $name);
7885
}
7986

80-
protected function getPhotosLocationInfo() {
87+
protected function getPhotosLocationInfo()
88+
{
8189
$photosLocation = $this->userConfigService->getUserConfig('photosLocation');
8290
$userFolder = $this->rootFolder->getUserFolder($this->userId);
8391
return [$photosLocation, $userFolder];
@@ -91,7 +99,8 @@ protected function getPhotosLocationInfo() {
9199
* @param null|resource|string $data
92100
* @return void
93101
*/
94-
public function createFile($name, $data = null) {
102+
public function createFile($name, $data = null)
103+
{
95104
try {
96105
[$photosLocation, $userFolder] = $this->getPhotosLocationInfo();
97106

@@ -121,24 +130,28 @@ public function createFile($name, $data = null) {
121130
\header('OC-FileId: ' . $node->getId());
122131
return '"' . $node->getEtag() . '"';
123132
} catch (\Exception $e) {
133+
$this->logger->error('Could not create file', ['exception' => $e]);
124134
throw new Forbidden('Could not create file');
125135
}
126136
}
127137

128138
/**
129139
* @return never
130140
*/
131-
public function createDirectory($name) {
141+
public function createDirectory($name)
142+
{
132143
throw new Forbidden('Not allowed to create directories in this folder');
133144
}
134145

135-
public function getChildren(): array {
146+
public function getChildren(): array
147+
{
136148
return array_map(function (AlbumFile $file) {
137149
return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId));
138150
}, $this->album->getFiles());
139151
}
140152

141-
public function getChild($name): AlbumPhoto {
153+
public function getChild($name): AlbumPhoto
154+
{
142155
foreach ($this->album->getFiles() as $file) {
143156
if ($file->getFileId() . "-" . $file->getName() === $name) {
144157
return new AlbumPhoto($this->albumMapper, $this->album->getAlbum(), $file, $this->rootFolder, $this->rootFolder->getUserFolder($this->userId));
@@ -147,7 +160,8 @@ public function getChild($name): AlbumPhoto {
147160
throw new NotFound("$name not found");
148161
}
149162

150-
public function childExists($name): bool {
163+
public function childExists($name): bool
164+
{
151165
try {
152166
$this->getChild($name);
153167
return true;
@@ -156,11 +170,13 @@ public function childExists($name): bool {
156170
}
157171
}
158172

159-
public function getLastModified(): int {
173+
public function getLastModified(): int
174+
{
160175
return 0;
161176
}
162177

163-
public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
178+
public function copyInto($targetName, $sourcePath, INode $sourceNode): bool
179+
{
164180
if (!$sourceNode instanceof File) {
165181
throw new Forbidden("The source is not a file");
166182
}
@@ -175,7 +191,8 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
175191
return $this->addFile($sourceId, $ownerUID);
176192
}
177193

178-
protected function addFile(int $sourceId, string $ownerUID): bool {
194+
protected function addFile(int $sourceId, string $ownerUID): bool
195+
{
179196
if (in_array($sourceId, $this->album->getFileIds())) {
180197
throw new Conflict("File $sourceId is already in the folder");
181198
}
@@ -188,11 +205,13 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
188205
return false;
189206
}
190207

191-
public function getAlbum(): AlbumWithFiles {
208+
public function getAlbum(): AlbumWithFiles
209+
{
192210
return $this->album;
193211
}
194212

195-
public function getDateRange(): array {
213+
public function getDateRange(): array
214+
{
196215
$earliestDate = null;
197216
$latestDate = null;
198217

@@ -218,7 +237,8 @@ public function getDateRange(): array {
218237
/**
219238
* @return int|null
220239
*/
221-
public function getCover() {
240+
public function getCover()
241+
{
222242
$children = $this->getChildren();
223243

224244
if (count($children) > 0) {
@@ -231,7 +251,8 @@ public function getCover() {
231251
/**
232252
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
233253
*/
234-
public function getCollaborators(): array {
254+
public function getCollaborators(): array
255+
{
235256
return array_map(
236257
fn (array $collaborator) => [ 'nc:collaborator' => $collaborator ],
237258
$this->albumMapper->getCollaborators($this->album->getAlbum()->getId()),
@@ -242,7 +263,8 @@ public function getCollaborators(): array {
242263
* @param array{'id': string, 'type': int} $collaborators
243264
* @return array{array{'nc:collaborator': array{'id': string, 'label': string, 'type': int}}}
244265
*/
245-
public function setCollaborators($collaborators): array {
266+
public function setCollaborators($collaborators): array
267+
{
246268
$this->albumMapper->setCollaborators($this->getAlbum()->getAlbum()->getId(), $collaborators);
247269
return $this->getCollaborators();
248270
}

lib/Sabre/Album/AlbumsHome.php

+15-17
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

+15-13
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

+17-11
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

0 commit comments

Comments
 (0)