Skip to content

Commit a392e60

Browse files
committed
feat: Add log when file could not be created in an album
1 parent a821ea5 commit a392e60

7 files changed

+263
-213
lines changed

lib/Sabre/Album/AlbumRoot.php

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

+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
@@ -29,46 +29,46 @@
2929
use OCA\Photos\Album\AlbumWithFiles;
3030
use OCA\Photos\Service\UserConfigService;
3131
use OCP\Files\IRootFolder;
32-
use OCP\IUserManager;
33-
34-
class SharedAlbumRoot extends AlbumRoot {
35-
private IUserManager $userManager;
32+
use Psr\Log\LoggerInterface;
3633

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

+58-50
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,69 @@
3030
use OCP\IGroupManager;
3131
use OCA\Photos\Album\AlbumMapper;
3232
use OCP\Files\IRootFolder;
33+
use Psr\Log\LoggerInterface;
3334

34-
class SharedAlbumsHome extends AlbumsHome {
35-
private IUserManager $userManager;
36-
private IGroupManager $groupManager;
35+
class SharedAlbumsHome extends AlbumsHome
36+
{
37+
public const NAME = 'sharedalbums';
3738

38-
public const NAME = 'sharedalbums';
39+
public function __construct(
40+
array $principalInfo,
41+
AlbumMapper $albumMapper,
42+
string $userId,
43+
IRootFolder $rootFolder,
44+
private IUserManager $userManager,
45+
private IGroupManager $groupManager,
46+
UserConfigService $userConfigService,
47+
LoggerInterface $logger,
48+
) {
49+
parent::__construct(
50+
$principalInfo,
51+
$albumMapper,
52+
$userId,
53+
$rootFolder,
54+
$userConfigService,
55+
$logger,
56+
);
57+
}
3958

40-
public function __construct(
41-
array $principalInfo,
42-
AlbumMapper $albumMapper,
43-
string $userId,
44-
IRootFolder $rootFolder,
45-
IUserManager $userManager,
46-
IGroupManager $groupManager,
47-
UserConfigService $userConfigService
48-
) {
49-
parent::__construct(
50-
$principalInfo,
51-
$albumMapper,
52-
$userId,
53-
$rootFolder,
54-
$userConfigService
55-
);
59+
/**
60+
* @return never
61+
*/
62+
public function createDirectory($name)
63+
{
64+
throw new Forbidden('Not allowed to create folders in this folder');
65+
}
5666

57-
$this->userManager = $userManager;
58-
$this->groupManager = $groupManager;
59-
}
67+
/**
68+
* @return SharedAlbumRoot[]
69+
*/
70+
public function getChildren(): array
71+
{
72+
if ($this->children === null) {
73+
$albums = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($this->userId, AlbumMapper::TYPE_USER);
6074

61-
/**
62-
* @return never
63-
*/
64-
public function createDirectory($name) {
65-
throw new Forbidden('Not allowed to create folders in this folder');
66-
}
75+
$user = $this->userManager->get($this->userId);
76+
$userGroups = $this->groupManager->getUserGroupIds($user);
77+
foreach ($userGroups as $groupId) {
78+
$albumsForGroup = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($groupId, AlbumMapper::TYPE_GROUP);
79+
$albumsForGroup = array_udiff($albumsForGroup, $albums, fn ($a, $b) => $a->getAlbum()->getId() - $b->getAlbum()->getId());
80+
$albums = array_merge($albums, $albumsForGroup);
81+
}
6782

68-
/**
69-
* @return SharedAlbumRoot[]
70-
*/
71-
public function getChildren(): array {
72-
if ($this->children === null) {
73-
$albums = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($this->userId, AlbumMapper::TYPE_USER);
83+
$this->children = array_map(function (AlbumWithFiles $album) {
84+
return new SharedAlbumRoot(
85+
$this->albumMapper,
86+
$album,
87+
$this->rootFolder,
88+
$this->userId,
89+
$this->userConfigService,
90+
$this->logger,
91+
$this->userManager,
92+
);
93+
}, $albums);
94+
}
7495

75-
$user = $this->userManager->get($this->userId);
76-
$userGroups = $this->groupManager->getUserGroupIds($user);
77-
foreach ($userGroups as $groupId) {
78-
$albumsForGroup = $this->albumMapper->getSharedAlbumsForCollaboratorWithFiles($groupId, AlbumMapper::TYPE_GROUP);
79-
$albumsForGroup = array_udiff($albumsForGroup, $albums, fn ($a, $b) => $a->getAlbum()->getId() - $b->getAlbum()->getId());
80-
$albums = array_merge($albums, $albumsForGroup);
81-
}
82-
83-
$this->children = array_map(function (AlbumWithFiles $album) {
84-
return new SharedAlbumRoot($this->albumMapper, $album, $this->rootFolder, $this->userId, $this->userConfigService, $this->userManager);
85-
}, $albums);
86-
}
87-
88-
return $this->children;
89-
}
96+
return $this->children;
97+
}
9098
}

0 commit comments

Comments
 (0)