1
0

ExpireTrash.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud GmbH.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin\Command;
  8. use OC\Files\View;
  9. use OCA\Files_Trashbin\Expiration;
  10. use OCA\Files_Trashbin\Helper;
  11. use OCA\Files_Trashbin\Trashbin;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Helper\ProgressBar;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. class ExpireTrash extends Command {
  20. /**
  21. * @param IUserManager|null $userManager
  22. * @param Expiration|null $expiration
  23. */
  24. public function __construct(
  25. private ?IUserManager $userManager = null,
  26. private ?Expiration $expiration = null,
  27. ) {
  28. parent::__construct();
  29. }
  30. protected function configure() {
  31. $this
  32. ->setName('trashbin:expire')
  33. ->setDescription('Expires the users trashbin')
  34. ->addArgument(
  35. 'user_id',
  36. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  37. 'expires the trashbin of the given user(s), if no user is given the trash for all users will be expired'
  38. );
  39. }
  40. protected function execute(InputInterface $input, OutputInterface $output): int {
  41. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  42. if (!$maxAge) {
  43. $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)');
  44. return 1;
  45. }
  46. $users = $input->getArgument('user_id');
  47. if (!empty($users)) {
  48. foreach ($users as $user) {
  49. if ($this->userManager->userExists($user)) {
  50. $output->writeln("Remove deleted files of <info>$user</info>");
  51. $userObject = $this->userManager->get($user);
  52. $this->expireTrashForUser($userObject);
  53. } else {
  54. $output->writeln("<error>Unknown user $user</error>");
  55. return 1;
  56. }
  57. }
  58. } else {
  59. $p = new ProgressBar($output);
  60. $p->start();
  61. $this->userManager->callForSeenUsers(function (IUser $user) use ($p): void {
  62. $p->advance();
  63. $this->expireTrashForUser($user);
  64. });
  65. $p->finish();
  66. $output->writeln('');
  67. }
  68. return 0;
  69. }
  70. public function expireTrashForUser(IUser $user) {
  71. $uid = $user->getUID();
  72. if (!$this->setupFS($uid)) {
  73. return;
  74. }
  75. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  76. Trashbin::deleteExpiredFiles($dirContent, $uid);
  77. }
  78. /**
  79. * Act on behalf on trash item owner
  80. * @param string $user
  81. * @return boolean
  82. */
  83. protected function setupFS($user) {
  84. \OC_Util::tearDownFS();
  85. \OC_Util::setupFS($user);
  86. // Check if this user has a trashbin directory
  87. $view = new View('/' . $user);
  88. if (!$view->is_dir('/files_trashbin/files')) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. }