Skip to content

Commit

Permalink
Uses early returns.
Browse files Browse the repository at this point in the history
To improve code readability.

Signed-off-by: Faraz Samapoor <fsa@adlas.at>
  • Loading branch information
Faraz Samapoor authored and fsamapoor committed Jan 3, 2024
1 parent f6b76af commit e5e9aed
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 101 deletions.
93 changes: 52 additions & 41 deletions apps/files_trashbin/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>$user</info>");
$this->removeDeletedFiles($user, $output, $verbose);
} else {
if (!$this->userManager->userExists($user)) {
$output->writeln("<error>Unknown user $user</error>");
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 self::SUCCESS;
}

Expand All @@ -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 <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
}
}
31 changes: 17 additions & 14 deletions apps/files_trashbin/lib/Command/ExpireTrash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 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;
}

$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;
}

Expand Down
1 change: 1 addition & 0 deletions apps/files_trashbin/lib/Command/RestoreAllFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} else {
throw new InvalidOptionException('Either specify a user_id or --all-users');
}

return self::SUCCESS;
}

Expand Down
100 changes: 54 additions & 46 deletions apps/files_trashbin/lib/Command/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<error>Failed to parse input size</error>");
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("<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 {
if (!$size) {
$this->printTrashbinSize($input, $output, $user);
return self::SUCCESS;
}

$parsedSize = \OC_Helper::computerFileSize($size);
if ($parsedSize === false) {
$output->writeln("<error>Failed to parse input size</error>");
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("<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>");

return self::SUCCESS;
}

Expand All @@ -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,
]);
}
}

0 comments on commit e5e9aed

Please sign in to comment.