|
15 | 15 | use OC\Files\Storage\Wrapper\Jail; |
16 | 16 | use OC\Files\Storage\Wrapper\PermissionsMask; |
17 | 17 | use OCA\Collectives\ACL\ACLStorageWrapper; |
| 18 | +use OCA\Collectives\Model\FileInfo; |
18 | 19 | use OCP\DB\QueryBuilder\IQueryBuilder; |
19 | 20 | use OCP\Files\Cache\ICacheEntry; |
20 | 21 | use OCP\Files\Folder; |
@@ -250,6 +251,53 @@ public function getFolderFileCachePerCollectiveId(array $ids): array { |
250 | 251 | return $result; |
251 | 252 | } |
252 | 253 |
|
| 254 | + /** |
| 255 | + * Load all filecache entries (files and folders) of a collective folder in a single query. |
| 256 | + * |
| 257 | + * @return FileInfo[] Indexed by file id, with paths relative to the collective root folder |
| 258 | + * |
| 259 | + * @throws NotFoundException |
| 260 | + * @throws \OCP\DB\Exception |
| 261 | + */ |
| 262 | + public function getFileCacheForCollective(int $collectiveId): array { |
| 263 | + $jailPath = $this->getJailPath($collectiveId); |
| 264 | + $storageId = $this->getRootFolderStorageId(); |
| 265 | + |
| 266 | + $qb = $this->connection->getQueryBuilder(); |
| 267 | + $qb->select('fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', |
| 268 | + 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions') |
| 269 | + ->from('filecache') |
| 270 | + ->where($qb->expr()->eq('storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))) |
| 271 | + // Trailing slash matters: it restricts to descendants of this collective and |
| 272 | + // avoids matching sibling collectives (e.g. `16` must not match `160`). |
| 273 | + ->andWhere($qb->expr()->like('path', $qb->createNamedParameter($this->connection->escapeLikeParameter($jailPath . '/') . '%'))); |
| 274 | + |
| 275 | + $prefixLength = strlen($jailPath . '/'); |
| 276 | + $result = []; |
| 277 | + $cursor = $qb->executeQuery(); |
| 278 | + while ($row = $cursor->fetch()) { |
| 279 | + $relativePath = substr($row['path'], $prefixLength); |
| 280 | + $result[(int)$row['fileid']] = new FileInfo( |
| 281 | + fileId: (int)$row['fileid'], |
| 282 | + storage: (int)$row['storage'], |
| 283 | + path: $relativePath, |
| 284 | + parent: (int)$row['parent'], |
| 285 | + name: (string)$row['name'], |
| 286 | + mimetype: (int)$row['mimetype'], |
| 287 | + mimepart: (int)$row['mimepart'], |
| 288 | + size: (int)$row['size'], |
| 289 | + mtime: (int)$row['mtime'], |
| 290 | + storageMtime: (int)$row['storage_mtime'], |
| 291 | + encrypted: (int)$row['encrypted'], |
| 292 | + etag: (string)$row['etag'], |
| 293 | + permissions: (int)$row['permissions'], |
| 294 | + ); |
| 295 | + } |
| 296 | + $cursor->closeCursor(); |
| 297 | + |
| 298 | + return $result; |
| 299 | + } |
| 300 | + |
253 | 301 | /** |
254 | 302 | * @throws InvalidPathException |
255 | 303 | * @throws NotFoundException |
|
0 commit comments