diff options
author | John Molakvoæ <skjnldsv@protonmail.com> | 2023-08-11 14:32:42 +0200 |
---|---|---|
committer | John Molakvoæ <skjnldsv@protonmail.com> | 2023-08-17 18:56:38 +0200 |
commit | 28725c46a84db6e733c42e38cbc5e1b7f0e3854b (patch) | |
tree | 6915d025434815635017439555006a3d2e5e0ee8 /core/Controller/PreviewController.php | |
parent | 9df5212a40f5a5011755f987eaf9b63652ef6848 (diff) | |
download | nextcloud-server-28725c46a84db6e733c42e38cbc5e1b7f0e3854b.tar.gz nextcloud-server-28725c46a84db6e733c42e38cbc5e1b7f0e3854b.zip |
feat: redirect to the mime icon if no preview available
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'core/Controller/PreviewController.php')
-rw-r--r-- | core/Controller/PreviewController.php | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php index c9183466f90..4cfc8143491 100644 --- a/core/Controller/PreviewController.php +++ b/core/Controller/PreviewController.php @@ -32,12 +32,14 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\AppFramework\Http\RedirectResponse; use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IPreview; use OCP\IRequest; +use OCP\Preview\IMimeIconProvider; class PreviewController extends Controller { public function __construct( @@ -46,6 +48,7 @@ class PreviewController extends Controller { private IPreview $preview, private IRootFolder $root, private ?string $userId, + private IMimeIconProvider $mimeIconProvider, ) { parent::__construct($appName, $request); } @@ -62,9 +65,11 @@ class PreviewController extends Controller { * @param bool $a Whether to not crop the preview * @param bool $forceIcon Force returning an icon * @param string $mode How to crop the image - * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}> + * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available + * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}> * * 200: Preview returned + * 303: Redirect to the mime icon url if mimeFallback is true * 400: Getting preview is not possible * 403: Getting preview is not allowed * 404: Preview not found @@ -75,7 +80,8 @@ class PreviewController extends Controller { int $y = 32, bool $a = false, bool $forceIcon = true, - string $mode = 'fill'): Http\Response { + string $mode = 'fill', + bool $mimeFallback): Http\Response { if ($file === '' || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } @@ -87,7 +93,7 @@ class PreviewController extends Controller { return new DataResponse([], Http::STATUS_NOT_FOUND); } - return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode); + return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback); } /** @@ -102,9 +108,11 @@ class PreviewController extends Controller { * @param bool $a Whether to not crop the preview * @param bool $forceIcon Force returning an icon * @param string $mode How to crop the image - * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}> + * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available + * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}> * * 200: Preview returned + * 303: Redirect to the mime icon url if mimeFallback is true * 400: Getting preview is not possible * 403: Getting preview is not allowed * 404: Preview not found @@ -115,7 +123,8 @@ class PreviewController extends Controller { int $y = 32, bool $a = false, bool $forceIcon = true, - string $mode = 'fill') { + string $mode = 'fill', + bool $mimeFallback = false) { if ($fileId === -1 || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } @@ -129,11 +138,11 @@ class PreviewController extends Controller { $node = array_pop($nodes); - return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode); + return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback); } /** - * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}> + * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}> */ private function fetchPreview( Node $node, @@ -141,7 +150,8 @@ class PreviewController extends Controller { int $y, bool $a, bool $forceIcon, - string $mode) : Http\Response { + string $mode, + bool $mimeFallback = false) : Http\Response { if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) { return new DataResponse([], Http::STATUS_NOT_FOUND); } @@ -167,6 +177,13 @@ class PreviewController extends Controller { $response->cacheFor(3600 * 24, false, true); return $response; } catch (NotFoundException $e) { + // If we have no preview enabled, we can redirect to the mime icon if any + if ($mimeFallback) { + if ($url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType())) { + return new RedirectResponse($url); + } + } + return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (\InvalidArgumentException $e) { return new DataResponse([], Http::STATUS_BAD_REQUEST); |