From 390585217256d316cbc4369bf88f8f16dd730154 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Tue, 28 Dec 2021 11:43:39 +0100 Subject: fix other stuff after PR comments Signed-off-by: Julien Veyssier --- lib/Controller/ImageController.php | 25 ++++++++++---------- lib/Service/ImageService.php | 47 ++++++++++++-------------------------- 2 files changed, 27 insertions(+), 45 deletions(-) (limited to 'lib') diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index 3f50f0363..fc2e1a4bd 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -200,24 +200,25 @@ class ImageController extends Controller { * @param string|null $shareToken * @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 $documentId, int $sessionId, string $sessionToken, ?string $shareToken = null, string $imageFileName): DataDisplayResponse { if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) { return new DataDisplayResponse('', Http::STATUS_NOT_FOUND); } - if ($shareToken) { - $imageFile = $this->imageService->getImagePublic($documentId, $imageFileName, $shareToken); - } else { - $imageFile = $this->imageService->getImage($documentId, $imageFileName, $this->userId); - } - if ($imageFile !== null) { - return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]); - } else { + try { + if ($shareToken) { + $imageFile = $this->imageService->getImagePublic($documentId, $imageFileName, $shareToken); + } else { + $imageFile = $this->imageService->getImage($documentId, $imageFileName, $this->userId); + } + if ($imageFile !== null) { + return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]); + } else { + return new DataDisplayResponse('', Http::STATUS_NOT_FOUND); + } + } catch (Exception $e) { + $this->logger->error('getImage error', ['exception' => $e]); return new DataDisplayResponse('', Http::STATUS_NOT_FOUND); } } diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index db691a95f..778be8c82 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -94,7 +94,7 @@ class ImageService { */ public function getImage(int $documentId, string $imageFileName, string $userId) { $textFile = $this->getTextFile($documentId, $userId); - return $textFile === null ? null : $this->getImagePreview($imageFileName, $textFile); + return $this->getImagePreview($imageFileName, $textFile); } /** @@ -109,7 +109,7 @@ class ImageService { */ public function getImagePublic(int $documentId, string $imageFileName, string $shareToken) { $textFile = $this->getTextFilePublic($documentId, $shareToken); - return $textFile === null ? null : $this->getImagePreview($imageFileName, $textFile); + return $this->getImagePreview($imageFileName, $textFile); } /** @@ -380,28 +380,6 @@ class ImageService { } } - /** - * Get or create the user-specific attachment folder - * - * @param string $userId - * @return Folder|null - * @throws NotFoundException - * @throws \OCP\Files\NotPermittedException - */ - private function getOrCreateTextDirectory(string $userId): ?Folder { - $userFolder = $this->rootFolder->getUserFolder($userId); - if ($userFolder->nodeExists('/Text')) { - $node = $userFolder->get('Text'); - if ($node instanceof Folder) { - return $node; - } else { - return null; - } - } else { - return $userFolder->newFolder('/Text'); - } - } - /** * Get or create file-specific attachment folder * @@ -474,17 +452,20 @@ class ImageService { * Get a user file from file ID * * @param int $documentId - * @param string $userId - * @return File|null - * @throws \OCP\Files\NotPermittedException + * @param string $userIdd + * @return File + * @throws NotFoundException + * @throws NotPermittedException + * @throws \OC\User\NoUserException */ - private function getTextFile(int $documentId, string $userId): ?File { + private function getTextFile(int $documentId, string $userId): File { $userFolder = $this->rootFolder->getUserFolder($userId); $textFile = $userFolder->getById($documentId); if (count($textFile) > 0 && $textFile[0] instanceof File) { return $textFile[0]; + } else { + throw new NotFoundException('Text file with id=' . $documentId . ' was not found in storage of ' . $userId); } - return null; } /** @@ -492,10 +473,10 @@ class ImageService { * * @param int|null $documentId * @param string $shareToken - * @return File|null + * @return File * @throws NotFoundException */ - private function getTextFilePublic(?int $documentId, string $shareToken): ?File { + private function getTextFilePublic(?int $documentId, string $shareToken): File { // is the file shared with this token? try { $share = $this->shareManager->getShareByToken($shareToken); @@ -517,9 +498,9 @@ class ImageService { } } } catch (ShareNotFound $e) { - return null; + // same as below } - return null; + throw new NotFoundException('Text file with id=' . $documentId . ' and shareToken ' . $shareToken . ' was not found.'); } /** -- cgit v1.2.3