diff options
Diffstat (limited to 'apps/files_trashbin/lib')
47 files changed, 1484 insertions, 1540 deletions
diff --git a/apps/files_trashbin/lib/AppInfo/Application.php b/apps/files_trashbin/lib/AppInfo/Application.php index 41466a865ac..76d566f4286 100644 --- a/apps/files_trashbin/lib/AppInfo/Application.php +++ b/apps/files_trashbin/lib/AppInfo/Application.php @@ -1,43 +1,37 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Victor Dubiniuk <dubiniuk@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: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin\AppInfo; use OCA\DAV\Connector\Sabre\Principal; +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Files_Trashbin\Capabilities; +use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent; use OCA\Files_Trashbin\Expiration; +use OCA\Files_Trashbin\Listener\EventListener; +use OCA\Files_Trashbin\Listeners\BeforeTemplateRendered; +use OCA\Files_Trashbin\Listeners\LoadAdditionalScripts; +use OCA\Files_Trashbin\Listeners\SyncLivePhotosListener; use OCA\Files_Trashbin\Trash\ITrashManager; use OCA\Files_Trashbin\Trash\TrashManager; +use OCA\Files_Trashbin\Trashbin; use OCA\Files_Trashbin\UserMigration\TrashbinMigrator; +use OCP\App\IAppManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; -use OCP\App\IAppManager; -use OCP\ILogger; -use OCP\IServerContainer; +use OCP\Files\Events\BeforeFileSystemSetupEvent; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\User\Events\BeforeUserDeletedEvent; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; class Application extends App implements IBootstrap { public const APP_ID = 'files_trashbin'; @@ -55,35 +49,33 @@ class Application extends App implements IBootstrap { $context->registerServiceAlias('principalBackend', Principal::class); $context->registerUserMigrator(TrashbinMigrator::class); - } - public function boot(IBootContext $context): void { - $context->injectFn([$this, 'registerTrashBackends']); + $context->registerEventListener( + LoadAdditionalScriptsEvent::class, + LoadAdditionalScripts::class + ); + + $context->registerEventListener( + BeforeTemplateRenderedEvent::class, + BeforeTemplateRendered::class + ); + + $context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class); + + $context->registerEventListener(NodeWrittenEvent::class, EventListener::class); + $context->registerEventListener(BeforeUserDeletedEvent::class, EventListener::class); + $context->registerEventListener(BeforeFileSystemSetupEvent::class, EventListener::class); - // create storage wrapper on setup - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); - //Listen to delete user signal - \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); - //Listen to post write hook - \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); // pre and post-rename, disable trash logic for the copy+unlink case - \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); + $context->registerEventListener(BeforeNodeDeletedEvent::class, Trashbin::class); + } - \OCA\Files\App::getNavigationManager()->add(function () { - $l = \OC::$server->getL10N(self::APP_ID); - return [ - 'id' => 'trashbin', - 'appname' => self::APP_ID, - 'script' => 'list.php', - 'order' => 50, - 'name' => $l->t('Deleted files'), - 'classes' => 'pinned', - ]; - }); + public function boot(IBootContext $context): void { + $context->injectFn([$this, 'registerTrashBackends']); } - public function registerTrashBackends(IServerContainer $serverContainer, ILogger $logger, IAppManager $appManager, ITrashManager $trashManager) { - foreach ($appManager->getInstalledApps() as $app) { + public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void { + foreach ($appManager->getEnabledApps() as $app) { $appInfo = $appManager->getAppInfo($app); if (isset($appInfo['trash'])) { $backends = $appInfo['trash']; @@ -92,10 +84,10 @@ class Application extends App implements IBootstrap { $for = $backend['@attributes']['for']; try { - $backendObject = $serverContainer->query($class); + $backendObject = $serverContainer->get($class); $trashManager->registerBackend($for, $backendObject); } catch (\Exception $e) { - $logger->logException($e); + $logger->error($e->getMessage(), ['exception' => $e]); } } } diff --git a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php index c76033e0c79..bb383dab78d 100644 --- a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php +++ b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php @@ -1,67 +1,37 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Victor Dubiniuk <dubiniuk@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_Trashbin\BackgroundJob; -use OCA\Files_Trashbin\AppInfo\Application; +use OC\Files\View; use OCA\Files_Trashbin\Expiration; 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 { - private IConfig $config; - private Expiration $expiration; - private IUserManager $userManager; - public function __construct( - IConfig $config, - IUserManager $userManager, - Expiration $expiration, - ITimeFactory $time + private IAppConfig $appConfig, + private IUserManager $userManager, + private Expiration $expiration, + private LoggerInterface $logger, + ITimeFactory $time, ) { parent::__construct($time); // Run once per 30 minutes $this->setInterval(60 * 30); - - $this->config = $config; - $this->userManager = $userManager; - $this->expiration = $expiration; } - /** - * @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; } @@ -71,15 +41,32 @@ class ExpireTrash extends TimedJob { return; } - $this->userManager->callForSeenUsers(function (IUser $user) { - $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(); } @@ -91,7 +78,7 @@ class ExpireTrash extends TimedJob { \OC_Util::setupFS($user); // Check if this user has a trashbin directory - $view = new \OC\Files\View('/' . $user); + $view = new View('/' . $user); if (!$view->is_dir('/files_trashbin/files')) { return false; } diff --git a/apps/files_trashbin/lib/Capabilities.php b/apps/files_trashbin/lib/Capabilities.php index 28acf0c8d27..53c17a475ff 100644 --- a/apps/files_trashbin/lib/Capabilities.php +++ b/apps/files_trashbin/lib/Capabilities.php @@ -1,27 +1,13 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Lukas Reschke <lukas@statuscode.ch> - * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin; +use OCA\Files_Trashbin\Service\ConfigService; use OCP\Capabilities\ICapability; /** @@ -34,12 +20,18 @@ class Capabilities implements ICapability { /** * Return this classes capabilities * - * @return array + * @return array{ + * files: array{ + * undelete: bool, + * delete_from_trash: bool + * } + * } */ public function getCapabilities() { return [ 'files' => [ - 'undelete' => true + 'undelete' => true, + 'delete_from_trash' => ConfigService::getDeleteFromTrashEnabled(), ] ]; } diff --git a/apps/files_trashbin/lib/Command/CleanUp.php b/apps/files_trashbin/lib/Command/CleanUp.php index e3ed527e535..e9b4fa8ae60 100644 --- a/apps/files_trashbin/lib/Command/CleanUp.php +++ b/apps/files_trashbin/lib/Command/CleanUp.php @@ -1,27 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Liam Dennehy <liam@wiemax.net> - * @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: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin\Command; @@ -29,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; @@ -38,25 +21,12 @@ use Symfony\Component\Console\Output\OutputInterface; class CleanUp extends Command { - /** @var IUserManager */ - protected $userManager; - - /** @var IRootFolder */ - protected $rootFolder; - - /** @var \OCP\IDBConnection */ - protected $dbConnection; - - /** - * @param IRootFolder $rootFolder - * @param IUserManager $userManager - * @param IDBConnection $dbConnection - */ - public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) { + public function __construct( + protected IRootFolder $rootFolder, + protected IUserManager $userManager, + protected IDBConnection $dbConnection, + ) { parent::__construct(); - $this->userManager = $userManager; - $this->rootFolder = $rootFolder; - $this->dbConnection = $dbConnection; } protected function configure() { @@ -78,13 +48,14 @@ class CleanUp extends Command { protected function execute(InputInterface $input, OutputInterface $output): int { $users = $input->getArgument('user_id'); + $verbose = $input->getOption('verbose'); if ((!empty($users)) and ($input->getOption('all-users'))) { throw new InvalidOptionException('Either specify a user_id or --all-users'); } elseif (!empty($users)) { foreach ($users as $user) { if ($this->userManager->userExists($user)) { $output->writeln("Remove deleted files of <info>$user</info>"); - $this->removeDeletedFiles($user); + $this->removeDeletedFiles($user, $output, $verbose); } else { $output->writeln("<error>Unknown user $user</error>"); return 1; @@ -104,7 +75,7 @@ class CleanUp extends Command { $users = $backend->getUsers('', $limit, $offset); foreach ($users as $user) { $output->writeln(" <info>$user</info>"); - $this->removeDeletedFiles($user); + $this->removeDeletedFiles($user, $output, $verbose); } $offset += $limit; } while (count($users) >= $limit); @@ -117,19 +88,31 @@ class CleanUp extends Command { /** * remove deleted files for the given user - * - * @param string $uid */ - protected function removeDeletedFiles($uid) { + protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void { \OC_Util::tearDownFS(); \OC_Util::setupFS($uid); - if ($this->rootFolder->nodeExists('/' . $uid . '/files_trashbin')) { - $this->rootFolder->get('/' . $uid . '/files_trashbin')->delete(); + $path = '/' . $uid . '/files_trashbin'; + if ($this->rootFolder->nodeExists($path)) { + $node = $this->rootFolder->get($path); + + if ($verbose) { + $output->writeln('Deleting <info>' . Util::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>."); + } + $node->delete(); + if ($this->rootFolder->nodeExists($path)) { + $output->writeln('<error>Trash folder sill exists after attempting to delete it</error>'); + return; + } $query = $this->dbConnection->getQueryBuilder(); $query->delete('files_trash') ->where($query->expr()->eq('user', $query->createParameter('uid'))) ->setParameter('uid', $uid); - $query->execute(); + $query->executeStatement(); + } else { + if ($verbose) { + $output->writeln("No trash found for <info>$uid</info>"); + } } } } diff --git a/apps/files_trashbin/lib/Command/Expire.php b/apps/files_trashbin/lib/Command/Expire.php index 568506737eb..73a42cd4749 100644 --- a/apps/files_trashbin/lib/Command/Expire.php +++ b/apps/files_trashbin/lib/Command/Expire.php @@ -1,52 +1,31 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ 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; /** - * @var string - */ - private $user; - - /** * @param string $user */ - public function __construct($user) { - $this->user = $user; + public function __construct( + private $user, + ) { } 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 bcecbfceeff..422d8379984 100644 --- a/apps/files_trashbin/lib/Command/ExpireTrash.php +++ b/apps/files_trashbin/lib/Command/ExpireTrash.php @@ -1,35 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud GmbH. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud GmbH. + * SPDX-License-Identifier: AGPL-3.0-only */ 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; @@ -39,25 +22,15 @@ use Symfony\Component\Console\Output\OutputInterface; class ExpireTrash extends Command { /** - * @var Expiration - */ - private $expiration; - - /** - * @var IUserManager - */ - private $userManager; - - /** * @param IUserManager|null $userManager * @param Expiration|null $expiration */ - public function __construct(IUserManager $userManager = null, - Expiration $expiration = null) { + public function __construct( + private LoggerInterface $logger, + private ?IUserManager $userManager = null, + private ?Expiration $expiration = null, + ) { parent::__construct(); - - $this->userManager = $userManager; - $this->expiration = $expiration; } protected function configure() { @@ -72,9 +45,10 @@ class ExpireTrash extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { + $minAge = $this->expiration->getMinAgeAsTimestamp(); $maxAge = $this->expiration->getMaxAgeAsTimestamp(); - if (!$maxAge) { - $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)"); + 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; } @@ -93,10 +67,12 @@ class ExpireTrash extends Command { } else { $p = new ProgressBar($output); $p->start(); - $this->userManager->callForSeenUsers(function (IUser $user) use ($p) { + + $users = $this->userManager->getSeenUsers(); + foreach ($users as $user) { $p->advance(); $this->expireTrashForUser($user); - }); + } $p->finish(); $output->writeln(''); } @@ -104,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); } /** @@ -122,7 +101,7 @@ class ExpireTrash extends Command { \OC_Util::setupFS($user); // Check if this user has a trashbin directory - $view = new \OC\Files\View('/' . $user); + $view = new View('/' . $user); if (!$view->is_dir('/files_trashbin/files')) { return false; } diff --git a/apps/files_trashbin/lib/Command/RestoreAllFiles.php b/apps/files_trashbin/lib/Command/RestoreAllFiles.php index 748ead798d8..ce31f759c0e 100644 --- a/apps/files_trashbin/lib/Command/RestoreAllFiles.php +++ b/apps/files_trashbin/lib/Command/RestoreAllFiles.php @@ -1,30 +1,18 @@ <?php + /** - * @copyright Copyright (c) 2021, Caitlin Hogan (cahogan16@gmail.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: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin\Command; use OC\Core\Command\Base; +use OCA\Files_Trashbin\Trash\ITrashManager; +use OCA\Files_Trashbin\Trash\TrashItem; use OCP\Files\IRootFolder; use OCP\IDBConnection; use OCP\IL10N; use OCP\IUserBackend; -use OCA\Files_Trashbin\Trashbin; -use OCA\Files_Trashbin\Helper; use OCP\IUserManager; use OCP\L10N\IFactory; use Symfony\Component\Console\Exception\InvalidOptionException; @@ -35,14 +23,15 @@ use Symfony\Component\Console\Output\OutputInterface; class RestoreAllFiles extends Base { - /** @var IUserManager */ - protected $userManager; - - /** @var IRootFolder */ - protected $rootFolder; + private const SCOPE_ALL = 0; + private const SCOPE_USER = 1; + private const SCOPE_GROUPFOLDERS = 2; - /** @var \OCP\IDBConnection */ - protected $dbConnection; + private static array $SCOPE_MAP = [ + 'user' => self::SCOPE_USER, + 'groupfolders' => self::SCOPE_GROUPFOLDERS, + 'all' => self::SCOPE_ALL + ]; /** @var IL10N */ protected $l10n; @@ -51,12 +40,17 @@ class RestoreAllFiles extends Base { * @param IRootFolder $rootFolder * @param IUserManager $userManager * @param IDBConnection $dbConnection + * @param ITrashManager $trashManager + * @param IFactory $l10nFactory */ - public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, IFactory $l10nFactory) { + public function __construct( + protected IRootFolder $rootFolder, + protected IUserManager $userManager, + protected IDBConnection $dbConnection, + protected ITrashManager $trashManager, + IFactory $l10nFactory, + ) { parent::__construct(); - $this->userManager = $userManager; - $this->rootFolder = $rootFolder; - $this->dbConnection = $dbConnection; $this->l10n = $l10nFactory->get('files_trashbin'); } @@ -64,7 +58,7 @@ class RestoreAllFiles extends Base { parent::configure(); $this ->setName('trashbin:restore') - ->setDescription('Restore all deleted files') + ->setDescription('Restore all deleted files according to the given filters') ->addArgument( 'user_id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, @@ -75,23 +69,47 @@ class RestoreAllFiles extends Base { null, InputOption::VALUE_NONE, 'run action on all users' + ) + ->addOption( + 'scope', + 's', + InputOption::VALUE_OPTIONAL, + 'Restore files from the given scope. Possible values are "user", "groupfolders" or "all"', + 'user' + ) + ->addOption( + 'since', + null, + InputOption::VALUE_OPTIONAL, + 'Only restore files deleted after the given date and time, see https://www.php.net/manual/en/function.strtotime.php for more information on supported formats' + ) + ->addOption( + 'until', + null, + InputOption::VALUE_OPTIONAL, + 'Only restore files deleted before the given date and time, see https://www.php.net/manual/en/function.strtotime.php for more information on supported formats' + ) + ->addOption( + 'dry-run', + 'd', + InputOption::VALUE_NONE, + 'Only show which files would be restored but do not perform any action' ); } protected function execute(InputInterface $input, OutputInterface $output): int { /** @var string[] $users */ $users = $input->getArgument('user_id'); - if ((!empty($users)) and ($input->getOption('all-users'))) { + if ((!empty($users)) && ($input->getOption('all-users'))) { throw new InvalidOptionException('Either specify a user_id or --all-users'); - } elseif (!empty($users)) { + } + + [$scope, $since, $until, $dryRun] = $this->parseArgs($input); + + if (!empty($users)) { foreach ($users as $user) { - if ($this->userManager->userExists($user)) { - $output->writeln("Restoring deleted files for user <info>$user</info>"); - $this->restoreDeletedFiles($user, $output); - } else { - $output->writeln("<error>Unknown user $user</error>"); - return 1; - } + $output->writeln("Restoring deleted files for user <info>$user</info>"); + $this->restoreDeletedFiles($user, $scope, $since, $until, $dryRun, $output); } } elseif ($input->getOption('all-users')) { $output->writeln('Restoring deleted files for all users'); @@ -107,7 +125,7 @@ class RestoreAllFiles extends Base { $users = $backend->getUsers('', $limit, $offset); foreach ($users as $user) { $output->writeln("<info>$user</info>"); - $this->restoreDeletedFiles($user, $output); + $this->restoreDeletedFiles($user, $scope, $since, $until, $dryRun, $output); } $offset += $limit; } while (count($users) >= $limit); @@ -119,47 +137,137 @@ class RestoreAllFiles extends Base { } /** - * Restore deleted files for the given user - * - * @param string $uid - * @param OutputInterface $output + * Restore deleted files for the given user according to the given filters */ - protected function restoreDeletedFiles(string $uid, OutputInterface $output): void { + protected function restoreDeletedFiles(string $uid, int $scope, ?int $since, ?int $until, bool $dryRun, OutputInterface $output): void { \OC_Util::tearDownFS(); \OC_Util::setupFS($uid); \OC_User::setUserId($uid); - // Sort by most recently deleted first - // (Restoring in order of most recently deleted preserves nested file paths. - // See https://github.com/nextcloud/server/issues/31200#issuecomment-1130358549) - $filesInTrash = Helper::getTrashFiles('/', $uid, 'mtime', true); + $user = $this->userManager->get($uid); + + if ($user === null) { + $output->writeln("<error>Unknown user $uid</error>"); + return; + } - $trashCount = count($filesInTrash); + $userTrashItems = $this->filterTrashItems( + $this->trashManager->listTrashRoot($user), + $scope, + $since, + $until, + $output); + + $trashCount = count($userTrashItems); if ($trashCount == 0) { - $output->writeln("User has no deleted files in the trashbin"); + $output->writeln('User has no deleted files in the trashbin matching the given filters'); return; } - $output->writeln("Preparing to restore <info>$trashCount</info> files..."); + $prepMsg = $dryRun ? 'Would restore' : 'Preparing to restore'; + $output->writeln("$prepMsg <info>$trashCount</info> files..."); $count = 0; - foreach ($filesInTrash as $trashFile) { - $filename = $trashFile->getName(); - $timestamp = $trashFile->getMtime(); - $humanTime = $this->l10n->l('datetime', $timestamp); - $output->write("File <info>$filename</info> originally deleted at <info>$humanTime</info> "); - $file = $filename . '.d' . $timestamp; - $location = Trashbin::getLocation($uid, $filename, (string) $timestamp); - if ($location === '.') { - $location = ''; + foreach ($userTrashItems as $trashItem) { + $filename = $trashItem->getName(); + $humanTime = $this->l10n->l('datetime', $trashItem->getDeletedTime()); + // We use getTitle() here instead of getOriginalLocation() because + // for groupfolders this contains the groupfolder name itself as prefix + // which makes it more human readable + $location = $trashItem->getTitle(); + + if ($dryRun) { + $output->writeln("Would restore <info>$filename</info> originally deleted at <info>$humanTime</info> to <info>/$location</info>"); + continue; } - $output->write("restoring to <info>/$location</info>:"); - if (Trashbin::restore($file, $filename, $timestamp)) { - $count = $count + 1; - $output->writeln(" <info>success</info>"); - } else { - $output->writeln(" <error>failed</error>"); + + $output->write("File <info>$filename</info> originally deleted at <info>$humanTime</info> restoring to <info>/$location</info>:"); + + try { + $trashItem->getTrashBackend()->restoreItem($trashItem); + } catch (\Throwable $e) { + $output->writeln(' <error>Failed: ' . $e->getMessage() . '</error>'); + $output->writeln(' <error>' . $e->getTraceAsString() . '</error>', OutputInterface::VERBOSITY_VERY_VERBOSE); + continue; } + + $count++; + $output->writeln(' <info>success</info>'); } - $output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files."); + if (!$dryRun) { + $output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files."); + } + } + + protected function parseArgs(InputInterface $input): array { + $since = $this->parseTimestamp($input->getOption('since')); + $until = $this->parseTimestamp($input->getOption('until')); + + if ($since !== null && $until !== null && $since > $until) { + throw new InvalidOptionException('since must be before until'); + } + + return [ + $this->parseScope($input->getOption('scope')), + $since, + $until, + $input->getOption('dry-run') + ]; + } + + protected function parseScope(string $scope): int { + if (isset(self::$SCOPE_MAP[$scope])) { + return self::$SCOPE_MAP[$scope]; + } + + throw new InvalidOptionException("Invalid scope '$scope'"); + } + + protected function parseTimestamp(?string $timestamp): ?int { + if ($timestamp === null) { + return null; + } + $timestamp = strtotime($timestamp); + if ($timestamp === false) { + throw new InvalidOptionException("Invalid timestamp '$timestamp'"); + } + return $timestamp; + } + + protected function filterTrashItems(array $trashItems, int $scope, ?int $since, ?int $until, OutputInterface $output): array { + $filteredTrashItems = []; + foreach ($trashItems as $trashItem) { + $trashItemClass = get_class($trashItem); + + // Check scope with exact class name for locally deleted files + if ($scope === self::SCOPE_USER && $trashItemClass !== TrashItem::class) { + $output->writeln('Skipping <info>' . $trashItem->getName() . '</info> because it is not a user trash item', OutputInterface::VERBOSITY_VERBOSE); + continue; + } + + /** + * Check scope for groupfolders by string because the groupfolders app might not be installed. + * That's why PSALM doesn't know the class GroupTrashItem. + * @psalm-suppress RedundantCondition + */ + if ($scope === self::SCOPE_GROUPFOLDERS && $trashItemClass !== 'OCA\GroupFolders\Trash\GroupTrashItem') { + $output->writeln('Skipping <info>' . $trashItem->getName() . '</info> because it is not a groupfolders trash item', OutputInterface::VERBOSITY_VERBOSE); + continue; + } + + // Check left timestamp boundary + if ($since !== null && $trashItem->getDeletedTime() <= $since) { + $output->writeln('Skipping <info>' . $trashItem->getName() . "</info> because it was deleted before the 'since' timestamp", OutputInterface::VERBOSITY_VERBOSE); + continue; + } + + // Check right timestamp boundary + if ($until !== null && $trashItem->getDeletedTime() >= $until) { + $output->writeln('Skipping <info>' . $trashItem->getName() . "</info> because it was deleted after the 'until' timestamp", OutputInterface::VERBOSITY_VERBOSE); + continue; + } + + $filteredTrashItems[] = $trashItem; + } + return $filteredTrashItems; } } diff --git a/apps/files_trashbin/lib/Command/Size.php b/apps/files_trashbin/lib/Command/Size.php index 30ac474199e..9c19d4d92b3 100644 --- a/apps/files_trashbin/lib/Command/Size.php +++ b/apps/files_trashbin/lib/Command/Size.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Command; @@ -30,26 +13,19 @@ 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; use Symfony\Component\Console\Output\OutputInterface; class Size extends Base { - private $config; - private $userManager; - private $commandBus; - public function __construct( - IConfig $config, - IUserManager $userManager, - IBus $commandBus + private IConfig $config, + private IUserManager $userManager, + private IBus $commandBus, ) { parent::__construct(); - - $this->config = $config; - $this->userManager = $userManager; - $this->commandBus = $commandBus; } protected function configure() { @@ -70,9 +46,9 @@ 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>"); + $output->writeln('<error>Failed to parse input size</error>'); return -1; } if ($user) { @@ -80,8 +56,8 @@ class Size extends Base { $this->commandBus->push(new Expire($user)); } else { $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize); - $output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>"); - $output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>"); + $output->writeln('<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>'); + $output->writeln('<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>'); } } else { $this->printTrashbinSize($input, $output, $user); @@ -93,9 +69,9 @@ class Size extends Base { private function printTrashbinSize(InputInterface $input, OutputInterface $output, ?string $user) { $globalSize = (int)$this->config->getAppValue('files_trashbin', 'trashbin_size', '-1'); if ($globalSize < 0) { - $globalHumanSize = "default (50% of available space)"; + $globalHumanSize = 'default (50% of available space)'; } else { - $globalHumanSize = \OC_Helper::humanFileSize($globalSize); + $globalHumanSize = Util::humanFileSize($globalSize); } if ($user) { @@ -104,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) { @@ -120,21 +96,21 @@ class Size extends Base { } } else { $users = []; - $this->userManager->callForSeenUsers(function (IUser $user) use (&$users) { + $this->userManager->callForSeenUsers(function (IUser $user) use (&$users): void { $users[] = $user->getUID(); }); $userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users); if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { $output->writeln("Default size: $globalHumanSize"); - $output->writeln(""); + $output->writeln(''); if (count($userValues)) { - $output->writeln("Per-user sizes:"); + $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"); + $output->writeln('No per-user sizes configured'); } } else { $globalValue = ($globalSize < 0) ? 'default' : $globalSize; diff --git a/apps/files_trashbin/lib/Controller/PreviewController.php b/apps/files_trashbin/lib/Controller/PreviewController.php index 22ca56d8f36..a4e911d88ef 100644 --- a/apps/files_trashbin/lib/Controller/PreviewController.php +++ b/apps/files_trashbin/lib/Controller/PreviewController.php @@ -3,36 +3,19 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Jakub Onderka <ahoj@jakubonderka.cz> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author simonspa <1677436+simonspa@users.noreply.github.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Controller; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; +use OCP\AppFramework\Http\Attribute\OpenAPI; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\Folder; use OCP\Files\IMimeTypeDetector; @@ -42,55 +25,42 @@ use OCP\IPreview; use OCP\IRequest; use OCP\IUserSession; +#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] class PreviewController extends Controller { - /** @var IRootFolder */ - private $rootFolder; - - /** @var ITrashManager */ - private $trashManager; - - /** @var IUserSession */ - private $userSession; - - /** @var IMimeTypeDetector */ - private $mimeTypeDetector; - - /** @var IPreview */ - private $previewManager; - - /** @var ITimeFactory */ - private $time; - public function __construct( string $appName, IRequest $request, - IRootFolder $rootFolder, - ITrashManager $trashManager, - IUserSession $userSession, - IMimeTypeDetector $mimeTypeDetector, - IPreview $previewManager, - ITimeFactory $time + private IRootFolder $rootFolder, + private ITrashManager $trashManager, + private IUserSession $userSession, + private IMimeTypeDetector $mimeTypeDetector, + private IPreview $previewManager, + private ITimeFactory $time, ) { parent::__construct($appName, $request); - - $this->trashManager = $trashManager; - $this->rootFolder = $rootFolder; - $this->userSession = $userSession; - $this->mimeTypeDetector = $mimeTypeDetector; - $this->previewManager = $previewManager; - $this->time = $time; } /** - * @NoAdminRequired - * @NoCSRFRequired + * Get the preview for a file + * + * @param int $fileId ID of the file + * @param int $x Width of the preview + * @param int $y Height of the preview + * @param bool $a Whether to not crop the preview + * + * @return Http\FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, list<empty>, array{}> * - * @return DataResponse|Http\FileDisplayResponse + * 200: Preview returned + * 400: Getting preview is not possible + * 404: Preview not found */ + #[NoAdminRequired] + #[NoCSRFRequired] public function getPreview( int $fileId = -1, - int $x = 128, - int $y = 128 + int $x = 32, + int $y = 32, + bool $a = false, ) { if ($fileId === -1 || $x === 0 || $y === 0) { return new DataResponse([], Http::STATUS_BAD_REQUEST); @@ -118,8 +88,8 @@ class PreviewController extends Controller { $mimeType = $this->mimeTypeDetector->detectPath($file->getName()); } - $f = $this->previewManager->getPreview($file, $x, $y, true, IPreview::MODE_FILL, $mimeType); - $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + $f = $this->previewManager->getPreview($file, $x, $y, !$a, IPreview::MODE_FILL, $mimeType); + $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); // Cache previews for 24H $response->cacheFor(3600 * 24); diff --git a/apps/files_trashbin/lib/Events/BeforeNodeRestoredEvent.php b/apps/files_trashbin/lib/Events/BeforeNodeRestoredEvent.php new file mode 100644 index 00000000000..0bc6b37c35b --- /dev/null +++ b/apps/files_trashbin/lib/Events/BeforeNodeRestoredEvent.php @@ -0,0 +1,39 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files_Trashbin\Events; + +use Exception; +use OCP\Files\Events\Node\AbstractNodesEvent; +use OCP\Files\Node; + +/** + * @since 28.0.0 + */ +class BeforeNodeRestoredEvent extends AbstractNodesEvent { + public function __construct( + Node $source, + Node $target, + private bool &$run, + ) { + parent::__construct($source, $target); + } + + /** + * @return never + */ + public function abortOperation(?\Throwable $ex = null) { + $this->stopPropagation(); + $this->run = false; + if ($ex !== null) { + throw $ex; + } else { + throw new Exception('Operation aborted'); + } + } +} diff --git a/apps/files_trashbin/lib/Events/MoveToTrashEvent.php b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php index 3dce70f96bd..0d776b606b1 100644 --- a/apps/files_trashbin/lib/Events/MoveToTrashEvent.php +++ b/apps/files_trashbin/lib/Events/MoveToTrashEvent.php @@ -1,26 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Events; @@ -33,18 +15,17 @@ use OCP\Files\Node; * Event to allow other apps to disable the trash bin for specific files * * @package OCA\Files_Trashbin\Events + * @since 28.0.0 Dispatched as a typed event */ class MoveToTrashEvent extends Event { /** @var bool */ private $moveToTrashBin; - /** @var Node */ - private $node; - - public function __construct(Node $node) { + public function __construct( + private Node $node, + ) { $this->moveToTrashBin = true; - $this->node = $node; } /** diff --git a/apps/files_trashbin/lib/Events/NodeRestoredEvent.php b/apps/files_trashbin/lib/Events/NodeRestoredEvent.php new file mode 100644 index 00000000000..4278d6cfe95 --- /dev/null +++ b/apps/files_trashbin/lib/Events/NodeRestoredEvent.php @@ -0,0 +1,21 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files_Trashbin\Events; + +use OCP\Files\Events\Node\AbstractNodesEvent; +use OCP\Files\Node; + +/** + * @since 28.0.0 + */ +class NodeRestoredEvent extends AbstractNodesEvent { + public function __construct(Node $source, Node $target) { + parent::__construct($source, $target); + } +} diff --git a/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php b/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php index ea715dff302..3ea1293e5d7 100644 --- a/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php +++ b/apps/files_trashbin/lib/Exceptions/CopyRecursiveException.php @@ -1,23 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * - * @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 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin\Exceptions; diff --git a/apps/files_trashbin/lib/Expiration.php b/apps/files_trashbin/lib/Expiration.php index 5705fc23b8a..0bbe39a9314 100644 --- a/apps/files_trashbin/lib/Expiration.php +++ b/apps/files_trashbin/lib/Expiration.php @@ -1,28 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Victor Dubiniuk <dubiniuk@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: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_Trashbin; @@ -35,9 +16,6 @@ class Expiration { public const DEFAULT_RETENTION_OBLIGATION = 30; public const NO_OBLIGATION = -1; - /** @var ITimeFactory */ - private $timeFactory; - /** @var string */ private $retentionObligation; @@ -50,8 +28,10 @@ class Expiration { /** @var bool */ private $canPurgeToSaveSpace; - public function __construct(IConfig $config,ITimeFactory $timeFactory) { - $this->timeFactory = $timeFactory; + public function __construct( + IConfig $config, + private ITimeFactory $timeFactory, + ) { $this->setRetentionObligation($config->getSystemValue('trashbin_retention_obligation', 'auto')); } @@ -115,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 61d8eb9c715..746832e9280 100644 --- a/apps/files_trashbin/lib/Helper.php +++ b/apps/files_trashbin/lib/Helper.php @@ -1,45 +1,25 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @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; 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 { /** * Retrieves the contents of a trash bin directory. * * @param string $dir path to the directory inside the trashbin - * or empty to retrieve the root of the trashbin + * or empty to retrieve the root of the trashbin * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise @@ -49,7 +29,7 @@ class Helper { $result = []; $timestamp = null; - $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); + $view = new View('/' . $user . '/files_trashbin/files'); if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { throw new \Exception('Directory does not exists'); @@ -60,7 +40,7 @@ class Helper { $absoluteDir = $view->getAbsolutePath($dir); $internalPath = $mount->getInternalPath($absoluteDir); - $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); + $extraData = Trashbin::getExtraData($user); $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir))); foreach ($dirContent as $entry) { $entryName = $entry->getName(); @@ -76,8 +56,8 @@ class Helper { } $originalPath = ''; $originalName = substr($entryName, 0, -strlen($timestamp) - 2); - if (isset($originalLocations[$originalName][$timestamp])) { - $originalPath = $originalLocations[$originalName][$timestamp]; + if (isset($extraData[$originalName][$timestamp]['location'])) { + $originalPath = $extraData[$originalName][$timestamp]['location']; if (substr($originalPath, -1) === '/') { $originalPath = substr($originalPath, 0, -1); } @@ -86,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(), @@ -101,6 +81,7 @@ class Helper { $i['extraData'] = $originalName; } } + $i['deletedBy'] = $extraData[$originalName][$timestamp]['deletedBy'] ?? null; $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); } @@ -121,7 +102,7 @@ class Helper { $entry = \OCA\Files\Helper::formatFileInfo($i); $entry['id'] = $i->getId(); $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image - $entry['permissions'] = \OCP\Constants::PERMISSION_READ; + $entry['permissions'] = Constants::PERMISSION_READ; $files[] = $entry; } return $files; diff --git a/apps/files_trashbin/lib/Hooks.php b/apps/files_trashbin/lib/Hooks.php deleted file mode 100644 index 4f4cf8bb705..00000000000 --- a/apps/files_trashbin/lib/Hooks.php +++ /dev/null @@ -1,48 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @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/> - * - */ -namespace OCA\Files_Trashbin; - -class Hooks { - - /** - * clean up user specific settings if user gets deleted - * @param array $params array with uid - * - * This function is connected to the pre_deleteUser signal of OC_Users - * to remove the used space for the trash bin stored in the database - */ - public static function deleteUser_hook($params) { - $uid = $params['uid']; - Trashbin::deleteUser($uid); - } - - public static function post_write_hook($params) { - $user = \OC_User::getUser(); - if (!empty($user)) { - Trashbin::resizeTrash($user); - } - } -} diff --git a/apps/files_trashbin/lib/Listener/EventListener.php b/apps/files_trashbin/lib/Listener/EventListener.php new file mode 100644 index 00000000000..63ecc9c81f7 --- /dev/null +++ b/apps/files_trashbin/lib/Listener/EventListener.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace OCA\Files_Trashbin\Listener; + +use OCA\Files_Trashbin\Storage; +use OCA\Files_Trashbin\Trashbin; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\BeforeFileSystemSetupEvent; +use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\User\Events\BeforeUserDeletedEvent; + +/** @template-implements IEventListener<NodeWrittenEvent|BeforeUserDeletedEvent|BeforeFileSystemSetupEvent> */ +class EventListener implements IEventListener { + public function __construct( + private ?string $userId = null, + ) { + } + + public function handle(Event $event): void { + if ($event instanceof NodeWrittenEvent) { + // Resize trash + if (!empty($this->userId)) { + Trashbin::resizeTrash($this->userId); + } + } + + // Clean up user specific settings if user gets deleted + if ($event instanceof BeforeUserDeletedEvent) { + Trashbin::deleteUser($event->getUser()->getUID()); + } + + if ($event instanceof BeforeFileSystemSetupEvent) { + Storage::setupStorage(); + } + } +} diff --git a/apps/files_trashbin/lib/Listeners/BeforeTemplateRendered.php b/apps/files_trashbin/lib/Listeners/BeforeTemplateRendered.php new file mode 100644 index 00000000000..d62618583f7 --- /dev/null +++ b/apps/files_trashbin/lib/Listeners/BeforeTemplateRendered.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files_Trashbin\Listeners; + +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; +use OCA\Files_Trashbin\Service\ConfigService; +use OCP\AppFramework\Services\IInitialState; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +/** @template-implements IEventListener<BeforeTemplateRenderedEvent> */ +class BeforeTemplateRendered implements IEventListener { + public function __construct( + private IInitialState $initialState, + ) { + } + + public function handle(Event $event): void { + if (!($event instanceof BeforeTemplateRenderedEvent)) { + return; + } + + ConfigService::injectInitialState($this->initialState); + } +} diff --git a/apps/files_trashbin/lib/Listeners/LoadAdditionalScripts.php b/apps/files_trashbin/lib/Listeners/LoadAdditionalScripts.php new file mode 100644 index 00000000000..7940b934ace --- /dev/null +++ b/apps/files_trashbin/lib/Listeners/LoadAdditionalScripts.php @@ -0,0 +1,35 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\Files_Trashbin\Listeners; + +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\Files_Trashbin\AppInfo\Application; +use OCA\Files_Trashbin\Service\ConfigService; +use OCP\AppFramework\Services\IInitialState; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Util; + +/** @template-implements IEventListener<LoadAdditionalScriptsEvent> */ +class LoadAdditionalScripts implements IEventListener { + public function __construct( + private IInitialState $initialState, + ) { + } + + public function handle(Event $event): void { + if (!($event instanceof LoadAdditionalScriptsEvent)) { + return; + } + + Util::addInitScript(Application::APP_ID, 'init'); + + ConfigService::injectInitialState($this->initialState); + } +} diff --git a/apps/files_trashbin/lib/Listeners/SyncLivePhotosListener.php b/apps/files_trashbin/lib/Listeners/SyncLivePhotosListener.php new file mode 100644 index 00000000000..2cb3a94aa1d --- /dev/null +++ b/apps/files_trashbin/lib/Listeners/SyncLivePhotosListener.php @@ -0,0 +1,132 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files_Trashbin\Listeners; + +use OCA\Files\Service\LivePhotosService; +use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent; +use OCA\Files_Trashbin\Trash\ITrashItem; +use OCA\Files_Trashbin\Trash\ITrashManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Folder; +use OCP\Files\Node; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IUserSession; + +/** + * @template-implements IEventListener<BeforeNodeRestoredEvent> + */ +class SyncLivePhotosListener implements IEventListener { + /** @var Array<int, bool> */ + private array $pendingRestores = []; + + public function __construct( + private ?IUserSession $userSession, + private ITrashManager $trashManager, + private LivePhotosService $livePhotosService, + ) { + } + + public function handle(Event $event): void { + if ($this->userSession === null) { + return; + } + + /** @var BeforeNodeRestoredEvent $event */ + $peerFileId = $this->livePhotosService->getLivePhotoPeerId($event->getSource()->getId()); + + if ($peerFileId === null) { + return; // Not a live photo. + } + + // Check the user's trashbin. + $user = $this->userSession->getUser(); + if ($user === null) { + return; + } + + $peerFile = $this->trashManager->getTrashNodeById($user, $peerFileId); + + if ($peerFile === null) { + return; // Peer file not found. + } + + $this->handleRestore($event, $peerFile); + } + + /** + * During restore event, we trigger another recursive restore on the peer file. + * Restore operations on the .mov file directly are currently blocked. + * The event listener being singleton, we can store the current state + * of pending restores inside the 'pendingRestores' property, + * to prevent infinite recursivity. + */ + private function handleRestore(BeforeNodeRestoredEvent $event, Node $peerFile): void { + $sourceFile = $event->getSource(); + + if ($sourceFile->getMimetype() === 'video/quicktime') { + if (isset($this->pendingRestores[$peerFile->getId()])) { + unset($this->pendingRestores[$peerFile->getId()]); + return; + } else { + $event->abortOperation(new NotPermittedException('Cannot restore the video part of a live photo')); + } + } else { + $user = $this->userSession?->getUser(); + if ($user === null) { + return; + } + + $peerTrashItem = $this->trashManager->getTrashNodeById($user, $peerFile->getId()); + // Peer file is not in the bin, no need to restore it. + if ($peerTrashItem === null) { + return; + } + + $trashRoot = $this->trashManager->listTrashRoot($user); + $trashItem = $this->getTrashItem($trashRoot, $peerFile->getInternalPath()); + + if ($trashItem === null) { + $event->abortOperation(new NotFoundException("Couldn't find peer file in trashbin")); + } + + $this->pendingRestores[$sourceFile->getId()] = true; + try { + $this->trashManager->restoreItem($trashItem); + } catch (\Throwable $ex) { + $event->abortOperation($ex); + } + } + } + + /** + * There is currently no method to restore a file based on its fileId or path. + * So we have to manually find a ITrashItem from the trash item list. + * TODO: This should be replaced by a proper method in the TrashManager. + */ + private function getTrashItem(array $trashFolder, string $path): ?ITrashItem { + foreach ($trashFolder as $trashItem) { + if (str_starts_with($path, 'files_trashbin/files' . $trashItem->getTrashPath())) { + if ($path === 'files_trashbin/files' . $trashItem->getTrashPath()) { + return $trashItem; + } + + if ($trashItem instanceof Folder) { + $node = $this->getTrashItem($trashItem->getDirectoryListing(), $path); + if ($node !== null) { + return $node; + } + } + } + } + + return null; + } +} diff --git a/apps/files_trashbin/lib/Migration/Version1010Date20200630192639.php b/apps/files_trashbin/lib/Migration/Version1010Date20200630192639.php index 778bd76c218..3de908e2d78 100644 --- a/apps/files_trashbin/lib/Migration/Version1010Date20200630192639.php +++ b/apps/files_trashbin/lib/Migration/Version1010Date20200630192639.php @@ -3,33 +3,14 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Migration; use Closure; -use OCP\DB\Types; use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; diff --git a/apps/files_trashbin/lib/Migration/Version1020Date20240403003535.php b/apps/files_trashbin/lib/Migration/Version1020Date20240403003535.php new file mode 100644 index 00000000000..3e85edf40b6 --- /dev/null +++ b/apps/files_trashbin/lib/Migration/Version1020Date20240403003535.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files_Trashbin\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\Attributes\AddColumn; +use OCP\Migration\Attributes\ColumnType; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +#[AddColumn(table: 'files_trash', name: 'deleted_by', type: ColumnType::STRING)] +class Version1020Date20240403003535 extends SimpleMigrationStep { + + /** + * @param Closure(): ISchemaWrapper $schemaClosure + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('files_trash')) { + return null; + } + + $table = $schema->getTable('files_trash'); + $table->addColumn('deleted_by', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + + return $schema; + } +} diff --git a/apps/files_trashbin/lib/Sabre/AbstractTrash.php b/apps/files_trashbin/lib/Sabre/AbstractTrash.php index 412e432a19e..f032395437b 100644 --- a/apps/files_trashbin/lib/Sabre/AbstractTrash.php +++ b/apps/files_trashbin/lib/Sabre/AbstractTrash.php @@ -3,42 +3,23 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Service\ConfigService; use OCA\Files_Trashbin\Trash\ITrashItem; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\Files\FileInfo; +use OCP\IUser; +use Sabre\DAV\Exception\Forbidden; abstract class AbstractTrash implements ITrash { - /** @var ITrashItem */ - protected $data; - - /** @var ITrashManager */ - protected $trashManager; - - public function __construct(ITrashManager $trashManager, ITrashItem $data) { - $this->trashManager = $trashManager; - $this->data = $data; + public function __construct( + protected ITrashManager $trashManager, + protected ITrashItem $data, + ) { } public function getFilename(): string { @@ -57,7 +38,11 @@ abstract class AbstractTrash implements ITrash { return $this->data; } - public function getSize(): int { + /** + * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit + * @return int|float + */ + public function getSize(): int|float { return $this->data->getSize(); } @@ -85,7 +70,15 @@ abstract class AbstractTrash implements ITrash { return $this->data->getTitle(); } + public function getDeletedBy(): ?IUser { + return $this->data->getDeletedBy(); + } + public function delete() { + if (!ConfigService::getDeleteFromTrashEnabled()) { + throw new Forbidden('Not allowed to delete items from the trash bin'); + } + $this->trashManager->removeItem($this->data); } diff --git a/apps/files_trashbin/lib/Sabre/AbstractTrashFile.php b/apps/files_trashbin/lib/Sabre/AbstractTrashFile.php index 520a2a6b6ec..03014d23669 100644 --- a/apps/files_trashbin/lib/Sabre/AbstractTrashFile.php +++ b/apps/files_trashbin/lib/Sabre/AbstractTrashFile.php @@ -3,26 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; diff --git a/apps/files_trashbin/lib/Sabre/AbstractTrashFolder.php b/apps/files_trashbin/lib/Sabre/AbstractTrashFolder.php index 8a61823cf10..9e8f67f4db6 100644 --- a/apps/files_trashbin/lib/Sabre/AbstractTrashFolder.php +++ b/apps/files_trashbin/lib/Sabre/AbstractTrashFolder.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; diff --git a/apps/files_trashbin/lib/Sabre/ITrash.php b/apps/files_trashbin/lib/Sabre/ITrash.php index dcda1abe259..f37e1ccd9c3 100644 --- a/apps/files_trashbin/lib/Sabre/ITrash.php +++ b/apps/files_trashbin/lib/Sabre/ITrash.php @@ -3,30 +3,13 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; use OCP\Files\FileInfo; +use OCP\IUser; interface ITrash { public function restore(): bool; @@ -39,7 +22,9 @@ interface ITrash { public function getDeletionTime(): int; - public function getSize(); + public function getDeletedBy(): ?IUser; + + public function getSize(): int|float; public function getFileId(): int; diff --git a/apps/files_trashbin/lib/Sabre/PropfindPlugin.php b/apps/files_trashbin/lib/Sabre/PropfindPlugin.php deleted file mode 100644 index 219f236f93c..00000000000 --- a/apps/files_trashbin/lib/Sabre/PropfindPlugin.php +++ /dev/null @@ -1,113 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ -namespace OCA\Files_Trashbin\Sabre; - -use OCA\DAV\Connector\Sabre\FilesPlugin; -use OCP\IPreview; -use Sabre\DAV\INode; -use Sabre\DAV\PropFind; -use Sabre\DAV\Server; -use Sabre\DAV\ServerPlugin; - -class PropfindPlugin extends ServerPlugin { - public const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename'; - public const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location'; - public const TRASHBIN_DELETION_TIME = '{http://nextcloud.org/ns}trashbin-deletion-time'; - public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title'; - - /** @var Server */ - private $server; - - /** @var IPreview */ - private $previewManager; - - public function __construct( - IPreview $previewManager - ) { - $this->previewManager = $previewManager; - } - - public function initialize(Server $server) { - $this->server = $server; - - $this->server->on('propFind', [$this, 'propFind']); - } - - - public function propFind(PropFind $propFind, INode $node) { - if (!($node instanceof ITrash)) { - return; - } - - $propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) { - return $node->getFilename(); - }); - - $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) { - return $node->getOriginalLocation(); - }); - - $propFind->handle(self::TRASHBIN_TITLE, function () use ($node) { - return $node->getTitle(); - }); - - $propFind->handle(self::TRASHBIN_DELETION_TIME, function () use ($node) { - return $node->getDeletionTime(); - }); - - $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) { - return $node->getSize(); - }); - - $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) { - return $node->getFileId(); - }); - - $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () { - return 'GD'; // read + delete - }); - - $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) { - // add fake etag, it is only needed to identify the preview image - return $node->getLastModified(); - }); - - $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { - // add fake etag, it is only needed to identify the preview image - return $node->getFileId(); - }); - - $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { - return $this->previewManager->isAvailable($node->getFileInfo()); - }); - - $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () { - return ''; - }); - } -} diff --git a/apps/files_trashbin/lib/Sabre/RestoreFolder.php b/apps/files_trashbin/lib/Sabre/RestoreFolder.php index 9076b3dc284..781a28bbc25 100644 --- a/apps/files_trashbin/lib/Sabre/RestoreFolder.php +++ b/apps/files_trashbin/lib/Sabre/RestoreFolder.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; diff --git a/apps/files_trashbin/lib/Sabre/RootCollection.php b/apps/files_trashbin/lib/Sabre/RootCollection.php index f1b705f3e13..8886dae0895 100644 --- a/apps/files_trashbin/lib/Sabre/RootCollection.php +++ b/apps/files_trashbin/lib/Sabre/RootCollection.php @@ -3,47 +3,26 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ 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; class RootCollection extends AbstractPrincipalCollection { - /** @var ITrashManager */ - private $trashManager; - public function __construct( - ITrashManager $trashManager, + private ITrashManager $trashManager, PrincipalBackend\BackendInterface $principalBackend, - IConfig $config + IConfig $config, ) { parent::__construct($principalBackend, 'principals/users'); - - $this->trashManager = $trashManager; $this->disableListing = !$config->getSystemValue('debug', false); } @@ -59,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/TrashFile.php b/apps/files_trashbin/lib/Sabre/TrashFile.php index 13df15ff084..29bcde769d9 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFile.php +++ b/apps/files_trashbin/lib/Sabre/TrashFile.php @@ -3,35 +3,19 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Trashbin; + class TrashFile extends AbstractTrashFile { public function get() { - return $this->data->getStorage()->fopen($this->data->getInternalPath() . '.d' . $this->getLastModified(), 'rb'); + return $this->data->getStorage()->fopen(Trashbin::getTrashFilename($this->data->getInternalPath(), $this->getDeletionTime()), 'rb'); } public function getName(): string { - return $this->data->getName() . '.d' . $this->getLastModified(); + return Trashbin::getTrashFilename($this->data->getName(), $this->getDeletionTime()); } } diff --git a/apps/files_trashbin/lib/Sabre/TrashFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolder.php index 094f80dad98..e1c495bf08e 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolder.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolder.php @@ -3,31 +3,15 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Trashbin; + class TrashFolder extends AbstractTrashFolder { public function getName(): string { - return $this->data->getName() . '.d' . $this->getLastModified(); + return Trashbin::getTrashFilename($this->data->getName(), $this->getDeletionTime()); } } diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFile.php b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php index 5067d88cb48..37e70b717ae 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolderFile.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolderFile.php @@ -3,26 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; diff --git a/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php index b276a6f29f9..1a5cb98e114 100644 --- a/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php +++ b/apps/files_trashbin/lib/Sabre/TrashFolderFolder.php @@ -3,26 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; diff --git a/apps/files_trashbin/lib/Sabre/TrashHome.php b/apps/files_trashbin/lib/Sabre/TrashHome.php index dda347f4c99..fc291c76f17 100644 --- a/apps/files_trashbin/lib/Sabre/TrashHome.php +++ b/apps/files_trashbin/lib/Sabre/TrashHome.php @@ -3,26 +3,8 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; @@ -33,23 +15,11 @@ use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; class TrashHome implements ICollection { - /** @var ITrashManager */ - private $trashManager; - - /** @var array */ - private $principalInfo; - - /** @var IUser */ - private $user; - public function __construct( - array $principalInfo, - ITrashManager $trashManager, - IUser $user + private array $principalInfo, + private ITrashManager $trashManager, + private IUser $user, ) { - $this->principalInfo = $principalInfo; - $this->trashManager = $trashManager; - $this->user = $user; } public function delete() { diff --git a/apps/files_trashbin/lib/Sabre/TrashRoot.php b/apps/files_trashbin/lib/Sabre/TrashRoot.php index b61daacdf43..dd89583d9a1 100644 --- a/apps/files_trashbin/lib/Sabre/TrashRoot.php +++ b/apps/files_trashbin/lib/Sabre/TrashRoot.php @@ -3,32 +3,15 @@ declare(strict_types=1); /** - * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Julius Härtl <jus@bitgrid.net> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Service\ConfigService; use OCA\Files_Trashbin\Trash\ITrashItem; use OCA\Files_Trashbin\Trash\ITrashManager; +use OCA\Files_Trashbin\Trashbin; use OCP\Files\FileInfo; use OCP\IUser; use Sabre\DAV\Exception\Forbidden; @@ -37,19 +20,18 @@ use Sabre\DAV\ICollection; class TrashRoot implements ICollection { - /** @var IUser */ - private $user; - - /** @var ITrashManager */ - private $trashManager; - - public function __construct(IUser $user, ITrashManager $trashManager) { - $this->user = $user; - $this->trashManager = $trashManager; + public function __construct( + private IUser $user, + private ITrashManager $trashManager, + ) { } public function delete() { - \OCA\Files_Trashbin\Trashbin::deleteAll(); + if (!ConfigService::getDeleteFromTrashEnabled()) { + throw new Forbidden('Not allowed to delete items from the trash bin'); + } + + Trashbin::deleteAll(); foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) { $this->trashManager->removeItem($trashItem); } diff --git a/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php new file mode 100644 index 00000000000..54bb1326966 --- /dev/null +++ b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php @@ -0,0 +1,180 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +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; +use Sabre\DAV\ServerPlugin; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; + +class TrashbinPlugin extends ServerPlugin { + public const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename'; + public const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location'; + public const TRASHBIN_DELETION_TIME = '{http://nextcloud.org/ns}trashbin-deletion-time'; + public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title'; + public const TRASHBIN_DELETED_BY_ID = '{http://nextcloud.org/ns}trashbin-deleted-by-id'; + public const TRASHBIN_DELETED_BY_DISPLAY_NAME = '{http://nextcloud.org/ns}trashbin-deleted-by-display-name'; + public const TRASHBIN_BACKEND = '{http://nextcloud.org/ns}trashbin-backend'; + + /** @var Server */ + private $server; + + public function __construct( + private IPreview $previewManager, + private View $view, + ) { + } + + public function initialize(Server $server) { + $this->server = $server; + + $this->server->on('propFind', [$this, 'propFind']); + $this->server->on('afterMethod:GET', [$this,'httpGet']); + $this->server->on('beforeMove', [$this, 'beforeMove']); + } + + + public function propFind(PropFind $propFind, INode $node) { + if (!($node instanceof ITrash)) { + return; + } + + $propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) { + return $node->getFilename(); + }); + + $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) { + return $node->getOriginalLocation(); + }); + + $propFind->handle(self::TRASHBIN_TITLE, function () use ($node) { + return $node->getTitle(); + }); + + $propFind->handle(self::TRASHBIN_DELETION_TIME, function () use ($node) { + return $node->getDeletionTime(); + }); + + $propFind->handle(self::TRASHBIN_DELETED_BY_ID, function () use ($node) { + return $node->getDeletedBy()?->getUID(); + }); + + $propFind->handle(self::TRASHBIN_DELETED_BY_DISPLAY_NAME, function () use ($node) { + return $node->getDeletedBy()?->getDisplayName(); + }); + + // Pass the real filename as the DAV display name + $propFind->handle(FilesPlugin::DISPLAYNAME_PROPERTYNAME, function () use ($node) { + return $node->getFilename(); + }); + + $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) { + return $node->getSize(); + }); + + $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) { + return $node->getFileId(); + }); + + $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () { + return 'GD'; // read + delete + }); + + $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) { + // add fake etag, it is only needed to identify the preview image + return $node->getLastModified(); + }); + + $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { + // add fake etag, it is only needed to identify the preview image + return $node->getFileId(); + }); + + $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 () { + return ''; + }); + + $propFind->handle(self::TRASHBIN_BACKEND, function () use ($node) { + $fileInfo = $node->getFileInfo(); + if (!($fileInfo instanceof ITrashItem)) { + return ''; + } + return $fileInfo->getTrashBackend()::class; + }); + } + + /** + * Set real filename on trashbin download + * + * @param RequestInterface $request + * @param ResponseInterface $response + */ + public function httpGet(RequestInterface $request, ResponseInterface $response): void { + $path = $request->getPath(); + $node = $this->server->tree->getNodeForPath($path); + if ($node instanceof ITrash) { + $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/Service/ConfigService.php b/apps/files_trashbin/lib/Service/ConfigService.php new file mode 100644 index 00000000000..9e7826fe580 --- /dev/null +++ b/apps/files_trashbin/lib/Service/ConfigService.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace OCA\Files_Trashbin\Service; + +use OCP\AppFramework\Services\IInitialState; +use OCP\IConfig; +use OCP\Server; + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +class ConfigService { + public static function getDeleteFromTrashEnabled(): bool { + return Server::get(IConfig::class)->getSystemValueBool('files.trash.delete', true); + } + + public static function injectInitialState(IInitialState $initialState): void { + $initialState->provideLazyInitialState('config', function () { + return [ + 'allow_delete' => ConfigService::getDeleteFromTrashEnabled(), + ]; + }); + } +} diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index f08d8d25a55..82b7af5a934 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -1,30 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @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; @@ -32,79 +11,47 @@ use OC\Files\Filesystem; use OC\Files\Storage\Wrapper\Wrapper; use OCA\Files_Trashbin\Events\MoveToTrashEvent; use OCA\Files_Trashbin\Trash\ITrashManager; +use OCP\App\IAppManager; use OCP\Encryption\Exceptions\GenericEncryptionException; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\IRootFolder; -use OCP\Files\Mount\IMountPoint; use OCP\Files\Node; use OCP\Files\Storage\IStorage; -use OCP\ILogger; +use OCP\IRequest; use OCP\IUserManager; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\Server; +use Psr\Log\LoggerInterface; class Storage extends Wrapper { - /** @var IMountPoint */ - private $mountPoint; - - /** @var IUserManager */ - private $userManager; - - /** @var ILogger */ - private $logger; - - /** @var EventDispatcherInterface */ - private $eventDispatcher; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var ITrashManager */ - private $trashManager; - - private $trashEnabled = true; + private string $mountPoint; + private bool $trashEnabled = true; /** * Storage constructor. - * * @param array $parameters - * @param ITrashManager $trashManager - * @param IUserManager|null $userManager - * @param ILogger|null $logger - * @param EventDispatcherInterface|null $eventDispatcher - * @param IRootFolder|null $rootFolder */ public function __construct( $parameters, - ITrashManager $trashManager = null, - IUserManager $userManager = null, - ILogger $logger = null, - EventDispatcherInterface $eventDispatcher = null, - IRootFolder $rootFolder = null + private ?ITrashManager $trashManager = null, + private ?IUserManager $userManager = null, + private ?LoggerInterface $logger = null, + private ?IEventDispatcher $eventDispatcher = null, + private ?IRootFolder $rootFolder = null, + private ?IRequest $request = null, ) { $this->mountPoint = $parameters['mountPoint']; - $this->trashManager = $trashManager; - $this->userManager = $userManager; - $this->logger = $logger; - $this->eventDispatcher = $eventDispatcher; - $this->rootFolder = $rootFolder; parent::__construct($parameters); } - /** - * Deletes the given file by moving it into the trashbin. - * - * @param string $path path of file or folder to delete - * - * @return bool true if the operation succeeded, false otherwise - */ - public function unlink($path) { + public function unlink(string $path): bool { if ($this->trashEnabled) { try { return $this->doDelete($path, 'unlink'); } 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); } @@ -113,14 +60,7 @@ class Storage extends Wrapper { } } - /** - * Deletes the given folder by moving it into the trashbin. - * - * @param string $path path of folder to delete - * - * @return bool true if the operation succeeded, false otherwise - */ - public function rmdir($path) { + public function rmdir(string $path): bool { if ($this->trashEnabled) { return $this->doDelete($path, 'rmdir'); } else { @@ -131,11 +71,8 @@ class Storage extends Wrapper { /** * check if it is a file located in data/user/files only files in the * 'files' directory should be moved to the trash - * - * @param $path - * @return bool */ - protected function shouldMoveToTrash($path) { + protected function shouldMoveToTrash(string $path): bool { $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); $parts = explode('/', $normalized); if (count($parts) < 4 || strpos($normalized, '/appdata_') === 0) { @@ -153,6 +90,7 @@ class Storage extends Wrapper { foreach ($nodes as $node) { $event = $this->createMoveToTrashEvent($node); + $this->eventDispatcher->dispatchTyped($event); $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); if ($event->shouldMoveToTrashBin() === false) { return false; @@ -172,7 +110,7 @@ class Storage extends Wrapper { * @param Node $node * @return MoveToTrashEvent */ - protected function createMoveToTrashEvent(Node $node) { + protected function createMoveToTrashEvent(Node $node): MoveToTrashEvent { return new MoveToTrashEvent($node); } @@ -184,50 +122,60 @@ class Storage extends Wrapper { * * @return bool true if the operation succeeded, false otherwise */ - private function doDelete($path, $method) { - if ( - !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin') - || (pathinfo($path, PATHINFO_EXTENSION) === 'part') - || $this->shouldMoveToTrash($path) === false - ) { - return call_user_func([$this->storage, $method], $path); - } + private function doDelete(string $path, string $method): bool { + $isTrashbinEnabled = Server::get(IAppManager::class)->isEnabledForUser('files_trashbin'); + $isPartFile = pathinfo($path, PATHINFO_EXTENSION) === 'part'; + $isSkipTrashHeaderSet = $this->request !== null && $this->request->getHeader('X-NC-Skip-Trashbin') === 'true'; + // We keep the shouldMoveToTrash call at the end to prevent emitting unnecessary event. + $shouldMoveToTrash = $isTrashbinEnabled && !$isPartFile && !$isSkipTrashHeaderSet && $this->shouldMoveToTrash($path); + + if ($shouldMoveToTrash) { + // check permissions before we continue, this is especially important for + // shared files + if (!$this->isDeletable($path)) { + return false; + } - // check permissions before we continue, this is especially important for - // shared files - if (!$this->isDeletable($path)) { - return false; + $isMovedToTrash = $this->trashManager->moveToTrash($this, $path); + if ($isMovedToTrash) { + return true; + } } - $isMovedToTrash = $this->trashManager->moveToTrash($this, $path); - if (!$isMovedToTrash) { - return call_user_func([$this->storage, $method], $path); - } else { - return true; - } + return call_user_func([$this->storage, $method], $path); } /** - * Setup the storate wrapper callback + * Setup the storage wrapper callback */ - public static function setupStorage() { - \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { - return new \OCA\Files_Trashbin\Storage( - ['storage' => $storage, 'mountPoint' => $mountPoint], - \OC::$server->query(ITrashManager::class), - \OC::$server->getUserManager(), - \OC::$server->getLogger(), - \OC::$server->getEventDispatcher(), - \OC::$server->getLazyRootFolder() - ); - }, 1); + public static function setupStorage(): void { + $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) { + return new Storage( + ['storage' => $storage, 'mountPoint' => $mountPoint], + $trashManager, + $userManager, + $logger, + $eventDispatcher, + $rootFolder, + $request, + ); + }, + 1); } public function getMountPoint() { return $this->mountPoint; } - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { $sourceIsTrashbin = $sourceStorage->instanceOfStorage(Storage::class); try { // the fallback for moving between storage involves a copy+delete @@ -251,11 +199,11 @@ class Storage extends Wrapper { } } - protected function disableTrash() { + protected function disableTrash(): void { $this->trashEnabled = false; } - protected function enableTrash() { + protected function enableTrash(): void { $this->trashEnabled = true; } } diff --git a/apps/files_trashbin/lib/Trash/BackendNotFoundException.php b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php index e52d5f667c9..292b6ee293c 100644 --- a/apps/files_trashbin/lib/Trash/BackendNotFoundException.php +++ b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; diff --git a/apps/files_trashbin/lib/Trash/ITrashBackend.php b/apps/files_trashbin/lib/Trash/ITrashBackend.php index 52eaad7737e..11b3132bfba 100644 --- a/apps/files_trashbin/lib/Trash/ITrashBackend.php +++ b/apps/files_trashbin/lib/Trash/ITrashBackend.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; diff --git a/apps/files_trashbin/lib/Trash/ITrashItem.php b/apps/files_trashbin/lib/Trash/ITrashItem.php index 0f9c8144a59..299cac49a69 100644 --- a/apps/files_trashbin/lib/Trash/ITrashItem.php +++ b/apps/files_trashbin/lib/Trash/ITrashItem.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; @@ -77,5 +61,10 @@ interface ITrashItem extends FileInfo { */ public function getUser(): IUser; + /** + * @since 30.0.0 + */ + public function getDeletedBy(): ?IUser; + public function getTitle(): string; } diff --git a/apps/files_trashbin/lib/Trash/ITrashManager.php b/apps/files_trashbin/lib/Trash/ITrashManager.php index 24617c0287d..743ea01358a 100644 --- a/apps/files_trashbin/lib/Trash/ITrashManager.php +++ b/apps/files_trashbin/lib/Trash/ITrashManager.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; diff --git a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php index 22f12c9a062..204defde35c 100644 --- a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php +++ b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; @@ -28,20 +12,21 @@ use OCA\Files_Trashbin\Helper; use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trashbin; use OCP\Files\FileInfo; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\Storage\IStorage; use OCP\IUser; +use OCP\IUserManager; class LegacyTrashBackend implements ITrashBackend { /** @var array */ private $deletedFiles = []; - /** @var IRootFolder */ - private $rootFolder; - - public function __construct(IRootFolder $rootFolder) { - $this->rootFolder = $rootFolder; + public function __construct( + private IRootFolder $rootFolder, + private IUserManager $userManager, + ) { } /** @@ -50,7 +35,7 @@ class LegacyTrashBackend implements ITrashBackend { * @param ITrashItem $parent * @return ITrashItem[] */ - private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array { + private function mapTrashItems(array $items, IUser $user, ?ITrashItem $parent = null): array { $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : ''; $isRoot = $parent === null; return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) { @@ -58,13 +43,17 @@ class LegacyTrashBackend implements ITrashBackend { if (!$originalLocation) { $originalLocation = $file->getName(); } + /** @psalm-suppress UndefinedInterfaceMethod */ + $deletedBy = $this->userManager->get($file['deletedBy']) ?? $parent?->getDeletedBy(); + $trashFilename = Trashbin::getTrashFilename($file->getName(), $file->getMtime()); return new TrashItem( $this, $originalLocation, $file->getMTime(), - $parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''), + $parentTrashPath . '/' . ($isRoot ? $trashFilename : $file->getName()), $file, - $user + $user, + $deletedBy, ); }, $items); } @@ -77,7 +66,7 @@ class LegacyTrashBackend implements ITrashBackend { public function listTrashFolder(ITrashItem $folder): array { $user = $folder->getUser(); $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID()); - return $this->mapTrashItems($entries, $user ,$folder); + return $this->mapTrashItems($entries, $user, $folder); } public function restoreItem(ITrashItem $item) { @@ -104,7 +93,7 @@ class LegacyTrashBackend implements ITrashBackend { $this->deletedFiles[$normalized] = $normalized; if ($filesPath = $view->getRelativePath($normalized)) { $filesPath = trim($filesPath, '/'); - $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath); + $result = Trashbin::move2trash($filesPath); } else { $result = false; } @@ -120,11 +109,11 @@ class LegacyTrashBackend implements ITrashBackend { try { $userFolder = $this->rootFolder->getUserFolder($user->getUID()); $trash = $userFolder->getParent()->get('files_trashbin/files'); - $trashFiles = $trash->getById($fileId); - if (!$trashFiles) { + if ($trash instanceof Folder) { + return $trash->getFirstNodeById($fileId); + } else { return null; } - return $trashFiles ? array_pop($trashFiles) : null; } catch (NotFoundException $e) { return null; } diff --git a/apps/files_trashbin/lib/Trash/TrashItem.php b/apps/files_trashbin/lib/Trash/TrashItem.php index 29de0a2ed5a..2ae999a2069 100644 --- a/apps/files_trashbin/lib/Trash/TrashItem.php +++ b/apps/files_trashbin/lib/Trash/TrashItem.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; @@ -26,33 +10,16 @@ use OCP\Files\FileInfo; use OCP\IUser; class TrashItem implements ITrashItem { - /** @var ITrashBackend */ - private $backend; - /** @var string */ - private $orignalLocation; - /** @var int */ - private $deletedTime; - /** @var string */ - private $trashPath; - /** @var FileInfo */ - private $fileInfo; - /** @var IUser */ - private $user; public function __construct( - ITrashBackend $backend, - string $originalLocation, - int $deletedTime, - string $trashPath, - FileInfo $fileInfo, - IUser $user + private ITrashBackend $backend, + private string $originalLocation, + private int $deletedTime, + private string $trashPath, + private FileInfo $fileInfo, + private IUser $user, + private ?IUser $deletedBy, ) { - $this->backend = $backend; - $this->orignalLocation = $originalLocation; - $this->deletedTime = $deletedTime; - $this->trashPath = $trashPath; - $this->fileInfo = $fileInfo; - $this->user = $user; } public function getTrashBackend(): ITrashBackend { @@ -60,7 +27,7 @@ class TrashItem implements ITrashItem { } public function getOriginalLocation(): string { - return $this->orignalLocation; + return $this->originalLocation; } public function getDeletedTime(): int { @@ -186,4 +153,20 @@ class TrashItem implements ITrashItem { public function getUploadTime(): int { return $this->fileInfo->getUploadTime(); } + + public function getParentId(): int { + return $this->fileInfo->getParentId(); + } + + public function getDeletedBy(): ?IUser { + return $this->deletedBy; + } + + /** + * @inheritDoc + * @return array<string, int|string|bool|float|string[]|int[]> + */ + public function getMetadata(): array { + return $this->fileInfo->getMetadata(); + } } diff --git a/apps/files_trashbin/lib/Trash/TrashManager.php b/apps/files_trashbin/lib/Trash/TrashManager.php index cc73ce09ccb..521a576c00a 100644 --- a/apps/files_trashbin/lib/Trash/TrashManager.php +++ b/apps/files_trashbin/lib/Trash/TrashManager.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\Trash; @@ -77,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 46a2b07fc07..667066c2fca 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -1,91 +1,69 @@ <?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; -use OC_User; +use Exception; use OC\Files\Cache\Cache; use OC\Files\Cache\CacheEntry; use OC\Files\Cache\CacheQueryBuilder; use OC\Files\Filesystem; -use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\Node\NonExistingFile; +use OC\Files\Node\NonExistingFolder; use OC\Files\View; +use OC\User\NoUserException; +use OC_User; use OCA\Files_Trashbin\AppInfo\Application; use OCA\Files_Trashbin\Command\Expire; -use OCP\AppFramework\Utility\ITimeFactory; +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; /** - * Whether versions have already be rescanned during this PHP request - * - * @var bool - */ - private static $scannedVersions = false; - - /** * 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 } } @@ -95,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 @@ -124,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; @@ -153,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))) @@ -174,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'); @@ -197,10 +175,10 @@ class Trashbin { * @param string $sourcePath * @param string $owner * @param string $targetPath - * @param $user - * @param integer $timestamp + * @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); @@ -210,8 +188,8 @@ class Trashbin { $view = new View('/'); - $target = $user . '/files_trashbin/files/' . $targetFilename . '.d' . $timestamp; - $source = $owner . '/files_trashbin/files/' . $sourceFilename . '.d' . $timestamp; + $target = $user . '/files_trashbin/files/' . static::getTrashFilename($targetFilename, $timestamp); + $source = $owner . '/files_trashbin/files/' . static::getTrashFilename($sourceFilename, $timestamp); $free = $view->free_space($target); $isUnknownOrUnlimitedFreeSpace = $free < 0; $isEnoughFreeSpaceLeft = $view->filesize($source) < $free; @@ -221,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']); } } } @@ -279,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/' . $filename . '.d' . $timestamp; + $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) { @@ -301,9 +279,9 @@ class Trashbin { $timestamp = $timestamp + 1; - $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; + $trashPath = '/files_trashbin/files/' . static::getTrashFilename($filename, $timestamp); } - } + } while (!$gotLock); $sourceStorage = $sourceInfo->getStorage(); $sourceInternalPath = $sourceInfo->getInternalPath(); @@ -312,29 +290,24 @@ class Trashbin { $trashStorage->unlink($trashInternalPath); } - $config = \OC::$server->getConfig(); - $systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1'); - $userTrashbinSize = (int)$config->getUserValue($owner, 'files_trashbin', 'trashbin_size', '-1'); - $configuredTrashbinSize = ($userTrashbinSize < 0) ? $systemTrashbinSize : $userTrashbinSize; + $configuredTrashbinSize = static::getConfiguredTrashbinSize($owner); if ($configuredTrashbinSize >= 0 && $sourceInfo->getSize() >= $configuredTrashbinSize) { 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 @@ -354,18 +327,19 @@ 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), - 'trashPath' => Filesystem::normalizePath($filename . '.d' . $timestamp)]); + 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,30 +361,43 @@ class Trashbin { return $moveSuccessful; } + private static function getConfiguredTrashbinSize(string $user): int|float { + $config = Server::get(IConfig::class); + $userTrashbinSize = $config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1'); + if (is_numeric($userTrashbinSize) && ($userTrashbinSize > -1)) { + return Util::numericToNumber($userTrashbinSize); + } + $systemTrashbinSize = $config->getAppValue('files_trashbin', 'trashbin_size', '-1'); + if (is_numeric($systemTrashbinSize)) { + return Util::numericToNumber($systemTrashbinSize); + } + return -1; + } + /** * Move file versions to trash so that they can be restored later * * @param string $filename of deleted file * @param string $owner owner user id * @param string $ownerPath path relative to the owner's home storage - * @param integer $timestamp when the file was deleted + * @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('/'); if ($rootView->is_dir($owner . '/files_versions/' . $ownerPath)) { if ($owner !== $user) { - self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView); + 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/' . $filename . '.d' . $timestamp); - } elseif ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) { + self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . static::getTrashFilename($filename, $timestamp)); + } 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/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp); + self::copy($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $owner . '/files_trashbin/versions/' . static::getTrashFilename($v['name'] . '.v' . $v['version'], $timestamp)); } - self::move($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $filename . '.v' . $v['version'] . '.d' . $timestamp); + self::move($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v['version'], $timestamp)); } } } @@ -464,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 * @@ -478,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 = ''; } @@ -504,6 +491,21 @@ class Trashbin { if (!$view->isCreatable(dirname($target))) { throw new NotPermittedException("Can't restore trash item because the target folder is not writable"); } + + $sourcePath = Filesystem::normalizePath($file); + $targetPath = Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename); + + $sourceNode = self::getNodeForPath($sourcePath); + $targetNode = self::getNodeForPath($targetPath); + $run = true; + $event = new BeforeNodeRestoredEvent($sourceNode, $targetNode, $run); + $dispatcher = Server::get(IEventDispatcher::class); + $dispatcher->dispatchTyped($event); + + if (!$run) { + return false; + } + $restoreResult = $view->rename($source, $target); // handle the restore result @@ -512,13 +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' => Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename), - 'trashPath' => Filesystem::normalizePath($file)]); + 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 = 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))) @@ -544,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('/'); @@ -568,7 +575,7 @@ class Trashbin { } elseif ($versions = self::getVersionsFromTrash($versionedFile, $timestamp, $user)) { foreach ($versions as $v) { if ($timestamp) { - $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v . '.d' . $timestamp, $owner . '/files_versions/' . $ownerPath . '.v' . $v); + $rootView->rename($user . '/files_trashbin/versions/' . static::getTrashFilename($versionedFile . '.v' . $v, $timestamp), $owner . '/files_versions/' . $ownerPath . '.v' . $v); } else { $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v, $owner . '/files_versions/' . $ownerPath . '.v' . $v); } @@ -610,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(); @@ -654,7 +661,7 @@ class Trashbin { * @param string $user * @param int $timestamp of deletion time * - * @return int size of deleted files + * @return int|float size of deleted files */ public static function delete($filename, $user, $timestamp = null) { $userRoot = \OC::$server->getUserFolder($user)->getParent(); @@ -662,14 +669,14 @@ 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))) ->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp))); $query->executeStatement(); - $file = $filename . '.d' . $timestamp; + $file = static::getTrashFilename($filename, $timestamp); } else { $file = $filename; } @@ -696,24 +703,21 @@ class Trashbin { } /** - * @param View $view * @param string $file * @param string $filename - * @param integer|null $timestamp - * @param string $user - * @return int + * @param ?int $timestamp */ - private static function deleteVersions(View $view, $file, $filename, $timestamp, $user) { + 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); } elseif ($versions = self::getVersionsFromTrash($filename, $timestamp, $user)) { foreach ($versions as $v) { if ($timestamp) { - $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); - $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); + $size += $view->filesize('/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v, $timestamp)); + $view->unlink('/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v, $timestamp)); } else { $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v); $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v); @@ -736,7 +740,7 @@ class Trashbin { $view = new View('/' . $user); if ($timestamp) { - $filename = $filename . '.d' . $timestamp; + $filename = static::getTrashFilename($filename, $timestamp); } $target = Filesystem::normalizePath('files_trashbin/files/' . $filename); @@ -750,35 +754,30 @@ 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(); } /** * calculate remaining free space for trash bin * - * @param integer $trashbinSize current size of the trash bin + * @param int|float $trashbinSize current size of the trash bin * @param string $user - * @return int available free space for trash bin + * @return int|float available free space for trash bin */ - private static function calculateFreeSpace($trashbinSize, $user) { - $config = \OC::$server->getConfig(); - $userTrashbinSize = (int)$config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1'); - if ($userTrashbinSize > -1) { - return $userTrashbinSize - $trashbinSize; - } - $systemTrashbinSize = (int)$config->getAppValue('files_trashbin', 'trashbin_size', '-1'); - if ($systemTrashbinSize > -1) { - return $systemTrashbinSize - $trashbinSize; + private static function calculateFreeSpace(int|float $trashbinSize, string $user): int|float { + $configuredTrashbinSize = static::getConfiguredTrashbinSize($user); + if ($configuredTrashbinSize > -1) { + return $configuredTrashbinSize - $trashbinSize; } - $softQuota = true; - $userObject = \OC::$server->getUserManager()->get($user); + $userObject = Server::get(IUserManager::class)->get($user); if (is_null($userObject)) { return 0; } + $softQuota = true; $quota = $userObject->getQuota(); if ($quota === null || $quota === 'none') { $quota = Filesystem::free_space('/'); @@ -788,7 +787,11 @@ 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; + } } // calculate available space for trash bin @@ -808,7 +811,7 @@ class Trashbin { $availableSpace = $quota; } - return (int)$availableSpace; + return Util::numericToNumber($availableSpace); } /** @@ -852,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)); } } @@ -865,12 +868,12 @@ class Trashbin { * * @param array $files * @param string $user - * @param int $availableSpace available disc space - * @return int size of deleted files + * @param int|float $availableSpace available disc space + * @return int|float size of deleted files */ - protected static function deleteFiles($files, $user, $availableSpace) { + 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; @@ -878,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 { @@ -894,11 +903,11 @@ class Trashbin { * * @param array $files list of files sorted by mtime * @param string $user - * @return integer[] size of deleted files and number of deleted files + * @return array{int|float, int} size of deleted files and number of deleted files */ 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) { @@ -908,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; @@ -934,10 +947,10 @@ class Trashbin { * @param string $source source path, relative to the users files directory * @param string $destination destination path relative to the users root directory * @param View $view file view for the users root directory - * @return int + * @return int|float * @throws Exceptions\CopyRecursiveException */ - private static function copy_recursive($source, $destination, View $view) { + private static function copy_recursive($source, $destination, View $view): int|float { $size = 0; if ($view->is_dir($source)) { $view->mkdir($destination); @@ -950,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)); } @@ -959,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)); } @@ -971,36 +984,18 @@ class Trashbin { * * @param string $filename name of the file which should be restored * @param int $timestamp timestamp when the file was deleted - * @return array */ - private static function getVersionsFromTrash($filename, $timestamp, $user) { + private static function getVersionsFromTrash($filename, $timestamp, string $user): array { $view = new View('/' . $user . '/files_trashbin/versions'); $versions = []; /** @var \OC\Files\Storage\Storage $storage */ [$storage,] = $view->resolvePath('/'); - //force rescan of versions, local storage may not have updated the cache - $waitstart = time(); - while (!self::$scannedVersions) { - try { - $storage->getScanner()->scan('files_trashbin/versions'); - self::$scannedVersions = true; - } catch (LockedException $e) { - /* a concurrent remove/restore from trash occurred, - * retry with a maximum wait time of approx. 15 seconds - */ - if (time() - $waitstart > 15) { - throw $e; - } - usleep(50000 + rand(0, 10000)); - } - } - - $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 { @@ -1010,11 +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) + 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 []; @@ -1031,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) { @@ -1058,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, '/'); @@ -1069,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++; } @@ -1085,10 +1079,10 @@ class Trashbin { * get the size from a given root folder * * @param View $view file view on the root folder - * @return integer size of the folder + * @return int|float size of the folder */ - private static function calculateSize($view) { - $root = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); + private static function calculateSize(View $view): int|float { + $root = Server::get(IConfig::class)->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); if (!file_exists($root)) { return 0; } @@ -1116,9 +1110,9 @@ class Trashbin { * get current size of trash bin from a given user * * @param string $user user who owns the trash bin - * @return integer trash bin size + * @return int|float trash bin size */ - private static function getTrashbinSize($user) { + private static function getTrashbinSize(string $user): int|float { $view = new View('/' . $user); $fileInfo = $view->getFileInfo('/files_trashbin'); return isset($fileInfo['size']) ? $fileInfo['size'] : 0; @@ -1133,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; } @@ -1147,6 +1141,60 @@ 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]); + } + + /** + * Return the filename used in the trash bin + */ + public static function getTrashFilename(string $filename, int $timestamp): string { + $trashFilename = $filename . '.d' . $timestamp; + $length = strlen($trashFilename); + // oc_filecache `name` column has a limit of 250 chars + $maxLength = 250; + if ($length > $maxLength) { + $trashFilename = substr_replace( + $trashFilename, + '', + $maxLength / 2, + $length - $maxLength + ); + } + return $trashFilename; + } + + private static function getNodeForPath(string $path): Node { + $user = OC_User::getUser(); + $rootFolder = Server::get(IRootFolder::class); + + if ($user !== false) { + $userFolder = $rootFolder->getUserFolder($user); + /** @var Folder */ + $trashFolder = $userFolder->getParent()->get('files_trashbin/files'); + try { + return $trashFolder->get($path); + } catch (NotFoundException $ex) { + } + } + + $view = Server::get(View::class); + $fsView = Filesystem::getView(); + if ($fsView === null) { + throw new Exception('View should not be null'); + } + + $fullPath = $fsView->getAbsolutePath($path); + + if (Filesystem::is_dir($path)) { + return new NonExistingFolder($rootFolder, $view, $fullPath); + } else { + return new NonExistingFile($rootFolder, $view, $fullPath); + } + } + + public function handle(Event $event): void { + if ($event instanceof BeforeNodeDeletedEvent) { + self::ensureFileScannedHook($event->getNode()); + } } } diff --git a/apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php b/apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php index 70338a469d3..baff1ef6032 100644 --- a/apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php +++ b/apps/files_trashbin/lib/UserMigration/TrashbinMigrator.php @@ -3,30 +3,14 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022 Côme Chilliet <come.chilliet@nextcloud.com> - * - * @author Côme Chilliet <come.chilliet@nextcloud.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_Trashbin\UserMigration; use OCA\Files_Trashbin\AppInfo\Application; +use OCA\Files_Trashbin\Trashbin; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -45,37 +29,28 @@ class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator { use TMigratorBasicVersionHandling; - protected const PATH_FILES_FOLDER = Application::APP_ID.'/files'; - protected const PATH_LOCATIONS_FILE = Application::APP_ID.'/locations.json'; - - protected IRootFolder $root; - - protected IDBConnection $dbc; - - protected IL10N $l10n; + protected const PATH_FILES_FOLDER = Application::APP_ID . '/files'; + protected const PATH_LOCATIONS_FILE = Application::APP_ID . '/locations.json'; public function __construct( - IRootFolder $rootFolder, - IDBConnection $dbc, - IL10N $l10n + protected IRootFolder $root, + protected IDBConnection $dbc, + protected IL10N $l10n, ) { - $this->root = $rootFolder; - $this->dbc = $dbc; - $this->l10n = $l10n; } /** * {@inheritDoc} */ - public function getEstimatedExportSize(IUser $user): int { + public function getEstimatedExportSize(IUser $user): int|float { $uid = $user->getUID(); try { - $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin'); + $trashbinFolder = $this->root->get('/' . $uid . '/files_trashbin'); if (!$trashbinFolder instanceof Folder) { return 0; } - return (int)ceil($trashbinFolder->getSize() / 1024); + return ceil($trashbinFolder->getSize() / 1024); } catch (\Throwable $e) { return 0; } @@ -90,18 +65,26 @@ class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator { $uid = $user->getUID(); try { - $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin'); + $trashbinFolder = $this->root->get('/' . $uid . '/files_trashbin'); if (!$trashbinFolder instanceof Folder) { - throw new UserMigrationException('/'.$uid.'/files_trashbin is not a folder'); + throw new UserMigrationException('/' . $uid . '/files_trashbin is not a folder'); } - $output->writeln("Exporting trashbin files…"); + $output->writeln('Exporting trashbin files…'); $exportDestination->copyFolder($trashbinFolder, static::PATH_FILES_FOLDER); - $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($uid); + $originalLocations = []; + // TODO Export all extra data and bump migrator to v2 + foreach (Trashbin::getExtraData($uid) as $filename => $extraData) { + $locationData = []; + foreach ($extraData as $timestamp => ['location' => $location]) { + $locationData[$timestamp] = $location; + } + $originalLocations[$filename] = $locationData; + } $exportDestination->addFileContents(static::PATH_LOCATIONS_FILE, json_encode($originalLocations)); } catch (NotFoundException $e) { - $output->writeln("No trashbin to export…"); + $output->writeln('No trashbin to export…'); } catch (\Throwable $e) { - throw new UserMigrationException("Could not export trashbin: ".$e->getMessage(), 0, $e); + throw new UserMigrationException('Could not export trashbin: ' . $e->getMessage(), 0, $e); } } @@ -120,18 +103,18 @@ class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator { if ($importSource->pathExists(static::PATH_FILES_FOLDER)) { try { - $trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin'); + $trashbinFolder = $this->root->get('/' . $uid . '/files_trashbin'); if (!$trashbinFolder instanceof Folder) { - throw new UserMigrationException('Could not import trashbin, /'.$uid.'/files_trashbin is not a folder'); + throw new UserMigrationException('Could not import trashbin, /' . $uid . '/files_trashbin is not a folder'); } } catch (NotFoundException $e) { - $trashbinFolder = $this->root->newFolder('/'.$uid.'/files_trashbin'); + $trashbinFolder = $this->root->newFolder('/' . $uid . '/files_trashbin'); } - $output->writeln("Importing trashbin files…"); + $output->writeln('Importing trashbin files…'); try { $importSource->copyToFolder($trashbinFolder, static::PATH_FILES_FOLDER); } catch (\Throwable $e) { - throw new UserMigrationException("Could not import trashbin.", 0, $e); + throw new UserMigrationException('Could not import trashbin.', 0, $e); } $locations = json_decode($importSource->getFileContents(static::PATH_LOCATIONS_FILE), true, 512, JSON_THROW_ON_ERROR); $qb = $this->dbc->getQueryBuilder(); @@ -148,13 +131,13 @@ class TrashbinMigrator implements IMigrator, ISizeEstimationMigrator { ->setParameter('id', $id) ->setParameter('timestamp', $timestamp) ->setParameter('location', $location) - ; + ; $qb->executeStatement(); } } } else { - $output->writeln("No trashbin to import…"); + $output->writeln('No trashbin to import…'); } } |