123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OC\Files\Cache\Wrapper;
- use OC\Files\Cache\Cache;
- use OC\Files\Cache\CacheDependencies;
- use OCP\Files\Cache\ICache;
- use OCP\Files\Cache\ICacheEntry;
- use OCP\Files\Search\ISearchOperator;
- use OCP\Files\Search\ISearchQuery;
- use OCP\Server;
- class CacheWrapper extends Cache {
- /**
- * @var ?ICache
- */
- protected $cache;
- public function __construct(?ICache $cache, ?CacheDependencies $dependencies = null) {
- $this->cache = $cache;
- if (!$dependencies && $cache instanceof Cache) {
- $this->mimetypeLoader = $cache->mimetypeLoader;
- $this->connection = $cache->connection;
- $this->querySearchHelper = $cache->querySearchHelper;
- } else {
- if (!$dependencies) {
- $dependencies = Server::get(CacheDependencies::class);
- }
- $this->mimetypeLoader = $dependencies->getMimeTypeLoader();
- $this->connection = $dependencies->getConnection();
- $this->querySearchHelper = $dependencies->getQuerySearchHelper();
- }
- }
- protected function getCache() {
- return $this->cache;
- }
- protected function hasEncryptionWrapper(): bool {
- $cache = $this->getCache();
- if ($cache instanceof Cache) {
- return $cache->hasEncryptionWrapper();
- } else {
- return false;
- }
- }
- /**
- * Make it easy for wrappers to modify every returned cache entry
- *
- * @param ICacheEntry $entry
- * @return ICacheEntry|false
- */
- protected function formatCacheEntry($entry) {
- return $entry;
- }
- /**
- * get the stored metadata of a file or folder
- *
- * @param string|int $file
- * @return ICacheEntry|false
- */
- public function get($file) {
- $result = $this->getCache()->get($file);
- if ($result instanceof ICacheEntry) {
- $result = $this->formatCacheEntry($result);
- }
- return $result;
- }
- /**
- * get the metadata of all files stored in $folder
- *
- * @param string $folder
- * @return ICacheEntry[]
- */
- public function getFolderContents($folder) {
- // can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
- // and not the wrapped cache
- $fileId = $this->getId($folder);
- return $this->getFolderContentsById($fileId);
- }
- /**
- * get the metadata of all files stored in $folder
- *
- * @param int $fileId the file id of the folder
- * @return array
- */
- public function getFolderContentsById($fileId) {
- $results = $this->getCache()->getFolderContentsById($fileId);
- return array_map([$this, 'formatCacheEntry'], $results);
- }
- /**
- * insert or update meta data for a file or folder
- *
- * @param string $file
- * @param array $data
- *
- * @return int file id
- * @throws \RuntimeException
- */
- public function put($file, array $data) {
- if (($id = $this->getId($file)) > -1) {
- $this->update($id, $data);
- return $id;
- } else {
- return $this->insert($file, $data);
- }
- }
- /**
- * insert meta data for a new file or folder
- *
- * @param string $file
- * @param array $data
- *
- * @return int file id
- * @throws \RuntimeException
- */
- public function insert($file, array $data) {
- return $this->getCache()->insert($file, $data);
- }
- /**
- * update the metadata in the cache
- *
- * @param int $id
- * @param array $data
- */
- public function update($id, array $data) {
- $this->getCache()->update($id, $data);
- }
- /**
- * get the file id for a file
- *
- * @param string $file
- * @return int
- */
- public function getId($file) {
- return $this->getCache()->getId($file);
- }
- /**
- * get the id of the parent folder of a file
- *
- * @param string $file
- * @return int
- */
- public function getParentId($file) {
- return $this->getCache()->getParentId($file);
- }
- /**
- * check if a file is available in the cache
- *
- * @param string $file
- * @return bool
- */
- public function inCache($file) {
- return $this->getCache()->inCache($file);
- }
- /**
- * remove a file or folder from the cache
- *
- * @param string $file
- */
- public function remove($file) {
- $this->getCache()->remove($file);
- }
- /**
- * Move a file or folder in the cache
- *
- * @param string $source
- * @param string $target
- */
- public function move($source, $target) {
- $this->getCache()->move($source, $target);
- }
- protected function getMoveInfo($path) {
- /** @var Cache $cache */
- $cache = $this->getCache();
- return $cache->getMoveInfo($path);
- }
- public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
- $this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
- }
- /**
- * remove all entries for files that are stored on the storage from the cache
- */
- public function clear() {
- $this->getCache()->clear();
- }
- /**
- * @param string $file
- *
- * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
- */
- public function getStatus($file) {
- return $this->getCache()->getStatus($file);
- }
- public function searchQuery(ISearchQuery $query) {
- return current($this->querySearchHelper->searchInCaches($query, [$this]));
- }
- /**
- * update the folder size and the size of all parent folders
- *
- * @param string|boolean $path
- * @param array $data (optional) meta data of the folder
- */
- public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
- if ($this->getCache() instanceof Cache) {
- $this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
- }
- }
- /**
- * get the size of a folder and set it in the cache
- *
- * @param string $path
- * @param array|null|ICacheEntry $entry (optional) meta data of the folder
- * @return int|float
- */
- public function calculateFolderSize($path, $entry = null) {
- if ($this->getCache() instanceof Cache) {
- return $this->getCache()->calculateFolderSize($path, $entry);
- } else {
- return 0;
- }
- }
- /**
- * get all file ids on the files on the storage
- *
- * @return int[]
- */
- public function getAll() {
- return $this->getCache()->getAll();
- }
- /**
- * find a folder in the cache which has not been fully scanned
- *
- * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
- * use the one with the highest id gives the best result with the background scanner, since that is most
- * likely the folder where we stopped scanning previously
- *
- * @return string|false the path of the folder or false when no folder matched
- */
- public function getIncomplete() {
- return $this->getCache()->getIncomplete();
- }
- /**
- * get the path of a file on this storage by it's id
- *
- * @param int $id
- * @return string|null
- */
- public function getPathById($id) {
- return $this->getCache()->getPathById($id);
- }
- /**
- * Returns the numeric storage id
- *
- * @return int
- */
- public function getNumericStorageId() {
- return $this->getCache()->getNumericStorageId();
- }
- /**
- * get the storage id of the storage for a file and the internal path of the file
- * unlike getPathById this does not limit the search to files on this storage and
- * instead does a global search in the cache table
- *
- * @param int $id
- * @return array first element holding the storage id, second the path
- */
- public static function getById($id) {
- return parent::getById($id);
- }
- public function getQueryFilterForStorage(): ISearchOperator {
- return $this->getCache()->getQueryFilterForStorage();
- }
- public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
- $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
- if ($rawEntry) {
- $entry = $this->formatCacheEntry(clone $rawEntry);
- return $entry ?: null;
- }
- return null;
- }
- }
|