From b164af46c73387d05f5c67126de51e4d0fc2750f Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Mon, 19 Jun 2023 17:51:53 +0200 Subject: 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 --- apps/files_versions/lib/Sabre/Plugin.php | 26 ++++++++++++++++++++++++-- apps/files_versions/lib/Sabre/VersionFile.php | 5 +++++ 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'apps/files_versions/lib') 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(); } -- cgit v1.2.3