From 849b4f050569297ebbd8f9630f569dcb86fcbe60 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Fri, 22 Oct 2021 16:39:13 +0200 Subject: identify image by name instead of id in text file content Signed-off-by: Julien Veyssier --- lib/Controller/ImageController.php | 4 ++-- lib/Service/ImageService.php | 25 ++++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index 91bafa925..435b09559 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -89,8 +89,8 @@ class ImageController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function getImage(int $textFileId, int $imageFileId): DataDisplayResponse { - $imageContent = $this->imageService->getImage($textFileId, $imageFileId, $this->userId); + public function getImage(int $textFileId, string $imageFileName): DataDisplayResponse { + $imageContent = $this->imageService->getImage($textFileId, $imageFileName, $this->userId); if ($imageContent !== null) { return new DataDisplayResponse($imageContent); } else { diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index 3075ef5b8..0c3c06985 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -29,6 +29,7 @@ namespace OCA\Text\Service; use Exception; use OCP\Files\Folder; use OCP\Files\File; +use OCP\Files\NotFoundException; use Throwable; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; @@ -72,27 +73,25 @@ class ImageService { /** * @param int $textFileId - * @param int $imageFileId + * @param string $imageFileName * @param string $userId * @return string|null - * @throws \OCP\Files\NotFoundException + * @throws NotFoundException + * @throws \OCP\Files\InvalidPathException * @throws \OCP\Files\NotPermittedException * @throws \OCP\Lock\LockedException * @throws \OC\User\NoUserException */ - public function getImage(int $textFileId, int $imageFileId, string $userId): ?string { - error_log('ImageService::getImage'); + public function getImage(int $textFileId, string $imageFileName, string $userId): ?string { $attachmentFolder = $this->getOrCreateAttachmentDirectory($textFileId, $userId); if ($attachmentFolder !== null) { - error_log('ATT OK'); - $imageFile = $attachmentFolder->getById($imageFileId); - if (count($imageFile) > 0) { - error_log('IMG file found'); - $imageFile = $imageFile[0]; - if ($imageFile instanceof File) { - error_log('IMG IS FILE'); - return $imageFile->getContent(); - } + try { + $imageFile = $attachmentFolder->get($imageFileName); + } catch (NotFoundException $e) { + return null; + } + if ($imageFile instanceof File) { + return $imageFile->getContent(); } } return null; -- cgit v1.2.3