Skip to content

Commit

Permalink
Also cache nodes for copy
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Mar 26, 2024
1 parent 56d9a89 commit 6430699
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions apps/files_versions/lib/Listener/VersionStorageMoveListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\AbstractNodesEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
Expand All @@ -44,8 +45,8 @@

/** @template-implements IEventListener<Event> */
class VersionStorageMoveListener implements IEventListener {
/** @var File[] */
private array $movedNodes = [];
/** @var Node[] */
private array $originalSourceNodes = [];

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

if ($event instanceof BeforeNodeRenamedEvent) {
if ($event instanceof BeforeNodeRenamedEvent || $event instanceof BeforeNodeCopiedEvent) {
$this->recursivelyPrepareMove($source);
} elseif ($event instanceof NodeRenamedEvent || $event instanceof NodeCopiedEvent) {
$this->recursivelyHandleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend);
}
}

/**
* Store all sub files in this->movedNodes so their info can be used after the operation.
* Store all sub files in this->originalSourceNodes so their info can be used after the operation.
*/
private function recursivelyPrepareMove(Node $source): void {
if ($source instanceof File) {
$this->movedNodes[$source->getId()] = $source;
$this->originalSourceNodes[$source->getId()] = $source;
} elseif ($source instanceof Folder) {
$this->originalSourceNodes[$source->getId()] = $source;
foreach ($source->getDirectoryListing() as $child) {
$this->recursivelyPrepareMove($child);
}
Expand All @@ -108,20 +110,13 @@ private function recursivelyPrepareMove(Node $source): void {
*/
private function recursivelyHandleMoveOrCopy(Event $event, IUser $user, ?Node $source, Node $target, IVersionBackend $sourceBackend, IVersionBackend $targetBackend): void {
if ($target instanceof File) {
if ($event instanceof NodeRenamedEvent) {
$source = $this->movedNodes[$target->getId()];
}

/** @var File $source */
$this->handleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend);
$this->handleMoveOrCopy($event, $user, $this->originalSourceNodes[$target->getId()], $target, $sourceBackend, $targetBackend);

Check notice

Code scanning / Psalm

ArgumentTypeCoercion Note

Argument 3 of OCA\Files_Versions\Listener\VersionStorageMoveListener::handleMoveOrCopy expects OCP\Files\File, but parent type OCP\Files\Node provided
} elseif ($target instanceof Folder) {
/** @var Folder $source */
$source = $source ?? $this->originalSourceNodes[$target->getId()];
foreach ($target->getDirectoryListing() as $targetChild) {
if ($event instanceof NodeCopiedEvent) {
$sourceChild = $source->get($targetChild->getName());
}

$this->recursivelyHandleMoveOrCopy($event, $user, $sourceChild, $targetChild, $sourceBackend, $targetBackend);
$this->recursivelyHandleMoveOrCopy($event, $user, null, $targetChild, $sourceBackend, $targetBackend);
}
}
}
Expand Down

0 comments on commit 6430699

Please sign in to comment.