diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 67d01bb699907..49e4c25c450f0 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -169,6 +169,9 @@ public function get($file) { $data = $result->fetch(); $result->closeCursor(); + // @Louis: use asArray() +// $data['metadata'] = $metadataQuery?->extractMetadata($data)?->asArray() ?? []; + //merge partial data if (!$data && is_string($file) && isset($this->partial[$file])) { return $this->partial[$file]; diff --git a/lib/private/Files/Cache/CacheQueryBuilder.php b/lib/private/Files/Cache/CacheQueryBuilder.php index 34d2177b84e9e..be3b64425f34a 100644 --- a/lib/private/Files/Cache/CacheQueryBuilder.php +++ b/lib/private/Files/Cache/CacheQueryBuilder.php @@ -35,7 +35,7 @@ * Query builder with commonly used helpers for filecache queries */ class CacheQueryBuilder extends QueryBuilder { - private $alias = null; + private ?string $alias = null; public function __construct(IDBConnection $connection, SystemConfig $systemConfig, LoggerInterface $logger) { parent::__construct($connection, $systemConfig, $logger); diff --git a/lib/private/Files/Cache/QuerySearchHelper.php b/lib/private/Files/Cache/QuerySearchHelper.php index f02b7f00beb99..b8a3aa0257c3d 100644 --- a/lib/private/Files/Cache/QuerySearchHelper.php +++ b/lib/private/Files/Cache/QuerySearchHelper.php @@ -194,7 +194,7 @@ public function searchInCaches(ISearchQuery $searchQuery, array $caches): array $rawEntries = array_map(function (array $data) use ($metadataQuery) { // extract metadata from the result and convert it to array. // TODO: using jsonSerialize() or another method that present each metadata as key => value ? - $data['metadata'] = $metadataQuery?->extractMetadata($data)?->jsonSerialize() ?? []; + $data['metadata'] = $metadataQuery?->extractMetadata($data)?->asArray() ?? []; return Cache::cacheEntryFromData($data, $this->mimetypeLoader); }, $files); diff --git a/lib/private/FilesMetadata/FilesMetadataManager.php b/lib/private/FilesMetadata/FilesMetadataManager.php index da700c138b429..1a35f56999e22 100644 --- a/lib/private/FilesMetadata/FilesMetadataManager.php +++ b/lib/private/FilesMetadata/FilesMetadataManager.php @@ -39,14 +39,22 @@ public function __construct( /** * @param int $fileId + * @param bool $generate - returns an empty FilesMetadata if FilesMetadataNotFoundException * * @return IFilesMetadata * @throws FilesMetadataNotFoundException */ - public function getMetadata(int $fileId): IFilesMetadata { - return $this->metadataRequestService->getMetadataFromFileId($fileId); - } + public function getMetadata(int $fileId, bool $generate = false): IFilesMetadata { + try { + return $this->metadataRequestService->getMetadataFromFileId($fileId); + } catch (FilesMetadataNotFoundException $e) { + if ($generate) { + return new FilesMetadata($fileId, true); + } + throw $e; + } + } public function refreshMetadata( Node $node, diff --git a/lib/private/FilesMetadata/Model/FilesMetadata.php b/lib/private/FilesMetadata/Model/FilesMetadata.php index 4460ac1e4d5c3..1d63c5fde3b77 100644 --- a/lib/private/FilesMetadata/Model/FilesMetadata.php +++ b/lib/private/FilesMetadata/Model/FilesMetadata.php @@ -61,9 +61,9 @@ public function importFromDatabase(array $data, string $prefix = ''): IFilesMeta try { return $this->import( json_decode($data[$prefix . 'json'] ?? '[]', - true, - 512, - JSON_THROW_ON_ERROR) + true, + 512, + JSON_THROW_ON_ERROR) ); } catch (JsonException $e) { throw new FilesMetadataNotFoundException(); @@ -348,6 +348,27 @@ public function getValueWrapper(string $key): MetadataValueWrapper { public function jsonSerialize(): array { - return $this->metadata; + $data = []; + foreach ($this->metadata as $metaKey => $metaValueWrapper) { + $data[$metaKey] = $metaValueWrapper->jsonSerialize(); + } + + return $data; + } + + /** + * @return array + */ + public function asArray(): array { + $data = []; + foreach ($this->metadata as $metaKey => $metaValueWrapper) { + try { + $data[$metaKey] = $metaValueWrapper->getValueAny(); + } catch (FilesMetadataNotFoundException $e) { + // ignore exception + } + } + + return $data; } } diff --git a/lib/private/FilesMetadata/Model/MetadataValueWrapper.php b/lib/private/FilesMetadata/Model/MetadataValueWrapper.php index 132a5a8328d4a..e4d6f1040e05d 100644 --- a/lib/private/FilesMetadata/Model/MetadataValueWrapper.php +++ b/lib/private/FilesMetadata/Model/MetadataValueWrapper.php @@ -266,6 +266,18 @@ public function getValueIntList(): array { return (array) $this->value; } + /** + * @return string|int|float|bool|array|string[]|int[] + * @throws FilesMetadataNotFoundException + */ + public function getValueAny(): mixed { + if (null === $this->value) { + throw new FilesMetadataNotFoundException('value is not set'); + } + + return $this->value; + } + public function setIndexed(bool $indexed): self { $this->indexed = $indexed; diff --git a/lib/public/FilesMetadata/Model/IFilesMetadata.php b/lib/public/FilesMetadata/Model/IFilesMetadata.php index 4ddeb6e92ed8d..b5f0a34c50f2d 100644 --- a/lib/public/FilesMetadata/Model/IFilesMetadata.php +++ b/lib/public/FilesMetadata/Model/IFilesMetadata.php @@ -76,5 +76,5 @@ public function setBool(string $key, bool $value): self; public function setArray(string $key, array $value): self; public function setStringList(string $key, array $value): self; public function setIntList(string $key, array $value): self; - + public function asArray(): array; }