@ -30,6 +30,7 @@ use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\GlobalStoragesService;
use OCA\Files_External\Service\ImportLegacyStoragesService;
use OCA\Files_External\Service\StoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\IUserManager;
use OCP\IUserSession;
@ -40,27 +41,15 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Import extends Base {
private GlobalStoragesService $globalService;
private UserStoragesService $userService;
private IUserSession $userSession;
private IUserManager $userManager;
private ImportLegacyStoragesService $importLegacyStorageService;
private BackendService $backendService;
public function __construct(GlobalStoragesService $globalService,
UserStoragesService $userService,
IUserSession $userSession,
IUserManager $userManager,
ImportLegacyStoragesService $importLegacyStorageService,
BackendService $backendService
public function __construct(
private GlobalStoragesService $globalService,
private UserStoragesService $userService,
private IUserSession $userSession,
private IUserManager $userManager,
private ImportLegacyStoragesService $importLegacyStorageService,
private BackendService $backendService,
) {
parent::__construct();
$this->globalService = $globalService;
$this->userService = $userService;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->importLegacyStorageService = $importLegacyStorageService;
$this->backendService = $backendService;
}
protected function configure(): void {
@ -95,18 +84,18 @@ class Import extends Base {
} else {
if (!file_exists($path)) {
$output->writeln('< error > File not found: ' . $path . '< / error > ');
return 1 ;
return self::FAILURE ;
}
$json = file_get_contents($path);
}
if (!is_string($json) || strlen($json) < 2 ) {
$output->writeln('< error > Error while reading json< / error > ');
return 1 ;
return self::FAILURE ;
}
$data = json_decode($json, true);
if (!is_array($data)) {
$output->writeln('< error > Error while parsing json< / error > ');
return 1 ;
return self::FAILURE ;
}
$isLegacy = isset($data['user']) || isset($data['group']);
@ -116,7 +105,7 @@ class Import extends Base {
foreach ($mounts as $mount) {
if ($mount->getBackendOption('password') === false) {
$output->writeln('< error > Failed to decrypt password< / error > ');
return 1 ;
return self::FAILURE ;
}
}
} else {
@ -147,7 +136,7 @@ class Import extends Base {
$existingMount->getBackendOptions() === $mount->getBackendOptions()
) {
$output->writeln("< error > Duplicate mount (" . $mount->getMountPoint() . ")< / error > ");
return 1 ;
return self::FAILURE ;
}
}
}
@ -155,7 +144,7 @@ class Import extends Base {
if ($input->getOption('dry')) {
if (count($mounts) === 0) {
$output->writeln('< error > No mounts to be imported< / error > ');
return 1 ;
return self::FAILURE ;
}
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
@ -167,7 +156,7 @@ class Import extends Base {
$storageService->addStorage($mount);
}
}
return 0 ;
return self::SUCCESS ;
}
private function parseData(array $data): StorageConfig {
@ -178,8 +167,8 @@ class Import extends Base {
$mount->setAuthMechanism($authBackend);
$mount->setBackendOptions($data['configuration']);
$mount->setMountOptions($data['options']);
$mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []);
$mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []);
$mount->setApplicableUsers($data['applicable_users'] ?? []);
$mount->setApplicableGroups($data['applicable_groups'] ?? []);
return $mount;
}
@ -192,16 +181,16 @@ class Import extends Base {
}
}
protected function getStorageService($userId) {
if (!empty($userId)) {
$user = $this->userManager->get($userId);
if (is_null($user)) {
throw new NoUserException("user $userId not found");
}
$this->userSession->setUser($user);
return $this->userService;
} else {
protected function getStorageService($userId): StoragesService {
if (empty($userId)) {
return $this->globalService;
}
$user = $this->userManager->get($userId);
if (is_null($user)) {
throw new NoUserException("user $userId not found");
}
$this->userSession->setUser($user);
return $this->userService;
}
}