Skip to content

Commit 9b7b845

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

5 files changed

Lines changed: 143 additions & 6 deletions

File tree

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@
4343
'OCA\\Files_Trashbin\\Trash\\TrashItem' => $baseDir . '/../lib/Trash/TrashItem.php',
4444
'OCA\\Files_Trashbin\\Trash\\TrashManager' => $baseDir . '/../lib/Trash/TrashManager.php',
4545
'OCA\\Files_Trashbin\\Trashbin' => $baseDir . '/../lib/Trashbin.php',
46+
'OCA\\Files_Trashbin\\UserMigration\\TrashbinMigrator' => $baseDir . '/../lib/UserMigration/TrashbinMigrator.php',
4647
);

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ComposerStaticInitFiles_Trashbin
5858
'OCA\\Files_Trashbin\\Trash\\TrashItem' => __DIR__ . '/..' . '/../lib/Trash/TrashItem.php',
5959
'OCA\\Files_Trashbin\\Trash\\TrashManager' => __DIR__ . '/..' . '/../lib/Trash/TrashManager.php',
6060
'OCA\\Files_Trashbin\\Trashbin' => __DIR__ . '/..' . '/../lib/Trashbin.php',
61+
'OCA\\Files_Trashbin\\UserMigration\\TrashbinMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/TrashbinMigrator.php',
6162
);
6263

6364
public static function getInitializer(ClassLoader $loader)

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' => '863fb583da53fa2ba43f7c7dc8d213835fd6b362',
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' => '863fb583da53fa2ba43f7c7dc8d213835fd6b362',
2020
'dev_requirement' => false,
2121
),
2222
),

apps/files_trashbin/lib/AppInfo/Application.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@
3030
use OCA\Files_Trashbin\Expiration;
3131
use OCA\Files_Trashbin\Trash\ITrashManager;
3232
use OCA\Files_Trashbin\Trash\TrashManager;
33-
use OCP\App\IAppManager;
33+
use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
3434
use OCP\AppFramework\App;
3535
use OCP\AppFramework\Bootstrap\IBootContext;
3636
use OCP\AppFramework\Bootstrap\IBootstrap;
3737
use OCP\AppFramework\Bootstrap\IRegistrationContext;
38+
use OCP\App\IAppManager;
3839
use OCP\ILogger;
3940
use OCP\IServerContainer;
4041

4142
class Application extends App implements IBootstrap {
43+
public const APP_ID = 'files_trashbin';
44+
4245
public function __construct(array $urlParams = []) {
43-
parent::__construct('files_trashbin', $urlParams);
46+
parent::__construct(self::APP_ID, $urlParams);
4447
}
4548

4649
public function register(IRegistrationContext $context): void {
@@ -50,6 +53,8 @@ public function register(IRegistrationContext $context): void {
5053
$context->registerServiceAlias(ITrashManager::class, TrashManager::class);
5154
/** Register $principalBackend for the DAV collection */
5255
$context->registerServiceAlias('principalBackend', Principal::class);
56+
57+
$context->registerUserMigrator(TrashbinMigrator::class);
5358
}
5459

5560
public function boot(IBootContext $context): void {
@@ -65,10 +70,10 @@ public function boot(IBootContext $context): void {
6570
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
6671

6772
\OCA\Files\App::getNavigationManager()->add(function () {
68-
$l = \OC::$server->getL10N('files_trashbin');
73+
$l = \OC::$server->getL10N(self::APP_ID);
6974
return [
7075
'id' => 'trashbin',
71-
'appname' => 'files_trashbin',
76+
'appname' => self::APP_ID,
7277
'script' => 'list.php',
7378
'order' => 50,
7479
'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(static::PATH_FILES)) {
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)