Skip to content

Commit 50ecebf

Browse files
committed
Fix importing from user folder
Signed-off-by: Côme Chilliet <[email protected]>
1 parent fd901c0 commit 50ecebf

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

lib/BackgroundJob/UserImportJob.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\UserMigration\Db\UserImport;
3131
use OCA\UserMigration\Db\UserImportMapper;
3232
use OCA\UserMigration\Service\UserMigrationService;
33+
use OCA\UserMigration\UserFolderImportSource;
3334
use OCP\AppFramework\Utility\ITimeFactory;
3435
use OCP\BackgroundJob\QueuedJob;
3536
use OCP\Files\IRootFolder;
@@ -76,8 +77,6 @@ public function run($argument): void {
7677
$sourceUser = $import->getSourceUser();
7778
$targetUser = $import->getTargetUser();
7879
$path = $import->getPath();
79-
$dataDir = $this->config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data');
80-
$absolutePath = $dataDir . $this->root->getUserFolder($sourceUser)->get($path)->getPath();
8180

8281
$sourceUserObject = $this->userManager->get($sourceUser);
8382
$targetUserObject = $this->userManager->get($targetUser);
@@ -96,8 +95,9 @@ public function run($argument): void {
9695
try {
9796
$import->setStatus(UserImport::STATUS_STARTED);
9897
$this->mapper->update($import);
98+
$importSource = new UserFolderImportSource($this->root->getUserFolder($sourceUser), $path);
9999

100-
$this->migrationService->import($absolutePath, $targetUserObject);
100+
$this->migrationService->import($importSource, $targetUserObject);
101101
$this->successNotification($import);
102102
} catch (\Exception $e) {
103103
$this->logger->error($e->getMessage(), ['exception' => $e]);

lib/Command/Import.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use OCA\UserMigration\Service\UserMigrationService;
2929
use OCP\IUserManager;
30+
use OCP\UserMigration\ImportSource;
3031
use Symfony\Component\Console\Command\Command;
3132
use Symfony\Component\Console\Helper\QuestionHelper;
3233
use Symfony\Component\Console\Input\InputArgument;
@@ -90,7 +91,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9091
} else {
9192
$user = null;
9293
}
93-
$this->migrationService->import($input->getArgument('archive'), $user, $output);
94+
$path = $input->getArgument('archive');
95+
$output->writeln("Importing from ${path}");
96+
$importSource = new ImportSource($path);
97+
$this->migrationService->import($importSource, $user, $output);
98+
$output->writeln("Successfully imported from ${path}");
9499
} catch (\Exception $e) {
95100
$output->writeln("$e");
96101
$output->writeln("<error>" . $e->getMessage() . "</error>");

lib/Service/UserMigrationService.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
namespace OCA\UserMigration\Service;
2929

30-
use OCA\UserMigration\ImportSource;
3130
use OCP\Files\IRootFolder;
3231
use OCP\IConfig;
3332
use OCP\IUser;
@@ -120,12 +119,9 @@ public function export(IExportDestination $exportDestination, IUser $user, ?arra
120119
$exportDestination->close();
121120
}
122121

123-
public function import(string $path, ?IUser $user = null, ?OutputInterface $output = null): void {
122+
public function import(IImportSource $importSource, ?IUser $user = null, ?OutputInterface $output = null): void {
124123
$output = $output ?? new NullOutput();
125124

126-
$output->writeln("Importing from ${path}");
127-
$importSource = new ImportSource($path);
128-
129125
try {
130126
$migratorVersions = $importSource->getMigratorVersions();
131127

@@ -149,7 +145,7 @@ public function import(string $path, ?IUser $user = null, ?OutputInterface $outp
149145
}
150146

151147
$uid = $user->getUID();
152-
$output->writeln("Successfully imported $uid from $path");
148+
$output->writeln("Successfully imported $uid");
153149
} finally {
154150
$importSource->close();
155151
}

lib/UserFolderImportSource.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\UserMigration;
28+
29+
use OCP\Files\File;
30+
use OCP\Files\Folder;
31+
use OCP\Files\NotFoundException;
32+
use OCP\UserMigration\UserMigrationException;
33+
34+
class UserFolderImportSource extends ImportSource {
35+
private File $file;
36+
37+
public function __construct(Folder $userFolder, string $path) {
38+
try {
39+
$this->file = $userFolder->get($path);
40+
if (!$this->file instanceof File) {
41+
throw new UserMigrationException("$path is not a valid file");
42+
}
43+
} catch (NotFoundException $e) {
44+
throw new UserMigrationException("$path not found", 0, $e);
45+
}
46+
$localPath = $this->file->getStorage()->getLocalFile($this->file->getInternalPath());
47+
parent::__construct($localPath);
48+
}
49+
}

0 commit comments

Comments
 (0)