Skip to content

Commit

Permalink
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 7, 2024
1 parent 2596dc5 commit 4ab8075
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand All @@ -46,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 Down Expand Up @@ -105,7 +106,12 @@ public function handle(Event $event): void {
* of pending renames inside the 'pendingRenames' property,
* to prevent infinite recursive.
*/
private function handleMove(Event $event, Node $peerFile, bool $prepForCopyOnly = false): 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();
Expand All @@ -120,30 +126,29 @@ private function handleMove(Event $event, Node $peerFile, bool $prepForCopyOnly
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()] = $peerFile->getId();
$this->pendingRenames[] = $sourceFile->getId();
try {
if (!$prepForCopyOnly) {
$peerFile->move($targetParent->getPath() . '/' . $peerTargetName);
}
$peerFile->move($targetParent->getPath() . '/' . $peerTargetName);
} catch (\Throwable $ex) {
$event->abortOperation($ex);
}
unset($this->pendingRenames[$sourceFile->getId()]);

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


Expand Down

0 comments on commit 4ab8075

Please sign in to comment.