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
25 changes: 21 additions & 4 deletions apps/files_versions/lib/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Preview\IMimeIconProvider;

class PreviewController extends Controller {

Expand All @@ -38,7 +40,8 @@ public function __construct(
IRootFolder $rootFolder,
IUserSession $userSession,
IVersionManager $versionManager,
IPreview $previewManager
IPreview $previewManager,
private IMimeIconProvider $mimeIconProvider,
) {
parent::__construct($appName, $request);

Expand All @@ -55,9 +58,11 @@ public function __construct(
* @param int $x Width of the preview
* @param int $y Height of the preview
* @param string $version Version of the file to get the preview for
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<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_NOT_FOUND, array<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
* 404: Preview not found
*/
Expand All @@ -67,20 +72,32 @@ public function getPreview(
string $file = '',
int $x = 44,
int $y = 44,
string $version = ''
string $version = '',
bool $mimeFallback = false,
) {
if ($file === '' || $version === '' || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$versionFile = null;
try {
$user = $this->userSession->getUser();
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$file = $userFolder->get($file);
$versionFile = $this->versionManager->getVersionFile($user, $file, $version);
$preview = $this->previewManager->getPreview($versionFile, $x, $y, true, IPreview::MODE_FILL, $versionFile->getMimetype());
return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
$response = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
$response->cacheFor(3600 * 24, false, true);
return $response;
} catch (NotFoundException $e) {
// If we have no preview enabled, we can redirect to the mime icon if any
if ($mimeFallback && $versionFile !== null) {
$url = $this->mimeIconProvider->getMimeIconUrl($versionFile->getMimeType());
if ($url !== null) {
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_versions/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@
"type": "string",
"default": ""
}
},
{
"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 @@ -132,6 +145,16 @@
"schema": {}
}
}
},
"303": {
"description": "Redirect to the mime icon url if mimeFallback is true",
"headers": {
"Location": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files_versions/src/components/Version.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- Icon -->
<template #icon>
<div v-if="!(loadPreview || previewLoaded)" class="version__image" />
<img v-else-if="(isCurrent || version.hasPreview) && !previewErrored"
<img v-else-if="version.previewUrl && !previewErrored"
:src="version.previewUrl"
alt=""
decoding="async"
Expand Down
6 changes: 2 additions & 4 deletions apps/files_versions/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export interface Version {
type: string, // 'file'
mtime: number, // Version creation date as a timestamp
permissions: string, // Only readable: 'R'
hasPreview: boolean, // Whether the version has a preview
previewUrl: string, // Preview URL of the version
url: string, // Download URL of the version
source: string, // The WebDAV endpoint of the ressource
Expand Down Expand Up @@ -78,12 +77,12 @@ 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&forceIcon=0&a=0&forceIcon=1&mimeFallback=1', {
fileId: fileInfo.id,
fileEtag: fileInfo.etag,
})
} else {
previewUrl = generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}', {
previewUrl = generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}&mimeFallback=1', {
file: joinPaths(fileInfo.path, fileInfo.name),
fileVersion: version.basename,
})
Expand All @@ -102,7 +101,6 @@ function formatVersion(version: any, fileInfo: any): Version {
type: version.type,
mtime,
permissions: 'R',
hasPreview: version.props['has-preview'] === 1,
previewUrl,
url: joinPaths('/remote.php/dav', version.filename),
source: generateRemoteUrl('dav') + encodePath(version.filename),
Expand Down
5 changes: 2 additions & 3 deletions apps/files_versions/src/views/VersionTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,12 @@ export default {
return
}

// Versions previews are too small for our use case, so we override hasPreview and previewUrl
// Versions previews are too small for our use case, so we override previewUrl
// which makes the viewer render the original file.
// We also point to the original filename if the version is the current one.
const versions = this.versions.map(version => ({
...version,
filename: version.mtime === this.fileInfo.mtime ? path.join('files', getCurrentUser()?.uid ?? '', this.fileInfo.path, this.fileInfo.name) : version.filename,
hasPreview: false,
previewUrl: undefined,
}))

Expand All @@ -291,7 +290,7 @@ export default {
},

compareVersion({ version }) {
const versions = this.versions.map(version => ({ ...version, hasPreview: false, previewUrl: undefined }))
const versions = this.versions.map(version => ({ ...version, previewUrl: undefined }))

OCA.Viewer.compare(this.viewerFileInfo, versions.find(v => v.source === version.source))
},
Expand Down
15 changes: 11 additions & 4 deletions apps/files_versions/tests/Controller/PreviewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files_Versions\Tests\Controller;

use OCA\Files_Versions\Controller\PreviewController;
use OCA\Files_Versions\Versions\IVersionManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
Expand All @@ -20,6 +20,8 @@
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Preview\IMimeIconProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class PreviewControllerTest extends TestCase {
Expand All @@ -45,6 +47,8 @@ class PreviewControllerTest extends TestCase {
/** @var IVersionManager|\PHPUnit\Framework\MockObject\MockObject */
private $versionManager;

private IMimeIconProvider&MockObject $mimeIconProvider;

protected function setUp(): void {
parent::setUp();

Expand All @@ -60,14 +64,16 @@ protected function setUp(): void {
->method('getUser')
->willReturn($user);
$this->versionManager = $this->createMock(IVersionManager::class);
$this->mimeIconProvider = $this->createMock(IMimeIconProvider::class);

$this->controller = new PreviewController(
'files_versions',
$this->createMock(IRequest::class),
$this->rootFolder,
$this->userSession,
$this->versionManager,
$this->previewManager
$this->previewManager,
$this->mimeIconProvider,
);
}

Expand Down Expand Up @@ -131,9 +137,10 @@ public function testValidPreview() {
->willReturn('previewMime');

$res = $this->controller->getPreview('file', 10, 10, '42');
$expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);

$this->assertEquals($expected, $res);
$this->assertEquals('previewMime', $res->getHeaders()['Content-Type']);
$this->assertEquals(Http::STATUS_OK, $res->getStatus());
$this->assertEquals($preview, $this->invokePrivate($res, 'file'));
}

public function testVersionNotFound() {
Expand Down
4 changes: 2 additions & 2 deletions dist/files_versions-files_versions.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files_versions-files_versions.js.map

Large diffs are not rendered by default.

Loading