diff options
Diffstat (limited to 'lib/private/Cache/File.php')
-rw-r--r-- | lib/private/Cache/File.php | 63 |
1 files changed, 22 insertions, 41 deletions
diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php index 1f63e462bb5..ceddf472ebd 100644 --- a/lib/private/Cache/File.php +++ b/lib/private/Cache/File.php @@ -1,38 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Sebastian Wessalowski <sebastian@wessalowski.org> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.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: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Cache; use OC\Files\Filesystem; use OC\Files\View; use OCP\ICache; +use OCP\IUserSession; use OCP\Security\ISecureRandom; +use OCP\Server; use Psr\Log\LoggerInterface; class File implements ICache { @@ -50,17 +30,18 @@ class File implements ICache { if ($this->storage !== null) { return $this->storage; } - if (\OC::$server->getUserSession()->isLoggedIn()) { + $session = Server::get(IUserSession::class); + if ($session->isLoggedIn()) { $rootView = new View(); - $user = \OC::$server->getUserSession()->getUser(); - Filesystem::initMountPoints($user->getUID()); - if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) { - $rootView->mkdir('/' . $user->getUID() . '/cache'); + $userId = $session->getUser()->getUID(); + Filesystem::initMountPoints($userId); + if (!$rootView->file_exists('/' . $userId . '/cache')) { + $rootView->mkdir('/' . $userId . '/cache'); } - $this->storage = new View('/' . $user->getUID() . '/cache'); + $this->storage = new View('/' . $userId . '/cache'); return $this->storage; } else { - \OC::$server->get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']); + Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']); throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in'); } } @@ -105,7 +86,7 @@ class File implements ICache { $storage = $this->getStorage(); $result = false; // unique id to avoid chunk collision, just in case - $uniqueId = \OC::$server->getSecureRandom()->generate( + $uniqueId = Server::get(ISecureRandom::class)->generate( 16, ISecureRandom::CHAR_ALPHANUMERIC ); @@ -113,7 +94,7 @@ class File implements ICache { // use part file to prevent hasKey() to find the key // while it is being written $keyPart = $key . '.' . $uniqueId . '.part'; - if ($storage and $storage->file_put_contents($keyPart, $value)) { + if ($storage && $storage->file_put_contents($keyPart, $value)) { if ($ttl === 0) { $ttl = 86400; // 60*60*24 } @@ -156,11 +137,11 @@ class File implements ICache { */ public function clear($prefix = '') { $storage = $this->getStorage(); - if ($storage and $storage->is_dir('/')) { + if ($storage && $storage->is_dir('/')) { $dh = $storage->opendir('/'); if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { - if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) { + if ($file !== '.' && $file !== '..' && ($prefix === '' || str_starts_with($file, $prefix))) { $storage->unlink('/' . $file); } } @@ -184,7 +165,7 @@ class File implements ICache { return null; } while (($file = readdir($dh)) !== false) { - if ($file != '.' and $file != '..') { + if ($file !== '.' && $file !== '..') { try { $mtime = $storage->filemtime('/' . $file); if ($mtime < $now) { @@ -192,11 +173,11 @@ class File implements ICache { } } catch (\OCP\Lock\LockedException $e) { // ignore locked chunks - \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); } catch (\OCP\Files\ForbiddenException $e) { - \OC::$server->getLogger()->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); + Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); } catch (\OCP\Files\LockNotAcquiredException $e) { - \OC::$server->getLogger()->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); } } } |