diff options
author | Julien Veyssier <eneiluj@posteo.net> | 2022-09-20 18:19:10 +0300 |
---|---|---|
committer | Julien Veyssier <eneiluj@posteo.net> | 2022-09-20 19:49:38 +0300 |
commit | ea8be6b8428054f428964ed514f31375c2608aea (patch) | |
tree | 7d30b513362c3e987b62796b40634f98f326d069 | |
parent | 70b30d65d5628558eb771018431eab2c657a2449 (diff) |
add parameter to image attachment endpoint to choose if raw image is preferredenh/noid/optionally-serve-raw-image-attachment
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
-rw-r--r-- | lib/Controller/AttachmentController.php | 19 | ||||
-rw-r--r-- | lib/Service/AttachmentService.php | 40 | ||||
-rw-r--r-- | src/services/AttachmentResolver.js | 8 |
3 files changed, 47 insertions, 20 deletions
diff --git a/lib/Controller/AttachmentController.php b/lib/Controller/AttachmentController.php index c495c1aac..918ec92a5 100644 --- a/lib/Controller/AttachmentController.php +++ b/lib/Controller/AttachmentController.php @@ -54,6 +54,17 @@ class AttachmentController extends Controller { 'image/heic', 'image/heif', ]; + public const BROWSER_SUPPORTED_IMAGE_MIME_TYPES = [ + 'image/png', + 'image/jpeg', + 'image/jpg', + 'image/gif', + 'image/x-xbitmap', + 'image/x-ms-bmp', + 'image/bmp', + 'image/svg+xml', + 'image/webp', + ]; private AttachmentService $attachmentService; private LoggerInterface $logger; @@ -190,19 +201,21 @@ class AttachmentController extends Controller { * @param string $sessionToken * @param string $imageFileName * @param string|null $shareToken + * @param int $preferRawImage * @return DataDownloadResponse|DataResponse */ - public function getImageFile(int $documentId, int $sessionId, string $sessionToken, string $imageFileName, ?string $shareToken = null) { + public function getImageFile(int $documentId, int $sessionId, string $sessionToken, string $imageFileName, ?string $shareToken = null, + int $preferRawImage = 0) { if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) { return new DataResponse('', Http::STATUS_FORBIDDEN); } try { if ($shareToken) { - $imageFile = $this->attachmentService->getImageFilePublic($documentId, $imageFileName, $shareToken); + $imageFile = $this->attachmentService->getImageFilePublic($documentId, $imageFileName, $shareToken, $preferRawImage === 1); } else { $userId = $this->getUserIdFromSession($documentId, $sessionId, $sessionToken); - $imageFile = $this->attachmentService->getImageFile($documentId, $imageFileName, $userId); + $imageFile = $this->attachmentService->getImageFile($documentId, $imageFileName, $userId, $preferRawImage === 1); } return $imageFile !== null ? new DataDownloadResponse( diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 1a27265ef..f27a9de02 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -26,11 +26,14 @@ declare(strict_types=1); namespace OCA\Text\Service; +use OC\User\NoUserException; use OCA\Text\Controller\AttachmentController; use OCP\Constants; use OCP\Files\Folder; use OCP\Files\File; use OCP\Files\IMimeTypeDetector; +use OCP\Files\InvalidPathException; +use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; @@ -75,14 +78,16 @@ class AttachmentService { * @param int $documentId * @param string $imageFileName * @param string $userId - * @return File|\OCP\Files\Node|ISimpleFile|null + * @param bool $preferRawImage + * @return File|Node|ISimpleFile|null + * @throws InvalidPathException + * @throws NoUserException * @throws NotFoundException - * @throws \OCP\Files\InvalidPathException - * @throws \OCP\Files\NotPermittedException + * @throws NotPermittedException */ - public function getImageFile(int $documentId, string $imageFileName, string $userId) { + public function getImageFile(int $documentId, string $imageFileName, string $userId, bool $preferRawImage) { $textFile = $this->getTextFile($documentId, $userId); - return $this->getImageFilePreview($imageFileName, $textFile); + return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage); } /** @@ -90,33 +95,40 @@ class AttachmentService { * @param int $documentId * @param string $imageFileName * @param string $shareToken - * @return File|\OCP\Files\Node|ISimpleFile|null + * @param bool $preferRawImage + * @return File|Node|ISimpleFile|null * @throws NotFoundException * @throws NotPermittedException - * @throws \OCP\Files\InvalidPathException - * @throws \OC\User\NoUserException + * @throws InvalidPathException + * @throws NoUserException */ - public function getImageFilePublic(int $documentId, string $imageFileName, string $shareToken) { + public function getImageFilePublic(int $documentId, string $imageFileName, string $shareToken, bool $preferRawImage) { $textFile = $this->getTextFilePublic($documentId, $shareToken); - return $this->getImageFilePreview($imageFileName, $textFile); + return $this->getImageFileContent($imageFileName, $textFile, $preferRawImage); } /** * @param string $imageFileName * @param File $textFile - * @return File|\OCP\Files\Node|ISimpleFile|null + * @param bool $preferRawImage + * @return File|Node|ISimpleFile|null + * @throws InvalidPathException + * @throws NoUserException * @throws NotFoundException * @throws NotPermittedException - * @throws \OCP\Files\InvalidPathException - * @throws \OC\User\NoUserException */ - private function getImageFilePreview(string $imageFileName, File $textFile) { + private function getImageFileContent(string $imageFileName, File $textFile, bool $preferRawImage) { $attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true); $imageFile = $attachmentFolder->get($imageFileName); if ($imageFile instanceof File && in_array($imageFile->getMimetype(), AttachmentController::IMAGE_MIME_TYPES)) { + // we might prefer the raw image + if ($preferRawImage && in_array($imageFile->getMimetype(), AttachmentController::BROWSER_SUPPORTED_IMAGE_MIME_TYPES)) { + return $imageFile; + } if ($this->previewManager->isMimeSupported($imageFile->getMimeType())) { return $this->previewManager->getPreview($imageFile, 1024, 1024); } + // fallback: raw image return $imageFile; } return null; diff --git a/src/services/AttachmentResolver.js b/src/services/AttachmentResolver.js index ed6062b97..e88400dd7 100644 --- a/src/services/AttachmentResolver.js +++ b/src/services/AttachmentResolver.js @@ -118,7 +118,7 @@ export default class AttachmentResolver { }] } - #getImageAttachmentUrl(imageFileName) { + #getImageAttachmentUrl(imageFileName, preferRawImage = false) { if (!this.#session) { return this.#davUrl( `${this.#attachmentDirectory}/${imageFileName}` @@ -126,16 +126,18 @@ export default class AttachmentResolver { } if (this.#user || !this.#shareToken) { - return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}', { + return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}&preferRawImage={preferRawImage}', { ...this.#textApiParams(), imageFileName, + preferRawImage: preferRawImage ? 1 : 0, }) } - return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}&shareToken={shareToken}', { + return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}&shareToken={shareToken}&preferRawImage={preferRawImage}', { ...this.#textApiParams(), imageFileName, shareToken: this.#shareToken, + preferRawImage: preferRawImage ? 1 : 0, }) } |