diff options
Diffstat (limited to 'apps/files_sharing/lib/Cache.php')
-rw-r--r-- | apps/files_sharing/lib/Cache.php | 129 |
1 files changed, 66 insertions, 63 deletions
diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index c3f31ac3e4f..f9042fc0765 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -1,39 +1,26 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christopher Schäpers <kondou@ts.unde.re> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Michael Gapczynski <GapczynskiM@gmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_Sharing; +use OC\Files\Cache\CacheDependencies; use OC\Files\Cache\FailedCache; use OC\Files\Cache\Wrapper\CacheJail; +use OC\Files\Search\SearchBinaryOperator; +use OC\Files\Search\SearchComparison; use OC\Files\Storage\Wrapper\Jail; +use OC\User\DisplayNameCache; +use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\Search\ISearchBinaryOperator; +use OCP\Files\Search\ISearchComparison; +use OCP\Files\Search\ISearchOperator; use OCP\Files\StorageNotAvailableException; +use OCP\Share\IShare; /** * Metadata cache for shared files @@ -41,34 +28,27 @@ use OCP\Files\StorageNotAvailableException; * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead */ class Cache extends CacheJail { - /** - * @var \OCA\Files_Sharing\SharedStorage - */ - private $storage; - - /** - * @var ICacheEntry - */ - private $sourceRootInfo; - - private $rootUnchanged = true; - - private $ownerDisplayName; - + private bool $rootUnchanged = true; + private ?string $ownerDisplayName = null; private $numericId; + private DisplayNameCache $displayNameCache; /** - * @param \OCA\Files_Sharing\SharedStorage $storage - * @param ICacheEntry $sourceRootInfo + * @param SharedStorage $storage */ - public function __construct($storage, ICacheEntry $sourceRootInfo) { - $this->storage = $storage; - $this->sourceRootInfo = $sourceRootInfo; - $this->numericId = $sourceRootInfo->getStorageId(); + public function __construct( + private $storage, + private ICacheEntry $sourceRootInfo, + CacheDependencies $dependencies, + private IShare $share, + ) { + $this->numericId = $this->sourceRootInfo->getStorageId(); + $this->displayNameCache = $dependencies->getDisplayNameCache(); parent::__construct( null, - '' + '', + $dependencies, ); } @@ -78,18 +58,22 @@ class Cache extends CacheJail { // the sourceRootInfo path is the absolute path of the folder in the "real" storage // in the case where a folder is shared from a Jail we need to ensure that the share Jail - // has it's root set relative to the source Jail + // has its root set relative to the source Jail $currentStorage = $this->storage->getSourceStorage(); if ($currentStorage->instanceOfStorage(Jail::class)) { /** @var Jail $currentStorage */ $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot); } - $this->root = $absoluteRoot; + $this->root = $absoluteRoot ?? ''; } return $this->root; } - public function getCache() { + public function getGetUnjailedRoot(): string { + return $this->sourceRootInfo->getPath(); + } + + public function getCache(): ICache { if (is_null($this->cache)) { $sourceStorage = $this->storage->getSourceStorage(); if ($sourceStorage) { @@ -106,7 +90,7 @@ class Cache extends CacheJail { if (isset($this->numericId)) { return $this->numericId; } else { - return false; + return -1; } } @@ -132,7 +116,7 @@ class Cache extends CacheJail { parent::remove($file); } - public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { $this->rootUnchanged = false; return parent::moveFromCache($sourceCache, $sourcePath, $targetPath); } @@ -147,16 +131,20 @@ class Cache extends CacheJail { try { if (isset($entry['permissions'])) { - $entry['permissions'] &= $this->storage->getShare()->getPermissions(); + $entry['permissions'] &= $this->share->getPermissions(); } else { $entry['permissions'] = $this->storage->getPermissions($entry['path']); } + + if ($this->share->getNodeId() === $entry['fileid']) { + $entry['name'] = basename($this->share->getTarget()); + } } catch (StorageNotAvailableException $e) { // thrown by FailedStorage e.g. when the sharer does not exist anymore // (IDE may say the exception is never thrown – false negative) $sharePermissions = 0; } - $entry['uid_owner'] = $this->storage->getOwner(''); + $entry['uid_owner'] = $this->share->getShareOwner(); $entry['displayname_owner'] = $this->getOwnerDisplayName(); if ($path === '') { $entry['is_share_mount_point'] = true; @@ -166,7 +154,8 @@ class Cache extends CacheJail { private function getOwnerDisplayName() { if (!$this->ownerDisplayName) { - $this->ownerDisplayName = \OC_User::getDisplayName($this->storage->getOwner('')); + $uid = $this->share->getShareOwner(); + $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid) ?? $uid; } return $this->ownerDisplayName; } @@ -178,19 +167,33 @@ class Cache extends CacheJail { // Not a valid action for Shared Cache } - public function search($pattern) { - // Do the normal search on the whole storage for non files + public function getQueryFilterForStorage(): ISearchOperator { + $storageFilter = \OC\Files\Cache\Cache::getQueryFilterForStorage(); + + // Do the normal jail behavior for non files if ($this->storage->getItemType() !== 'file') { - return parent::search($pattern); + return $this->addJailFilterQuery($storageFilter); } - $regex = '/' . str_replace('%', '.*', $pattern) . '/i'; + // for single file shares we don't need to do the LIKE + return new SearchBinaryOperator( + ISearchBinaryOperator::OPERATOR_AND, + [ + $storageFilter, + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()), + ] + ); + } - $data = $this->get(''); - if (preg_match($regex, $data->getName()) === 1) { - return [$data]; + public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry { + if ($rawEntry->getStorageId() === $this->getNumericStorageId()) { + return parent::getCacheEntryFromSearchResult($rawEntry); + } else { + return null; } + } - return []; + public function markRootChanged(): void { + $this->rootUnchanged = false; } } |