diff options
Diffstat (limited to 'apps/files_trashbin/lib')
23 files changed, 206 insertions, 82 deletions
diff --git a/apps/files_trashbin/lib/AppInfo/Application.php b/apps/files_trashbin/lib/AppInfo/Application.php index 000677de96c..76d566f4286 100644 --- a/apps/files_trashbin/lib/AppInfo/Application.php +++ b/apps/files_trashbin/lib/AppInfo/Application.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -74,7 +75,7 @@ class Application extends App implements IBootstrap { } public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void { - foreach ($appManager->getInstalledApps() as $app) { + foreach ($appManager->getEnabledApps() as $app) { $appInfo = $appManager->getAppInfo($app); if (isset($appInfo['trash'])) { $backends = $appInfo['trash']; diff --git a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php index 4e1c1b95fde..bb383dab78d 100644 --- a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php +++ b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -12,15 +13,16 @@ use OCA\Files_Trashbin\Helper; use OCA\Files_Trashbin\Trashbin; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCP\IConfig; -use OCP\IUser; +use OCP\IAppConfig; use OCP\IUserManager; +use Psr\Log\LoggerInterface; class ExpireTrash extends TimedJob { public function __construct( - private IConfig $config, + private IAppConfig $appConfig, private IUserManager $userManager, private Expiration $expiration, + private LoggerInterface $logger, ITimeFactory $time, ) { parent::__construct($time); @@ -28,12 +30,8 @@ class ExpireTrash extends TimedJob { $this->setInterval(60 * 30); } - /** - * @param $argument - * @throws \Exception - */ protected function run($argument) { - $backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes'); + $backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes'); if ($backgroundJob === 'no') { return; } @@ -43,15 +41,32 @@ class ExpireTrash extends TimedJob { return; } - $this->userManager->callForSeenUsers(function (IUser $user): void { - $uid = $user->getUID(); - if (!$this->setupFS($uid)) { + $stopTime = time() + 60 * 30; // Stops after 30 minutes. + $offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0); + $users = $this->userManager->getSeenUsers($offset); + + foreach ($users as $user) { + try { + $uid = $user->getUID(); + if (!$this->setupFS($uid)) { + continue; + } + $dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); + Trashbin::deleteExpiredFiles($dirContent, $uid); + } catch (\Throwable $e) { + $this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]); + } + + $offset++; + + if ($stopTime < time()) { + $this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset); + \OC_Util::tearDownFS(); return; } - $dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); - Trashbin::deleteExpiredFiles($dirContent, $uid); - }); + } + $this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0); \OC_Util::tearDownFS(); } diff --git a/apps/files_trashbin/lib/Capabilities.php b/apps/files_trashbin/lib/Capabilities.php index 62be7bcb1a1..53c17a475ff 100644 --- a/apps/files_trashbin/lib/Capabilities.php +++ b/apps/files_trashbin/lib/Capabilities.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. diff --git a/apps/files_trashbin/lib/Command/CleanUp.php b/apps/files_trashbin/lib/Command/CleanUp.php index daaa4003f7a..e9b4fa8ae60 100644 --- a/apps/files_trashbin/lib/Command/CleanUp.php +++ b/apps/files_trashbin/lib/Command/CleanUp.php @@ -11,6 +11,7 @@ use OCP\Files\IRootFolder; use OCP\IDBConnection; use OCP\IUserBackend; use OCP\IUserManager; +use OCP\Util; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidOptionException; use Symfony\Component\Console\Input\InputArgument; @@ -96,7 +97,7 @@ class CleanUp extends Command { $node = $this->rootFolder->get($path); if ($verbose) { - $output->writeln('Deleting <info>' . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); + $output->writeln('Deleting <info>' . Util::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); } $node->delete(); if ($this->rootFolder->nodeExists($path)) { diff --git a/apps/files_trashbin/lib/Command/Expire.php b/apps/files_trashbin/lib/Command/Expire.php index f526438600e..73a42cd4749 100644 --- a/apps/files_trashbin/lib/Command/Expire.php +++ b/apps/files_trashbin/lib/Command/Expire.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -9,6 +10,8 @@ namespace OCA\Files_Trashbin\Command; use OC\Command\FileAccess; use OCA\Files_Trashbin\Trashbin; use OCP\Command\ICommand; +use OCP\IUserManager; +use OCP\Server; class Expire implements ICommand { use FileAccess; @@ -22,7 +25,7 @@ class Expire implements ICommand { } public function handle() { - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); if (!$userManager->userExists($this->user)) { // User has been deleted already return; diff --git a/apps/files_trashbin/lib/Command/ExpireTrash.php b/apps/files_trashbin/lib/Command/ExpireTrash.php index 37b35689666..422d8379984 100644 --- a/apps/files_trashbin/lib/Command/ExpireTrash.php +++ b/apps/files_trashbin/lib/Command/ExpireTrash.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud GmbH. @@ -8,10 +9,10 @@ namespace OCA\Files_Trashbin\Command; use OC\Files\View; use OCA\Files_Trashbin\Expiration; -use OCA\Files_Trashbin\Helper; use OCA\Files_Trashbin\Trashbin; use OCP\IUser; use OCP\IUserManager; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -25,6 +26,7 @@ class ExpireTrash extends Command { * @param Expiration|null $expiration */ public function __construct( + private LoggerInterface $logger, private ?IUserManager $userManager = null, private ?Expiration $expiration = null, ) { @@ -43,8 +45,9 @@ class ExpireTrash extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { + $minAge = $this->expiration->getMinAgeAsTimestamp(); $maxAge = $this->expiration->getMaxAgeAsTimestamp(); - if (!$maxAge) { + if ($minAge === false && $maxAge === false) { $output->writeln('Auto expiration is configured - keeps files and folders in the trash bin for 30 days and automatically deletes anytime after that if space is needed (note: files may not be deleted if space is not needed)'); return 1; } @@ -64,10 +67,12 @@ class ExpireTrash extends Command { } else { $p = new ProgressBar($output); $p->start(); - $this->userManager->callForSeenUsers(function (IUser $user) use ($p): void { + + $users = $this->userManager->getSeenUsers(); + foreach ($users as $user) { $p->advance(); $this->expireTrashForUser($user); - }); + } $p->finish(); $output->writeln(''); } @@ -75,12 +80,15 @@ class ExpireTrash extends Command { } public function expireTrashForUser(IUser $user) { - $uid = $user->getUID(); - if (!$this->setupFS($uid)) { - return; + try { + $uid = $user->getUID(); + if (!$this->setupFS($uid)) { + return; + } + Trashbin::expire($uid); + } catch (\Throwable $e) { + $this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]); } - $dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); - Trashbin::deleteExpiredFiles($dirContent, $uid); } /** diff --git a/apps/files_trashbin/lib/Command/RestoreAllFiles.php b/apps/files_trashbin/lib/Command/RestoreAllFiles.php index cb4e7f97ecd..ce31f759c0e 100644 --- a/apps/files_trashbin/lib/Command/RestoreAllFiles.php +++ b/apps/files_trashbin/lib/Command/RestoreAllFiles.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only diff --git a/apps/files_trashbin/lib/Command/Size.php b/apps/files_trashbin/lib/Command/Size.php index 11699ce25ea..9c19d4d92b3 100644 --- a/apps/files_trashbin/lib/Command/Size.php +++ b/apps/files_trashbin/lib/Command/Size.php @@ -13,6 +13,7 @@ use OCP\Command\IBus; use OCP\IConfig; use OCP\IUser; use OCP\IUserManager; +use OCP\Util; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -45,7 +46,7 @@ class Size extends Base { $size = $input->getArgument('size'); if ($size) { - $parsedSize = \OC_Helper::computerFileSize($size); + $parsedSize = Util::computerFileSize($size); if ($parsedSize === false) { $output->writeln('<error>Failed to parse input size</error>'); return -1; @@ -70,7 +71,7 @@ class Size extends Base { if ($globalSize < 0) { $globalHumanSize = 'default (50% of available space)'; } else { - $globalHumanSize = \OC_Helper::humanFileSize($globalSize); + $globalHumanSize = Util::humanFileSize($globalSize); } if ($user) { @@ -79,7 +80,7 @@ class Size extends Base { if ($userSize < 0) { $userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)"; } else { - $userHumanSize = \OC_Helper::humanFileSize($userSize); + $userHumanSize = Util::humanFileSize($userSize); } if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { @@ -106,7 +107,7 @@ class Size extends Base { if (count($userValues)) { $output->writeln('Per-user sizes:'); $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) { - return \OC_Helper::humanFileSize($size); + return Util::humanFileSize($size); }, $userValues)); } else { $output->writeln('No per-user sizes configured'); diff --git a/apps/files_trashbin/lib/Events/MoveToTrashEvent.php b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php index 1596315dd20..0d776b606b1 100644 --- a/apps/files_trashbin/lib/Events/MoveToTrashEvent.php +++ b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php b/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php index 9da2631c97b..3ea1293e5d7 100644 --- a/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php +++ b/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only diff --git a/apps/files_trashbin/lib/Expiration.php b/apps/files_trashbin/lib/Expiration.php index ed5d62aa294..0bbe39a9314 100644 --- a/apps/files_trashbin/lib/Expiration.php +++ b/apps/files_trashbin/lib/Expiration.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -94,6 +95,20 @@ class Expiration { } /** + * Get minimal retention obligation as a timestamp + * + * @return int|false + */ + public function getMinAgeAsTimestamp() { + $minAge = false; + if ($this->isEnabled() && $this->minAge !== self::NO_OBLIGATION) { + $time = $this->timeFactory->getTime(); + $minAge = $time - ($this->minAge * 86400); + } + return $minAge; + } + + /** * @return bool|int */ public function getMaxAgeAsTimestamp() { diff --git a/apps/files_trashbin/lib/Helper.php b/apps/files_trashbin/lib/Helper.php index 7aeb737a56c..746832e9280 100644 --- a/apps/files_trashbin/lib/Helper.php +++ b/apps/files_trashbin/lib/Helper.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -10,6 +11,8 @@ use OC\Files\FileInfo; use OC\Files\View; use OCP\Constants; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\IMimeTypeDetector; +use OCP\Server; class Helper { /** @@ -63,7 +66,7 @@ class Helper { $i = [ 'name' => $name, 'mtime' => $timestamp, - 'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name), + 'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : Server::get(IMimeTypeDetector::class)->detectPath($name), 'type' => $type, 'directory' => ($dir === '/') ? '' : $dir, 'size' => $entry->getSize(), diff --git a/apps/files_trashbin/lib/Sabre/RootCollection.php b/apps/files_trashbin/lib/Sabre/RootCollection.php index 06b0ffbeba2..8886dae0895 100644 --- a/apps/files_trashbin/lib/Sabre/RootCollection.php +++ b/apps/files_trashbin/lib/Sabre/RootCollection.php @@ -10,6 +10,8 @@ namespace OCA\Files_Trashbin\Sabre; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\IConfig; +use OCP\IUserSession; +use OCP\Server; use Sabre\DAV\INode; use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\PrincipalBackend; @@ -36,7 +38,7 @@ class RootCollection extends AbstractPrincipalCollection { */ public function getChildForPrincipal(array $principalInfo): TrashHome { [, $name] = \Sabre\Uri\split($principalInfo['uri']); - $user = \OC::$server->getUserSession()->getUser(); + $user = Server::get(IUserSession::class)->getUser(); if (is_null($user) || $name !== $user->getUID()) { throw new \Sabre\DAV\Exception\Forbidden(); } diff --git a/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php index 2a2e3a141dc..54bb1326966 100644 --- a/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php +++ b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php @@ -8,9 +8,12 @@ declare(strict_types=1); */ namespace OCA\Files_Trashbin\Sabre; +use OC\Files\FileInfo; +use OC\Files\View; use OCA\DAV\Connector\Sabre\FilesPlugin; use OCA\Files_Trashbin\Trash\ITrashItem; use OCP\IPreview; +use Psr\Log\LoggerInterface; use Sabre\DAV\INode; use Sabre\DAV\PropFind; use Sabre\DAV\Server; @@ -32,6 +35,7 @@ class TrashbinPlugin extends ServerPlugin { public function __construct( private IPreview $previewManager, + private View $view, ) { } @@ -40,6 +44,7 @@ class TrashbinPlugin extends ServerPlugin { $this->server->on('propFind', [$this, 'propFind']); $this->server->on('afterMethod:GET', [$this,'httpGet']); + $this->server->on('beforeMove', [$this, 'beforeMove']); } @@ -99,8 +104,8 @@ class TrashbinPlugin extends ServerPlugin { return $node->getFileId(); }); - $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { - return $this->previewManager->isAvailable($node->getFileInfo()); + $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node): string { + return $this->previewManager->isAvailable($node->getFileInfo()) ? 'true' : 'false'; }); $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () { @@ -129,4 +134,47 @@ class TrashbinPlugin extends ServerPlugin { $response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"'); } } + + /** + * Check if a user has available space before attempting to + * restore from trashbin unless they have unlimited quota. + * + * @param string $sourcePath + * @param string $destinationPath + * @return bool + */ + public function beforeMove(string $sourcePath, string $destinationPath): bool { + try { + $node = $this->server->tree->getNodeForPath($sourcePath); + $destinationNodeParent = $this->server->tree->getNodeForPath(dirname($destinationPath)); + } catch (\Sabre\DAV\Exception $e) { + \OCP\Server::get(LoggerInterface::class) + ->error($e->getMessage(), ['app' => 'files_trashbin', 'exception' => $e]); + return true; + } + + // Check if a file is being restored before proceeding + if (!$node instanceof ITrash || !$destinationNodeParent instanceof RestoreFolder) { + return true; + } + + $fileInfo = $node->getFileInfo(); + if (!$fileInfo instanceof ITrashItem) { + return true; + } + $restoreFolder = dirname($fileInfo->getOriginalLocation()); + $freeSpace = $this->view->free_space($restoreFolder); + if ($freeSpace === FileInfo::SPACE_NOT_COMPUTED + || $freeSpace === FileInfo::SPACE_UNKNOWN + || $freeSpace === FileInfo::SPACE_UNLIMITED) { + return true; + } + $filesize = $fileInfo->getSize(); + if ($freeSpace < $filesize) { + $this->server->httpResponse->setStatus(507); + return false; + } + + return true; + } } diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index a2855a97ac9..82b7af5a934 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -49,8 +50,8 @@ class Storage extends Wrapper { } catch (GenericEncryptionException $e) { // in case of a encryption exception we delete the file right away $this->logger->info( - "Can't move file " . $path . - ' to the trash bin, therefore it was deleted right away'); + "Can't move file " . $path + . ' to the trash bin, therefore it was deleted right away'); return $this->storage->unlink($path); } @@ -148,12 +149,12 @@ class Storage extends Wrapper { * Setup the storage wrapper callback */ public static function setupStorage(): void { - $trashManager = \OC::$server->get(ITrashManager::class); - $userManager = \OC::$server->get(IUserManager::class); - $logger = \OC::$server->get(LoggerInterface::class); - $eventDispatcher = \OC::$server->get(IEventDispatcher::class); - $rootFolder = \OC::$server->get(IRootFolder::class); - $request = \OC::$server->get(IRequest::class); + $trashManager = Server::get(ITrashManager::class); + $userManager = Server::get(IUserManager::class); + $logger = Server::get(LoggerInterface::class); + $eventDispatcher = Server::get(IEventDispatcher::class); + $rootFolder = Server::get(IRootFolder::class); + $request = Server::get(IRequest::class); Filesystem::addStorageWrapper( 'oc_trashbin', function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder, $request) { diff --git a/apps/files_trashbin/lib/Trash/BackendNotFoundException.php b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php index 8e23a04851a..292b6ee293c 100644 --- a/apps/files_trashbin/lib/Trash/BackendNotFoundException.php +++ b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/ITrashBackend.php b/apps/files_trashbin/lib/Trash/ITrashBackend.php index f5d4657bfbc..11b3132bfba 100644 --- a/apps/files_trashbin/lib/Trash/ITrashBackend.php +++ b/apps/files_trashbin/lib/Trash/ITrashBackend.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/ITrashItem.php b/apps/files_trashbin/lib/Trash/ITrashItem.php index f67276e6f54..299cac49a69 100644 --- a/apps/files_trashbin/lib/Trash/ITrashItem.php +++ b/apps/files_trashbin/lib/Trash/ITrashItem.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/ITrashManager.php b/apps/files_trashbin/lib/Trash/ITrashManager.php index 4a2eaead11b..743ea01358a 100644 --- a/apps/files_trashbin/lib/Trash/ITrashManager.php +++ b/apps/files_trashbin/lib/Trash/ITrashManager.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php index 0fd370a6cf1..204defde35c 100644 --- a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php +++ b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/TrashItem.php b/apps/files_trashbin/lib/Trash/TrashItem.php index 31dbb10def2..2ae999a2069 100644 --- a/apps/files_trashbin/lib/Trash/TrashItem.php +++ b/apps/files_trashbin/lib/Trash/TrashItem.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/apps/files_trashbin/lib/Trash/TrashManager.php b/apps/files_trashbin/lib/Trash/TrashManager.php index bf3eaebdc2a..521a576c00a 100644 --- a/apps/files_trashbin/lib/Trash/TrashManager.php +++ b/apps/files_trashbin/lib/Trash/TrashManager.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -60,8 +61,8 @@ class TrashManager implements ITrashManager { $fullType = get_class($storage); $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) { if ( - $storage->instanceOfStorage($registeredType) && - ($type === '' || is_subclass_of($registeredType, $type)) + $storage->instanceOfStorage($registeredType) + && ($type === '' || is_subclass_of($registeredType, $type)) ) { return $registeredType; } else { diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index 32fd81465fa..667066c2fca 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -24,12 +25,14 @@ 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; @@ -39,6 +42,8 @@ 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; @@ -72,7 +77,7 @@ class Trashbin implements IEventListener { */ 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 @@ -103,7 +108,7 @@ class Trashbin implements IEventListener { * @return array<string, array<string, array{location: string, deletedBy: string}>> */ public static function getExtraData($user) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->select('id', 'timestamp', 'location', 'deleted_by') ->from('files_trash') ->where($query->expr()->eq('user', $query->createNamedParameter($user))); @@ -128,7 +133,7 @@ class Trashbin implements IEventListener { * @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))) @@ -194,7 +199,7 @@ class Trashbin implements IEventListener { 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)) @@ -203,7 +208,7 @@ class Trashbin implements IEventListener { ->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']); } } } @@ -253,10 +258,10 @@ class Trashbin implements IEventListener { $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); @@ -302,7 +307,7 @@ class Trashbin implements IEventListener { 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 @@ -322,7 +327,7 @@ class Trashbin implements IEventListener { } 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)) @@ -331,7 +336,7 @@ class Trashbin implements IEventListener { ->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']); } Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', ['filePath' => Filesystem::normalizePath($file_path), 'trashPath' => Filesystem::normalizePath(static::getTrashFilename($filename, $timestamp))]); @@ -357,7 +362,7 @@ class Trashbin implements IEventListener { } 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 Util::numericToNumber($userTrashbinSize); @@ -460,12 +465,12 @@ class Trashbin implements IEventListener { 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 = ''; } @@ -494,7 +499,7 @@ class Trashbin implements IEventListener { $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) { @@ -514,13 +519,13 @@ class Trashbin implements IEventListener { $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))) @@ -612,7 +617,7 @@ class Trashbin implements IEventListener { // 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(); @@ -664,7 +669,7 @@ class Trashbin implements IEventListener { $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))) @@ -749,7 +754,7 @@ class Trashbin implements IEventListener { * @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(); @@ -768,7 +773,7 @@ class Trashbin implements IEventListener { return $configuredTrashbinSize - $trashbinSize; } - $userObject = \OC::$server->getUserManager()->get($user); + $userObject = Server::get(IUserManager::class)->get($user); if (is_null($userObject)) { return 0; } @@ -850,10 +855,10 @@ class Trashbin implements IEventListener { 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)); } } @@ -868,7 +873,7 @@ class Trashbin implements IEventListener { */ 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; @@ -876,7 +881,13 @@ class Trashbin implements IEventListener { 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 { @@ -896,7 +907,7 @@ class Trashbin implements IEventListener { */ 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) { @@ -907,16 +918,20 @@ class Trashbin implements IEventListener { $size += self::delete($filename, $user, $timestamp); $count++; } catch (NotPermittedException $e) { - \OC::$server->get(LoggerInterface::class)->warning('Removing "' . $filename . '" from trashbin failed.', + 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; @@ -977,10 +992,10 @@ class Trashbin implements IEventListener { /** @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((string)$timestamp); + $escapedTimestamp = Server::get(IDBConnection::class)->escapeLikeParameter((string)$timestamp); $pattern .= '.v%.d' . $escapedTimestamp; $offset = -strlen($escapedTimestamp) - 2; } else { @@ -1010,7 +1025,7 @@ class Trashbin implements IEventListener { /** @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) { @@ -1067,7 +1082,7 @@ class Trashbin implements IEventListener { * @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; } @@ -1126,7 +1141,7 @@ class Trashbin implements IEventListener { * @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]); } /** @@ -1150,7 +1165,7 @@ class Trashbin implements IEventListener { 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); @@ -1162,7 +1177,7 @@ class Trashbin implements IEventListener { } } - $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'); |