Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulien Veyssier <eneiluj@posteo.net>2021-12-21 14:42:04 +0300
committerJulien Veyssier <eneiluj@posteo.net>2022-01-03 12:27:37 +0300
commit32c0a37669f7d0cc9ee23d891c1432e74f583de4 (patch)
treeee918405a47ae254ee17769a449dd1b78a94d6d5 /lib
parent4f54f4dee2eceae5e0036556a30c7d1dd1aea00b (diff)
use image file name instead of file ID in content links
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ImageController.php12
-rw-r--r--lib/Service/ImageService.php59
2 files changed, 31 insertions, 40 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 {