Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion apps/files_sharing/lib/Controller/PublicPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\PublicShareController;
use OCP\Constants;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\IPreview;
use OCP\IRequest;
use OCP\ISession;
use OCP\Preview\IMimeIconProvider;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
Expand All @@ -33,6 +35,7 @@ public function __construct(
private ShareManager $shareManager,
ISession $session,
private IPreview $previewManager,
private IMimeIconProvider $mimeIconProvider,
) {
parent::__construct($appName, $request, $session);
}
Expand Down Expand Up @@ -63,9 +66,11 @@ protected function isPasswordProtected(): bool {
* @param int $x Width of the preview
* @param int $y Height of the preview
* @param bool $a Whether to not crop the preview
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<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, list<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: Share or preview not found
Expand All @@ -79,6 +84,7 @@ public function getPreview(
int $x = 32,
int $y = 32,
$a = false,
bool $mimeFallback = false,
) {
$cacheForSeconds = 60 * 60 * 24; // 1 day

Expand Down Expand Up @@ -124,6 +130,12 @@ public function getPreview(
$response->cacheFor($cacheForSeconds);
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($file->getMimeType())) {
return new RedirectResponse($url);
}
}
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
Expand Down
23 changes: 23 additions & 0 deletions apps/files_sharing/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,19 @@
1
]
}
},
{
"name": "mimeFallback",
"in": "query",
"description": "Whether to fallback to the mime icon if no preview is available",
"schema": {
"type": "integer",
"default": 0,
"enum": [
0,
1
]
}
}
],
"responses": {
Expand Down Expand Up @@ -1514,6 +1527,16 @@
"schema": {}
}
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"headers": {
"Location": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\IPreview;
use OCP\IRequest;
use OCP\ISession;
use OCP\Preview\IMimeIconProvider;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IAttributes;
use OCP\Share\IManager;
Expand Down Expand Up @@ -52,7 +53,8 @@ protected function setUp(): void {
$this->request,
$this->shareManager,
$this->createMock(ISession::class),
$this->previewManager
$this->previewManager,
$this->createMock(IMimeIconProvider::class),
);
}

Expand Down
23 changes: 23 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -19696,6 +19696,19 @@
1
]
}
},
{
"name": "mimeFallback",
"in": "query",
"description": "Whether to fallback to the mime icon if no preview is available",
"schema": {
"type": "integer",
"default": 0,
"enum": [
0,
1
]
}
}
],
"responses": {
Expand Down Expand Up @@ -19733,6 +19746,16 @@
"schema": {}
}
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"headers": {
"Location": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
Loading