diff options
author | Louis Chemineau <louis@chmn.me> | 2023-06-19 17:51:53 +0200 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2023-06-28 16:46:48 +0200 |
commit | 36c3846475cf132caca656c11a954343495dccbe (patch) | |
tree | a61ebc97f3e0263efb584a34db51a40e1e12e90b | |
parent | 0d9abed75454d022190024f5a5951cd1aed315b8 (diff) | |
download | nextcloud-server-36c3846475cf132caca656c11a954343495dccbe.tar.gz nextcloud-server-36c3846475cf132caca656c11a954343495dccbe.zip |
Provide hasPreview in files_versions DAV API
This allow to no request non existing previews
I also set some properties to the img element to reduce preview loading to what the browser think is necessary
Signed-off-by: Louis Chemineau <louis@chmn.me>
-rw-r--r-- | apps/files_versions/lib/Sabre/Plugin.php | 26 | ||||
-rw-r--r-- | apps/files_versions/lib/Sabre/VersionFile.php | 5 | ||||
-rw-r--r-- | apps/files_versions/src/components/Version.vue | 5 | ||||
-rw-r--r-- | apps/files_versions/src/utils/davRequest.js | 1 | ||||
-rw-r--r-- | apps/files_versions/src/utils/versions.js | 2 |
5 files changed, 36 insertions, 3 deletions
diff --git a/apps/files_versions/lib/Sabre/Plugin.php b/apps/files_versions/lib/Sabre/Plugin.php index 4fd17194ba6..b53f21039be 100644 --- a/apps/files_versions/lib/Sabre/Plugin.php +++ b/apps/files_versions/lib/Sabre/Plugin.php @@ -27,7 +27,11 @@ declare(strict_types=1); namespace OCA\Files_Versions\Sabre; use OC\AppFramework\Http\Request; +use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCA\Files_Versions\Versions\IVersionManager; +use OCP\Files\NotFoundException; use OCP\IRequest; +use OCP\IUserSession; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\INode; use Sabre\DAV\PropFind; @@ -39,12 +43,13 @@ use Sabre\HTTP\ResponseInterface; class Plugin extends ServerPlugin { private Server $server; - private IRequest $request; public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label'; public function __construct( - IRequest $request + private IRequest $request, + private IVersionManager $versionManager, + private IUserSession $userSession, ) { $this->request = $request; } @@ -89,8 +94,25 @@ class Plugin extends ServerPlugin { } public function propFind(PropFind $propFind, INode $node): void { + $user = $this->userSession->getUser(); + if ($node instanceof VersionFile) { $propFind->handle(self::VERSION_LABEL, fn() => $node->getLabel()); + + if ($user !== null) { + $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node, $user) { + try { + $this->versionManager->getVersionFile( + $user, + $node->getSourceFile(), + $node->getVersion()->getRevisionId() + ); + return true; + } catch (NotFoundException $ex) { + return false; + } + }); + } } } diff --git a/apps/files_versions/lib/Sabre/VersionFile.php b/apps/files_versions/lib/Sabre/VersionFile.php index 8fd97b0636f..bb05d66460a 100644 --- a/apps/files_versions/lib/Sabre/VersionFile.php +++ b/apps/files_versions/lib/Sabre/VersionFile.php @@ -31,6 +31,7 @@ use OCA\Files_Versions\Versions\INameableVersion; use OCA\Files_Versions\Versions\INameableVersionBackend; use OCA\Files_Versions\Versions\IVersion; use OCA\Files_Versions\Versions\IVersionManager; +use OCP\Files\FileInfo; use OCP\Files\NotFoundException; use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\NotFound; @@ -60,6 +61,10 @@ class VersionFile implements IFile { } } + public function getSourceFile(): FileInfo { + return $this->version->getSourceFile(); + } + public function getContentType(): string { return $this->version->getMimeType(); } diff --git a/apps/files_versions/src/components/Version.vue b/apps/files_versions/src/components/Version.vue index def5abbcf48..096c31990f0 100644 --- a/apps/files_versions/src/components/Version.vue +++ b/apps/files_versions/src/components/Version.vue @@ -23,9 +23,12 @@ :force-display-actions="true" data-files-versions-version> <template #icon> - <img v-if="!previewError" + <img v-if="(isCurrent || version.hasPreview) && !previewError" :src="previewURL" alt="" + decoding="async" + fetchpriority="low" + loading="lazy" class="version__image" @error="previewError = true"> <div v-else diff --git a/apps/files_versions/src/utils/davRequest.js b/apps/files_versions/src/utils/davRequest.js index fb2126d98bf..fec64caa2d5 100644 --- a/apps/files_versions/src/utils/davRequest.js +++ b/apps/files_versions/src/utils/davRequest.js @@ -30,5 +30,6 @@ export default `<?xml version="1.0"?> <d:getcontenttype /> <d:getlastmodified /> <nc:version-label /> + <nc:has-preview /> </d:prop> </d:propfind>` diff --git a/apps/files_versions/src/utils/versions.js b/apps/files_versions/src/utils/versions.js index 1a5dde10824..71593dd0ce8 100644 --- a/apps/files_versions/src/utils/versions.js +++ b/apps/files_versions/src/utils/versions.js @@ -36,6 +36,7 @@ import moment from '@nextcloud/moment' * @property {string} size - Human readable size * @property {string} type - 'file' * @property {number} mtime - Version creation date as a timestamp + * @property {boolean} hasPreview - Whether the version has a preview * @property {string} preview - Preview URL of the version * @property {string} url - Download URL of the version * @property {string|null} fileVersion - The version id, null for the current version @@ -98,6 +99,7 @@ function formatVersion(version, fileInfo) { size: version.size, type: version.type, mtime: moment(version.lastmod).unix() * 1000, + hasPreview: version.props['has-preview'] === 1, preview: generateUrl('/apps/files_versions/preview?file={file}&version={fileVersion}', { file: joinPaths(fileInfo.path, fileInfo.name), fileVersion: version.basename, |