Skip to content

Commit f8972d6

Browse files
committed
fix(AttachmentService): update referenced attachment file IDs when copying markdown files
Signed-off-by: Peter Birrer <peter.birrer@optonic.com>
1 parent 7143598 commit f8972d6

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

lib/Listeners/NodeCopiedListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function handle(Event $event): void {
3636
&& $target instanceof File
3737
&& $target->getMimeType() === 'text/markdown'
3838
) {
39-
$this->attachmentService->copyAttachments($source, $target);
39+
$fileIdMapping = $this->attachmentService->copyAttachments($source, $target);
4040
$target->unlock(ILockingProvider::LOCK_SHARED);
4141
AttachmentService::replaceAttachmentFolderId($source, $target);
42+
AttachmentService::replaceAttachmentFileIds($target, $fileIdMapping);
4243
$target->lock(ILockingProvider::LOCK_SHARED);
4344
}
4445
}

lib/Service/AttachmentService.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,27 +693,34 @@ public function deleteAttachments(File $source): void {
693693
* @param File $source
694694
* @param File $target
695695
*
696+
* @return array file id translation map
696697
* @throws InvalidPathException
697698
* @throws NoUserException
698699
* @throws NotFoundException
699700
* @throws NotPermittedException
700701
* @throws LockedException
701702
*/
702-
public function copyAttachments(File $source, File $target): void {
703+
public function copyAttachments(File $source, File $target): array {
703704
try {
704705
$sourceAttachmentDir = $this->getAttachmentDirectoryForFile($source);
705706
} catch (NotFoundException $e) {
706707
// silently return if no attachment dir was found for source file
707-
return;
708+
return [];
708709
}
709710
// create a new attachment dir next to the new file
710711
$targetAttachmentDir = $this->getAttachmentDirectoryForFile($target, true);
711712
// copy the attachment files
713+
$fileIdMapping = [];
712714
foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) {
713715
if ($sourceAttachment instanceof File) {
714-
$targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent());
716+
$newFile = $targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent());
717+
$fileIdMapping[] = [
718+
$sourceAttachment->getId(),
719+
$newFile->getId()
720+
];
715721
}
716722
}
723+
return $fileIdMapping;
717724
}
718725

719726
public static function replaceAttachmentFolderId(File $source, File $target): void {
@@ -735,4 +742,18 @@ public static function replaceAttachmentFolderId(File $source, File $target): vo
735742
$target->putContent($content);
736743
}
737744
}
745+
746+
public static function replaceAttachmentFileIds(File $target, array $fileIdMapping): void {
747+
$targetId = $target->getId();
748+
$patterns = [];
749+
$replacements = [];
750+
foreach ($fileIdMapping as $mapping) {
751+
$patterns[] = '/\[\b((\S+\/f\/)' . $mapping[0] . ')\b\]\(\b\1\b/';
752+
$replacements[] = '[${2}' . $mapping[1] . '](${2}' . $mapping[1];
753+
}
754+
$content = preg_replace($patterns, $replacements, $target->getContent());
755+
if ($content !== null) {
756+
$target->putContent($content);
757+
}
758+
}
738759
}

0 commit comments

Comments
 (0)