Skip to content

Commit

Permalink
feat(files): copy live photos
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Feb 29, 2024
1 parent 4a75fb0 commit aa4b3f0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
4 changes: 4 additions & 0 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
Expand Down Expand Up @@ -129,6 +131,8 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeDeletedEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(CacheEntryRemovedEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(BeforeNodeCopiedEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(NodeCopiedEvent::class, SyncLivePhotosListener::class);

$context->registerSearchProvider(FilesSearchProvider::class);

Expand Down
63 changes: 53 additions & 10 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\AbstractNodesEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
Expand All @@ -44,7 +47,7 @@
* @template-implements IEventListener<Event>
*/
class SyncLivePhotosListener implements IEventListener {
/** @var Array<int, string> */
/** @var Array<int> */
private array $pendingRenames = [];
/** @var Array<int, bool> */
private array $pendingDeletion = [];
Expand All @@ -65,7 +68,6 @@ public function handle(Event $event): void {
}

$peerFile = null;

if ($event instanceof BeforeNodeRenamedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getSource()->getId());
} elseif ($event instanceof BeforeNodeRestoredEvent) {
Expand All @@ -74,20 +76,26 @@ public function handle(Event $event): void {
$peerFile = $this->getLivePhotoPeer($event->getNode()->getId());
} elseif ($event instanceof CacheEntryRemovedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getFileId());
} elseif ($event instanceof BeforeNodeCopiedEvent || $event instanceof NodeCopiedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getSource()->getId());
}

if ($peerFile === null) {
return; // not a Live Photo
}

if ($event instanceof BeforeNodeRenamedEvent) {
$this->handleMove($event, $peerFile);
$this->handleMove($event, $peerFile, false);
} elseif ($event instanceof BeforeNodeDeletedEvent) {
$this->handleDeletion($event, $peerFile);
} elseif ($event instanceof CacheEntryRemovedEvent) {
$peerFile->delete();
} elseif ($event instanceof BeforeNodeRestoredEvent) {
$this->handleRestore($event, $peerFile);
} elseif ($event instanceof BeforeNodeCopiedEvent) {
$this->handleMove($event, $peerFile, true);
} elseif ($event instanceof NodeCopiedEvent) {
$this->handleCopy($event, $peerFile);
}
}

Expand All @@ -98,14 +106,18 @@ public function handle(Event $event): void {
* of pending renames inside the 'pendingRenames' property,
* to prevent infinite recursive.
*/
private function handleMove(BeforeNodeRenamedEvent $event, Node $peerFile): void {
private function handleMove(AbstractNodesEvent $event, Node $peerFile, bool $prepForCopyOnly = false): void {
if (!($event instanceof BeforeNodeCopiedEvent) &&
!($event instanceof BeforeNodeRenamedEvent)) {
return;
}

$sourceFile = $event->getSource();
$targetFile = $event->getTarget();
$targetParent = $targetFile->getParent();
$sourceExtension = $sourceFile->getExtension();
$peerFileExtension = $peerFile->getExtension();
$targetName = $targetFile->getName();
$targetPath = $targetFile->getPath();

if (!str_ends_with($targetName, ".".$sourceExtension)) {
$event->abortOperation(new NotPermittedException("Cannot change the extension of a Live Photo"));
Expand All @@ -114,28 +126,59 @@ private function handleMove(BeforeNodeRenamedEvent $event, Node $peerFile): void
try {
$targetParent->get($targetName);
$event->abortOperation(new NotPermittedException("A file already exist at destination path of the Live Photo"));
} catch (NotFoundException $ex) {
} catch (NotFoundException) {
}

$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;
try {
$targetParent->get($peerTargetName);
$event->abortOperation(new NotPermittedException("A file already exist at destination path of the Live Photo"));
} catch (NotFoundException $ex) {
} catch (NotFoundException) {
}

// in case the rename was initiated from this listener, we stop right now
if (array_key_exists($peerFile->getId(), $this->pendingRenames)) {
if ($prepForCopyOnly || in_array($peerFile->getId(), $this->pendingRenames)) {
return;
}

$this->pendingRenames[$sourceFile->getId()] = $targetPath;
$this->pendingRenames[] = $sourceFile->getId();
try {
$peerFile->move($targetParent->getPath() . '/' . $peerTargetName);
} catch (\Throwable $ex) {
$event->abortOperation($ex);
}
unset($this->pendingRenames[$sourceFile->getId()]);

array_diff($this->pendingRenames, [$sourceFile->getId()]);
}


/**
* handle copy, we already know if it is doable from BeforeNodeCopiedEvent, so we just copy the linked file
*
* @param NodeCopiedEvent $event
* @param Node $peerFile
*/
private function handleCopy(NodeCopiedEvent $event, Node $peerFile): void {
$sourceFile = $event->getSource();
$sourceExtension = $sourceFile->getExtension();
$peerFileExtension = $peerFile->getExtension();
$targetFile = $event->getTarget();
$targetParent = $targetFile->getParent();
$targetName = $targetFile->getName();
$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;

/**
* let's use freshly set variable.
* we copy the file and get its id. We already have the id of the current copy
* We have everything to update metadata and keep the link between the 2 copies.
*/
$newPeerFile = $peerFile->copy($targetParent->getPath() . '/' . $peerTargetName);
$targetMetadata = $this->filesMetadataManager->getMetadata($targetFile->getId(), true);
$targetMetadata->setString('files-live-photo', (string)$newPeerFile->getId());
$this->filesMetadataManager->saveMetadata($targetMetadata);
$peerMetadata = $this->filesMetadataManager->getMetadata($newPeerFile->getId(), true);
$peerMetadata->setString('files-live-photo', (string)$targetFile->getId());
$this->filesMetadataManager->saveMetadata($peerMetadata);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/HookConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function copy($arguments) {
$this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));

$event = new BeforeNodeCopiedEvent($source, $target);
$event = new BeforeNodeCopiedEvent($source, $target, $arguments['run']);
$this->dispatcher->dispatchTyped($event);
}

Expand Down
23 changes: 23 additions & 0 deletions lib/public/Files/Events/Node/BeforeNodeCopiedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,31 @@
*/
namespace OCP\Files\Events\Node;

use OCP\DB\Exception;
use OCP\Files\Node;

/**
* @since 20.0.0
*/
class BeforeNodeCopiedEvent extends AbstractNodesEvent {
/**
* @since 20.0.0
*/
public function __construct(Node $source, Node $target, private bool &$run) {
parent::__construct($source, $target);
}

/**
* @since 28.0.0
* @return never
*/
public function abortOperation(\Throwable $ex = null) {
$this->stopPropagation();
$this->run = false;
if ($ex !== null) {
throw $ex;
} else {
throw new Exception('Operation aborted');
}
}
}

0 comments on commit aa4b3f0

Please sign in to comment.