|
| 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 | + |
| 21 | +use OC\Core\Command\Base; |
| 22 | +use OCP\Files\IRootFolder; |
| 23 | +use OCP\IDBConnection; |
| 24 | +use OCP\IL10N; |
| 25 | +use OCP\IUserBackend; |
| 26 | +use OCA\Files_Trashbin\Trashbin; |
| 27 | +use OCA\Files_Trashbin\Helper; |
| 28 | +use OCP\IUserManager; |
| 29 | +use OCP\L10N\IFactory; |
| 30 | +use Symfony\Component\Console\Exception\InvalidOptionException; |
| 31 | +use Symfony\Component\Console\Input\InputArgument; |
| 32 | +use Symfony\Component\Console\Input\InputInterface; |
| 33 | +use Symfony\Component\Console\Input\InputOption; |
| 34 | +use Symfony\Component\Console\Output\OutputInterface; |
| 35 | + |
| 36 | +class RestoreAllFiles extends Base { |
| 37 | + |
| 38 | + /** @var IUserManager */ |
| 39 | + protected $userManager; |
| 40 | + |
| 41 | + /** @var IRootFolder */ |
| 42 | + protected $rootFolder; |
| 43 | + |
| 44 | + /** @var \OCP\IDBConnection */ |
| 45 | + protected $dbConnection; |
| 46 | + |
| 47 | + /** @var IL10N */ |
| 48 | + protected $l10n; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param IRootFolder $rootFolder |
| 52 | + * @param IUserManager $userManager |
| 53 | + * @param IDBConnection $dbConnection |
| 54 | + */ |
| 55 | + public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, IFactory $l10nFactory) { |
| 56 | + parent::__construct(); |
| 57 | + $this->userManager = $userManager; |
| 58 | + $this->rootFolder = $rootFolder; |
| 59 | + $this->dbConnection = $dbConnection; |
| 60 | + $this->l10n = $l10nFactory->get('files_trashbin'); |
| 61 | + } |
| 62 | + |
| 63 | + protected function configure(): void { |
| 64 | + parent::configure(); |
| 65 | + $this |
| 66 | + ->setName('trashbin:restore') |
| 67 | + ->setDescription('Restore all deleted files') |
| 68 | + ->addArgument( |
| 69 | + 'user_id', |
| 70 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
| 71 | + 'restore all deleted files of the given user(s)' |
| 72 | + ) |
| 73 | + ->addOption( |
| 74 | + 'all-users', |
| 75 | + null, |
| 76 | + InputOption::VALUE_NONE, |
| 77 | + 'run action on all users' |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 82 | + /** @var string[] $users */ |
| 83 | + $users = $input->getArgument('user_id'); |
| 84 | + if ((!empty($users)) and ($input->getOption('all-users'))) { |
| 85 | + throw new InvalidOptionException('Either specify a user_id or --all-users'); |
| 86 | + } elseif (!empty($users)) { |
| 87 | + foreach ($users as $user) { |
| 88 | + if ($this->userManager->userExists($user)) { |
| 89 | + $output->writeln("Restoring deleted files for user <info>$user</info>"); |
| 90 | + $this->restoreDeletedFiles($user, $output); |
| 91 | + } else { |
| 92 | + $output->writeln("<error>Unknown user $user</error>"); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + } |
| 96 | + } elseif ($input->getOption('all-users')) { |
| 97 | + $output->writeln('Restoring deleted files for all users'); |
| 98 | + foreach ($this->userManager->getBackends() as $backend) { |
| 99 | + $name = get_class($backend); |
| 100 | + if ($backend instanceof IUserBackend) { |
| 101 | + $name = $backend->getBackendName(); |
| 102 | + } |
| 103 | + $output->writeln("Restoring deleted files for users on backend <info>$name</info>"); |
| 104 | + $limit = 500; |
| 105 | + $offset = 0; |
| 106 | + do { |
| 107 | + $users = $backend->getUsers('', $limit, $offset); |
| 108 | + foreach ($users as $user) { |
| 109 | + $output->writeln("<info>$user</info>"); |
| 110 | + $this->restoreDeletedFiles($user, $output); |
| 111 | + } |
| 112 | + $offset += $limit; |
| 113 | + } while (count($users) >= $limit); |
| 114 | + } |
| 115 | + } else { |
| 116 | + throw new InvalidOptionException('Either specify a user_id or --all-users'); |
| 117 | + } |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Restore deleted files for the given user |
| 123 | + * |
| 124 | + * @param string $uid |
| 125 | + * @param OutputInterface $output |
| 126 | + */ |
| 127 | + protected function restoreDeletedFiles(string $uid, OutputInterface $output): void { |
| 128 | + \OC_Util::tearDownFS(); |
| 129 | + \OC_Util::setupFS($uid); |
| 130 | + \OC_User::setUserId($uid); |
| 131 | + |
| 132 | + $filesInTrash = Helper::getTrashFiles('/', $uid, 'mtime'); |
| 133 | + $trashCount = count($filesInTrash); |
| 134 | + if ($trashCount == 0) { |
| 135 | + $output->writeln("User has no deleted files in the trashbin"); |
| 136 | + return; |
| 137 | + } |
| 138 | + $output->writeln("Preparing to restore <info>$trashCount</info> files..."); |
| 139 | + $count = 0; |
| 140 | + foreach ($filesInTrash as $trashFile) { |
| 141 | + $filename = $trashFile->getName(); |
| 142 | + $timestamp = $trashFile->getMtime(); |
| 143 | + $humanTime = $this->l10n->l('datetime', $timestamp); |
| 144 | + $output->write("File <info>$filename</info> originally deleted at <info>$humanTime</info> "); |
| 145 | + $file = $filename . '.d' . $timestamp; |
| 146 | + $location = Trashbin::getLocation($uid, $filename, (string) $timestamp); |
| 147 | + if ($location === '.') { |
| 148 | + $location = ''; |
| 149 | + } |
| 150 | + $output->write("restoring to <info>/$location</info>:"); |
| 151 | + if (Trashbin::restore($file, $filename, $timestamp)) { |
| 152 | + $count = $count + 1; |
| 153 | + $output->writeln(" <info>success</info>"); |
| 154 | + } else { |
| 155 | + $output->writeln(" <error>failed</error>"); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + $output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files."); |
| 160 | + } |
| 161 | +} |
0 commit comments