Skip to content

Commit 0fc3830

Browse files
committed
Allow to PUT files into an album
Signed-off-by: John Molakvoæ <[email protected]>
1 parent 9687ee4 commit 0fc3830

4 files changed

Lines changed: 58 additions & 23 deletions

File tree

lib/Sabre/Album/AlbumRoot.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\Photos\Album\AlbumFile;
2828
use OCA\Photos\Album\AlbumMapper;
2929
use OCA\Photos\Album\AlbumWithFiles;
30+
use OCA\Photos\Service\UserConfigService;
3031
use OCP\Files\Folder;
3132
use OCP\Files\IRootFolder;
3233
use OCP\IUser;
@@ -40,16 +41,22 @@
4041
class AlbumRoot implements ICollection, ICopyTarget {
4142
private AlbumMapper $albumMapper;
4243
private AlbumWithFiles $album;
43-
private IRootFolder $rootFolder;
4444
private Folder $userFolder;
4545
private IUser $user;
46-
47-
public function __construct(AlbumMapper $albumMapper, AlbumWithFiles $album, IRootFolder $rootFolder, Folder $userFolder, IUser $user) {
46+
private UserConfigService $userConfigService;
47+
48+
public function __construct(AlbumMapper $albumMapper,
49+
AlbumWithFiles $album,
50+
IRootFolder $rootFolder,
51+
Folder $userFolder,
52+
IUser $user,
53+
UserConfigService $userConfigService) {
4854
$this->albumMapper = $albumMapper;
4955
$this->album = $album;
5056
$this->rootFolder = $rootFolder;
5157
$this->userFolder = $userFolder;
5258
$this->user = $user;
59+
$this->userConfigService = $userConfigService;
5360
}
5461

5562
/**
@@ -70,8 +77,23 @@ public function setName($name) {
7077
$this->albumMapper->rename($this->album->getAlbum()->getId(), $name);
7178
}
7279

80+
/**
81+
* We cannot create files in an Album
82+
* We add the file to the default Photos folder and then link it there.
83+
*
84+
* @param [type] $name
85+
* @param [type] $data
86+
* @return void
87+
*/
7388
public function createFile($name, $data = null) {
74-
throw new Forbidden('Not allowed to create files in this folder, copy files into this folder instead');
89+
try {
90+
$photosLocation = $this->userConfigService->getUserConfig('photosLocation');
91+
$photosFolder = $this->userFolder->get($photosLocation);
92+
$node = $photosFolder->newFile($name, $data);
93+
return $this->addFile($node->getId(), $node->getOwner()->getUID());
94+
} catch (\Exception) {
95+
throw new \Exception('The file could not be created');
96+
}
7597
}
7698

7799
/**
@@ -113,17 +135,23 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode): bool {
113135
$uid = $this->user->getUID();
114136
if ($sourceNode instanceof File) {
115137
$sourceId = $sourceNode->getId();
116-
if (in_array($sourceId, $this->album->getFileIds())) {
117-
throw new Conflict("File $sourceId is already in the folder");
118-
}
119-
if ($sourceNode->getFileInfo()->getOwner()->getUID() === $uid) {
120-
$this->albumMapper->addFile($this->album->getAlbum()->getId(), $sourceId);
121-
return true;
122-
}
138+
$ownerUID = $sourceNode->getFileInfo()->getOwner()->getUID();
139+
return $this->addFile($sourceId, $ownerUID);
123140
}
124141
throw new \Exception("Can't add file to album, only files from $uid can be added");
125142
}
126143

144+
private function addFile(int $sourceId, string $ownerUID): bool {
145+
$uid = $this->user->getUID();
146+
if (in_array($sourceId, $this->album->getFileIds())) {
147+
throw new Conflict("File $sourceId is already in the folder");
148+
}
149+
if ($ownerUID === $uid) {
150+
$this->albumMapper->addFile($this->album->getAlbum()->getId(), $sourceId);
151+
return true;
152+
}
153+
}
154+
127155
public function getAlbum(): AlbumWithFiles {
128156
return $this->album;
129157
}

lib/Sabre/Album/AlbumsHome.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use OCA\Photos\Album\AlbumMapper;
2727
use OCA\Photos\Album\AlbumWithFiles;
28+
use OCA\Photos\Service\UserConfigService;
2829
use OCP\Files\Folder;
2930
use OCP\Files\IRootFolder;
3031
use OCP\IUser;
@@ -38,6 +39,7 @@ class AlbumsHome implements ICollection {
3839
private IUser $user;
3940
private IRootFolder $rootFolder;
4041
private Folder $userFolder;
42+
private UserConfigService $userConfigService;
4143

4244
/**
4345
* @var AlbumRoot[]
@@ -48,13 +50,14 @@ public function __construct(
4850
array $principalInfo,
4951
AlbumMapper $albumMapper,
5052
IUser $user,
51-
IRootFolder $rootFolder
52-
) {
53+
IRootFolder $rootFolder,
54+
UserConfigService $userConfigService) {
5355
$this->principalInfo = $principalInfo;
5456
$this->albumMapper = $albumMapper;
5557
$this->user = $user;
5658
$this->rootFolder = $rootFolder;
5759
$this->userFolder = $rootFolder->getUserFolder($user->getUID());
60+
$this->userConfigService = $userConfigService;
5861
}
5962

6063
/**
@@ -104,7 +107,7 @@ public function getChildren(): array {
104107
if ($this->children === null) {
105108
$folders = $this->albumMapper->getForUserWithFiles($this->user->getUID());
106109
$this->children = array_map(function (AlbumWithFiles $folder) {
107-
return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user);
110+
return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService);
108111
}, $folders);
109112
}
110113

lib/Sabre/PhotosHome.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
use OCA\Photos\Album\AlbumMapper;
2727
use OCA\Photos\Sabre\Album\AlbumsHome;
28-
use OCP\Files\Folder;
28+
use OCA\Photos\Service\UserConfigService;
2929
use OCP\Files\IRootFolder;
3030
use OCP\IUser;
3131
use Sabre\DAV\Exception\Forbidden;
@@ -37,19 +37,20 @@ class PhotosHome implements ICollection {
3737
private array $principalInfo;
3838
private IUser $user;
3939
private IRootFolder $rootFolder;
40-
private Folder $userFolder;
40+
private UserConfigService $userConfigService;
4141

4242
public function __construct(
4343
array $principalInfo,
4444
AlbumMapper $albumMapper,
4545
IUser $user,
46-
IRootFolder $rootFolder
46+
IRootFolder $rootFolder,
47+
UserConfigService $userConfigService
4748
) {
4849
$this->principalInfo = $principalInfo;
4950
$this->albumMapper = $albumMapper;
5051
$this->user = $user;
5152
$this->rootFolder = $rootFolder;
52-
$this->userFolder = $rootFolder->getUserFolder($user->getUID());
53+
$this->userConfigService = $userConfigService;
5354
}
5455

5556
/**
@@ -84,7 +85,7 @@ public function createDirectory($name) {
8485

8586
public function getChild($name) {
8687
if ($name === 'albums') {
87-
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder);
88+
return new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder, $this->userConfigService);
8889
}
8990

9091
throw new NotFound();
@@ -94,7 +95,7 @@ public function getChild($name) {
9495
* @return AlbumsHome[]
9596
*/
9697
public function getChildren(): array {
97-
return [new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder)];
98+
return [new AlbumsHome($this->principalInfo, $this->albumMapper, $this->user, $this->rootFolder, $this->userConfigService)];
9899
}
99100

100101
public function childExists($name): bool {

lib/Sabre/RootCollection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace OCA\Photos\Sabre;
2525

2626
use OCA\Photos\Album\AlbumMapper;
27+
use OCA\Photos\Service\UserConfigService;
2728
use OCP\Files\IRootFolder;
2829
use OCP\IUserSession;
2930
use Sabre\DAVACL\AbstractPrincipalCollection;
@@ -33,18 +34,20 @@ class RootCollection extends AbstractPrincipalCollection {
3334
private AlbumMapper $folderMapper;
3435
private IUserSession $userSession;
3536
private IRootFolder $rootFolder;
37+
private UserConfigService $userConfigService;
3638

3739
public function __construct(
3840
AlbumMapper $folderMapper,
3941
IUserSession $userSession,
4042
IRootFolder $rootFolder,
41-
PrincipalBackend\BackendInterface $principalBackend
42-
) {
43+
PrincipalBackend\BackendInterface $principalBackend,
44+
UserConfigService $userConfigService) {
4345
parent::__construct($principalBackend, 'principals/users');
4446

4547
$this->folderMapper = $folderMapper;
4648
$this->userSession = $userSession;
4749
$this->rootFolder = $rootFolder;
50+
$this->userConfigService = $userConfigService;
4851
}
4952

5053
/**
@@ -62,7 +65,7 @@ public function getChildForPrincipal(array $principalInfo): PhotosHome {
6265
if (is_null($user) || $name !== $user->getUID()) {
6366
throw new \Sabre\DAV\Exception\Forbidden();
6467
}
65-
return new PhotosHome($principalInfo, $this->folderMapper, $user, $this->rootFolder);
68+
return new PhotosHome($principalInfo, $this->folderMapper, $user, $this->rootFolder, $this->userConfigService);
6669
}
6770

6871
public function getName(): string {

0 commit comments

Comments
 (0)