ExpireTrash.php 3.0 KB

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