diff options
-rw-r--r-- | lib/Controller/ImageController.php | 12 | ||||
-rw-r--r-- | lib/Service/ImageService.php | 59 | ||||
-rw-r--r-- | src/components/MenuBar.vue | 2 | ||||
-rw-r--r-- | src/nodes/ImageView.vue | 10 |
4 files changed, 37 insertions, 46 deletions
diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index 86111846a..8c18dd57e 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -194,15 +194,15 @@ class ImageController extends Controller { * * Serve the images in the editor * @param int $textFileId - * @param int $imageFileId + * @param string $imageFileName * @return DataDisplayResponse * @throws \OCP\Files\InvalidPathException * @throws \OCP\Files\NotFoundException * @throws \OCP\Files\NotPermittedException * @throws \OCP\Lock\LockedException */ - public function getImage(int $textFileId, int $imageFileId): DataDisplayResponse { - $imageFile = $this->imageService->getImage($textFileId, $imageFileId, $this->userId); + public function getImage(int $textFileId, string $imageFileName): DataDisplayResponse { + $imageFile = $this->imageService->getImage($textFileId, $imageFileName, $this->userId); if ($imageFile !== null) { return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]); } else { @@ -216,7 +216,7 @@ class ImageController extends Controller { * @PublicPage * * @param int $textFileId - * @param int $imageFileId + * @param string $imageFileName * @param string $shareToken * @return DataDisplayResponse * @throws \OCP\Files\InvalidPathException @@ -224,8 +224,8 @@ class ImageController extends Controller { * @throws \OCP\Files\NotPermittedException * @throws \OCP\Lock\LockedException */ - public function getImagePublic(int $textFileId, int $imageFileId, string $shareToken): DataDisplayResponse { - $imageFile = $this->imageService->getImagePublic($textFileId, $imageFileId, $shareToken); + public function getImagePublic(int $textFileId, string $imageFileName, string $shareToken): DataDisplayResponse { + $imageFile = $this->imageService->getImagePublic($textFileId, $imageFileName, $shareToken); if ($imageFile !== null) { return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]); } else { diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index f407c16c1..2a714ebbc 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -86,49 +86,49 @@ class ImageService { /** * Get image content or preview from file id * @param int $textFileId - * @param int $imageFileId + * @param string $imageFileName * @param string $userId * @return File|\OCP\Files\Node|ISimpleFile|null * @throws NotFoundException * @throws \OCP\Files\InvalidPathException * @throws \OCP\Files\NotPermittedException */ - public function getImage(int $textFileId, int $imageFileId, string $userId) { + public function getImage(int $textFileId, string $imageFileName, string $userId) { $textFile = $this->getTextFile($textFileId, $userId); - return $textFile === null ? null : $this->getImagePreview($imageFileId, $textFile); + return $textFile === null ? null : $this->getImagePreview($imageFileName, $textFile); } /** * Get image content or preview from file id in public context * @param int $textFileId - * @param int $imageFileId + * @param string $imageFileName * @param string $shareToken * @return File|\OCP\Files\Node|ISimpleFile|null * @throws NotFoundException * @throws \OCP\Files\InvalidPathException * @throws \OCP\Files\NotPermittedException */ - public function getImagePublic(int $textFileId, int $imageFileId, string $shareToken) { + public function getImagePublic(int $textFileId, string $imageFileName, string $shareToken) { $textFile = $this->getTextFilePublic($textFileId, $shareToken); - return $textFile === null ? null : $this->getImagePreview($imageFileId, $textFile); + return $textFile === null ? null : $this->getImagePreview($imageFileName, $textFile); } /** - * @param int $imageFileId + * @param string $imageFileName * @param File $textFile * @return File|\OCP\Files\Node|ISimpleFile|null * @throws NotFoundException * @throws \OCP\Files\InvalidPathException * @throws \OCP\Files\NotPermittedException */ - private function getImagePreview(int $imageFileId, File $textFile) { + private function getImagePreview(string $imageFileName, File $textFile) { $attachmentFolder = $this->getOrCreateAttachmentDirectoryForFile($textFile); if ($attachmentFolder !== null) { - $imageFiles = $attachmentFolder->getById($imageFileId); - if (count($imageFiles) === 0) { + try { + $imageFile = $attachmentFolder->get($imageFileName); + } catch (NotFoundException $e) { return null; } - $imageFile = $imageFiles[0]; if ($imageFile instanceof File) { if ($this->previewManager->isMimeSupported($imageFile->getMimeType())) { return $this->previewManager->getPreview($imageFile, 1024, 1024); @@ -589,16 +589,16 @@ class ImageService { if ($textFile->getMimeType() === 'text/markdown') { // get IDs of the files inside the attachment dir $attachmentDir = $this->getOrCreateAttachmentDirectoryForFile($textFile); - $attachmentsById = []; + $attachmentsByName = []; foreach ($attachmentDir->getDirectoryListing() as $attNode) { - $attachmentsById[$attNode->getId()] = $attNode; + $attachmentsByName[$attNode->getName()] = $attNode; } - $contentAttachmentIds = $this->getAttachmentIdsFromContent($textFile->getContent()); + $contentAttachmentNames = $this->getAttachmentNamesFromContent($textFile->getContent()); - $toDelete = array_diff(array_keys($attachmentsById), $contentAttachmentIds); - foreach ($toDelete as $id) { - $attachmentsById[$id]->delete(); + $toDelete = array_diff(array_keys($attachmentsByName), $contentAttachmentNames); + foreach ($toDelete as $name) { + $attachmentsByName[$name]->delete(); } return count($toDelete); } @@ -608,15 +608,15 @@ class ImageService { /** - * Get IDs of attachment listed in the markdown file content + * Get attachment file names listed in the markdown file content * * @param string $content * @return array */ - private function getAttachmentIdsFromContent(string $content): array { + private function getAttachmentNamesFromContent(string $content): array { $matches = []; preg_match_all( - '/\!\[[^\[\]]+\]\(text:\/\/image\?[^)]*imageFileId=([\d]+)[^)]*\)/', + '/\!\[[^\[\]]+\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/', $content, $matches, PREG_SET_ORDER @@ -668,25 +668,16 @@ class ImageService { // create a new attachment dir next to the new file $targetAttachmentDir = $this->getOrCreateAttachmentDirectoryForFile($target); $markdownContent = $source->getContent(); - $sourceAttachmentIds = $this->getAttachmentIdsFromContent($markdownContent); // replace the text file ID in each attachment link in the markdown content $markdownContent = preg_replace( - '/\!\[([^\[\]]+)\]\(text:\/\/image\?textFileId=' . $source->getId() . '&imageFileId=([\d]+[^)]*)\)/', - '![$1](text://image?textFileId=' . $target->getId() . '&imageFileId=$2)', + '/\!\[([^\[\]]+)\]\(text:\/\/image\?textFileId=' . $source->getId() . '&imageFileName=([^)&]+)\)/', + '![$1](text://image?textFileId=' . $target->getId() . '&imageFileName=$2)', $markdownContent ); - // copy the attachments and replace attachment file IDs in the markdown content - foreach ($sourceAttachmentIds as $sourceAttachmentId) { - $sourceAttachment = $sourceAttachmentDir->getById($sourceAttachmentId); - if (count($sourceAttachment) > 0 && $sourceAttachment[0] instanceof File) { - $sourceAttachment = $sourceAttachment[0]; + // copy the attachment files + foreach ($sourceAttachmentDir->getDirectoryListing() as $sourceAttachment) { + if ($sourceAttachment instanceof File) { $copied = $targetAttachmentDir->newFile($sourceAttachment->getName(), $sourceAttachment->getContent()); - $copiedId = $copied->getId(); - $markdownContent = preg_replace( - '/\!\[([^\[\]]+)\]\(text:\/\/image\?([^)]*)imageFileId=' . $sourceAttachmentId . '([^)]*)\)/', - '![$1](text://image?$2imageFileId=' . $copiedId . '$3)', - $markdownContent - ); } } try { diff --git a/src/components/MenuBar.vue b/src/components/MenuBar.vue index 88fee8746..3e60aa40e 100644 --- a/src/components/MenuBar.vue +++ b/src/components/MenuBar.vue @@ -432,7 +432,7 @@ export default { }, false, [], true, undefined, this.imagePath) }, insertAttachmentImage(name, fileId, command, textFileId) { - const src = 'text://image?textFileId=' + textFileId + '&imageFileId=' + fileId + const src = 'text://image?textFileId=' + textFileId + '&imageFileName=' + encodeURIComponent(name) command({ src, alt: name, diff --git a/src/nodes/ImageView.vue b/src/nodes/ImageView.vue index df92bf71b..ce62fc7a1 100644 --- a/src/nodes/ImageView.vue +++ b/src/nodes/ImageView.vue @@ -118,19 +118,19 @@ export default { }, imageUrl() { if (this.src.startsWith('text://')) { - const imageFileId = getQueryVariable(this.src, 'imageFileId') + const imageFileName = getQueryVariable(this.src, 'imageFileName') const textFileId = getQueryVariable(this.src, 'textFileId') if (getCurrentUser()) { - return generateUrl('/apps/text/image?textFileId={textFileId}&imageFileId={imageFileId}', + return generateUrl('/apps/text/image?textFileId={textFileId}&imageFileName={imageFileName}', { textFileId, - imageFileId, + imageFileName, }) } else { - return generateUrl('/apps/text/public/image?textFileId={textFileId}&imageFileId={imageFileId}&shareToken={token}', + return generateUrl('/apps/text/public/image?textFileId={textFileId}&imageFileName={imageFileName}&shareToken={token}', { textFileId, - imageFileId, + imageFileName, token: this.token, }) } |