Merge pull request #52792 from nextcloud/fix/mime-fallback-public

pull/52798/head
John Molakvoæ 5 months ago committed by GitHub
commit 3aae7ae305
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 14
      apps/files_sharing/lib/Controller/PublicPreviewController.php
  2. 23
      apps/files_sharing/openapi.json
  3. 4
      apps/files_sharing/tests/Controller/PublicPreviewControllerTest.php
  4. 23
      openapi.json

@ -11,6 +11,7 @@ use OCP\AppFramework\Http\Attribute\OpenAPI;
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;
@ -18,6 +19,7 @@ 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;
@ -33,6 +35,7 @@ class PublicPreviewController extends PublicShareController {
private ShareManager $shareManager,
ISession $session,
private IPreview $previewManager,
private IMimeIconProvider $mimeIconProvider,
) {
parent::__construct($appName, $request, $session);
}
@ -63,9 +66,11 @@ class PublicPreviewController extends PublicShareController {
* @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
@ -79,6 +84,7 @@ class PublicPreviewController extends PublicShareController {
int $x = 32,
int $y = 32,
$a = false,
bool $mimeFallback = false,
) {
$cacheForSeconds = 60 * 60 * 24; // 1 day
@ -124,6 +130,12 @@ class PublicPreviewController extends PublicShareController {
$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);

@ -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": {
@ -1514,6 +1527,16 @@
"schema": {}
}
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"headers": {
"Location": {
"schema": {
"type": "string"
}
}
}
}
}
}

@ -18,6 +18,7 @@ use OCP\Files\SimpleFS\ISimpleFile;
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;
@ -52,7 +53,8 @@ class PublicPreviewControllerTest extends TestCase {
$this->request,
$this->shareManager,
$this->createMock(ISession::class),
$this->previewManager
$this->previewManager,
$this->createMock(IMimeIconProvider::class),
);
}

@ -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": {
@ -19733,6 +19746,16 @@
"schema": {}
}
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"headers": {
"Location": {
"schema": {
"type": "string"
}
}
}
}
}
}

Loading…
Cancel
Save