Skip to content

Commit 9ab8020

Browse files
Merge pull request #42010 from nextcloud/backport/42008/stable28
[stable28] fix(metadata): Allow to load metadata of multiple files at once
2 parents a5eaa56 + c6acd3f commit 9ab8020

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

lib/private/FilesMetadata/FilesMetadataManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ public function getMetadata(int $fileId, bool $generate = false): IFilesMetadata
147147
}
148148
}
149149

150+
/**
151+
* returns metadata of multiple file ids
152+
*
153+
* @param int[] $fileIds file ids
154+
*
155+
* @return array File ID is the array key, files without metadata are not returned in the array
156+
* @psalm-return array<int, IFilesMetadata>
157+
* @since 28.0.0
158+
*/
159+
public function getMetadataForFiles(array $fileIds): array {
160+
return $this->metadataRequestService->getMetadataFromFileIds($fileIds);
161+
}
162+
150163
/**
151164
* @param IFilesMetadata $filesMetadata metadata
152165
*

lib/private/FilesMetadata/Service/MetadataRequestService.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ public function getMetadataFromFileId(int $fileId): IFilesMetadata {
9797
return $metadata;
9898
}
9999

100+
/**
101+
* returns metadata for multiple file ids
102+
*
103+
* If
104+
*
105+
* @param array $fileIds file ids
106+
*
107+
* @return array File ID is the array key, files without metadata are not returned in the array
108+
* @psalm-return array<int, IFilesMetadata>
109+
*/
110+
public function getMetadataFromFileIds(array $fileIds): array {
111+
$qb = $this->dbConnection->getQueryBuilder();
112+
$qb->select('file_id', 'json', 'sync_token')->from(self::TABLE_METADATA);
113+
$qb->where(
114+
$qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY))
115+
);
116+
117+
$list = [];
118+
$result = $qb->executeQuery();
119+
while ($data = $result->fetch()) {
120+
$fileId = (int) $data['file_id'];
121+
$metadata = new FilesMetadata($fileId);
122+
try {
123+
$metadata->importFromDatabase($data);
124+
} catch (FilesMetadataNotFoundException) {
125+
continue;
126+
}
127+
$list[$fileId] = $metadata;
128+
}
129+
$result->closeCursor();
130+
131+
return $list;
132+
}
133+
100134
/**
101135
* drop metadata related to a file id
102136
*

lib/public/FilesMetadata/IFilesMetadataManager.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function refreshMetadata(
7171
): IFilesMetadata;
7272

7373
/**
74-
* returns metadata from a file id
74+
* returns metadata of a file id
7575
*
7676
* @param int $fileId file id
7777
* @param boolean $generate Generate if metadata does not exist
@@ -82,6 +82,17 @@ public function refreshMetadata(
8282
*/
8383
public function getMetadata(int $fileId, bool $generate = false): IFilesMetadata;
8484

85+
/**
86+
* returns metadata of multiple file ids
87+
*
88+
* @param int[] $fileIds file ids
89+
*
90+
* @return array File ID is the array key, files without metadata are not returned in the array
91+
* @psalm-return array<int, IFilesMetadata>
92+
* @since 28.0.0
93+
*/
94+
public function getMetadataForFiles(array $fileIds): array;
95+
8596
/**
8697
* save metadata to database and refresh indexes.
8798
* metadata are saved if new data are available.

0 commit comments

Comments
 (0)