Skip to content

Commit

Permalink
fixup! Add a metadata service to store file metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Apr 8, 2022
1 parent f9cb568 commit 9586920
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class File extends Node implements IFile {
protected IL10N $l10n;

/** @var array<string, FileMetadata> */
private array $metadata;
private array $metadata = [];

/**
* Sets up the node, expects a full path name
Expand Down
11 changes: 9 additions & 2 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\IPreview;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\IFile;
Expand Down Expand Up @@ -448,8 +449,14 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
if ($node->hasMetadata('size')) {
$sizeMetadata = $node->getMetadata('size');
} else {
// TODO fetch metadata manually in that case
// or is that always the case?
// This code path should not be called since we try to preload
// the metadata when loading the folder or the search results
// in one go
$metadataManager = \OC::$server->get(IMetadataManager::class);
$sizeMetadata = $metadataManager->fetchMetadataFor('size', [$node->getId()])[$node->getId()];

// TODO would be nice to display this in the profiler...
\OC::$server->get(LoggerInterface::class)->warning('Inefficient fetching of metadata');
}

return json_encode($sizeMetadata->getMetadata());
Expand Down
31 changes: 29 additions & 2 deletions apps/dav/lib/Files/FileSearchBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
use OC\Files\View;
use OC\Metadata\IMetadataManager;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\TagsPlugin;
use OCP\Files\Cache\ICacheEntry;
Expand All @@ -44,6 +46,7 @@
use OCP\IUser;
use OCP\Share\IManager;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\INode;
use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Backend\SearchResult;
Expand Down Expand Up @@ -137,6 +140,32 @@ public function getPropertyDefinitionsForScope($href, $path) {
];
}

/**
* @param INode[] $nodes
* @param string[] $requestProperties
*/
public function preloadPropertyFor(array $nodes, array $requestProperties): void {
if (in_array(FilesPlugin::FILE_METADATA_SIZE, $requestProperties, true)) {
// Preloading of the metadata
$fileIds = [];
foreach ($nodes as $node) {
if (str_starts_with($node->getFileInfo()->getMimeType(), 'image')) {
/** @var File $node */
$fileIds[] = $node->getFileInfo()->getId();
}
}
/** @var IMetaDataManager $metadataManager */
$metadataManager = \OC::$server->get(IMetadataManager::class);
$preloadedMetadata = $metadataManager->fetchMetadataFor('size', $fileIds);
foreach ($nodes as $node) {
if (str_starts_with($node->getFileInfo()->getMimeType(), 'image')) {
/** @var File $child */
$node->setMetadata('size', $preloadedMetadata[$node->getFileInfo()->getId()]);
}
}
}
}

/**
* @param Query $search
* @return SearchResult[]
Expand Down Expand Up @@ -254,8 +283,6 @@ private function getSearchResultProperty(SearchResult $result, SearchPropertyDef
return $node->getSize();
case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
return $node->getInternalFileId();
case FilesPlugin::FILE_METADATA_SIZE:
return $node->getMetadata('size');
default:
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions apps/dav/lib/Files/LazySearchBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OCA\DAV\Files;

use Sabre\DAV\INode;
use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Query\Query;

Expand Down Expand Up @@ -66,4 +67,10 @@ public function search(Query $query) {
return [];
}
}

public function preloadPropertyFor(array $nodes, array $requestProperties): void {
if ($this->backend) {
$this->backend->preloadPropertyFor($nodes, $requestProperties);
}
}
}

0 comments on commit 9586920

Please sign in to comment.