1
0

FileAccess.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Files\Cache;
  8. use OC\FilesMetadata\FilesMetadataManager;
  9. use OC\SystemConfig;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\Files\Cache\IFileAccess;
  12. use OCP\Files\IMimeTypeLoader;
  13. use OCP\IDBConnection;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Low level access to the file cache
  17. */
  18. class FileAccess implements IFileAccess {
  19. public function __construct(
  20. private IDBConnection $connection,
  21. private SystemConfig $systemConfig,
  22. private LoggerInterface $logger,
  23. private FilesMetadataManager $metadataManager,
  24. private IMimeTypeLoader $mimeTypeLoader,
  25. ) {
  26. }
  27. private function getQuery(): CacheQueryBuilder {
  28. return new CacheQueryBuilder(
  29. $this->connection->getQueryBuilder(),
  30. $this->metadataManager,
  31. );
  32. }
  33. public function getByFileIdInStorage(int $fileId, int $storageId): ?CacheEntry {
  34. $items = array_values($this->getByFileIdsInStorage([$fileId], $storageId));
  35. return $items[0] ?? null;
  36. }
  37. public function getByPathInStorage(string $path, int $storageId): ?CacheEntry {
  38. $query = $this->getQuery()->selectFileCache();
  39. $query->andWhere($query->expr()->eq('filecache.path_hash', $query->createNamedParameter(md5($path))));
  40. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  41. $row = $query->executeQuery()->fetch();
  42. return $row ? Cache::cacheEntryFromData($row, $this->mimeTypeLoader) : null;
  43. }
  44. public function getByFileId(int $fileId): ?CacheEntry {
  45. $items = array_values($this->getByFileIds([$fileId]));
  46. return $items[0] ?? null;
  47. }
  48. /**
  49. * @param array[] $rows
  50. * @return array<int, CacheEntry>
  51. */
  52. private function rowsToEntries(array $rows): array {
  53. $result = [];
  54. foreach ($rows as $row) {
  55. $entry = Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
  56. $result[$entry->getId()] = $entry;
  57. }
  58. return $result;
  59. }
  60. /**
  61. * @param int[] $fileIds
  62. * @return array<int, CacheEntry>
  63. */
  64. public function getByFileIds(array $fileIds): array {
  65. $query = $this->getQuery()->selectFileCache();
  66. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  67. $rows = $query->executeQuery()->fetchAll();
  68. return $this->rowsToEntries($rows);
  69. }
  70. /**
  71. * @param int[] $fileIds
  72. * @param int $storageId
  73. * @return array<int, CacheEntry>
  74. */
  75. public function getByFileIdsInStorage(array $fileIds, int $storageId): array {
  76. $fileIds = array_values($fileIds);
  77. $query = $this->getQuery()->selectFileCache();
  78. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  79. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  80. $rows = $query->executeQuery()->fetchAll();
  81. return $this->rowsToEntries($rows);
  82. }
  83. }