diff --git a/apps/files_trashbin/lib/Command/CleanUp.php b/apps/files_trashbin/lib/Command/CleanUp.php index db0e528fdea3b..50e51e70ac924 100644 --- a/apps/files_trashbin/lib/Command/CleanUp.php +++ b/apps/files_trashbin/lib/Command/CleanUp.php @@ -65,40 +65,48 @@ protected function configure(): void { 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 $user"); - $this->removeDeletedFiles($user, $output, $verbose); - } else { + if (!$this->userManager->userExists($user)) { $output->writeln("Unknown user $user"); return self::FAILURE; } + + $output->writeln("Remove deleted files of $user"); + $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 $name"); - $limit = 500; - $offset = 0; - do { - $users = $backend->getUsers('', $limit, $offset); - foreach ($users as $user) { - $output->writeln(" $user"); - $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 $name"); + $limit = 500; + $offset = 0; + do { + $users = $backend->getUsers('', $limit, $offset); + foreach ($users as $user) { + $output->writeln(" $user"); + $this->removeDeletedFiles($user, $output, $verbose); + } + $offset += $limit; + } while (count($users) >= $limit); } + return self::SUCCESS; } @@ -109,26 +117,29 @@ protected function removeDeletedFiles(string $uid, OutputInterface $output, bool \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 " . \OC_Helper::humanFileSize($node->getSize()) . " in trash for $uid."); - } - $node->delete(); - if ($this->rootFolder->nodeExists($path)) { - $output->writeln("Trash folder sill exists after attempting to delete it"); - 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 $uid"); } + + return; + } + + $node = $this->rootFolder->get($path); + + if ($verbose) { + $output->writeln("Deleting " . \OC_Helper::humanFileSize($node->getSize()) . " in trash for $uid."); + } + $node->delete(); + if ($this->rootFolder->nodeExists($path)) { + $output->writeln("Trash folder sill exists after attempting to delete it"); + return; } + $query = $this->dbConnection->getQueryBuilder(); + $query->delete('files_trash') + ->where($query->expr()->eq('user', $query->createParameter('uid'))) + ->setParameter('uid', $uid); + $query->execute(); } } diff --git a/apps/files_trashbin/lib/Command/ExpireTrash.php b/apps/files_trashbin/lib/Command/ExpireTrash.php index 6794dc54627fe..a64e1e9b837fd 100644 --- a/apps/files_trashbin/lib/Command/ExpireTrash.php +++ b/apps/files_trashbin/lib/Command/ExpireTrash.php @@ -65,25 +65,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int $users = $input->getArgument('user_id'); if (!empty($users)) { foreach ($users as $user) { - if ($this->userManager->userExists($user)) { - $output->writeln("Remove deleted files of $user"); - $userObject = $this->userManager->get($user); - $this->expireTrashForUser($userObject); - } else { + if (! $this->userManager->userExists($user)) { $output->writeln("Unknown user $user"); return self::FAILURE; } + + $output->writeln("Remove deleted files of $user"); + $userObject = $this->userManager->get($user); + $this->expireTrashForUser($userObject); } - } 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; } + + $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; } diff --git a/apps/files_trashbin/lib/Command/RestoreAllFiles.php b/apps/files_trashbin/lib/Command/RestoreAllFiles.php index 129891bb442a5..62f8644d3e84f 100644 --- a/apps/files_trashbin/lib/Command/RestoreAllFiles.php +++ b/apps/files_trashbin/lib/Command/RestoreAllFiles.php @@ -67,40 +67,48 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { /** @var string[] $users */ $users = $input->getArgument('user_id'); + + 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("Restoring deleted files for user $user"); - $this->restoreDeletedFiles($user, $output); - } else { + if (!$this->userManager->userExists($user)) { $output->writeln("Unknown user $user"); return self::FAILURE; } + + $output->writeln("Restoring deleted files for user $user"); + $this->restoreDeletedFiles($user, $output); } - } elseif ($input->getOption('all-users')) { - $output->writeln('Restoring deleted files for all users'); - foreach ($this->userManager->getBackends() as $backend) { - $name = get_class($backend); - if ($backend instanceof IUserBackend) { - $name = $backend->getBackendName(); - } - $output->writeln("Restoring deleted files for users on backend $name"); - $limit = 500; - $offset = 0; - do { - $users = $backend->getUsers('', $limit, $offset); - foreach ($users as $user) { - $output->writeln("$user"); - $this->restoreDeletedFiles($user, $output); - } - $offset += $limit; - } while (count($users) >= $limit); + + return self::SUCCESS; + } + + $output->writeln('Restoring 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("Restoring deleted files for users on backend $name"); + $limit = 500; + $offset = 0; + do { + $users = $backend->getUsers('', $limit, $offset); + foreach ($users as $user) { + $output->writeln("$user"); + $this->restoreDeletedFiles($user, $output); + } + $offset += $limit; + } while (count($users) >= $limit); } + return self::SUCCESS; } diff --git a/apps/files_trashbin/lib/Command/Size.php b/apps/files_trashbin/lib/Command/Size.php index 511caa98bbc98..af682fa2892aa 100644 --- a/apps/files_trashbin/lib/Command/Size.php +++ b/apps/files_trashbin/lib/Command/Size.php @@ -61,24 +61,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int $user = $input->getOption('user'); $size = $input->getArgument('size'); - if ($size) { - $parsedSize = \OC_Helper::computerFileSize($size); - if ($parsedSize === false) { - $output->writeln("Failed to parse input size"); - return self::FAILURE; - } - if ($user) { - $this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize); - $this->commandBus->push(new Expire($user)); - } else { - $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize); - $output->writeln("Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,"); - $output->writeln("a users trashbin can exceed the configured size until they move a new file to the trashbin."); - } - } else { + if (!$size) { $this->printTrashbinSize($input, $output, $user); + return self::SUCCESS; + } + + $parsedSize = \OC_Helper::computerFileSize($size); + if ($parsedSize === false) { + $output->writeln("Failed to parse input size"); + return self::FAILURE; } + if ($user) { + $this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize); + $this->commandBus->push(new Expire($user)); + return self::SUCCESS; + } + + $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize); + $output->writeln("Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,"); + $output->writeln("a users trashbin can exceed the configured size until they move a new file to the trashbin."); + return self::SUCCESS; } @@ -101,40 +104,45 @@ private function printTrashbinSize(InputInterface $input, OutputInterface $outpu if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { $output->writeln($userHumanSize); - } else { - $userValue = ($userSize < 0) ? 'default' : $userSize; - $globalValue = ($globalSize < 0) ? 'default' : $globalSize; - $this->writeArrayInOutputFormat($input, $output, [ - 'user_size' => $userValue, - 'global_size' => $globalValue, - 'effective_size' => ($userSize < 0) ? $globalValue : $userValue, - ]); + return; } - } else { - $users = []; - $this->userManager->callForSeenUsers(function (IUser $user) use (&$users) { - $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(""); - if (count($userValues)) { - $output->writeln("Per-user sizes:"); - $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) { - return \OC_Helper::humanFileSize($size); - }, $userValues)); - } else { - $output->writeln("No per-user sizes configured"); - } - } else { - $globalValue = ($globalSize < 0) ? 'default' : $globalSize; - $this->writeArrayInOutputFormat($input, $output, [ - 'global_size' => $globalValue, - 'user_sizes' => $userValues, - ]); + $userValue = ($userSize < 0) ? 'default' : $userSize; + $globalValue = ($globalSize < 0) ? 'default' : $globalSize; + $this->writeArrayInOutputFormat($input, $output, [ + 'user_size' => $userValue, + 'global_size' => $globalValue, + 'effective_size' => ($userSize < 0) ? $globalValue : $userValue, + ]); + return; + } + + $users = []; + $this->userManager->callForSeenUsers(function (IUser $user) use (&$users) { + $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(""); + + if (!count($userValues)) { + $output->writeln("No per-user sizes configured"); + return; } + + $output->writeln("Per-user sizes:"); + $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) { + return \OC_Helper::humanFileSize($size); + }, $userValues)); + return; } + + $globalValue = ($globalSize < 0) ? 'default' : $globalSize; + $this->writeArrayInOutputFormat($input, $output, [ + 'global_size' => $globalValue, + 'user_sizes' => $userValues, + ]); } }