|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2024 Louis Chmn <louis@chmn.me> |
| 7 | + * |
| 8 | + * @author Louis Chmn <louis@chmn.me> |
| 9 | + * |
| 10 | + * @license GNU AGPL-3.0-or-later |
| 11 | + * |
| 12 | + * This code is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 14 | + * as published by the Free Software Foundation. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 23 | + * |
| 24 | + */ |
| 25 | +namespace OCA\Files_Versions\Listener; |
| 26 | + |
| 27 | +use Exception; |
| 28 | +use OC\Files\Node\NonExistingFile; |
| 29 | +use OCA\Files_Versions\Versions\IVersionBackend; |
| 30 | +use OCA\Files_Versions\Versions\IVersionManager; |
| 31 | +use OCA\Files_Versions\Versions\IVersionsImporterBackend; |
| 32 | +use OCP\EventDispatcher\Event; |
| 33 | +use OCP\EventDispatcher\IEventListener; |
| 34 | +use OCP\Files\Events\Node\AbstractNodesEvent; |
| 35 | +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; |
| 36 | +use OCP\Files\Events\Node\NodeCopiedEvent; |
| 37 | +use OCP\Files\Events\Node\NodeRenamedEvent; |
| 38 | +use OCP\Files\File; |
| 39 | +use OCP\Files\Folder; |
| 40 | +use OCP\Files\Node; |
| 41 | +use OCP\Files\Storage\IStorage; |
| 42 | +use OCP\IUser; |
| 43 | +use OCP\IUserSession; |
| 44 | + |
| 45 | +/** @template-implements IEventListener<Event> */ |
| 46 | +class VersionStorageMoveListener implements IEventListener { |
| 47 | + /** @var File[] */ |
| 48 | + private array $movedNodes = []; |
| 49 | + |
| 50 | + public function __construct( |
| 51 | + private IVersionManager $versionManager, |
| 52 | + private IUserSession $userSession, |
| 53 | + ) { |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @abstract Moves version across storages if necessary. |
| 58 | + * @throws Exception No user in session |
| 59 | + */ |
| 60 | + public function handle(Event $event): void { |
| 61 | + if (!($event instanceof AbstractNodesEvent)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $source = $event->getSource(); |
| 66 | + $target = $event->getTarget(); |
| 67 | + |
| 68 | + $sourceStorage = $this->getNodeStorage($source); |
| 69 | + $targetStorage = $this->getNodeStorage($target); |
| 70 | + |
| 71 | + $sourceBackend = $this->versionManager->getBackendForStorage($sourceStorage); |
| 72 | + $targetBackend = $this->versionManager->getBackendForStorage($targetStorage); |
| 73 | + |
| 74 | + // If same backend, nothing to do. |
| 75 | + if ($sourceBackend === $targetBackend) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $user = $this->userSession->getUser() ?? $source->getOwner(); |
| 80 | + |
| 81 | + if ($user === null) { |
| 82 | + throw new Exception("Cannot move versions across storages without a user."); |
| 83 | + } |
| 84 | + |
| 85 | + if ($event instanceof BeforeNodeRenamedEvent) { |
| 86 | + $this->recursivelyPrepareMove($source); |
| 87 | + } elseif ($event instanceof NodeRenamedEvent || $event instanceof NodeCopiedEvent) { |
| 88 | + $this->recursivelyHandleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Store all sub files in this->movedNodes so their info can be used after the operation. |
| 94 | + */ |
| 95 | + private function recursivelyPrepareMove(Node $source): void { |
| 96 | + if ($source instanceof File) { |
| 97 | + $this->movedNodes[$source->getId()] = $source; |
| 98 | + } elseif ($source instanceof Folder) { |
| 99 | + foreach ($source->getDirectoryListing() as $child) { |
| 100 | + $this->recursivelyPrepareMove($child); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Call handleMoveOrCopy on each sub files |
| 107 | + * @param NodeRenamedEvent|NodeCopiedEvent $event |
| 108 | + */ |
| 109 | + private function recursivelyHandleMoveOrCopy(Event $event, IUser $user, Node $source, Node $target, IVersionBackend $sourceBackend, IVersionBackend $targetBackend): void { |
| 110 | + if ($target instanceof File) { |
| 111 | + /** @var File $source */ |
| 112 | + $this->handleMoveOrCopy($event, $user, $source, $target, $sourceBackend, $targetBackend); |
| 113 | + } elseif ($target instanceof Folder) { |
| 114 | + /** @var Folder $source */ |
| 115 | + foreach ($target->getDirectoryListing() as $targetChild) { |
| 116 | + if ($event instanceof NodeRenamedEvent) { |
| 117 | + $sourceChild = $this->movedNodes[$target->getId()]; |
| 118 | + } else { |
| 119 | + $sourceChild = $source->get($targetChild->getName()); |
| 120 | + } |
| 121 | + $this->recursivelyHandleMoveOrCopy($event, $user, $sourceChild, $targetChild, $sourceBackend, $targetBackend); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Called only during NodeRenamedEvent or NodeCopiedEvent |
| 128 | + * Will send the source node versions to the new backend, and then delete them from the old backend. |
| 129 | + * @param NodeRenamedEvent|NodeCopiedEvent $event |
| 130 | + */ |
| 131 | + private function handleMoveOrCopy(Event $event, IUser $user, File $source, File $target, IVersionBackend $sourceBackend, IVersionBackend $targetBackend): void { |
| 132 | + if ($targetBackend instanceof IVersionsImporterBackend) { |
| 133 | + $versions = $sourceBackend->getVersionsForFile($user, $source); |
| 134 | + $targetBackend->importVersionsForFile($user, $source, $target, $versions); |
| 135 | + } |
| 136 | + |
| 137 | + if ($event instanceof NodeRenamedEvent && $sourceBackend instanceof IVersionsImporterBackend) { |
| 138 | + $sourceBackend->clearVersionsForFile($user, $source, $target); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private function getNodeStorage(Node $node): IStorage { |
| 143 | + if ($node instanceof NonExistingFile) { |
| 144 | + return $node->getParent()->getStorage(); |
| 145 | + } else { |
| 146 | + return $node->getStorage(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments