diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index 8c2bc1ee2c9..023f5d2cd18 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -478,18 +478,29 @@ public function cleanupAttachments(int $fileId): int { */ public static function getAttachmentNamesFromContent(string $content): array { $matches = []; + // get first level of parenthesis after ![.*] + $regex = '/!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\](\(((?>[^()]+)|(?-2))*\))/'; + preg_match_all( - // simple version with .+ between the brackets - // '/\!\[.+\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/', - // complex version of php-markdown - '/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/', + $regex, $content, $matches, PREG_SET_ORDER ); - return array_map(static function (array $match) { - return urldecode($match[1]); + + $linkTargets = array_map(static function (array $match) { + return trim(urldecode($match[1]), '()'); }, $matches); + + $attachmentNames = array_map(static function (string $target) { + preg_match('/^text:\/\/image\?imageFileName=(.*)$/', $target, $targetMatches); + return $targetMatches[1] ?? ''; + }, $linkTargets); + + // remove empty values + $nonEmptyAttachmentNames = array_filter($attachmentNames); + + return $nonEmptyAttachmentNames; } /** diff --git a/tests/TextTest.php b/tests/TextTest.php index 161446b8c81..fa068a81f81 100644 --- a/tests/TextTest.php +++ b/tests/TextTest.php @@ -20,6 +20,7 @@ public function testGetAttachmentNamesFromContent() { 'a](a.png', ',;:!?.ยง-_a_', 'a`a`.png', + 'aaa (2).png', ]; $content = "some content\n"; foreach ($contentNames as $name) {