diff options
Diffstat (limited to 'apps/files_external/lib/Service/StoragesService.php')
-rw-r--r-- | apps/files_external/lib/Service/StoragesService.php | 154 |
1 files changed, 47 insertions, 107 deletions
diff --git a/apps/files_external/lib/Service/StoragesService.php b/apps/files_external/lib/Service/StoragesService.php index d52bf410461..a12a8fc245a 100644 --- a/apps/files_external/lib/Service/StoragesService.php +++ b/apps/files_external/lib/Service/StoragesService.php @@ -1,70 +1,46 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Jesús Macias <jmacias@solidgear.es> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Stefan Weil <sw@weilnetz.de> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\Files_External\Service; -use \OC\Files\Filesystem; +use OC\Files\Cache\Storage; +use OC\Files\Filesystem; +use OCA\Files_External\Lib\Auth\AuthMechanism; use OCA\Files_External\Lib\Auth\InvalidAuth; +use OCA\Files_External\Lib\Backend\Backend; use OCA\Files_External\Lib\Backend\InvalidBackend; +use OCA\Files_External\Lib\DefinitionParameter; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\NotFoundException; -use \OCA\Files_External\Lib\Backend\Backend; -use \OCA\Files_External\Lib\Auth\AuthMechanism; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\IUserMountCache; -use \OCP\Files\StorageNotAvailableException; +use OCP\Files\Events\InvalidateMountCacheEvent; +use OCP\Files\StorageNotAvailableException; +use OCP\Server; +use OCP\Util; +use Psr\Log\LoggerInterface; /** - * Service class to manage external storages + * Service class to manage external storage */ abstract class StoragesService { - /** @var BackendService */ - protected $backendService; - - /** - * @var DBConfigService - */ - protected $dbConfig; - - /** - * @var IUserMountCache - */ - protected $userMountCache; - /** * @param BackendService $backendService - * @param DBConfigService $dbConfigService + * @param DBConfigService $dbConfig * @param IUserMountCache $userMountCache + * @param IEventDispatcher $eventDispatcher */ - public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { - $this->backendService = $backendService; - $this->dbConfig = $dbConfigService; - $this->userMountCache = $userMountCache; + public function __construct( + protected BackendService $backendService, + protected DBConfigService $dbConfig, + protected IUserMountCache $userMountCache, + protected IEventDispatcher $eventDispatcher, + ) { } protected function readDBConfig() { @@ -102,24 +78,22 @@ abstract class StoragesService { return $config; } catch (\UnexpectedValueException $e) { // don't die if a storage backend doesn't exist - \OCP\Util::writeLog( - 'files_external', - 'Could not load storage: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); + Server::get(LoggerInterface::class)->error('Could not load storage.', [ + 'app' => 'files_external', + 'exception' => $e, + ]); return null; } catch (\InvalidArgumentException $e) { - \OCP\Util::writeLog( - 'files_external', - 'Could not load storage: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); + Server::get(LoggerInterface::class)->error('Could not load storage.', [ + 'app' => 'files_external', + 'exception' => $e, + ]); return null; } } /** - * Read the external storages config + * Read the external storage config * * @return array map of storage id to storage config */ @@ -145,7 +119,7 @@ abstract class StoragesService { * @return StorageConfig * @throws NotFoundException if the storage with the given id was not found */ - public function getStorage($id) { + public function getStorage(int $id) { $mount = $this->dbConfig->getMountById($id); if (!is_array($mount)) { @@ -214,7 +188,7 @@ abstract class StoragesService { /** * Get the visibility type for this controller, used in validation * - * @return string BackendService::VISIBILITY_* constants + * @return int BackendService::VISIBILITY_* constants */ abstract public function getVisibilityType(); @@ -293,7 +267,7 @@ abstract class StoragesService { $mountOptions = null, $applicableUsers = null, $applicableGroups = null, - $priority = null + $priority = null, ) { $backend = $this->backendService->getBackend($backendIdentifier); if (!$backend) { @@ -328,13 +302,14 @@ abstract class StoragesService { * Triggers the given hook signal for all the applicables given * * @param string $signal signal - * @param string $mountPoint hook mount pount param + * @param string $mountPoint hook mount point param * @param string $mountType hook mount type param * @param array $applicableArray array of applicable users/groups for which to trigger the hook */ - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { + $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); foreach ($applicableArray as $applicable) { - \OCP\Util::emitHook( + Util::emitHook( Filesystem::CLASSNAME, $signal, [ @@ -415,7 +390,7 @@ abstract class StoragesService { if ($wasGlobal && !$isGlobal) { $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - } else if (!$wasGlobal && $isGlobal) { + } elseif (!$wasGlobal && $isGlobal) { $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); } @@ -423,7 +398,9 @@ abstract class StoragesService { $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); foreach ($changedConfig as $key => $value) { - $this->dbConfig->setConfig($id, $key, $value); + if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { + $this->dbConfig->setConfig($id, $key, $value); + } } foreach ($changedOptions as $key => $value) { $this->dbConfig->setOption($id, $key, $value); @@ -458,7 +435,7 @@ abstract class StoragesService { * * @throws NotFoundException if no storage was found with the given id */ - public function removeStorage($id) { + public function removeStorage(int $id) { $existingMount = $this->dbConfig->getMountById($id); if (!is_array($existingMount)) { @@ -471,45 +448,7 @@ abstract class StoragesService { $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); // delete oc_storages entries and oc_filecache - try { - $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); - \OC\Files\Cache\Storage::remove($rustyStorageId); - } catch (\Exception $e) { - // can happen either for invalid configs where the storage could not - // be instantiated or whenever $user vars where used, in which case - // the storage id could not be computed - \OCP\Util::writeLog( - 'files_external', - 'Exception: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); - } - } - - /** - * Returns the rusty storage id from oc_storages from the given storage config. - * - * @param StorageConfig $storageConfig - * @return string rusty storage id - */ - private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { - // if any of the storage options contains $user, it is not possible - // to compute the possible storage id as we don't know which users - // mounted it already (and we certainly don't want to iterate over ALL users) - foreach ($storageConfig->getBackendOptions() as $value) { - if (strpos($value, '$user') !== false) { - throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); - } - } - - // note: similar to ConfigAdapter->prepateStorageConfig() - $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); - $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); - - $class = $storageConfig->getBackend()->getStorageClass(); - $storageImpl = new $class($storageConfig->getBackendOptions()); - - return $storageImpl->getId(); + Storage::cleanByMountId($id); } /** @@ -528,6 +467,7 @@ abstract class StoragesService { $storage = $storageConfig->getBackend()->wrapStorage($storage); $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); + /** @var \OC\Files\Storage\Storage $storage */ return $storage->getStorageCache()->getNumericId(); } catch (\Exception $e) { return -1; |