VersionStorageMoveListener.php 5.0 KB

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