aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib/Command/RestoreAllFiles.php
blob: 172e1af385b039def4245695eaf8ee3bace26dc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
 * @copyright Copyright (c) 2021, Caitlin Hogan (cahogan16@gmail.com)
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace OCA\Files_Trashbin\Command;

use OC\Core\Command\Base;
use OCA\Files_Trashbin\Trash\ITrashItem;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\Files\IRootFolder;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RestoreAllFiles extends Base {

	private const SCOPE_ALL = 0;
	private const SCOPE_USER = 1;
	private const SCOPE_GROUPFOLDERS = 2;

	private static $SCOPE_MAP = [
		'user' => self::SCOPE_USER,
		'groupfolders' => self::SCOPE_GROUPFOLDERS,
		'all' => self::SCOPE_ALL
	];

	/** @var IUserManager */
	protected $userManager;

	/** @var IRootFolder */
	protected $rootFolder;

	/** @var \OCP\IDBConnection */
	protected $dbConnection;

	/** @var ITrashManager */
	protected $trashManager;

	/** @var IL10N */
	protected $l10n;

	/**
	 * @param IRootFolder $rootFolder
	 * @param IUserManager $userManager
	 * @param IDBConnection $dbConnection
	 * @param ITrashManager $trashManager
	 * @param IFactory $l10nFactory
	 */
	public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection, ITrashManager $trashManager, IFactory $l10nFactory) {
		parent::__construct();
		$this->userManager = $userManager;
		$this->rootFolder = $rootFolder;
		$this->dbConnection = $dbConnection;
		$this->trashManager = $trashManager;
		$this->l10n = $l10nFactory->get('files_trashbin');
	}

	protected function configure(): void {
		parent::configure();
		$this
			->setName('trashbin:restore')
			->setDescription('Restore all deleted files according to the given filters')
			->addArgument(
				'user_id',
				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
				'restore all deleted files of the given user(s)'
			)
			->addOption(
				'all-users',
				null,
				InputOption::VALUE_NONE,
				'run action on all users'
			)
			->addOption(
				'scope',
				's',
				InputOption::VALUE_OPTIONAL,
				'Restore files from the given scope. Possible values are "user", "groupfolders" or "all"',
				'user'
			)
			->addOption(
				'restore-from',
				'f',
				InputOption::VALUE_OPTIONAL,
				'Only restore files deleted after the given timestamp'
			)
			->addOption(
				'restore-to',
				't',
				InputOption::VALUE_OPTIONAL,
				'Only restore files deleted before the given timestamp'
			)
			->addOption(
				'dry-run',
				'd',
				InputOption::VALUE_NONE,
				'Only show which files would be restored but do not perform any action'
			);
	}

	protected function execute(InputInterface $input, OutputInterface $output): int {
		/** @var string[] $users */
		$users = $input->getArgument('user_id');
		if ((!empty($users)) and ($input->getOption('all-users'))) {
			throw new InvalidOptionException('Either specify a user_id or --all-users');
		}

		[$scope, $restoreFrom, $restoreTo, $dryRun] = $this->parseArgs($input);

		if (!empty($users)) {
			foreach ($users as $user) {
				$output->writeln("Restoring deleted files for user <info>$user</info>");
				$this->restoreDeletedFiles($user, $scope, $restoreFrom, $restoreTo, $dryRun, $output);
			}
		} elseif ($input->getOption('all-users')) {
			$output->writeln('Restoring deleted files for all users');
			foreach ($this->userManager->getBackends() as $backend) {
				$name = get_class($backend);
				if ($backend instanceof IUserBackend) {
					$name = $backend->getBackendName();
				}
				$output->writeln("Restoring deleted files for users on backend <info>$name</info>");
				$limit = 500;
				$offset = 0;
				do {
					$users = $backend->getUsers('', $limit, $offset);
					foreach ($users as $user) {
						$output->writeln("<info>$user</info>");
						$this->restoreDeletedFiles($user, $scope, $restoreFrom, $restoreTo, $dryRun, $output);
					}
					$offset += $limit;
				} while (count($users) >= $limit);
			}
		} else {
			throw new InvalidOptionException('Either specify a user_id or --all-users');
		}
		return 0;
	}

	/**
	 * Restore deleted files for the given user
	 *
	 * @param string $uid
	 * @param int $scope
	 * @param int|null $restoreFrom
	 * @param int|null $restoreTo
	 * @param bool $dryRun
	 * @param OutputInterface $output
	 */
	protected function restoreDeletedFiles(string $uid, int $scope, ?int $restoreFrom, ?int $restoreTo, bool $dryRun, OutputInterface $output): void {
		\OC_Util::tearDownFS();
		\OC_Util::setupFS($uid);
		\OC_User::setUserId($uid);

		$user = $this->userManager->get($uid);

		if ($user === null) {
			$output->writeln("<error>Unknown user $uid</error>");
			return;
		}

		$userTrashItems = $this->filterTrashItems(
			$this->trashManager->listTrashRoot($user),
			$scope,
			$restoreFrom,
			$restoreTo,
			$output);

		$trashCount = count($userTrashItems);
		if ($trashCount == 0) {
			$output->writeln("User has no deleted files in the trashbin");
			return;
		}
		$prepMsg = $dryRun ? 'Would restore' : 'Preparing to restore';
		$output->writeln("$prepMsg <info>$trashCount</info> files...");
		$count = 0;
		foreach($userTrashItems as $trashItem) {
			$filename = $trashItem->getName();
			$humanTime = $this->l10n->l('datetime', $trashItem->getDeletedTime());
			// We use getTitle() here instead of getOriginalLocation() because
			// for groupfolders this contains the groupfolder name itself as prefix
			// which makes it more human readable
			$location = $trashItem->getTitle();

			if ($dryRun) {
				$output->writeln("Would restore <info>$filename</info> originally deleted at <info>$humanTime</info> to <info>/$location</info>");
				continue;
			}

			$output->write("File <info>$filename</info> originally deleted at <info>$humanTime</info> restoring to <info>/$location</info>:");

			try {
				$trashItem->getTrashBackend()->restoreItem($trashItem);
			} catch (\Throwable $e) {
				$output->writeln(" <error>failed</error>");
				$output->writeln("<error>" . $e->getMessage() . "</error>");
				$output->writeln("<error>" . $e->getTraceAsString() . "</error>", OutputInterface::VERBOSITY_VERY_VERBOSE);
				continue;
			}

			$count = $count + 1;
			$output->writeln(" <info>success</info>");
		}

		if (!$dryRun) {
			$output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files.");
		}
	}

	protected function parseArgs(InputInterface $input): array {
		$restoreFrom = $this->parseTimestamp($input->getOption('restore-from'));
		$restoreTo = $this->parseTimestamp($input->getOption('restore-to'));

		if ($restoreFrom !== null and $restoreTo !== null and $restoreFrom > $restoreTo) {
			throw new InvalidOptionException('restore-from must be before restore-to');
		}

		return [
			$this->parseScope($input->getOption('scope')),
			$restoreFrom,
			$restoreTo,
			$input->getOption('dry-run')
		];
	}

	/**
	 * @param string $scope
	 * @return int
	 */
	protected function parseScope(string $scope): int {
		if (isset(self::$SCOPE_MAP[$scope])) {
			return self::$SCOPE_MAP[$scope];
		}

		throw new InvalidOptionException("Invalid scope '$scope'");
	}

	/**
	 * @param string|null $timestamp
	 * @return int|null
	 */
	protected function parseTimestamp(?string $timestamp): ?int {
		if ($timestamp === null) {
			return null;
		}
		$timestamp = strtotime($timestamp);
		if ($timestamp === false) {
			throw new InvalidOptionException("Invalid timestamp '$timestamp'");
		}
		return $timestamp;
	}

	/**
	 * @param ITrashItem[] $trashItems
	 * @param int $scope
	 * @param int|null $restoreFrom
	 * @param int|null $restoreTo
	 * @param OutputInterface $output
	 * @return ITrashItem[]
	 */
	protected function filterTrashItems(array $trashItems, int $scope, ?int $restoreFrom, ?int $restoreTo, OutputInterface $output): array {
		$filteredTrashItems = [];
		foreach ($trashItems as $trashItem) {
			$trashItemClass = get_class($trashItem);

			// Check scope with exact class name for locally deleted files
			if ($scope === self::SCOPE_USER && $trashItemClass !== \OCA\Files_Trashbin\Trash\TrashItem::class) {
				$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it is not a user trash item", OutputInterface::VERBOSITY_VERBOSE);
				continue;
			}

			// Check scope for groupfolders by string because the groupfolders app might not be installed
			if ($scope === self::SCOPE_GROUPFOLDERS && $trashItemClass !== 'OCA\GroupFolders\Trash\GroupTrashItem') {
				$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it is not a groupfolders trash item", OutputInterface::VERBOSITY_VERBOSE);
				continue;
			}

			// Check left timestamp boundary
			if ($restoreFrom !== null && $trashItem->getDeletedTime() <= $restoreFrom) {
				$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it was deleted before the restore-from timestamp", OutputInterface::VERBOSITY_VERBOSE);
				continue;
			}

			// Check right timestamp boundary
			if ($restoreTo !== null && $trashItem->getDeletedTime() >= $restoreTo) {
				$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it was deleted after the restore-to timestamp", OutputInterface::VERBOSITY_VERBOSE);
				continue;
			}

			$filteredTrashItems[] = $trashItem;
		}
		return $filteredTrashItems;
	}
}