Skip to content

Commit b88dc5f

Browse files
authored
Merge pull request #28935 from cahogan/trashbin-restore
Add trashbin:restore occ command
2 parents ef9890f + 90cfa31 commit b88dc5f

5 files changed

Lines changed: 166 additions & 2 deletions

File tree

apps/files_trashbin/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ To prevent a user from running out of disk space, the Deleted files app will not
3535
<command>OCA\Files_Trashbin\Command\CleanUp</command>
3636
<command>OCA\Files_Trashbin\Command\ExpireTrash</command>
3737
<command>OCA\Files_Trashbin\Command\Size</command>
38+
<command>OCA\Files_Trashbin\Command\RestoreAllFiles</command>
3839
</commands>
3940

4041
<sabre>

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'OCA\\Files_Trashbin\\Command\\CleanUp' => $baseDir . '/../lib/Command/CleanUp.php',
1414
'OCA\\Files_Trashbin\\Command\\Expire' => $baseDir . '/../lib/Command/Expire.php',
1515
'OCA\\Files_Trashbin\\Command\\ExpireTrash' => $baseDir . '/../lib/Command/ExpireTrash.php',
16+
'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => $baseDir . '/../lib/Command/RestoreAllFiles.php',
1617
'OCA\\Files_Trashbin\\Command\\Size' => $baseDir . '/../lib/Command/Size.php',
1718
'OCA\\Files_Trashbin\\Controller\\PreviewController' => $baseDir . '/../lib/Controller/PreviewController.php',
1819
'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => $baseDir . '/../lib/Events/MoveToTrashEvent.php',

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ComposerStaticInitFiles_Trashbin
2828
'OCA\\Files_Trashbin\\Command\\CleanUp' => __DIR__ . '/..' . '/../lib/Command/CleanUp.php',
2929
'OCA\\Files_Trashbin\\Command\\Expire' => __DIR__ . '/..' . '/../lib/Command/Expire.php',
3030
'OCA\\Files_Trashbin\\Command\\ExpireTrash' => __DIR__ . '/..' . '/../lib/Command/ExpireTrash.php',
31+
'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => __DIR__ . '/..' . '/../lib/Command/RestoreAllFiles.php',
3132
'OCA\\Files_Trashbin\\Command\\Size' => __DIR__ . '/..' . '/../lib/Command/Size.php',
3233
'OCA\\Files_Trashbin\\Controller\\PreviewController' => __DIR__ . '/..' . '/../lib/Controller/PreviewController.php',
3334
'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => __DIR__ . '/..' . '/../lib/Events/MoveToTrashEvent.php',

apps/files_trashbin/composer/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'type' => 'library',
66
'install_path' => __DIR__ . '/../',
77
'aliases' => array(),
8-
'reference' => 'c6429e6cd19c57582364338362e543580821cf99',
8+
'reference' => 'ff67123569a861301d14f83f7b2310e9a518c46d',
99
'name' => '__root__',
1010
'dev' => false,
1111
),
@@ -16,7 +16,7 @@
1616
'type' => 'library',
1717
'install_path' => __DIR__ . '/../',
1818
'aliases' => array(),
19-
'reference' => 'c6429e6cd19c57582364338362e543580821cf99',
19+
'reference' => 'ff67123569a861301d14f83f7b2310e9a518c46d',
2020
'dev_requirement' => false,
2121
),
2222
),
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)