You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.3 KiB

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