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>
pull/38905/head
Louis Chemineau 3 years ago
parent 0d9abed754
commit 36c3846475
  1. 26
      apps/files_versions/lib/Sabre/Plugin.php
  2. 5
      apps/files_versions/lib/Sabre/VersionFile.php
  3. 5
      apps/files_versions/src/components/Version.vue
  4. 1
      apps/files_versions/src/utils/davRequest.js
  5. 2
      apps/files_versions/src/utils/versions.js

@ -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;
}
});
}
}
}

@ -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();
}

@ -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

@ -30,5 +30,6 @@ export default `<?xml version="1.0"?>
<d:getcontenttype />
<d:getlastmodified />
<nc:version-label />
<nc:has-preview />
</d:prop>
</d:propfind>`

@ -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,

Loading…
Cancel
Save