ExpireTrash.php 3.4 KB

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