FileAccess.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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,
  30. $this->systemConfig,
  31. $this->logger,
  32. $this->metadataManager,
  33. );
  34. }
  35. public function getByFileIdInStorage(int $fileId, int $storageId): ?CacheEntry {
  36. $items = array_values($this->getByFileIdsInStorage([$fileId], $storageId));
  37. return $items[0] ?? null;
  38. }
  39. public function getByPathInStorage(string $path, int $storageId): ?CacheEntry {
  40. $query = $this->getQuery()->selectFileCache();
  41. $query->andWhere($query->expr()->eq('filecache.path_hash', $query->createNamedParameter(md5($path))));
  42. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  43. $row = $query->executeQuery()->fetch();
  44. return $row ? Cache::cacheEntryFromData($row, $this->mimeTypeLoader) : null;
  45. }
  46. public function getByFileId(int $fileId): ?CacheEntry {
  47. $items = array_values($this->getByFileIds([$fileId]));
  48. return $items[0] ?? null;
  49. }
  50. /**
  51. * @param array[] $rows
  52. * @return array<int, CacheEntry>
  53. */
  54. private function rowsToEntries(array $rows): array {
  55. $result = [];
  56. foreach ($rows as $row) {
  57. $entry = Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
  58. $result[$entry->getId()] = $entry;
  59. }
  60. return $result;
  61. }
  62. /**
  63. * @param int[] $fileIds
  64. * @return array<int, CacheEntry>
  65. */
  66. public function getByFileIds(array $fileIds): array {
  67. $query = $this->getQuery()->selectFileCache();
  68. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  69. $rows = $query->executeQuery()->fetchAll();
  70. return $this->rowsToEntries($rows);
  71. }
  72. /**
  73. * @param int[] $fileIds
  74. * @param int $storageId
  75. * @return array<int, CacheEntry>
  76. */
  77. public function getByFileIdsInStorage(array $fileIds, int $storageId): array {
  78. $fileIds = array_values($fileIds);
  79. $query = $this->getQuery()->selectFileCache();
  80. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  81. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  82. $rows = $query->executeQuery()->fetchAll();
  83. return $this->rowsToEntries($rows);
  84. }
  85. }