setName('trashbin:expire')
->setDescription('Expires the users trashbin')
->addArgument(
'user_id',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'expires the trashbin of the given user(s), if no user is given the trash for all users will be expired'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$minAge = $this->expiration->getMinAgeAsTimestamp();
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
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;
}
$userIds = $input->getArgument('user_id');
if (!empty($userIds)) {
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
if ($user) {
$output->writeln("Remove deleted files of $userId");
$this->expireTrashForUser($user, $output);
$output->writeln("Unknown user $userId");
return 1;
} else {
$output->writeln("Unknown user $userId");
return 1;
}
}
} else {
$p = new ProgressBar($output);
$p->start();
$users = $this->userManager->getSeenUsers();
foreach ($users as $user) {
$p->advance();
$this->expireTrashForUser($user, $output);
}
$p->finish();
$output->writeln('');
}
return 0;
}
private function expireTrashForUser(IUser $user, OutputInterface $output): void {
try {
$trashRoot = $this->getTrashRoot($user);
Trashbin::expire($trashRoot, $user);
} catch (\Throwable $e) {
$output->writeln('Error while expiring trashbin for user ' . $user->getUID() . '');
throw $e;
} finally {
$this->setupManager->tearDown();
}
}
private function getTrashRoot(IUser $user): Folder {
$this->setupManager->setupForUser($user);
$folder = $this->rootFolder->getUserFolder($user->getUID())->getParent()->get('files_trashbin');
if (!$folder instanceof Folder) {
throw new \LogicException("Didn't expect files_trashbin to be a file instead of a folder");
}
return $folder;
}
}