aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib/Command
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_trashbin/lib/Command')
-rw-r--r--apps/files_trashbin/lib/Command/CleanUp.php56
-rw-r--r--apps/files_trashbin/lib/Command/Expire.php41
-rw-r--r--apps/files_trashbin/lib/Command/ExpireTrash.php75
-rw-r--r--apps/files_trashbin/lib/Command/RestoreAllFiles.php66
-rw-r--r--apps/files_trashbin/lib/Command/Size.php60
5 files changed, 91 insertions, 207 deletions
diff --git a/apps/files_trashbin/lib/Command/CleanUp.php b/apps/files_trashbin/lib/Command/CleanUp.php
index a00a96d5ee2..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() {
@@ -127,18 +97,18 @@ class CleanUp extends Command {
$node = $this->rootFolder->get($path);
if ($verbose) {
- $output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
+ $output->writeln('Deleting <info>' . Util::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
}
$node->delete();
if ($this->rootFolder->nodeExists($path)) {
- $output->writeln("<error>Trash folder sill exists after attempting to delete it</error>");
+ $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 048631db7a5..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 cd79f1e8def..ce31f759c0e 100644
--- a/apps/files_trashbin/lib/Command/RestoreAllFiles.php
+++ b/apps/files_trashbin/lib/Command/RestoreAllFiles.php
@@ -1,25 +1,14 @@
<?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;
@@ -44,17 +33,6 @@ class RestoreAllFiles extends Base {
'all' => self::SCOPE_ALL
];
- /** @var IUserManager */
- protected $userManager;
-
- /** @var IRootFolder */
- protected $rootFolder;
-
- /** @var \OCP\IDBConnection */
- protected $dbConnection;
-
- protected ITrashManager $trashManager;
-
/** @var IL10N */
protected $l10n;
@@ -65,12 +43,14 @@ class RestoreAllFiles extends Base {
* @param ITrashManager $trashManager
* @param IFactory $l10nFactory
*/
- public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, ITrashManager $trashManager, 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->trashManager = $trashManager;
$this->l10n = $l10nFactory->get('files_trashbin');
}
@@ -101,13 +81,13 @@ class RestoreAllFiles extends Base {
'since',
null,
InputOption::VALUE_OPTIONAL,
- 'Only restore files deleted after the given timestamp'
+ '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 timestamp'
+ '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',
@@ -180,13 +160,13 @@ class RestoreAllFiles extends Base {
$trashCount = count($userTrashItems);
if ($trashCount == 0) {
- $output->writeln("User has no deleted files in the trashbin matching the given filters");
+ $output->writeln('User has no deleted files in the trashbin matching the given filters');
return;
}
$prepMsg = $dryRun ? 'Would restore' : 'Preparing to restore';
$output->writeln("$prepMsg <info>$trashCount</info> files...");
$count = 0;
- foreach($userTrashItems as $trashItem) {
+ foreach ($userTrashItems as $trashItem) {
$filename = $trashItem->getName();
$humanTime = $this->l10n->l('datetime', $trashItem->getDeletedTime());
// We use getTitle() here instead of getOriginalLocation() because
@@ -204,13 +184,13 @@ class RestoreAllFiles extends Base {
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);
+ $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(' <info>success</info>');
}
if (!$dryRun) {
@@ -259,8 +239,8 @@ class RestoreAllFiles extends Base {
$trashItemClass = get_class($trashItem);
// Check scope with exact class name for locally deleted files
- if ($scope === self::SCOPE_USER && $trashItemClass !== \OCA\Files_Trashbin\Trash\TrashItem::class) {
- $output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it is not a user trash item", OutputInterface::VERBOSITY_VERBOSE);
+ 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;
}
@@ -270,19 +250,19 @@ class RestoreAllFiles extends Base {
* @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);
+ $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);
+ $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);
+ $output->writeln('Skipping <info>' . $trashItem->getName() . "</info> because it was deleted after the 'until' timestamp", OutputInterface::VERBOSITY_VERBOSE);
continue;
}
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;