diff options
Diffstat (limited to 'apps/files_trashbin/lib/Trashbin.php')
-rw-r--r-- | apps/files_trashbin/lib/Trashbin.php | 267 |
1 files changed, 128 insertions, 139 deletions
diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index 442abc13670..667066c2fca 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -1,46 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bart Visscher <bartv@thisnet.nl> - * @author Bastien Ho <bastienho@urbancube.fr> - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Florin Peter <github@florin-peter.de> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es> - * @author Julius Härtl <jus@bitgrid.net> - * @author Lars Knickrehm <mail@lars-sh.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Qingping Hou <dave2008713@gmail.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Sjors van der Pluijm <sjors@desjors.nl> - * @author Steven Bühner <buehner@me.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Victor Dubiniuk <dubiniuk@owncloud.com> - * @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 OCA\Files_Trashbin; @@ -49,45 +12,58 @@ use OC\Files\Cache\Cache; use OC\Files\Cache\CacheEntry; use OC\Files\Cache\CacheQueryBuilder; use OC\Files\Filesystem; -use OC\Files\Node\File; -use OC\Files\Node\Folder; use OC\Files\Node\NonExistingFile; use OC\Files\Node\NonExistingFolder; -use OC\Files\ObjectStore\ObjectStoreStorage; use OC\Files\View; +use OC\User\NoUserException; use OC_User; use OCA\Files_Trashbin\AppInfo\Application; use OCA\Files_Trashbin\Command\Expire; use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent; use OCA\Files_Trashbin\Events\NodeRestoredEvent; +use OCA\Files_Trashbin\Exceptions\CopyRecursiveException; +use OCA\Files_Versions\Storage; use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Command\IBus; +use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\Storage\ILockingStorage; +use OCP\Files\Storage\IStorage; use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IURLGenerator; +use OCP\IUserManager; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; +use OCP\Server; +use OCP\Util; use Psr\Log\LoggerInterface; -class Trashbin { +/** @template-implements IEventListener<BeforeNodeDeletedEvent> */ +class Trashbin implements IEventListener { // unit: percentage; 50% of available disk space/quota public const DEFAULTMAXSIZE = 50; /** * Ensure we don't need to scan the file during the move to trash * by triggering the scan in the pre-hook - * - * @param array $params */ - public static function ensureFileScannedHook($params) { + public static function ensureFileScannedHook(Node $node): void { try { - self::getUidAndFilename($params['path']); + self::getUidAndFilename($node->getPath()); } catch (NotFoundException $e) { - // nothing to scan for non existing files + // Nothing to scan for non existing files } } @@ -97,11 +73,11 @@ class Trashbin { * * @param string $filename * @return array - * @throws \OC\User\NoUserException + * @throws NoUserException */ public static function getUidAndFilename($filename) { $uid = Filesystem::getOwner($filename); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); // if the user with the UID doesn't exists, e.g. because the UID points // to a remote user with a federated cloud ID we use the current logged-in // user. We need a valid local user to move the file to the right trash bin @@ -126,24 +102,23 @@ class Trashbin { } /** - * get original location of files for user + * get original location and deleted by of files for user * * @param string $user - * @return array (filename => array (timestamp => original location)) + * @return array<string, array<string, array{location: string, deletedBy: string}>> */ - public static function getLocations($user) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query->select('id', 'timestamp', 'location') + public static function getExtraData($user) { + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->select('id', 'timestamp', 'location', 'deleted_by') ->from('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))); $result = $query->executeQuery(); $array = []; while ($row = $result->fetch()) { - if (isset($array[$row['id']])) { - $array[$row['id']][$row['timestamp']] = $row['location']; - } else { - $array[$row['id']] = [$row['timestamp'] => $row['location']]; - } + $array[$row['id']][$row['timestamp']] = [ + 'location' => (string)$row['location'], + 'deletedBy' => (string)$row['deleted_by'], + ]; } $result->closeCursor(); return $array; @@ -155,10 +130,10 @@ class Trashbin { * @param string $user * @param string $filename * @param string $timestamp - * @return string original location + * @return string|false original location */ public static function getLocation($user, $filename, $timestamp) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->select('location') ->from('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))) @@ -176,7 +151,8 @@ class Trashbin { } } - private static function setUpTrash($user) { + /** @param string $user */ + private static function setUpTrash($user): void { $view = new View('/' . $user); if (!$view->is_dir('files_trashbin')) { $view->mkdir('files_trashbin'); @@ -199,10 +175,10 @@ class Trashbin { * @param string $sourcePath * @param string $owner * @param string $targetPath - * @param $user + * @param string $user * @param int $timestamp */ - private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp) { + private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp): void { self::setUpTrash($owner); $targetFilename = basename($targetPath); @@ -223,15 +199,16 @@ class Trashbin { if ($view->file_exists($target)) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->insert('files_trash') ->setValue('id', $query->createNamedParameter($targetFilename)) ->setValue('timestamp', $query->createNamedParameter($timestamp)) ->setValue('location', $query->createNamedParameter($targetLocation)) - ->setValue('user', $query->createNamedParameter($user)); + ->setValue('user', $query->createNamedParameter($user)) + ->setValue('deleted_by', $query->createNamedParameter($user)); $result = $query->executeStatement(); if (!$result) { - \OC::$server->get(LoggerInterface::class)->error('trash bin database couldn\'t be updated for the files owner', ['app' => 'files_trashbin']); + Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated for the files owner', ['app' => 'files_trashbin']); } } } @@ -281,20 +258,19 @@ class Trashbin { $filename = $path_parts['basename']; $location = $path_parts['dirname']; /** @var ITimeFactory $timeFactory */ - $timeFactory = \OC::$server->query(ITimeFactory::class); + $timeFactory = Server::get(ITimeFactory::class); $timestamp = $timeFactory->getTime(); - $lockingProvider = \OC::$server->getLockingProvider(); + $lockingProvider = Server::get(ILockingProvider::class); // disable proxy to prevent recursive calls $trashPath = '/files_trashbin/files/' . static::getTrashFilename($filename, $timestamp); $gotLock = false; - while (!$gotLock) { + do { + /** @var ILockingStorage & IStorage $trashStorage */ + [$trashStorage, $trashInternalPath] = $ownerView->resolvePath($trashPath); try { - /** @var \OC\Files\Storage\Storage $trashStorage */ - [$trashStorage, $trashInternalPath] = $ownerView->resolvePath($trashPath); - $trashStorage->acquireLock($trashInternalPath, ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider); $gotLock = true; } catch (LockedException $e) { @@ -305,7 +281,7 @@ class Trashbin { $trashPath = '/files_trashbin/files/' . static::getTrashFilename($filename, $timestamp); } - } + } while (!$gotLock); $sourceStorage = $sourceInfo->getStorage(); $sourceInternalPath = $sourceInfo->getInternalPath(); @@ -319,21 +295,19 @@ class Trashbin { return false; } - $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); - try { $moveSuccessful = true; - // when moving within the same object store, the cache update done above is enough to move the file - if (!($trashStorage->instanceOfStorage(ObjectStoreStorage::class) && $trashStorage->getId() === $sourceStorage->getId())) { - $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); + $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); + if ($sourceStorage->getCache()->inCache($sourceInternalPath)) { + $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); } - } catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) { + } catch (CopyRecursiveException $e) { $moveSuccessful = false; if ($trashStorage->file_exists($trashInternalPath)) { $trashStorage->unlink($trashInternalPath); } - \OC::$server->get(LoggerInterface::class)->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']); + Server::get(LoggerInterface::class)->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']); } if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort @@ -353,17 +327,18 @@ class Trashbin { } if ($moveSuccessful) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->insert('files_trash') ->setValue('id', $query->createNamedParameter($filename)) ->setValue('timestamp', $query->createNamedParameter($timestamp)) ->setValue('location', $query->createNamedParameter($location)) - ->setValue('user', $query->createNamedParameter($owner)); + ->setValue('user', $query->createNamedParameter($owner)) + ->setValue('deleted_by', $query->createNamedParameter($user)); $result = $query->executeStatement(); if (!$result) { - \OC::$server->get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); + Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); } - \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', ['filePath' => Filesystem::normalizePath($file_path), + Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', ['filePath' => Filesystem::normalizePath($file_path), 'trashPath' => Filesystem::normalizePath(static::getTrashFilename($filename, $timestamp))]); self::retainVersions($filename, $owner, $ownerPath, $timestamp); @@ -387,14 +362,14 @@ class Trashbin { } private static function getConfiguredTrashbinSize(string $user): int|float { - $config = \OC::$server->get(IConfig::class); + $config = Server::get(IConfig::class); $userTrashbinSize = $config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1'); if (is_numeric($userTrashbinSize) && ($userTrashbinSize > -1)) { - return \OCP\Util::numericToNumber($userTrashbinSize); + return Util::numericToNumber($userTrashbinSize); } $systemTrashbinSize = $config->getAppValue('files_trashbin', 'trashbin_size', '-1'); if (is_numeric($systemTrashbinSize)) { - return \OCP\Util::numericToNumber($systemTrashbinSize); + return Util::numericToNumber($systemTrashbinSize); } return -1; } @@ -408,7 +383,7 @@ class Trashbin { * @param int $timestamp when the file was deleted */ private static function retainVersions($filename, $owner, $ownerPath, $timestamp) { - if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions') && !empty($ownerPath)) { + if (Server::get(IAppManager::class)->isEnabledForUser('files_versions') && !empty($ownerPath)) { $user = OC_User::getUser(); $rootView = new View('/'); @@ -417,7 +392,7 @@ class Trashbin { self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . static::getTrashFilename(basename($ownerPath), $timestamp), $rootView); } self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . static::getTrashFilename($filename, $timestamp)); - } elseif ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) { + } elseif ($versions = Storage::getVersions($owner, $ownerPath)) { foreach ($versions as $v) { if ($owner !== $user) { self::copy($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $owner . '/files_trashbin/versions/' . static::getTrashFilename($v['name'] . '.v' . $v['version'], $timestamp)); @@ -476,7 +451,7 @@ class Trashbin { * Restore a file or folder from trash bin * * @param string $file path to the deleted file/folder relative to "files_trashbin/files/", - * including the timestamp suffix ".d12345678" + * including the timestamp suffix ".d12345678" * @param string $filename name of the file/folder * @param int $timestamp time when the file/folder was deleted * @@ -490,12 +465,12 @@ class Trashbin { if ($timestamp) { $location = self::getLocation($user, $filename, $timestamp); if ($location === false) { - \OC::$server->get(LoggerInterface::class)->error('trash bin database inconsistent! ($user: ' . $user . ' $filename: ' . $filename . ', $timestamp: ' . $timestamp . ')', ['app' => 'files_trashbin']); + Server::get(LoggerInterface::class)->error('trash bin database inconsistent! ($user: ' . $user . ' $filename: ' . $filename . ', $timestamp: ' . $timestamp . ')', ['app' => 'files_trashbin']); } else { // if location no longer exists, restore file in the root directory - if ($location !== '/' && - (!$view->is_dir('files/' . $location) || - !$view->isCreatable('files/' . $location)) + if ($location !== '/' + && (!$view->is_dir('files/' . $location) + || !$view->isCreatable('files/' . $location)) ) { $location = ''; } @@ -524,7 +499,7 @@ class Trashbin { $targetNode = self::getNodeForPath($targetPath); $run = true; $event = new BeforeNodeRestoredEvent($sourceNode, $targetNode, $run); - $dispatcher = \OC::$server->get(IEventDispatcher::class); + $dispatcher = Server::get(IEventDispatcher::class); $dispatcher->dispatchTyped($event); if (!$run) { @@ -539,18 +514,18 @@ class Trashbin { $view->chroot('/' . $user . '/files'); $view->touch('/' . $location . '/' . $uniqueFilename, $mtime); $view->chroot($fakeRoot); - \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', ['filePath' => $targetPath, 'trashPath' => $sourcePath]); + Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', ['filePath' => $targetPath, 'trashPath' => $sourcePath]); $sourceNode = self::getNodeForPath($sourcePath); $targetNode = self::getNodeForPath($targetPath); $event = new NodeRestoredEvent($sourceNode, $targetNode); - $dispatcher = \OC::$server->get(IEventDispatcher::class); + $dispatcher = Server::get(IEventDispatcher::class); $dispatcher->dispatchTyped($event); self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); if ($timestamp) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->delete('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))) ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) @@ -576,7 +551,7 @@ class Trashbin { * @return false|null */ private static function restoreVersions(View $view, $file, $filename, $uniqueFilename, $location, $timestamp) { - if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { + if (Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { $user = OC_User::getUser(); $rootView = new View('/'); @@ -642,7 +617,7 @@ class Trashbin { // actual file deletion $trash->delete(); - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->delete('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))); $query->executeStatement(); @@ -694,7 +669,7 @@ class Trashbin { $size = 0; if ($timestamp) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->delete('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))) ->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename))) @@ -734,7 +709,7 @@ class Trashbin { */ private static function deleteVersions(View $view, $file, $filename, $timestamp, string $user): int|float { $size = 0; - if (\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { + if (Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { if ($view->is_dir('files_trashbin/versions/' . $file)) { $size += self::calculateSize(new View('/' . $user . '/files_trashbin/versions/' . $file)); $view->unlink('files_trashbin/versions/' . $file); @@ -779,10 +754,10 @@ class Trashbin { * @return bool result of db delete operation */ public static function deleteUser($uid) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->delete('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($uid))); - return (bool) $query->executeStatement(); + return (bool)$query->executeStatement(); } /** @@ -798,7 +773,7 @@ class Trashbin { return $configuredTrashbinSize - $trashbinSize; } - $userObject = \OC::$server->getUserManager()->get($user); + $userObject = Server::get(IUserManager::class)->get($user); if (is_null($userObject)) { return 0; } @@ -812,7 +787,7 @@ class Trashbin { $quota = PHP_INT_MAX; } } else { - $quota = \OCP\Util::computerFileSize($quota); + $quota = Util::computerFileSize($quota); // invalid quota if ($quota === false) { $quota = PHP_INT_MAX; @@ -836,7 +811,7 @@ class Trashbin { $availableSpace = $quota; } - return \OCP\Util::numericToNumber($availableSpace); + return Util::numericToNumber($availableSpace); } /** @@ -880,10 +855,10 @@ class Trashbin { private static function scheduleExpire($user) { // let the admin disable auto expire /** @var Application $application */ - $application = \OC::$server->query(Application::class); + $application = Server::get(Application::class); $expiration = $application->getContainer()->query('Expiration'); if ($expiration->isEnabled()) { - \OC::$server->getCommandBus()->push(new Expire($user)); + Server::get(IBus::class)->push(new Expire($user)); } } @@ -898,7 +873,7 @@ class Trashbin { */ protected static function deleteFiles(array $files, string $user, int|float $availableSpace): int|float { /** @var Application $application */ - $application = \OC::$server->query(Application::class); + $application = Server::get(Application::class); $expiration = $application->getContainer()->query('Expiration'); $size = 0; @@ -906,7 +881,13 @@ class Trashbin { foreach ($files as $file) { if ($availableSpace < 0 && $expiration->isExpired($file['mtime'], true)) { $tmp = self::delete($file['name'], $user, $file['mtime']); - \OC::$server->get(LoggerInterface::class)->info('remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota)', ['app' => 'files_trashbin']); + Server::get(LoggerInterface::class)->info( + 'remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota) for user "{user}"', + [ + 'app' => 'files_trashbin', + 'user' => $user, + ] + ); $availableSpace += $tmp; $size += $tmp; } else { @@ -926,7 +907,7 @@ class Trashbin { */ public static function deleteExpiredFiles($files, $user) { /** @var Expiration $expiration */ - $expiration = \OC::$server->query(Expiration::class); + $expiration = Server::get(Expiration::class); $size = 0; $count = 0; foreach ($files as $file) { @@ -936,17 +917,21 @@ class Trashbin { try { $size += self::delete($filename, $user, $timestamp); $count++; - } catch (\OCP\Files\NotPermittedException $e) { - \OC::$server->get(LoggerInterface::class)->warning('Removing "' . $filename . '" from trashbin failed.', + } catch (NotPermittedException $e) { + Server::get(LoggerInterface::class)->warning('Removing "' . $filename . '" from trashbin failed for user "{user}"', [ 'exception' => $e, 'app' => 'files_trashbin', + 'user' => $user, ] ); } - \OC::$server->get(LoggerInterface::class)->info( - 'Remove "' . $filename . '" from trashbin because it exceeds max retention obligation term.', - ['app' => 'files_trashbin'] + Server::get(LoggerInterface::class)->info( + 'Remove "' . $filename . '" from trashbin for user "{user}" because it exceeds max retention obligation term.', + [ + 'app' => 'files_trashbin', + 'user' => $user, + ], ); } else { break; @@ -978,7 +963,7 @@ class Trashbin { $size += $view->filesize($pathDir); $result = $view->copy($pathDir, $destination . '/' . $i['name']); if (!$result) { - throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); + throw new CopyRecursiveException(); } $view->touch($destination . '/' . $i['name'], $view->filemtime($pathDir)); } @@ -987,7 +972,7 @@ class Trashbin { $size += $view->filesize($source); $result = $view->copy($source, $destination); if (!$result) { - throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); + throw new CopyRecursiveException(); } $view->touch($destination, $view->filemtime($source)); } @@ -1007,10 +992,10 @@ class Trashbin { /** @var \OC\Files\Storage\Storage $storage */ [$storage,] = $view->resolvePath('/'); - $pattern = \OC::$server->getDatabaseConnection()->escapeLikeParameter(basename($filename)); + $pattern = Server::get(IDBConnection::class)->escapeLikeParameter(basename($filename)); if ($timestamp) { // fetch for old versions - $escapedTimestamp = \OC::$server->getDatabaseConnection()->escapeLikeParameter($timestamp); + $escapedTimestamp = Server::get(IDBConnection::class)->escapeLikeParameter((string)$timestamp); $pattern .= '.v%.d' . $escapedTimestamp; $offset = -strlen($escapedTimestamp) - 2; } else { @@ -1020,12 +1005,10 @@ class Trashbin { // Manually fetch all versions from the file cache to be able to filter them by their parent $cache = $storage->getCache(''); $query = new CacheQueryBuilder( - \OC::$server->getDatabaseConnection(), - \OC::$server->getSystemConfig(), - \OC::$server->get(LoggerInterface::class), - \OC::$server->get(IFilesMetadataManager::class), + Server::get(IDBConnection::class)->getQueryBuilder(), + Server::get(IFilesMetadataManager::class), ); - $normalizedParentPath = ltrim(Filesystem::normalizePath(dirname('files_trashbin/versions/'. $filename)), '/'); + $normalizedParentPath = ltrim(Filesystem::normalizePath(dirname('files_trashbin/versions/' . $filename)), '/'); $parentId = $cache->getId($normalizedParentPath); if ($parentId === -1) { return []; @@ -1042,7 +1025,7 @@ class Trashbin { /** @var CacheEntry[] $matches */ $matches = array_map(function (array $data) { - return Cache::cacheEntryFromData($data, \OC::$server->getMimeTypeLoader()); + return Cache::cacheEntryFromData($data, Server::get(IMimeTypeLoader::class)); }, $entries); foreach ($matches as $ma) { @@ -1069,7 +1052,7 @@ class Trashbin { private static function getUniqueFilename($location, $filename, View $view) { $ext = pathinfo($filename, PATHINFO_EXTENSION); $name = pathinfo($filename, PATHINFO_FILENAME); - $l = \OC::$server->getL10N('files_trashbin'); + $l = Util::getL10N('files_trashbin'); $location = '/' . trim($location, '/'); @@ -1080,9 +1063,9 @@ class Trashbin { if ($view->file_exists('files' . $location . '/' . $filename)) { $i = 2; - $uniqueName = $name . " (" . $l->t("restored") . ")" . $ext; + $uniqueName = $name . ' (' . $l->t('restored') . ')' . $ext; while ($view->file_exists('files' . $location . '/' . $uniqueName)) { - $uniqueName = $name . " (" . $l->t("restored") . " " . $i . ")" . $ext; + $uniqueName = $name . ' (' . $l->t('restored') . ' ' . $i . ')' . $ext; $i++; } @@ -1099,7 +1082,7 @@ class Trashbin { * @return int|float size of the folder */ private static function calculateSize(View $view): int|float { - $root = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); + $root = Server::get(IConfig::class)->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); if (!file_exists($root)) { return 0; } @@ -1144,7 +1127,7 @@ class Trashbin { public static function isEmpty($user) { $view = new View('/' . $user . '/files_trashbin'); if ($view->is_dir('/files') && $dh = $view->opendir('/files')) { - while ($file = readdir($dh)) { + while (($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { return false; } @@ -1158,7 +1141,7 @@ class Trashbin { * @return string */ public static function preview_icon($path) { - return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_trashbin_preview', ['x' => 32, 'y' => 32, 'file' => $path]); + return Server::get(IURLGenerator::class)->linkToRoute('core_ajax_trashbin_preview', ['x' => 32, 'y' => 32, 'file' => $path]); } /** @@ -1182,7 +1165,7 @@ class Trashbin { private static function getNodeForPath(string $path): Node { $user = OC_User::getUser(); - $rootFolder = \OC::$server->get(IRootFolder::class); + $rootFolder = Server::get(IRootFolder::class); if ($user !== false) { $userFolder = $rootFolder->getUserFolder($user); @@ -1194,7 +1177,7 @@ class Trashbin { } } - $view = \OC::$server->get(View::class); + $view = Server::get(View::class); $fsView = Filesystem::getView(); if ($fsView === null) { throw new Exception('View should not be null'); @@ -1208,4 +1191,10 @@ class Trashbin { return new NonExistingFile($rootFolder, $view, $fullPath); } } + + public function handle(Event $event): void { + if ($event instanceof BeforeNodeDeletedEvent) { + self::ensureFileScannedHook($event->getNode()); + } + } } |