3232use OCP \EventDispatcher \Event ;
3333use OCP \EventDispatcher \IEventListener ;
3434use OCP \Files \Events \Node \AbstractNodesEvent ;
35+ use OCP \Files \Events \Node \BeforeNodeCopiedEvent ;
3536use OCP \Files \Events \Node \BeforeNodeRenamedEvent ;
3637use OCP \Files \Events \Node \NodeCopiedEvent ;
3738use OCP \Files \Events \Node \NodeRenamedEvent ;
4445
4546/** @template-implements IEventListener<Event> */
4647class 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 }
@@ -108,20 +110,13 @@ private function recursivelyPrepareMove(Node $source): void {
108110 */
109111 private function recursivelyHandleMoveOrCopy (Event $ event , IUser $ user , ?Node $ source , Node $ target , IVersionBackend $ sourceBackend , IVersionBackend $ targetBackend ): void {
110112 if ($ target instanceof File) {
111- if ($ event instanceof NodeRenamedEvent) {
112- $ source = $ this ->movedNodes [$ target ->getId ()];
113- }
114-
115113 /** @var File $source */
116- $ this ->handleMoveOrCopy ($ event , $ user , $ source , $ target , $ sourceBackend , $ targetBackend );
114+ $ this ->handleMoveOrCopy ($ event , $ user , $ this -> originalSourceNodes [ $ target -> getId ()] , $ target , $ sourceBackend , $ targetBackend );
117115 } elseif ($ target instanceof Folder) {
118116 /** @var Folder $source */
117+ $ source = $ source ?? $ this ->originalSourceNodes [$ target ->getId ()];
119118 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 );
119+ $ this ->recursivelyHandleMoveOrCopy ($ event , $ user , null , $ targetChild , $ sourceBackend , $ targetBackend );
125120 }
126121 }
127122 }
0 commit comments