CleanUp.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Liam Dennehy <liam@wiemax.net>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OCP\Files\IRootFolder;
  28. use OCP\IDBConnection;
  29. use OCP\IUserBackend;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Exception\InvalidOptionException;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class CleanUp extends Command {
  38. /** @var IUserManager */
  39. protected $userManager;
  40. /** @var IRootFolder */
  41. protected $rootFolder;
  42. /** @var \OCP\IDBConnection */
  43. protected $dbConnection;
  44. /**
  45. * @param IRootFolder $rootFolder
  46. * @param IUserManager $userManager
  47. * @param IDBConnection $dbConnection
  48. */
  49. public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) {
  50. parent::__construct();
  51. $this->userManager = $userManager;
  52. $this->rootFolder = $rootFolder;
  53. $this->dbConnection = $dbConnection;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('trashbin:cleanup')
  58. ->setDescription('Remove deleted files')
  59. ->addArgument(
  60. 'user_id',
  61. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  62. 'remove deleted files of the given user(s)'
  63. )
  64. ->addOption(
  65. 'all-users',
  66. null,
  67. InputOption::VALUE_NONE,
  68. 'run action on all users'
  69. );
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  72. $users = $input->getArgument('user_id');
  73. $verbose = $input->getOption('verbose');
  74. if ((!empty($users)) and ($input->getOption('all-users'))) {
  75. throw new InvalidOptionException('Either specify a user_id or --all-users');
  76. } elseif (!empty($users)) {
  77. foreach ($users as $user) {
  78. if ($this->userManager->userExists($user)) {
  79. $output->writeln("Remove deleted files of <info>$user</info>");
  80. $this->removeDeletedFiles($user, $output, $verbose);
  81. } else {
  82. $output->writeln("<error>Unknown user $user</error>");
  83. return 1;
  84. }
  85. }
  86. } elseif ($input->getOption('all-users')) {
  87. $output->writeln('Remove deleted files for all users');
  88. foreach ($this->userManager->getBackends() as $backend) {
  89. $name = get_class($backend);
  90. if ($backend instanceof IUserBackend) {
  91. $name = $backend->getBackendName();
  92. }
  93. $output->writeln("Remove deleted files for users on backend <info>$name</info>");
  94. $limit = 500;
  95. $offset = 0;
  96. do {
  97. $users = $backend->getUsers('', $limit, $offset);
  98. foreach ($users as $user) {
  99. $output->writeln(" <info>$user</info>");
  100. $this->removeDeletedFiles($user, $output, $verbose);
  101. }
  102. $offset += $limit;
  103. } while (count($users) >= $limit);
  104. }
  105. } else {
  106. throw new InvalidOptionException('Either specify a user_id or --all-users');
  107. }
  108. return 0;
  109. }
  110. /**
  111. * remove deleted files for the given user
  112. */
  113. protected function removeDeletedFiles(string $uid, OutputInterface $output, bool $verbose): void {
  114. \OC_Util::tearDownFS();
  115. \OC_Util::setupFS($uid);
  116. $path = '/' . $uid . '/files_trashbin';
  117. if ($this->rootFolder->nodeExists($path)) {
  118. $node = $this->rootFolder->get($path);
  119. if ($verbose) {
  120. $output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
  121. }
  122. $node->delete();
  123. if ($this->rootFolder->nodeExists($path)) {
  124. $output->writeln("<error>Trash folder sill exists after attempting to delete it</error>");
  125. return;
  126. }
  127. $query = $this->dbConnection->getQueryBuilder();
  128. $query->delete('files_trash')
  129. ->where($query->expr()->eq('user', $query->createParameter('uid')))
  130. ->setParameter('uid', $uid);
  131. $query->execute();
  132. } else {
  133. if ($verbose) {
  134. $output->writeln("No trash found for <info>$uid</info>");
  135. }
  136. }
  137. }
  138. }