Skip to content

Commit

Permalink
fix(core): Always try to generate some kind of preview
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Feb 3, 2025
1 parent 52f3f2b commit 2aef985
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 143 deletions.
1 change: 0 additions & 1 deletion apps/files/lib/Dashboard/FavoriteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public function getItems(string $userId, int $limit = 7): array {
'y' => 256,
'fileId' => $node->getId(),
'c' => $node->getEtag(),
'mimeFallback' => true,
]);
$favoriteNodes[] = new WidgetItem(
$node->getName(),
Expand Down
1 change: 0 additions & 1 deletion apps/files/src/components/FileEntry/FileEntryPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export default defineComponent({
// Request tiny previews
url.searchParams.set('x', this.gridMode ? '128' : '32')
url.searchParams.set('y', this.gridMode ? '128' : '32')
url.searchParams.set('mimeFallback', 'true')

// Etag to force refresh preview on change
const etag = this.source?.attributes?.etag || ''
Expand Down
2 changes: 1 addition & 1 deletion apps/files_versions/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function formatVersion(version: any, fileInfo: any): Version {
let previewUrl = ''

if (mtime === fileInfo.mtime) { // Version is the current one
previewUrl = generateUrl('/core/preview?fileId={fileId}&c={fileEtag}&x=250&y=250&forceIcon=0&a=0', {
previewUrl = generateUrl('/core/preview?fileId={fileId}&c={fileEtag}&x=250&y=250&a=0', {
fileId: fileInfo.id,
fileEtag: fileInfo.etag,
})
Expand Down
35 changes: 14 additions & 21 deletions core/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\Files\Storage\ISharedStorage;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Preview\IMimeIconProvider;

class PreviewController extends Controller {
Expand All @@ -35,6 +36,7 @@ public function __construct(
private IRootFolder $root,
private ?string $userId,
private IMimeIconProvider $mimeIconProvider,
private IURLGenerator $urlGenerator,
) {
parent::__construct($appName, $request);
}
Expand All @@ -48,13 +50,11 @@ public function __construct(
* @param int $x Width of the preview. A width of -1 will use the original image width.
* @param int $y Height of the preview. A height of -1 will use the original image height.
* @param bool $a Preserve the aspect ratio
* @param bool $forceIcon Force returning an icon
* @param 'fill'|'cover' $mode How to crop the image
* @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
* 303: Redirect to the mime icon url
* 400: Getting preview is not possible
* 403: Getting preview is not allowed
* 404: Preview not found
Expand All @@ -68,9 +68,8 @@ public function getPreview(
int $x = 32,
int $y = 32,
bool $a = false,
bool $forceIcon = true,
string $mode = 'fill',
bool $mimeFallback = false): Http\Response {
): Http\Response {
if ($file === '' || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
Expand All @@ -82,7 +81,7 @@ public function getPreview(
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
return $this->fetchPreview($node, $x, $y, $a, $mode);
}

/**
Expand All @@ -92,13 +91,11 @@ public function getPreview(
* @param int $x Width of the preview. A width of -1 will use the original image width.
* @param int $y Height of the preview. A height of -1 will use the original image height.
* @param bool $a Preserve the aspect ratio
* @param bool $forceIcon Force returning an icon
* @param 'fill'|'cover' $mode How to crop the image
* @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
* 303: Redirect to the mime icon url
* 400: Getting preview is not possible
* 403: Getting preview is not allowed
* 404: Preview not found
Expand All @@ -112,9 +109,8 @@ public function getPreviewByFileId(
int $x = 32,
int $y = 32,
bool $a = false,
bool $forceIcon = true,
string $mode = 'fill',
bool $mimeFallback = false) {
) {
if ($fileId === -1 || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
Expand All @@ -126,7 +122,7 @@ public function getPreviewByFileId(
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
return $this->fetchPreview($node, $x, $y, $a, $mode);
}

/**
Expand All @@ -137,11 +133,10 @@ private function fetchPreview(
int $x,
int $y,
bool $a,
bool $forceIcon,
string $mode,
bool $mimeFallback = false) : Http\Response {
if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
) : Http\Response {
if (!($node instanceof File)) {
return new RedirectResponse($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'filetypes/folder.svg')));
}
if (!$node->isReadable()) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
Expand Down Expand Up @@ -175,13 +170,11 @@ private function fetchPreview(
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);
}
if ($url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType())) {
return new RedirectResponse($url);
}

return new DataResponse([], Http::STATUS_NOT_FOUND);
return new RedirectResponse($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'filetypes/file.svg')));
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
Expand Down
56 changes: 2 additions & 54 deletions core/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -8253,19 +8253,6 @@
]
}
},
{
"name": "forceIcon",
"in": "query",
"description": "Force returning an icon",
"schema": {
"type": "integer",
"default": 1,
"enum": [
0,
1
]
}
},
{
"name": "mode",
"in": "query",
Expand All @@ -8278,19 +8265,6 @@
"cover"
]
}
},
{
"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 @@ -8330,7 +8304,7 @@
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"description": "Redirect to the mime icon url",
"headers": {
"Location": {
"schema": {
Expand Down Expand Up @@ -8401,19 +8375,6 @@
]
}
},
{
"name": "forceIcon",
"in": "query",
"description": "Force returning an icon",
"schema": {
"type": "integer",
"default": 1,
"enum": [
0,
1
]
}
},
{
"name": "mode",
"in": "query",
Expand All @@ -8426,19 +8387,6 @@
"cover"
]
}
},
{
"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 @@ -8478,7 +8426,7 @@
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"description": "Redirect to the mime icon url",
"headers": {
"Location": {
"schema": {
Expand Down
56 changes: 2 additions & 54 deletions core/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8253,19 +8253,6 @@
]
}
},
{
"name": "forceIcon",
"in": "query",
"description": "Force returning an icon",
"schema": {
"type": "integer",
"default": 1,
"enum": [
0,
1
]
}
},
{
"name": "mode",
"in": "query",
Expand All @@ -8278,19 +8265,6 @@
"cover"
]
}
},
{
"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 @@ -8330,7 +8304,7 @@
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"description": "Redirect to the mime icon url",
"headers": {
"Location": {
"schema": {
Expand Down Expand Up @@ -8401,19 +8375,6 @@
]
}
},
{
"name": "forceIcon",
"in": "query",
"description": "Force returning an icon",
"schema": {
"type": "integer",
"default": 1,
"enum": [
0,
1
]
}
},
{
"name": "mode",
"in": "query",
Expand All @@ -8426,19 +8387,6 @@
"cover"
]
}
},
{
"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 @@ -8478,7 +8426,7 @@
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"description": "Redirect to the mime icon url",
"headers": {
"Location": {
"schema": {
Expand Down
3 changes: 1 addition & 2 deletions core/src/OC/dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ const Dialogs = {
}

const dialog = builder.build()

if (allowHtml) {
dialog.setHTML(content)
}
Expand Down Expand Up @@ -560,7 +560,6 @@ const Dialogs = {
x: 96,
y: 96,
c: original.etag,
forceIcon: 0
}
var previewpath = Files.generatePreviewUrl(urlSpec)
// Escaping single quotes
Expand Down
Loading

0 comments on commit 2aef985

Please sign in to comment.