Skip to content

Commit

Permalink
Provide hasPreview in files_versions DAV API
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
artonge committed Jun 19, 2023
1 parent 4bfd7ee commit f6be02e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
26 changes: 24 additions & 2 deletions apps/files_versions/lib/Sabre/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
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;
Expand All @@ -39,12 +43,13 @@

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;
}
Expand Down Expand Up @@ -89,8 +94,25 @@ public function afterGet(RequestInterface $request, ResponseInterface $response)
}

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

Expand Down
5 changes: 5 additions & 0 deletions apps/files_versions/lib/Sabre/VersionFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -60,6 +61,10 @@ public function get() {
}
}

public function getSourceFile(): FileInfo {
return $this->version->getSourceFile();
}

public function getContentType(): string {
return $this->version->getMimeType();
}
Expand Down
5 changes: 4 additions & 1 deletion apps/files_versions/src/components/Version.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions apps/files_versions/src/utils/davRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export default `<?xml version="1.0"?>
<d:getcontenttype />
<d:getlastmodified />
<nc:version-label />
<nc:has-preview />
</d:prop>
</d:propfind>`
2 changes: 2 additions & 0 deletions apps/files_versions/src/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
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.

0 comments on commit f6be02e

Please sign in to comment.