summaryrefslogtreecommitdiffstats
path: root/lib/Sabre/Album/PropFindPlugin.php
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2022-08-12 13:57:18 +0200
committerLouis Chemineau <louis@chmn.me>2022-08-22 20:03:52 +0200
commitfe215a85754650cd2c085fe6ea813ce5365b6f0c (patch)
treedc94f806775470f95a4dce3142e16570c4f6acd3 /lib/Sabre/Album/PropFindPlugin.php
parent6bb2c7a864fd9a6ba3ce96cc9e2f42daba635011 (diff)
expose more metadata
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'lib/Sabre/Album/PropFindPlugin.php')
-rw-r--r--lib/Sabre/Album/PropFindPlugin.php64
1 files changed, 54 insertions, 10 deletions
diff --git a/lib/Sabre/Album/PropFindPlugin.php b/lib/Sabre/Album/PropFindPlugin.php
index 02b62096..68c306b2 100644
--- a/lib/Sabre/Album/PropFindPlugin.php
+++ b/lib/Sabre/Album/PropFindPlugin.php
@@ -23,28 +23,72 @@ declare(strict_types=1);
namespace OCA\Photos\Sabre\Album;
+use OC\Metadata\IMetadataManager;
+use OCA\DAV\Connector\Sabre\FilesPlugin;
+use OCP\IConfig;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
class PropFindPlugin extends ServerPlugin {
- private Server $server;
+ private IConfig $config;
+ private IMetadataManager $metadataManager;
+ private bool $metadataEnabled;
- public function initialize(Server $server) {
- $this->server = $server;
-
- $this->server->on('propFind', [$this, 'propFind']);
+ public function __construct(IConfig $config, IMetadataManager $metadataManager) {
+ $this->config = $config;
+ $this->metadataManager = $metadataManager;
+ $this->metadataEnabled = $this->config->getSystemValueBool('enable_file_metadata', true);
}
+ public function initialize(Server $server) {
+ $server->on('propFind', [$this, 'propFind']);
+ }
+
public function propFind(PropFind $propFind, INode $node) {
- if (!($node instanceof AlbumPhoto)) {
- return;
+ if ($node instanceof AlbumPhoto) {
+ $propFind->handle('{http://nextcloud.org/ns}file-name', function () use ($node) {
+ return $node->getFile()->getName();
+ });
+ $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
+ return $node->getFile()->getFileId();
+ });
+ $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node): string {
+ return $node->getETag();
+ });
+
+ if ($this->metadataEnabled) {
+ $propFind->handle(FilesPlugin::FILE_METADATA_SIZE, function () use ($node) {
+ if (!str_starts_with($node->getFile()->getMimetype(), 'image')) {
+ return json_encode((object)[]);
+ }
+
+ if ($node->getFile()->hasMetadata('size')) {
+ $sizeMetadata = $node->getFile()->getMetadata('size');
+ } else {
+ $sizeMetadata = $this->metadataManager->fetchMetadataFor('size', [$node->getFile()->getFileId()])[$node->getFile()->getFileId()];
+ }
+
+ return json_encode((object)$sizeMetadata->getMetadata());
+ });
+ }
}
- $propFind->handle('{http://nextcloud.org/ns}file-name', function () use ($node) {
- return $node->getFile()->getName();
- });
+ if ($node instanceof AlbumRoot) {
+ // TODO detect dynamically which metadata groups are requested and
+ // preload all of them and not just size
+ if ($this->metadataEnabled && in_array(FilesPlugin::FILE_METADATA_SIZE, $propFind->getRequestedProperties(), true)) {
+ $fileIds = $node->getAlbum()->getFileIds();
+
+ $preloadedMetadata = $this->metadataManager->fetchMetadataFor('size', $fileIds);
+ foreach ($node->getAlbum()->getFiles() as $file) {
+ if (str_starts_with($file->getMimeType(), 'image')) {
+ $file->setMetadata('size', $preloadedMetadata[$file->getFileId()]);
+ }
+ }
+ }
+ }
}
}