diff options
Diffstat (limited to 'apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php')
-rw-r--r-- | apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php | 119 |
1 files changed, 44 insertions, 75 deletions
diff --git a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php index 9e35273544b..bb383dab78d 100644 --- a/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php +++ b/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php @@ -1,83 +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\IConfig; -use OCP\IUser; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\IAppConfig; use OCP\IUserManager; +use Psr\Log\LoggerInterface; -class ExpireTrash extends \OC\BackgroundJob\TimedJob { - - /** @var IConfig */ - private $config; - - /** - * @var Expiration - */ - private $expiration; - - /** - * @var IUserManager - */ - private $userManager; - - public function __construct(IConfig $config = null, - IUserManager $userManager = null, - Expiration $expiration = null) { +class ExpireTrash extends TimedJob { + public function __construct( + 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); - - if ($config === null || $expiration === null || $userManager === null) { - $this->fixDIForJobs(); - } else { - $this->config = $config; - $this->userManager = $userManager; - $this->expiration = $expiration; - } - } - - protected function fixDIForJobs() { - /** @var Application $application */ - $application = \OC::$server->query(Application::class); - $this->config = $application->getContainer()->get(IConfig::class); - $this->userManager = \OC::$server->getUserManager(); - $this->expiration = $application->getContainer()->query('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; } @@ -87,29 +41,44 @@ class ExpireTrash extends \OC\BackgroundJob\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(); } /** * Act on behalf on trash item owner - * @param string $user - * @return boolean */ - protected function setupFS($user) { + protected function setupFS(string $user): bool { \OC_Util::tearDownFS(); \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; } |