Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Listeners/NodeCopiedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
}
}
34 changes: 34 additions & 0 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading