RestoreAllFiles.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, Caitlin Hogan (cahogan16@gmail.com)
  4. * @license AGPL-3.0
  5. *
  6. * This code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, version 3,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License, version 3,
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>
  17. *
  18. */
  19. namespace OCA\Files_Trashbin\Command;
  20. use OC\Core\Command\Base;
  21. use OCP\Files\IRootFolder;
  22. use OCP\IDBConnection;
  23. use OCP\IL10N;
  24. use OCP\IUserBackend;
  25. use OCA\Files_Trashbin\Trashbin;
  26. use OCA\Files_Trashbin\Helper;
  27. use OCP\IUserManager;
  28. use OCP\L10N\IFactory;
  29. use Symfony\Component\Console\Exception\InvalidOptionException;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class RestoreAllFiles extends Base {
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var IRootFolder */
  38. protected $rootFolder;
  39. /** @var \OCP\IDBConnection */
  40. protected $dbConnection;
  41. /** @var IL10N */
  42. protected $l10n;
  43. /**
  44. * @param IRootFolder $rootFolder
  45. * @param IUserManager $userManager
  46. * @param IDBConnection $dbConnection
  47. */
  48. public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, IFactory $l10nFactory) {
  49. parent::__construct();
  50. $this->userManager = $userManager;
  51. $this->rootFolder = $rootFolder;
  52. $this->dbConnection = $dbConnection;
  53. $this->l10n = $l10nFactory->get('files_trashbin');
  54. }
  55. protected function configure(): void {
  56. parent::configure();
  57. $this
  58. ->setName('trashbin:restore')
  59. ->setDescription('Restore all deleted files')
  60. ->addArgument(
  61. 'user_id',
  62. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  63. 'restore all deleted files of the given user(s)'
  64. )
  65. ->addOption(
  66. 'all-users',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'run action on all users'
  70. );
  71. }
  72. protected function execute(InputInterface $input, OutputInterface $output): int {
  73. /** @var string[] $users */
  74. $users = $input->getArgument('user_id');
  75. if ((!empty($users)) and ($input->getOption('all-users'))) {
  76. throw new InvalidOptionException('Either specify a user_id or --all-users');
  77. } elseif (!empty($users)) {
  78. foreach ($users as $user) {
  79. if ($this->userManager->userExists($user)) {
  80. $output->writeln("Restoring deleted files for user <info>$user</info>");
  81. $this->restoreDeletedFiles($user, $output);
  82. } else {
  83. $output->writeln("<error>Unknown user $user</error>");
  84. return 1;
  85. }
  86. }
  87. } elseif ($input->getOption('all-users')) {
  88. $output->writeln('Restoring deleted files for all users');
  89. foreach ($this->userManager->getBackends() as $backend) {
  90. $name = get_class($backend);
  91. if ($backend instanceof IUserBackend) {
  92. $name = $backend->getBackendName();
  93. }
  94. $output->writeln("Restoring deleted files for users on backend <info>$name</info>");
  95. $limit = 500;
  96. $offset = 0;
  97. do {
  98. $users = $backend->getUsers('', $limit, $offset);
  99. foreach ($users as $user) {
  100. $output->writeln("<info>$user</info>");
  101. $this->restoreDeletedFiles($user, $output);
  102. }
  103. $offset += $limit;
  104. } while (count($users) >= $limit);
  105. }
  106. } else {
  107. throw new InvalidOptionException('Either specify a user_id or --all-users');
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Restore deleted files for the given user
  113. *
  114. * @param string $uid
  115. * @param OutputInterface $output
  116. */
  117. protected function restoreDeletedFiles(string $uid, OutputInterface $output): void {
  118. \OC_Util::tearDownFS();
  119. \OC_Util::setupFS($uid);
  120. \OC_User::setUserId($uid);
  121. // Sort by most recently deleted first
  122. // (Restoring in order of most recently deleted preserves nested file paths.
  123. // See https://github.com/nextcloud/server/issues/31200#issuecomment-1130358549)
  124. $filesInTrash = Helper::getTrashFiles('/', $uid, 'mtime', true);
  125. $trashCount = count($filesInTrash);
  126. if ($trashCount == 0) {
  127. $output->writeln("User has no deleted files in the trashbin");
  128. return;
  129. }
  130. $output->writeln("Preparing to restore <info>$trashCount</info> files...");
  131. $count = 0;
  132. foreach ($filesInTrash as $trashFile) {
  133. $filename = $trashFile->getName();
  134. $timestamp = $trashFile->getMtime();
  135. $humanTime = $this->l10n->l('datetime', $timestamp);
  136. $output->write("File <info>$filename</info> originally deleted at <info>$humanTime</info> ");
  137. $file = Trashbin::getTrashFilename($filename, $timestamp);
  138. $location = Trashbin::getLocation($uid, $filename, (string) $timestamp);
  139. if ($location === '.') {
  140. $location = '';
  141. }
  142. $output->write("restoring to <info>/$location</info>:");
  143. if (Trashbin::restore($file, $filename, $timestamp)) {
  144. $count = $count + 1;
  145. $output->writeln(" <info>success</info>");
  146. } else {
  147. $output->writeln(" <error>failed</error>");
  148. }
  149. }
  150. $output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files.");
  151. }
  152. }