Skip to content

Commit 3aae7ae

Browse files
authored
Merge pull request #52792 from nextcloud/fix/mime-fallback-public
2 parents b67a437 + cd6a53e commit 3aae7ae

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

apps/files_sharing/lib/Controller/PublicPreviewController.php

Lines changed: 13 additions & 1 deletion
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
}
@@ -63,9 +66,11 @@ protected function isPasswordProtected(): bool {
6366
* @param int $x Width of the preview
6467
* @param int $y Height of the preview
6568
* @param bool $a Whether to not crop the preview
66-
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>
69+
* @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
70+
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
6771
*
6872
* 200: Preview returned
73+
* 303: Redirect to the mime icon url if mimeFallback is true
6974
* 400: Getting preview is not possible
7075
* 403: Getting preview is not allowed
7176
* 404: Share or preview not found
@@ -79,6 +84,7 @@ public function getPreview(
7984
int $x = 32,
8085
int $y = 32,
8186
$a = false,
87+
bool $mimeFallback = false,
8288
) {
8389
$cacheForSeconds = 60 * 60 * 24; // 1 day
8490

@@ -124,6 +130,12 @@ public function getPreview(
124130
$response->cacheFor($cacheForSeconds);
125131
return $response;
126132
} catch (NotFoundException $e) {
133+
// If we have no preview enabled, we can redirect to the mime icon if any
134+
if ($mimeFallback) {
135+
if ($url = $this->mimeIconProvider->getMimeIconUrl($file->getMimeType())) {
136+
return new RedirectResponse($url);
137+
}
138+
}
127139
return new DataResponse([], Http::STATUS_NOT_FOUND);
128140
} catch (\InvalidArgumentException $e) {
129141
return new DataResponse([], Http::STATUS_BAD_REQUEST);

apps/files_sharing/openapi.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,19 @@
14771477
1
14781478
]
14791479
}
1480+
},
1481+
{
1482+
"name": "mimeFallback",
1483+
"in": "query",
1484+
"description": "Whether to fallback to the mime icon if no preview is available",
1485+
"schema": {
1486+
"type": "integer",
1487+
"default": 0,
1488+
"enum": [
1489+
0,
1490+
1
1491+
]
1492+
}
14801493
}
14811494
],
14821495
"responses": {
@@ -1514,6 +1527,16 @@
15141527
"schema": {}
15151528
}
15161529
}
1530+
},
1531+
"303": {
1532+
"description": "Redirect to the mime icon url if mimeFallback is true",
1533+
"headers": {
1534+
"Location": {
1535+
"schema": {
1536+
"type": "string"
1537+
}
1538+
}
1539+
}
15171540
}
15181541
}
15191542
}

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

openapi.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19696,6 +19696,19 @@
1969619696
1
1969719697
]
1969819698
}
19699+
},
19700+
{
19701+
"name": "mimeFallback",
19702+
"in": "query",
19703+
"description": "Whether to fallback to the mime icon if no preview is available",
19704+
"schema": {
19705+
"type": "integer",
19706+
"default": 0,
19707+
"enum": [
19708+
0,
19709+
1
19710+
]
19711+
}
1969919712
}
1970019713
],
1970119714
"responses": {
@@ -19733,6 +19746,16 @@
1973319746
"schema": {}
1973419747
}
1973519748
}
19749+
},
19750+
"303": {
19751+
"description": "Redirect to the mime icon url if mimeFallback is true",
19752+
"headers": {
19753+
"Location": {
19754+
"schema": {
19755+
"type": "string"
19756+
}
19757+
}
19758+
}
1973619759
}
1973719760
}
1973819761
}

0 commit comments

Comments
 (0)