Skip to content

Commit f833e2d

Browse files
authored
Merge pull request #52996 from nextcloud/fix/emit_hooks_on_copy
fix(node): emit hooks on `Node::copy()`
2 parents 85c141e + e5b4ae4 commit f833e2d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

apps/files/lib/Listener/SyncLivePhotosListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SyncLivePhotosListener implements IEventListener {
3737
private array $pendingRenames = [];
3838
/** @var Array<int, bool> */
3939
private array $pendingDeletion = [];
40+
/** @var Array<int> */
41+
private array $pendingCopies = [];
4042

4143
public function __construct(
4244
private ?Folder $userFolder,
@@ -153,7 +155,6 @@ private function handleCopy(File $sourceFile, File $targetFile, File $peerFile):
153155
$targetName = $targetFile->getName();
154156
$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;
155157

156-
157158
if ($targetParent->nodeExists($peerTargetName)) {
158159
// If the copy was a folder copy, then the peer file already exists.
159160
$targetPeerFile = $targetParent->get($peerTargetName);
@@ -225,6 +226,11 @@ private function handleCopyRecursive(Event $event, Node $sourceNode, Node $targe
225226
$this->handleCopyRecursive($event, $sourceChild, $targetChild);
226227
}
227228
} elseif ($sourceNode instanceof File && $targetNode instanceof File) {
229+
// in case the copy was initiated from this listener, we stop right now
230+
if (in_array($sourceNode->getId(), $this->pendingCopies)) {
231+
return;
232+
}
233+
228234
$peerFileId = $this->livePhotosService->getLivePhotoPeerId($sourceNode->getId());
229235
if ($peerFileId === null) {
230236
return;
@@ -234,11 +240,13 @@ private function handleCopyRecursive(Event $event, Node $sourceNode, Node $targe
234240
return;
235241
}
236242

243+
$this->pendingCopies[] = $peerFileId;
237244
if ($event instanceof BeforeNodeCopiedEvent) {
238245
$this->runMoveOrCopyChecks($sourceNode, $targetNode, $peerFile);
239246
} elseif ($event instanceof NodeCopiedEvent) {
240247
$this->handleCopy($sourceNode, $targetNode, $peerFile);
241248
}
249+
$this->pendingCopies = array_diff($this->pendingCopies, [$peerFileId]);
242250
} else {
243251
throw new Exception('Source and target type are not matching');
244252
}

lib/private/Files/View.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ public function copy($source, $target, $preserveMtime = false) {
938938

939939
try {
940940
$exists = $this->file_exists($target);
941-
if ($this->shouldEmitHooks()) {
941+
if ($this->shouldEmitHooks($target)) {
942942
\OC_Hook::emit(
943943
Filesystem::CLASSNAME,
944944
Filesystem::signal_copy,
@@ -978,7 +978,7 @@ public function copy($source, $target, $preserveMtime = false) {
978978
$this->changeLock($target, ILockingProvider::LOCK_SHARED);
979979
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
980980

981-
if ($this->shouldEmitHooks() && $result !== false) {
981+
if ($this->shouldEmitHooks($target) && $result !== false) {
982982
\OC_Hook::emit(
983983
Filesystem::CLASSNAME,
984984
Filesystem::signal_post_copy,

0 commit comments

Comments
 (0)