From b697927a5ba3bb8795d5ceb061aa1795b99d1e4a Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 17 Feb 2026 18:19:33 +0100 Subject: [PATCH] fix(attachments): adjust fileId in attachments folder+path on folder copy Fixes: nextcloud/collectives#2086 Signed-off-by: Jonas --- lib/Listeners/NodeCopiedListener.php | 3 +++ lib/Service/AttachmentService.php | 34 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/Listeners/NodeCopiedListener.php b/lib/Listeners/NodeCopiedListener.php index ce9d1f01344..5f783d30b30 100644 --- a/lib/Listeners/NodeCopiedListener.php +++ b/lib/Listeners/NodeCopiedListener.php @@ -13,6 +13,7 @@ use OCP\EventDispatcher\IEventListener; use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\File; +use OCP\Files\Folder; use OCP\Lock\ILockingProvider; /** @@ -41,6 +42,8 @@ public function handle(Event $event): void { AttachmentService::replaceAttachmentFolderId($source, $target); AttachmentService::replaceAttachmentFileIds($target, $fileIdMapping); $target->lock(ILockingProvider::LOCK_SHARED); + } elseif ($source instanceof Folder && $target instanceof Folder) { + $this->attachmentService->copyAttachmentsInFolder($source, $target); } } } diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index b246c9028cc..e3654cd21ab 100755 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -689,6 +689,40 @@ public function copyAttachments(File $source, File $target): array { return $fileIdMapping; } + public function copyAttachmentsInFolder(Folder $sourceFolder, Folder $targetFolder): void { + foreach ($sourceFolder->getDirectoryListing() as $sourceNode) { + if ($sourceNode instanceof Folder && !str_starts_with($sourceNode->getName(), '.attachments.')) { + try { + $targetNode = $targetFolder->get($sourceNode->getName()); + } catch (NotFoundException) { + // ignore if target node doesn't exist + continue; + } + + if ($targetNode instanceof Folder) { + $this->copyAttachmentsInFolder($sourceNode, $targetNode); + } + } elseif ($sourceNode instanceof File + && $sourceNode->getMimeType() === 'text/markdown') { + $sourceAttachmentDirName = '.attachments.' . $sourceNode->getId(); + try { + $sourceAttachmentDir = $sourceFolder->get($sourceAttachmentDirName); + $targetNode = $targetFolder->get($sourceNode->getName()); + $targetAttachmentDirName = '.attachments.' . $targetNode->getId(); + $targetAttachmentDir = $targetFolder->get($sourceAttachmentDirName); + } catch (NotFoundException) { + // ignore if either of the attachment dirs don't exist + continue; + } + + if ($targetNode instanceof File && $targetAttachmentDir instanceof Folder) { + $targetAttachmentDir->move($targetFolder->getPath() . '/' . $targetAttachmentDirName); + self::replaceAttachmentFolderId($sourceNode, $targetNode); + } + } + } + } + /** * @throws NotFoundException * @throws NotPermittedException