diff options
author | Julien Veyssier <eneiluj@posteo.net> | 2022-05-24 15:59:57 +0300 |
---|---|---|
committer | Julien Veyssier <eneiluj@posteo.net> | 2022-05-24 15:59:57 +0300 |
commit | c44751edc28faf397f3209b24d11647559fb3017 (patch) | |
tree | 44642f0994301427de4e1e56eb15c8fa5f9f6550 /lib/Service | |
parent | edfd0d20e27c7af7a000fc882ecf409df0a54524 (diff) |
manual backport of #2426, refs #2411, Improve image attachment management
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/ImageService.php | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index 8c2bc1ee2..901575355 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -156,6 +156,7 @@ class ImageService { $savedFile = $saveDir->newFile($fileName, $newFileResource); return [ 'name' => $fileName, + 'dirname' => $saveDir->getName(), 'id' => $savedFile->getId(), 'documentId' => $textFile->getId(), ]; @@ -183,6 +184,7 @@ class ImageService { $savedFile = $saveDir->newFile($fileName, $newFileResource); return [ 'name' => $fileName, + 'dirname' => $saveDir->getName(), 'id' => $savedFile->getId(), 'documentId' => $textFile->getId(), ]; @@ -227,6 +229,7 @@ class ImageService { // get file type and name return [ 'name' => $fileName, + 'dirname' => $saveDir->getName(), 'id' => $targetFile->getId(), 'documentId' => $textFile->getId(), ]; @@ -457,7 +460,7 @@ class ImageService { $attachmentsByName[$attNode->getName()] = $attNode; } - $contentAttachmentNames = $this->getAttachmentNamesFromContent($textFile->getContent()); + $contentAttachmentNames = $this->getAttachmentNamesFromContent($textFile->getContent(), $fileId); $toDelete = array_diff(array_keys($attachmentsByName), $contentAttachmentNames); foreach ($toDelete as $name) { @@ -476,20 +479,35 @@ class ImageService { * @param string $content * @return array */ - public static function getAttachmentNamesFromContent(string $content): array { - $matches = []; + public static function getAttachmentNamesFromContent(string $content, int $fileId): array { + $oldMatches = []; preg_match_all( // simple version with .+ between the brackets // '/\!\[.+\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/', // complex version of php-markdown + // matches ![ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](text://image?ANYTHING&imageFileName=FILE_NAME) and captures FILE_NAME '/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/', $content, + $oldMatches, + PREG_SET_ORDER + ); + $oldNames = array_map(static function (array $match) { + return urldecode($match[1]); + }, $oldMatches); + + $matches = []; + // matches ![ANY_CONSIDERED_CORRECT_BY_PHP-MARKDOWN](.attachments.DOCUMENT_ID/ANY_FILE_NAME) and captures FILE_NAME + preg_match_all( + '/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(\.attachments\.'.$fileId.'\/([^)&]+)\)/', + $content, $matches, PREG_SET_ORDER ); - return array_map(static function (array $match) { + $names = array_map(static function (array $match) { return urldecode($match[1]); }, $matches); + + return array_merge($names, $oldNames); } /** |