ExpireTrash.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\Command;
  27. use OCA\Files_Trashbin\Expiration;
  28. use OCA\Files_Trashbin\Helper;
  29. use OCA\Files_Trashbin\Trashbin;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Helper\ProgressBar;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class ExpireTrash extends Command {
  38. /**
  39. * @var Expiration
  40. */
  41. private $expiration;
  42. /**
  43. * @var IUserManager
  44. */
  45. private $userManager;
  46. /**
  47. * @param IUserManager|null $userManager
  48. * @param Expiration|null $expiration
  49. */
  50. public function __construct(IUserManager $userManager = null,
  51. Expiration $expiration = null) {
  52. parent::__construct();
  53. $this->userManager = $userManager;
  54. $this->expiration = $expiration;
  55. }
  56. protected function configure() {
  57. $this
  58. ->setName('trashbin:expire')
  59. ->setDescription('Expires the users trashbin')
  60. ->addArgument(
  61. 'user_id',
  62. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  63. 'expires the trashbin of the given user(s), if no user is given the trash for all users will be expired'
  64. );
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  68. if (!$maxAge) {
  69. $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)");
  70. return 1;
  71. }
  72. $users = $input->getArgument('user_id');
  73. if (!empty($users)) {
  74. foreach ($users as $user) {
  75. if ($this->userManager->userExists($user)) {
  76. $output->writeln("Remove deleted files of <info>$user</info>");
  77. $userObject = $this->userManager->get($user);
  78. $this->expireTrashForUser($userObject);
  79. } else {
  80. $output->writeln("<error>Unknown user $user</error>");
  81. return 1;
  82. }
  83. }
  84. } else {
  85. $p = new ProgressBar($output);
  86. $p->start();
  87. $this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
  88. $p->advance();
  89. $this->expireTrashForUser($user);
  90. });
  91. $p->finish();
  92. $output->writeln('');
  93. }
  94. return 0;
  95. }
  96. public function expireTrashForUser(IUser $user) {
  97. $uid = $user->getUID();
  98. if (!$this->setupFS($uid)) {
  99. return;
  100. }
  101. $dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
  102. Trashbin::deleteExpiredFiles($dirContent, $uid);
  103. }
  104. /**
  105. * Act on behalf on trash item owner
  106. * @param string $user
  107. * @return boolean
  108. */
  109. protected function setupFS($user) {
  110. \OC_Util::tearDownFS();
  111. \OC_Util::setupFS($user);
  112. // Check if this user has a trashbin directory
  113. $view = new \OC\Files\View('/' . $user);
  114. if (!$view->is_dir('/files_trashbin/files')) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. }