Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cypress/integration/images.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const checkImage = (documentId, imageName, imageId, index) => {
cy.log('Check the image is visible and well formed', { documentId, imageName, imageId, index, encodedName })
return new Cypress.Promise((resolve, reject) => {
cy.get('#editor [data-component="image-view"]')
.filter('[data-src="text://image?imageFileName=' + encodedName + '"]')
.filter('[data-src=".attachments.' + documentId + '/' + encodedName + '"]')
.find('.image__view') // wait for load finish
.within(($el) => {
// keep track that we have created this image in the attachment dir
Expand Down
4 changes: 2 additions & 2 deletions js/editor-rich.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor-rich.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files-modal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/files-modal.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function uploadImage(int $documentId, string $newFileName, $newFileResour
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $savedFile->getId(),
'documentId' => $textFile->getId(),
];
Expand Down Expand Up @@ -183,6 +184,7 @@ public function uploadImagePublic(?int $documentId, string $newFileName, $newFil
$savedFile = $saveDir->newFile($fileName, $newFileResource);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $savedFile->getId(),
'documentId' => $textFile->getId(),
];
Expand Down Expand Up @@ -227,6 +229,7 @@ private function copyImageFile(File $imageFile, Folder $saveDir, File $textFile)
// get file type and name
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
'id' => $targetFile->getId(),
'documentId' => $textFile->getId(),
];
Expand Down Expand Up @@ -457,7 +460,7 @@ public function cleanupAttachments(int $fileId): int {
$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) {
Expand All @@ -476,20 +479,35 @@ public function cleanupAttachments(int $fileId): int {
* @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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/components/EditorWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export default {
}

return this.$syncService.uploadImage(file).then((response) => {
this.insertAttachmentImage(response.data?.name, response.data?.id, position)
this.insertAttachmentImage(response.data?.name, response.data?.id, position, response.data?.dirname)
}).catch((error) => {
console.error(error)
showError(error?.response?.data?.error)
Expand All @@ -625,18 +625,18 @@ export default {
insertImagePath(imagePath) {
this.uploadingImages = true
this.$syncService.insertImageFile(imagePath).then((response) => {
this.insertAttachmentImage(response.data?.name, response.data?.id)
this.insertAttachmentImage(response.data?.name, response.data?.id, null, response.data?.dirname)
}).catch((error) => {
console.error(error)
showError(error?.response?.data?.error)
}).then(() => {
this.uploadingImages = false
})
},
insertAttachmentImage(name, fileId, position = null) {
insertAttachmentImage(name, fileId, position = null, dirname = '') {
// inspired by the fixedEncodeURIComponent function suggested in
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
const src = 'text://image?imageFileName='
const src = dirname + '/'
+ encodeURIComponent(name).replace(/[!'()*]/g, (c) => {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
Expand Down
Loading