Skip to content

Commit 863fb58

Browse files
committed
Add trashbin migrator to export and import trashbin data
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 04a4562 commit 863fb58

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

apps/files_trashbin/lib/AppInfo/Application.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
use OCP\IServerContainer;
4040

4141
class Application extends App implements IBootstrap {
42+
public const APP_ID = 'files_trashbin';
43+
4244
public function __construct(array $urlParams = []) {
43-
parent::__construct('files_trashbin', $urlParams);
45+
parent::__construct(self::APP_ID, $urlParams);
4446
}
4547

4648
public function register(IRegistrationContext $context): void {
@@ -65,10 +67,10 @@ public function boot(IBootContext $context): void {
6567
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
6668

6769
\OCA\Files\App::getNavigationManager()->add(function () {
68-
$l = \OC::$server->getL10N('files_trashbin');
70+
$l = \OC::$server->getL10N(self::APP_ID);
6971
return [
7072
'id' => 'trashbin',
71-
'appname' => 'files_trashbin',
73+
'appname' => self::APP_ID,
7274
'script' => 'list.php',
7375
'order' => 50,
7476
'name' => $l->t('Deleted files'),
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Côme Chilliet <[email protected]>
7+
*
8+
* @author Côme Chilliet <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
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
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Files_Trashbin\UserMigration;
28+
29+
use OCA\Files_Trashbin\AppInfo\Application;
30+
use OCP\Files\IRootFolder;
31+
use OCP\Files\NotFoundException;
32+
use OCP\IUser;
33+
use OCP\UserMigration\IExportDestination;
34+
use OCP\UserMigration\IImportSource;
35+
use OCP\UserMigration\IMigrator;
36+
use OCP\UserMigration\TMigratorBasicVersionHandling;
37+
use OCP\UserMigration\UserMigrationException;
38+
use Symfony\Component\Console\Output\OutputInterface;
39+
use OCP\IDBConnection;
40+
41+
class TrashbinMigrator implements IMigrator {
42+
43+
use TMigratorBasicVersionHandling;
44+
45+
protected const PATH_FILES = Application::APP_ID.'/files';
46+
protected const PATH_LOCATIONS = Application::APP_ID.'/locations.json';
47+
48+
protected IRootFolder $root;
49+
50+
protected IDBConnection $dbc;
51+
52+
public function __construct(
53+
IRootFolder $rootFolder,
54+
IDBConnection $dbc
55+
) {
56+
$this->root = $rootFolder;
57+
$this->dbc = $dbc;
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
64+
$output->writeln('Exporting trashbin into ' . Application::APP_ID . '');
65+
66+
$uid = $user->getUID();
67+
68+
try {
69+
$trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
70+
$output->writeln("Exporting trashbin…");
71+
if ($exportDestination->copyFolder($trashbinFolder, static::PATH_FILES) === false) {
72+
throw new UserMigrationException("Could not export trashbin.");
73+
}
74+
$originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($uid);
75+
if ($exportDestination->addFileContents(static::PATH_LOCATIONS, json_encode($originalLocations)) === false) {
76+
throw new UserMigrationException("Could not export trashbin.");
77+
}
78+
} catch (NotFoundException $e) {
79+
$output->writeln("No trashbin to export…");
80+
}
81+
}
82+
83+
/**
84+
* {@inheritDoc}
85+
*/
86+
public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
87+
if ($importSource->getMigratorVersion(static::class) === null) {
88+
$output->writeln('No version for ' . static::class . ', skipping import…');
89+
return;
90+
}
91+
92+
$output->writeln('Importing trashbin from ' . Application::APP_ID . '');
93+
94+
$uid = $user->getUID();
95+
96+
if ($importSource->pathExists(Application::APP_ID)) {
97+
try {
98+
$trashbinFolder = $this->root->get('/'.$uid.'/files_trashbin');
99+
} catch (NotFoundException $e) {
100+
$trashbinFolder = $this->root->newFolder('/'.$uid.'/files_trashbin');
101+
}
102+
$output->writeln("Importing trashbin files…");
103+
if ($importSource->copyToFolder($trashbinFolder, static::PATH_FILES) === false) {
104+
throw new UserMigrationException("Could not import trashbin.");
105+
}
106+
$locations = json_decode($importSource->getFileContents(static::PATH_LOCATIONS), true, 512, JSON_THROW_ON_ERROR);
107+
$insert = $this->dbc->getQueryBuilder();
108+
$insert->insert('files_trash')
109+
->values([
110+
'id' => $insert->createParameter('id'),
111+
'timestamp' => $insert->createParameter('timestamp'),
112+
'location' => $insert->createParameter('location'),
113+
'user' => $insert->createNamedParameter($uid),
114+
]);
115+
foreach ($locations as $id => $fileLocations) {
116+
foreach ($fileLocations as $timestamp => $location) {
117+
$insert
118+
->setParameter('id', $id)
119+
->setParameter('timestamp', $timestamp)
120+
->setParameter('location', $location)
121+
;
122+
123+
$insert->executeStatement();
124+
}
125+
}
126+
} else {
127+
$output->writeln("No trashbin to import…");
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)