Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Always try to generate some kind of preview #50622

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion apps/files/lib/Dashboard/FavoriteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public function getItems(string $userId, int $limit = 7): array {
'y' => 256,
'fileId' => $node->getId(),
'c' => $node->getEtag(),
'mimeFallback' => true,
]);
} else {
$icon = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'filetypes/folder.svg'));
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
37 changes: 16 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,24 +36,25 @@ public function __construct(
private IRootFolder $root,
private ?string $userId,
private IMimeIconProvider $mimeIconProvider,
private IURLGenerator $urlGenerator,
) {
parent::__construct($appName, $request);
}

/**
* Get a preview by file path
*
* Deprecated: Use getPreviewByFileId instead
* @deprecated Use getPreviewByFileId instead
* @param string $file Path of the file
* @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 @@ -66,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 @@ -80,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 @@ -90,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 @@ -110,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 @@ -124,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 @@ -135,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 @@ -173,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
58 changes: 4 additions & 54 deletions core/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -8197,6 +8197,8 @@
"get": {
"operationId": "preview-get-preview",
"summary": "Get a preview by file path",
"description": "Deprecated: Use getPreviewByFileId instead",
"deprecated": true,
"tags": [
"preview"
],
Expand Down Expand Up @@ -8251,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 @@ -8276,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 @@ -8328,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 @@ -8399,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 @@ -8424,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 @@ -8476,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
58 changes: 4 additions & 54 deletions core/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8197,6 +8197,8 @@
"get": {
"operationId": "preview-get-preview",
"summary": "Get a preview by file path",
"description": "Deprecated: Use getPreviewByFileId instead",
"deprecated": true,
"tags": [
"preview"
],
Expand Down Expand Up @@ -8251,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 @@ -8276,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 @@ -8328,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 @@ -8399,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 @@ -8424,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 @@ -8476,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
Loading