Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor files_trashbin app commands #39882

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 60 additions & 63 deletions apps/files_trashbin/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,15 @@
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() {
protected function configure(): void {
$this
->setName('trashbin:cleanup')
->setDescription('Remove deleted files')
Expand All @@ -79,41 +65,49 @@
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');
}

if ((!empty($users)) and ($input->getOption('all-users'))) {
throw new InvalidOptionException('Either specify a user_id or --all-users');
} elseif (!empty($users)) {
}

if (!empty($users)) {
foreach ($users as $user) {
if ($this->userManager->userExists($user)) {
$output->writeln("Remove deleted files of <info>$user</info>");
$this->removeDeletedFiles($user, $output, $verbose);
} else {
if (!$this->userManager->userExists($user)) {
$output->writeln("<error>Unknown user $user</error>");
return 1;
return self::FAILURE;
}

$output->writeln("Remove deleted files of <info>$user</info>");
$this->removeDeletedFiles($user, $output, $verbose);
}
} elseif ($input->getOption('all-users')) {
$output->writeln('Remove deleted files for all users');
foreach ($this->userManager->getBackends() as $backend) {
$name = get_class($backend);
if ($backend instanceof IUserBackend) {
$name = $backend->getBackendName();
}
$output->writeln("Remove deleted files for users on backend <info>$name</info>");
$limit = 500;
$offset = 0;
do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$this->removeDeletedFiles($user, $output, $verbose);
}
$offset += $limit;
} while (count($users) >= $limit);

return self::SUCCESS;
}

$output->writeln('Remove deleted files for all users');
foreach ($this->userManager->getBackends() as $backend) {
$name = get_class($backend);
if ($backend instanceof IUserBackend) {
$name = $backend->getBackendName();
}
} else {
throw new InvalidOptionException('Either specify a user_id or --all-users');
$output->writeln("Remove deleted files for users on backend <info>$name</info>");
$limit = 500;
$offset = 0;
do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$this->removeDeletedFiles($user, $output, $verbose);
}
$offset += $limit;
} while (count($users) >= $limit);
}
return 0;

return self::SUCCESS;
}

/**
Expand All @@ -123,26 +117,29 @@
\OC_Util::tearDownFS();
\OC_Util::setupFS($uid);
$path = '/' . $uid . '/files_trashbin';
if ($this->rootFolder->nodeExists($path)) {
$node = $this->rootFolder->get($path);

if ($verbose) {
$output->writeln("Deleting <info>" . \OC_Helper::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();
} else {
if (!$this->rootFolder->nodeExists($path)) {
if ($verbose) {
$output->writeln("No trash found for <info>$uid</info>");
}

return;
}

$node = $this->rootFolder->get($path);

if ($verbose) {
$output->writeln("Deleting <info>" . \OC_Helper::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();

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\DB\QueryBuilder\IQueryBuilder::execute has been marked as deprecated
}
}
15 changes: 4 additions & 11 deletions apps/files_trashbin/lib/Command/Expire.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@
class Expire implements ICommand {
use FileAccess;

/**
* @var string
*/
private $user;

/**
* @param string $user
*/
public function __construct($user) {
$this->user = $user;
public function __construct(
private string $user,
) {
}

public function handle() {
public function handle(): void {
$userManager = \OC::$server->getUserManager();
if (!$userManager->userExists($this->user)) {
// User has been deleted already
Expand Down
69 changes: 27 additions & 42 deletions apps/files_trashbin/lib/Command/ExpireTrash.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,14 @@
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 ?IUserManager $userManager = null,
private ?Expiration $expiration = null,
) {
parent::__construct();

$this->userManager = $userManager;
$this->expiration = $expiration;
}

protected function configure() {
protected function configure(): void {
$this
->setName('trashbin:expire')
->setDescription('Expires the users trashbin')
Expand All @@ -75,35 +59,38 @@
$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)");
return 1;
return self::FAILURE;
}

$users = $input->getArgument('user_id');
if (!empty($users)) {
foreach ($users as $user) {
if ($this->userManager->userExists($user)) {
$output->writeln("Remove deleted files of <info>$user</info>");
$userObject = $this->userManager->get($user);
$this->expireTrashForUser($userObject);
} else {
if (! $this->userManager->userExists($user)) {

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method userExists on possibly null value
$output->writeln("<error>Unknown user $user</error>");
return 1;
return self::FAILURE;
}

$output->writeln("Remove deleted files of <info>$user</info>");
$userObject = $this->userManager->get($user);
$this->expireTrashForUser($userObject);

Check notice

Code scanning / Psalm

PossiblyNullArgument Note

Argument 1 of OCA\Files_Trashbin\Command\ExpireTrash::expireTrashForUser cannot be null, possibly null value provided
}
} else {
$p = new ProgressBar($output);
$p->start();
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
$p->advance();
$this->expireTrashForUser($user);
});
$p->finish();
$output->writeln('');

return self::SUCCESS;
}
return 0;

$p = new ProgressBar($output);
$p->start();
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method callForSeenUsers on possibly null value
$p->advance();
$this->expireTrashForUser($user);
});
$p->finish();
$output->writeln('');

return self::SUCCESS;
}

public function expireTrashForUser(IUser $user) {
public function expireTrashForUser(IUser $user): void {
$uid = $user->getUID();
if (!$this->setupFS($uid)) {
return;
Expand All @@ -114,10 +101,8 @@

/**
* 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);

Expand Down
38 changes: 11 additions & 27 deletions apps/files_trashbin/lib/Command/RestoreAllFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,16 @@ 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;

/**
* @param IRootFolder $rootFolder
* @param IUserManager $userManager
* @param IDBConnection $dbConnection
* @param ITrashManager $trashManager
* @param IFactory $l10nFactory
*/
public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, ITrashManager $trashManager, IFactory $l10nFactory) {
protected IL10N $l10n;

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');
}

Expand Down Expand Up @@ -153,7 +136,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} else {
throw new InvalidOptionException('Either specify a user_id or --all-users');
}
return 0;

return self::SUCCESS;
}

/**
Expand Down
Loading
Loading