Skip to content

Commit 041f85b

Browse files
committed
fix(files_sharing): add mime fallback to public preview too
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent 56897b6 commit 041f85b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

apps/files_sharing/lib/Controller/PublicPreviewController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use OCP\AppFramework\Http\Attribute\PublicPage;
1212
use OCP\AppFramework\Http\DataResponse;
1313
use OCP\AppFramework\Http\FileDisplayResponse;
14+
use OCP\AppFramework\Http\RedirectResponse;
1415
use OCP\AppFramework\PublicShareController;
1516
use OCP\Constants;
1617
use OCP\Files\Folder;
1718
use OCP\Files\NotFoundException;
1819
use OCP\IPreview;
1920
use OCP\IRequest;
2021
use OCP\ISession;
22+
use OCP\Preview\IMimeIconProvider;
2123
use OCP\Share\Exceptions\ShareNotFound;
2224
use OCP\Share\IManager as ShareManager;
2325
use OCP\Share\IShare;
@@ -33,6 +35,7 @@ public function __construct(
3335
private ShareManager $shareManager,
3436
ISession $session,
3537
private IPreview $previewManager,
38+
private IMimeIconProvider $mimeIconProvider,
3639
) {
3740
parent::__construct($appName, $request, $session);
3841
}
@@ -79,6 +82,7 @@ public function getPreview(
7982
int $x = 32,
8083
int $y = 32,
8184
$a = false,
85+
bool $mimeFallback = false,
8286
) {
8387
$cacheForSeconds = 60 * 60 * 24; // 1 day
8488

@@ -124,6 +128,12 @@ public function getPreview(
124128
$response->cacheFor($cacheForSeconds);
125129
return $response;
126130
} catch (NotFoundException $e) {
131+
// If we have no preview enabled, we can redirect to the mime icon if any
132+
if ($mimeFallback) {
133+
if ($url = $this->mimeIconProvider->getMimeIconUrl($file->getMimeType())) {
134+
return new RedirectResponse($url);
135+
}
136+
}
127137
return new DataResponse([], Http::STATUS_NOT_FOUND);
128138
} catch (\InvalidArgumentException $e) {
129139
return new DataResponse([], Http::STATUS_BAD_REQUEST);

apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\IPreview;
1919
use OCP\IRequest;
2020
use OCP\ISession;
21+
use OCP\Preview\IMimeIconProvider;
2122
use OCP\Share\Exceptions\ShareNotFound;
2223
use OCP\Share\IAttributes;
2324
use OCP\Share\IManager;
@@ -52,7 +53,8 @@ protected function setUp(): void {
5253
$this->request,
5354
$this->shareManager,
5455
$this->createMock(ISession::class),
55-
$this->previewManager
56+
$this->previewManager,
57+
$this->createMock(IMimeIconProvider::class),
5658
);
5759
}
5860

0 commit comments

Comments
 (0)