]> source.dussan.org Git - nextcloud-server.git/commitdiff
Provide hasPreview in files_versions DAV API
authorLouis Chemineau <louis@chmn.me>
Mon, 19 Jun 2023 15:51:53 +0000 (17:51 +0200)
committerLouis Chemineau <louis@chmn.me>
Wed, 28 Jun 2023 14:46:48 +0000 (16:46 +0200)
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>
apps/files_versions/lib/Sabre/Plugin.php
apps/files_versions/lib/Sabre/VersionFile.php
apps/files_versions/src/components/Version.vue
apps/files_versions/src/utils/davRequest.js
apps/files_versions/src/utils/versions.js

index 4fd17194ba6a36e51814f8ad7f95a383673eece9..b53f21039be167d9bdfcdb101425ecf2a73216bd 100644 (file)
@@ -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;
+                                       }
+                               });
+                       }
                }
        }
 
index 8fd97b0636f75e1df519db8e7ff2c1eebd026b10..bb05d66460a55f4cbde8095334b650c3ad771742 100644 (file)
@@ -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();
        }
index def5abbcf4875a31476821c7208cc4c55ac4e58a..096c31990f077e1996d4b7cb13d8c6f370df6eba 100644 (file)
                        :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
index fb2126d98bfc5380430c69ee5704eb6e7f212cbc..fec64caa2d5dea92ba17cb73cadf9734f53bce12 100644 (file)
@@ -30,5 +30,6 @@ export default `<?xml version="1.0"?>
                <d:getcontenttype />
                <d:getlastmodified />
                <nc:version-label />
+               <nc:has-preview />
        </d:prop>
 </d:propfind>`
index 1a5dde108244ae1b1e1cb612ba8ecc0dd58c0b1b..71593dd0ce80c7c7634ba057ceb07530949768ff 100644 (file)
@@ -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,