Skip to content

Commit

Permalink
returns metadata as array
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Oct 22, 2023
1 parent 4109bfc commit 2837291
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/CacheQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/QuerySearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 11 additions & 3 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 25 additions & 4 deletions lib/private/FilesMetadata/Model/FilesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<string, string|int|bool|float|string[]|int[]>
*/
public function asArray(): array {
$data = [];
foreach ($this->metadata as $metaKey => $metaValueWrapper) {
try {
$data[$metaKey] = $metaValueWrapper->getValueAny();
} catch (FilesMetadataNotFoundException $e) {
// ignore exception
}
}

return $data;
}
}
12 changes: 12 additions & 0 deletions lib/private/FilesMetadata/Model/MetadataValueWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/public/FilesMetadata/Model/IFilesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2837291

Please sign in to comment.