FileAccess.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Files\Cache;
  25. use OC\FilesMetadata\FilesMetadataManager;
  26. use OC\SystemConfig;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\Files\Cache\IFileAccess;
  29. use OCP\Files\IMimeTypeLoader;
  30. use OCP\IDBConnection;
  31. use Psr\Log\LoggerInterface;
  32. /**
  33. * Low level access to the file cache
  34. */
  35. class FileAccess implements IFileAccess {
  36. public function __construct(
  37. private IDBConnection $connection,
  38. private SystemConfig $systemConfig,
  39. private LoggerInterface $logger,
  40. private FilesMetadataManager $metadataManager,
  41. private IMimeTypeLoader $mimeTypeLoader,
  42. ) {
  43. }
  44. private function getQuery(): CacheQueryBuilder {
  45. return new CacheQueryBuilder(
  46. $this->connection,
  47. $this->systemConfig,
  48. $this->logger,
  49. $this->metadataManager,
  50. );
  51. }
  52. public function getByFileIdInStorage(int $fileId, int $storageId): ?CacheEntry {
  53. $items = array_values($this->getByFileIdsInStorage([$fileId], $storageId));
  54. return $items[0] ?? null;
  55. }
  56. public function getByPathInStorage(string $path, int $storageId): ?CacheEntry {
  57. $query = $this->getQuery()->selectFileCache();
  58. $query->andWhere($query->expr()->eq('filecache.path_hash', $query->createNamedParameter(md5($path))));
  59. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  60. $row = $query->executeQuery()->fetch();
  61. return $row ? Cache::cacheEntryFromData($row, $this->mimeTypeLoader) : null;
  62. }
  63. public function getByFileId(int $fileId): ?CacheEntry {
  64. $items = array_values($this->getByFileIds([$fileId]));
  65. return $items[0] ?? null;
  66. }
  67. /**
  68. * @param array[] $rows
  69. * @return array<int, CacheEntry>
  70. */
  71. private function rowsToEntries(array $rows): array {
  72. $result = [];
  73. foreach ($rows as $row) {
  74. $entry = Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
  75. $result[$entry->getId()] = $entry;
  76. }
  77. return $result;
  78. }
  79. /**
  80. * @param int[] $fileIds
  81. * @return array<int, CacheEntry>
  82. */
  83. public function getByFileIds(array $fileIds): array {
  84. $query = $this->getQuery()->selectFileCache();
  85. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  86. $rows = $query->executeQuery()->fetchAll();
  87. return $this->rowsToEntries($rows);
  88. }
  89. /**
  90. * @param int[] $fileIds
  91. * @param int $storageId
  92. * @return array<int, CacheEntry>
  93. */
  94. public function getByFileIdsInStorage(array $fileIds, int $storageId): array {
  95. $fileIds = array_values($fileIds);
  96. $query = $this->getQuery()->selectFileCache();
  97. $query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
  98. $query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  99. $rows = $query->executeQuery()->fetchAll();
  100. return $this->rowsToEntries($rows);
  101. }
  102. }