diff options
author | Louis Chemineau <louis@chmn.me> | 2024-11-26 11:23:19 +0100 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2024-12-04 16:23:39 +0100 |
commit | 8be7b9ea40e7e925d2d2f0adf2e3d6b166f0a1fa (patch) | |
tree | 849b396742b717ae41394374d95498315232a0f6 /apps | |
parent | 10c6f70778dff0a367703cc2f49f361e59c6d470 (diff) | |
download | nextcloud-server-8be7b9ea40e7e925d2d2f0adf2e3d6b166f0a1fa.tar.gz nextcloud-server-8be7b9ea40e7e925d2d2f0adf2e3d6b166f0a1fa.zip |
fix: Copying a live photo to a folder with a conflicting mov file
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/lib/Listener/SyncLivePhotosListener.php | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/apps/files/lib/Listener/SyncLivePhotosListener.php b/apps/files/lib/Listener/SyncLivePhotosListener.php index 1b60fcb227b..6334e5d16a6 100644 --- a/apps/files/lib/Listener/SyncLivePhotosListener.php +++ b/apps/files/lib/Listener/SyncLivePhotosListener.php @@ -18,7 +18,6 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Exceptions\AbortedEventException; 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; @@ -78,7 +77,8 @@ class SyncLivePhotosListener implements IEventListener { } if ($event instanceof BeforeNodeRenamedEvent) { - $this->handleMove($event->getSource(), $event->getTarget(), $peerFile, false); + $this->runMoveOrCopyChecks($event->getSource(), $event->getTarget(), $peerFile); + $this->handleMove($event->getSource(), $event->getTarget(), $peerFile); } elseif ($event instanceof BeforeNodeDeletedEvent) { $this->handleDeletion($event, $peerFile); } elseif ($event instanceof CacheEntryRemovedEvent) { @@ -87,18 +87,12 @@ class SyncLivePhotosListener implements IEventListener { } } - /** - * During rename events, which also include move operations, - * we rename the peer file using the same name. - * The event listener being singleton, we can store the current state - * of pending renames inside the 'pendingRenames' property, - * to prevent infinite recursive. - */ - private function handleMove(Node $sourceFile, Node $targetFile, Node $peerFile, bool $prepForCopyOnly = false): void { + private function runMoveOrCopyChecks(Node $sourceFile, Node $targetFile, Node $peerFile): void { $targetParent = $targetFile->getParent(); $sourceExtension = $sourceFile->getExtension(); $peerFileExtension = $peerFile->getExtension(); $targetName = $targetFile->getName(); + $peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension; if (!str_ends_with($targetName, '.' . $sourceExtension)) { throw new AbortedEventException('Cannot change the extension of a Live Photo'); @@ -110,7 +104,6 @@ class SyncLivePhotosListener implements IEventListener { } catch (NotFoundException) { } - $peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension; if (!($targetParent instanceof NonExistingFolder)) { try { $targetParent->get($peerTargetName); @@ -118,9 +111,24 @@ class SyncLivePhotosListener implements IEventListener { } catch (NotFoundException) { } } + } + + /** + * During rename events, which also include move operations, + * we rename the peer file using the same name. + * The event listener being singleton, we can store the current state + * of pending renames inside the 'pendingRenames' property, + * to prevent infinite recursive. + */ + private function handleMove(Node $sourceFile, Node $targetFile, Node $peerFile): void { + $targetParent = $targetFile->getParent(); + $sourceExtension = $sourceFile->getExtension(); + $peerFileExtension = $peerFile->getExtension(); + $targetName = $targetFile->getName(); + $peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension; // in case the rename was initiated from this listener, we stop right now - if ($prepForCopyOnly || in_array($peerFile->getId(), $this->pendingRenames)) { + if (in_array($peerFile->getId(), $this->pendingRenames)) { return; } @@ -131,7 +139,7 @@ class SyncLivePhotosListener implements IEventListener { throw new AbortedEventException($ex->getMessage()); } - array_diff($this->pendingRenames, [$sourceFile->getId()]); + $this->pendingRenames = array_diff($this->pendingRenames, [$sourceFile->getId()]); } @@ -227,7 +235,7 @@ class SyncLivePhotosListener implements IEventListener { } if ($event instanceof BeforeNodeCopiedEvent) { - $this->handleMove($sourceNode, $targetNode, $peerFile, true); + $this->runMoveOrCopyChecks($sourceNode, $targetNode, $peerFile); } elseif ($event instanceof NodeCopiedEvent) { $this->handleCopy($sourceNode, $targetNode, $peerFile); } |