|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Collectives\Db; |
| 11 | + |
| 12 | +use OCA\Collectives\Model\FileInfo; |
| 13 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | +use OCP\IDBConnection; |
| 15 | + |
| 16 | +class FileCacheMapper { |
| 17 | + private const BATCH_SIZE = 1000; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private readonly IDBConnection $db, |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Query filecache for file data by file IDs |
| 26 | + * |
| 27 | + * @param int[] $fileIds |
| 28 | + * @return array<int, FileInfo> Indexed by fileId |
| 29 | + */ |
| 30 | + public function findByFileIds(array $fileIds): array { |
| 31 | + if (empty($fileIds)) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + $fileInfos = []; |
| 36 | + foreach (array_chunk($fileIds, self::BATCH_SIZE) as $chunk) { |
| 37 | + $qb = $this->db->getQueryBuilder(); |
| 38 | + $qb->select('fileid', 'storage', 'parent', 'path', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'etag', 'encrypted', 'permissions', 'checksum', 'unencrypted_size') |
| 39 | + ->from('filecache') |
| 40 | + ->where($qb->expr()->in('fileid', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY))); |
| 41 | + |
| 42 | + $result = $qb->executeQuery(); |
| 43 | + while ($row = $result->fetch()) { |
| 44 | + $fileInfo = new FileInfo( |
| 45 | + fileId: (int)$row['fileid'], |
| 46 | + storage: (int)$row['storage'], |
| 47 | + path: (string)$row['path'], |
| 48 | + parent: (int)$row['parent'], |
| 49 | + name: (string)$row['name'], |
| 50 | + mimetype: (int)$row['mimetype'], |
| 51 | + mimepart: (int)$row['mimepart'], |
| 52 | + size: (int)$row['size'], |
| 53 | + mtime: (int)$row['mtime'], |
| 54 | + storage_mtime: (int)$row['storage_mtime'], |
| 55 | + encrypted: (int)$row['encrypted'], |
| 56 | + unencrypted_size: (int)$row['unencrypted_size'], |
| 57 | + etag: (string)$row['etag'], |
| 58 | + permissions: (int)$row['permissions'], |
| 59 | + checksum: $row['checksum'] !== null ? (string)$row['checksum'] : null, |
| 60 | + ); |
| 61 | + $fileInfos[$fileInfo->fileId] = $fileInfo; |
| 62 | + } |
| 63 | + $result->closeCursor(); |
| 64 | + } |
| 65 | + |
| 66 | + return $fileInfos; |
| 67 | + } |
| 68 | +} |
0 commit comments