Skip to content

Commit 0150a64

Browse files
committed
Also cache nodes for copy
Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 56d9a89 commit 0150a64

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

apps/files_versions/lib/Listener/VersionStorageMoveListener.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\EventDispatcher\Event;
3333
use OCP\EventDispatcher\IEventListener;
3434
use OCP\Files\Events\Node\AbstractNodesEvent;
35+
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
3536
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
3637
use OCP\Files\Events\Node\NodeCopiedEvent;
3738
use OCP\Files\Events\Node\NodeRenamedEvent;
@@ -44,8 +45,8 @@
4445

4546
/** @template-implements IEventListener<Event> */
4647
class VersionStorageMoveListener implements IEventListener {
47-
/** @var File[] */
48-
private array $movedNodes = [];
48+
/** @var Node[] */
49+
private array $originalSourceNodes = [];
4950

5051
public function __construct(
5152
private IVersionManager $versionManager,
@@ -82,20 +83,21 @@ public function handle(Event $event): void {
8283
throw new Exception("Cannot move versions across storages without a user.");
8384
}
8485

85-
if ($event instanceof BeforeNodeRenamedEvent) {
86+
if ($event instanceof BeforeNodeRenamedEvent || $event instanceof BeforeNodeCopiedEvent) {
8687
$this->recursivelyPrepareMove($source);
8788
} elseif ($event instanceof NodeRenamedEvent || $event instanceof NodeCopiedEvent) {
8889
$this->recursivelyHandleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend);
8990
}
9091
}
9192

9293
/**
93-
* Store all sub files in this->movedNodes so their info can be used after the operation.
94+
* Store all sub files in this->originalSourceNodes so their info can be used after the operation.
9495
*/
9596
private function recursivelyPrepareMove(Node $source): void {
9697
if ($source instanceof File) {
97-
$this->movedNodes[$source->getId()] = $source;
98+
$this->originalSourceNodes[$source->getId()] = $source;
9899
} elseif ($source instanceof Folder) {
100+
$this->originalSourceNodes[$source->getId()] = $source;
99101
foreach ($source->getDirectoryListing() as $child) {
100102
$this->recursivelyPrepareMove($child);
101103
}
@@ -107,21 +109,15 @@ private function recursivelyPrepareMove(Node $source): void {
107109
* @param NodeRenamedEvent|NodeCopiedEvent $event
108110
*/
109111
private function recursivelyHandleMoveOrCopy(Event $event, IUser $user, ?Node $source, Node $target, IVersionBackend $sourceBackend, IVersionBackend $targetBackend): void {
110-
if ($target instanceof File) {
111-
if ($event instanceof NodeRenamedEvent) {
112-
$source = $this->movedNodes[$target->getId()];
113-
}
112+
$source = $source ?? $this->originalSourceNodes[$target->getId()];
114113

114+
if ($target instanceof File) {
115115
/** @var File $source */
116116
$this->handleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend);
117117
} elseif ($target instanceof Folder) {
118118
/** @var Folder $source */
119119
foreach ($target->getDirectoryListing() as $targetChild) {
120-
if ($event instanceof NodeCopiedEvent) {
121-
$sourceChild = $source->get($targetChild->getName());
122-
}
123-
124-
$this->recursivelyHandleMoveOrCopy($event, $user, $sourceChild, $targetChild, $sourceBackend, $targetBackend);
120+
$this->recursivelyHandleMoveOrCopy($event, $user, null, $targetChild, $sourceBackend, $targetBackend);
125121
}
126122
}
127123
}

0 commit comments

Comments
 (0)