ExpireTrash.php 3.4 KB

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