Skip to content

Commit

Permalink
Add Albums upload
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 16, 2022
1 parent 0c6e855 commit 0c7061b
Show file tree
Hide file tree
Showing 8 changed files with 1,361 additions and 340 deletions.
8 changes: 8 additions & 0 deletions lib/Album/AlbumWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function getFiles(): array {
return $this->files;
}

/**
* @return AlbumFile[]
*/
public function addFile(AlbumFile $file): array {
array_push($this->files, $file);
return $this->files;
}

/**
* @return int[]
*/
Expand Down
13 changes: 12 additions & 1 deletion lib/Sabre/Album/AlbumPhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ public function getLastModified() {
}

public function put($data) {
throw new Forbidden('Can\'t write to photos trough the album api');
$nodes = $this->userFolder->getById($this->file->getFileId());
$node = current($nodes);
if ($node) {
/** @var Node $node */
if ($node instanceof File) {
return $node->putContent($data);
} else {
throw new NotFoundException("Photo is a folder");
}
} else {
throw new NotFoundException("Photo not found for user");
}
}

public function get() {
Expand Down
21 changes: 18 additions & 3 deletions lib/Sabre/Album/AlbumRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Photos\Service\UserConfigService;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IUser;
use Sabre\DAV\Exception\Conflict;
use Sabre\DAV\Exception\Forbidden;
Expand Down Expand Up @@ -97,8 +98,15 @@ public function createFile($name, $data = null) {
throw new Conflict('The destination exists and is not a folder');
}

$node = $photosFolder->newFile($name, $data);
return $this->addFile($node->getId(), $node->getOwner()->getUID());
// Check for conflict and rename the file accordingly
$newName = \basename(\OC_Helper::buildNotExistingFileName($photosLocation, $name));

$node = $photosFolder->newFile($newName, $data);
$this->addFile($node->getId(), $node->getOwner()->getUID());
// Cheating with header because we are using fileID-fileName
// https://github.com/nextcloud/server/blob/af29b978078ffd9169a9bd9146feccbb7974c900/apps/dav/lib/Connector/Sabre/FilesPlugin.php#L564-L585
\header('OC-FileId: ' . $node->getId());
return '"' . $node->getEtag() . '"';
} catch (\Exception $e) {
throw new Forbidden('Could not create file');
}
Expand Down Expand Up @@ -156,6 +164,8 @@ protected function addFile(int $sourceId, string $ownerUID): bool {
}
if ($ownerUID === $uid) {
$this->albumMapper->addFile($this->album->getAlbum()->getId(), $sourceId, $ownerUID);
$node = current($this->userFolder->getById($sourceId));
$this->album->addFile(new AlbumFile($sourceId, $node->getName(), $node->getMimetype(), $node->getSize(), $node->getMTime(), $node->getEtag(), $node->getCreationTime(), $ownerUID));
return true;
}
return false;
Expand All @@ -170,7 +180,12 @@ public function getDateRange(): array {
$latestDate = null;

foreach ($this->getChildren() as $child) {
$childCreationDate = $child->getFileInfo()->getMtime();
try {
$childCreationDate = $child->getFileInfo()->getMtime();
} catch (NotFoundException $e) {
continue;
}

if ($childCreationDate < $earliestDate || $earliestDate === null) {
$earliestDate = $childCreationDate;
}
Expand Down
Loading

0 comments on commit 0c7061b

Please sign in to comment.