mirror of
https://github.com/nextcloud/server.git
synced 2026-06-11 09:42:09 -04:00
Merge pull request #39131 from fsamapoor/refactor_lib_files_external_commands
Refactors files_external app commands
This commit is contained in:
commit
45cac16432
11 changed files with 135 additions and 204 deletions
|
|
@ -34,21 +34,15 @@ use Symfony\Component\Console\Input\InputArgument;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Applicable extends Base {
|
||||
protected GlobalStoragesService $globalService;
|
||||
private IUserManager $userManager;
|
||||
private IGroupManager $groupManager;
|
||||
|
||||
public function __construct(
|
||||
GlobalStoragesService $globalService,
|
||||
IUserManager $userManager,
|
||||
IGroupManager $groupManager
|
||||
protected GlobalStoragesService $globalService,
|
||||
private IUserManager $userManager,
|
||||
private IGroupManager $groupManager,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
$this->userManager = $userManager;
|
||||
$this->groupManager = $groupManager;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -94,12 +88,12 @@ class Applicable extends Base {
|
|||
$mount = $this->globalService->getStorage($mountId);
|
||||
} catch (NotFoundException $e) {
|
||||
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
|
||||
if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
|
||||
$output->writeln('<error>Can\'t change applicables on personal mounts</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$addUsers = $input->getOption('add-user');
|
||||
|
|
@ -114,13 +108,13 @@ class Applicable extends Base {
|
|||
foreach ($addUsers as $addUser) {
|
||||
if (!$this->userManager->userExists($addUser)) {
|
||||
$output->writeln('<error>User "' . $addUser . '" not found</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
foreach ($addGroups as $addGroup) {
|
||||
if (!$this->groupManager->groupExists($addGroup)) {
|
||||
$output->writeln('<error>Group "' . $addGroup . '" not found</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -142,6 +136,6 @@ class Applicable extends Base {
|
|||
'users' => $applicableUsers,
|
||||
'groups' => $applicableGroups
|
||||
]);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,13 +33,10 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Backends extends Base {
|
||||
private BackendService $backendService;
|
||||
|
||||
public function __construct(BackendService $backendService
|
||||
public function __construct(
|
||||
private BackendService $backendService,
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
$this->backendService = $backendService;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -72,24 +69,24 @@ class Backends extends Base {
|
|||
if ($type) {
|
||||
if (!isset($data[$type])) {
|
||||
$output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
$data = $data[$type];
|
||||
|
||||
if ($backend) {
|
||||
if (!isset($data[$backend])) {
|
||||
$output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
$data = $data[$backend];
|
||||
}
|
||||
}
|
||||
|
||||
$this->writeArrayInOutputFormat($input, $output, $data);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function serializeAuthBackend(\JsonSerializable $backend) {
|
||||
private function serializeAuthBackend(\JsonSerializable $backend): array {
|
||||
$data = $backend->jsonSerialize();
|
||||
$result = [
|
||||
'name' => $data['name'],
|
||||
|
|
@ -112,7 +109,7 @@ class Backends extends Base {
|
|||
* @param DefinitionParameter[] $parameters
|
||||
* @return string[]
|
||||
*/
|
||||
private function formatConfiguration(array $parameters) {
|
||||
private function formatConfiguration(array $parameters): array {
|
||||
$configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
|
||||
return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -31,13 +31,13 @@ use OCA\Files_External\Service\GlobalStoragesService;
|
|||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Config extends Base {
|
||||
protected GlobalStoragesService $globalService;
|
||||
|
||||
public function __construct(GlobalStoragesService $globalService) {
|
||||
public function __construct(
|
||||
protected GlobalStoragesService $globalService,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -67,7 +67,7 @@ class Config extends Base {
|
|||
$mount = $this->globalService->getStorage($mountId);
|
||||
} catch (NotFoundException $e) {
|
||||
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
|
||||
$value = $input->getArgument('value');
|
||||
|
|
@ -76,15 +76,13 @@ class Config extends Base {
|
|||
} else {
|
||||
$this->getOption($mount, $key, $output);
|
||||
}
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StorageConfig $mount
|
||||
* @param string $key
|
||||
* @param OutputInterface $output
|
||||
*/
|
||||
protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
|
||||
protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
|
||||
if ($key === 'mountpoint' || $key === 'mount_point') {
|
||||
$value = $mount->getMountPoint();
|
||||
} else {
|
||||
|
|
@ -97,12 +95,10 @@ class Config extends Base {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param StorageConfig $mount
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param OutputInterface $output
|
||||
*/
|
||||
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
|
||||
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
|
||||
$decoded = json_decode($value, true);
|
||||
if (!is_null($decoded) && json_encode($decoded) === $value) {
|
||||
$value = $decoded;
|
||||
|
|
|
|||
|
|
@ -42,26 +42,17 @@ use Symfony\Component\Console\Input\InputArgument;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Create extends Base {
|
||||
private GlobalStoragesService $globalService;
|
||||
private UserStoragesService $userService;
|
||||
private IUserManager $userManager;
|
||||
private BackendService $backendService;
|
||||
private IUserSession $userSession;
|
||||
|
||||
public function __construct(GlobalStoragesService $globalService,
|
||||
UserStoragesService $userService,
|
||||
IUserManager $userManager,
|
||||
IUserSession $userSession,
|
||||
BackendService $backendService
|
||||
public function __construct(
|
||||
private GlobalStoragesService $globalService,
|
||||
private UserStoragesService $userService,
|
||||
private IUserManager $userManager,
|
||||
private IUserSession $userSession,
|
||||
private BackendService $backendService,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
$this->userService = $userService;
|
||||
$this->userManager = $userManager;
|
||||
$this->userSession = $userSession;
|
||||
$this->backendService = $backendService;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -116,32 +107,32 @@ class Create extends Base {
|
|||
|
||||
if (!Filesystem::isValidPath($mountPoint)) {
|
||||
$output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
if (is_null($storageBackend)) {
|
||||
$output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
if (is_null($authBackend)) {
|
||||
$output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
$supportedSchemes = array_keys($storageBackend->getAuthSchemes());
|
||||
if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
|
||||
$output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$config = [];
|
||||
foreach ($configInput as $configOption) {
|
||||
if (!str_contains($configOption, '=')) {
|
||||
$output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
[$key, $value] = explode('=', $configOption, 2);
|
||||
if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
|
||||
$output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
$config[$key] = $value;
|
||||
}
|
||||
|
|
@ -155,7 +146,7 @@ class Create extends Base {
|
|||
if ($user) {
|
||||
if (!$this->userManager->userExists($user)) {
|
||||
$output->writeln('<error>User "' . $user . '" not found</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
$mount->setApplicableUsers([$user]);
|
||||
}
|
||||
|
|
@ -170,7 +161,7 @@ class Create extends Base {
|
|||
$output->writeln((string)$mount->getId());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool {
|
||||
|
|
@ -196,15 +187,15 @@ class Create extends Base {
|
|||
}
|
||||
|
||||
protected function getStorageService(string $userId): StoragesService {
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,19 +35,16 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Delete extends Base {
|
||||
protected GlobalStoragesService $globalService;
|
||||
protected UserStoragesService $userService;
|
||||
protected IUserSession $userSession;
|
||||
protected IUserManager $userManager;
|
||||
|
||||
public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
|
||||
public function __construct(
|
||||
protected GlobalStoragesService $globalService,
|
||||
protected UserStoragesService $userService,
|
||||
protected IUserSession $userSession,
|
||||
protected IUserManager $userManager,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
$this->userService = $userService;
|
||||
$this->userSession = $userSession;
|
||||
$this->userManager = $userManager;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -73,7 +70,7 @@ class Delete extends Base {
|
|||
$mount = $this->globalService->getStorage($mountId);
|
||||
} catch (NotFoundException $e) {
|
||||
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
|
||||
$noConfirm = $input->getOption('yes');
|
||||
|
|
@ -88,11 +85,11 @@ class Delete extends Base {
|
|||
$question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
|
||||
|
||||
if (!$questionHelper->ask($input, $output, $question)) {
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
$this->globalService->removeStorage($mountId);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,6 +54,6 @@ class Export extends ListCommand {
|
|||
$listInput->setOption('show-password', true);
|
||||
$listInput->setOption('full', true);
|
||||
$listCommand->execute($listInput, $output);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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(string $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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ use OC\Core\Command\Base;
|
|||
use OC\User\NoUserException;
|
||||
use OCA\Files_External\Lib\StorageConfig;
|
||||
use OCA\Files_External\Service\GlobalStoragesService;
|
||||
use OCA\Files_External\Service\StoragesService;
|
||||
use OCA\Files_External\Service\UserStoragesService;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
|
|
@ -39,19 +40,15 @@ use Symfony\Component\Console\Input\InputOption;
|
|||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class ListCommand extends Base {
|
||||
protected GlobalStoragesService $globalService;
|
||||
protected UserStoragesService $userService;
|
||||
protected IUserSession $userSession;
|
||||
protected IUserManager $userManager;
|
||||
|
||||
public const ALL = -1;
|
||||
|
||||
public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
|
||||
public function __construct(
|
||||
protected GlobalStoragesService $globalService,
|
||||
protected UserStoragesService $userService,
|
||||
protected IUserSession $userSession,
|
||||
protected IUserManager $userManager,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
$this->userService = $userService;
|
||||
$this->userSession = $userSession;
|
||||
$this->userManager = $userManager;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -93,7 +90,7 @@ class ListCommand extends Base {
|
|||
}
|
||||
|
||||
$this->listMounts($userId, $mounts, $input, $output);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -245,16 +242,16 @@ class ListCommand 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(string $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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,23 +50,13 @@ use Symfony\Component\Console\Input\InputOption;
|
|||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Notify extends Base {
|
||||
private GlobalStoragesService $globalService;
|
||||
private IDBConnection $connection;
|
||||
private LoggerInterface $logger;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
|
||||
public function __construct(
|
||||
GlobalStoragesService $globalService,
|
||||
IDBConnection $connection,
|
||||
LoggerInterface $logger,
|
||||
IUserManager $userManager
|
||||
private GlobalStoragesService $globalService,
|
||||
private IDBConnection $connection,
|
||||
private LoggerInterface $logger,
|
||||
private IUserManager $userManager
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
$this->connection = $connection;
|
||||
$this->logger = $logger;
|
||||
$this->userManager = $userManager;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -110,32 +100,24 @@ class Notify extends Base {
|
|||
private function getUserOption(InputInterface $input): ?string {
|
||||
if ($input->getOption('user')) {
|
||||
return (string)$input->getOption('user');
|
||||
} elseif (isset($_ENV['NOTIFY_USER'])) {
|
||||
return $_ENV['NOTIFY_USER'];
|
||||
} elseif (isset($_SERVER['NOTIFY_USER'])) {
|
||||
return $_SERVER['NOTIFY_USER'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $_ENV['NOTIFY_USER'] ?? $_SERVER['NOTIFY_USER'] ?? null;
|
||||
}
|
||||
|
||||
private function getPasswordOption(InputInterface $input): ?string {
|
||||
if ($input->getOption('password')) {
|
||||
return (string)$input->getOption('password');
|
||||
} elseif (isset($_ENV['NOTIFY_PASSWORD'])) {
|
||||
return $_ENV['NOTIFY_PASSWORD'];
|
||||
} elseif (isset($_SERVER['NOTIFY_PASSWORD'])) {
|
||||
return $_SERVER['NOTIFY_PASSWORD'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $_ENV['NOTIFY_PASSWORD'] ?? $_SERVER['NOTIFY_PASSWORD'] ?? null;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$mount = $this->globalService->getStorage($input->getArgument('mount_id'));
|
||||
if (is_null($mount)) {
|
||||
$output->writeln('<error>Mount not found</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
$noAuth = false;
|
||||
|
||||
|
|
@ -178,11 +160,11 @@ class Notify extends Base {
|
|||
if ($noAuth) {
|
||||
$output->writeln('<error>Username and/or password required</error>');
|
||||
}
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
if (!$storage instanceof INotifyStorage) {
|
||||
$output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>');
|
||||
return 1;
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$dryRun = $input->getOption('dry-run');
|
||||
|
|
@ -204,7 +186,7 @@ class Notify extends Base {
|
|||
}
|
||||
$this->markParentAsOutdated($mount->getId(), $change->getPath(), $output, $dryRun);
|
||||
});
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function createStorage(StorageConfig $mount): IStorage {
|
||||
|
|
@ -212,7 +194,7 @@ class Notify extends Base {
|
|||
return new $class($mount->getBackendOptions());
|
||||
}
|
||||
|
||||
private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $dryRun) {
|
||||
private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $dryRun): void {
|
||||
$parent = ltrim(dirname($path), '/');
|
||||
if ($parent === '.') {
|
||||
$parent = '';
|
||||
|
|
@ -253,22 +235,17 @@ class Notify extends Base {
|
|||
}
|
||||
}
|
||||
|
||||
private function logUpdate(IChange $change, OutputInterface $output) {
|
||||
switch ($change->getType()) {
|
||||
case INotifyStorage::NOTIFY_ADDED:
|
||||
$text = 'added';
|
||||
break;
|
||||
case INotifyStorage::NOTIFY_MODIFIED:
|
||||
$text = 'modified';
|
||||
break;
|
||||
case INotifyStorage::NOTIFY_REMOVED:
|
||||
$text = 'removed';
|
||||
break;
|
||||
case INotifyStorage::NOTIFY_RENAMED:
|
||||
$text = 'renamed';
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
private function logUpdate(IChange $change, OutputInterface $output): void {
|
||||
$text = match ($change->getType()) {
|
||||
INotifyStorage::NOTIFY_ADDED => 'added',
|
||||
INotifyStorage::NOTIFY_MODIFIED => 'modified',
|
||||
INotifyStorage::NOTIFY_REMOVED => 'removed',
|
||||
INotifyStorage::NOTIFY_RENAMED => 'renamed',
|
||||
default => '',
|
||||
};
|
||||
|
||||
if ($text === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$text .= ' ' . $change->getPath();
|
||||
|
|
@ -324,7 +301,7 @@ class Notify extends Base {
|
|||
}
|
||||
|
||||
|
||||
private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, OutputInterface $output) {
|
||||
private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, OutputInterface $output): void {
|
||||
usleep(100 * 1000); //give time for the notify to start
|
||||
if (!$storage->file_put_contents('/.nc_test_file.txt', 'test content')) {
|
||||
$output->writeln("Failed to create test file for self-test");
|
||||
|
|
|
|||
|
|
@ -47,11 +47,9 @@ class Option extends Config {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param StorageConfig $mount
|
||||
* @param string $key
|
||||
* @param OutputInterface $output
|
||||
*/
|
||||
protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
|
||||
protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
|
||||
$value = $mount->getMountOption($key);
|
||||
if (!is_string($value)) { // show bools and objects correctly
|
||||
$value = json_encode($value);
|
||||
|
|
@ -60,12 +58,10 @@ class Option extends Config {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param StorageConfig $mount
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param OutputInterface $output
|
||||
*/
|
||||
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
|
||||
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
|
||||
$decoded = json_decode($value, true);
|
||||
if (!is_null($decoded)) {
|
||||
$value = $decoded;
|
||||
|
|
|
|||
|
|
@ -36,13 +36,13 @@ use Symfony\Component\Console\Input\InputArgument;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Verify extends Base {
|
||||
protected GlobalStoragesService $globalService;
|
||||
|
||||
public function __construct(GlobalStoragesService $globalService) {
|
||||
public function __construct(
|
||||
protected GlobalStoragesService $globalService,
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->globalService = $globalService;
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
|
|
@ -70,7 +70,7 @@ class Verify extends Base {
|
|||
$mount = $this->globalService->getStorage($mountId);
|
||||
} catch (NotFoundException $e) {
|
||||
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
|
||||
return 404;
|
||||
return Response::HTTP_NOT_FOUND;
|
||||
}
|
||||
|
||||
$this->updateStorageStatus($mount, $configInput, $output);
|
||||
|
|
@ -80,19 +80,17 @@ class Verify extends Base {
|
|||
'code' => $mount->getStatus(),
|
||||
'message' => $mount->getStatusMessage()
|
||||
]);
|
||||
return 0;
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function manipulateStorageConfig(StorageConfig $storage) {
|
||||
/** @var AuthMechanism */
|
||||
private function manipulateStorageConfig(StorageConfig $storage): void {
|
||||
$authMechanism = $storage->getAuthMechanism();
|
||||
$authMechanism->manipulateStorageConfig($storage);
|
||||
/** @var Backend */
|
||||
$backend = $storage->getBackend();
|
||||
$backend->manipulateStorageConfig($storage);
|
||||
}
|
||||
|
||||
private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) {
|
||||
private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output): void {
|
||||
try {
|
||||
try {
|
||||
$this->manipulateStorageConfig($storage);
|
||||
|
|
@ -111,7 +109,6 @@ class Verify extends Base {
|
|||
$storage->setBackendOption($key, $value);
|
||||
}
|
||||
|
||||
/** @var Backend */
|
||||
$backend = $storage->getBackend();
|
||||
// update status (can be time-consuming)
|
||||
$storage->setStatus(
|
||||
|
|
@ -122,7 +119,7 @@ class Verify extends Base {
|
|||
)
|
||||
);
|
||||
} catch (InsufficientDataForMeaningfulAnswerException $e) {
|
||||
$status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE;
|
||||
$status = $e->getCode() ?: StorageNotAvailableException::STATUS_INDETERMINATE;
|
||||
$storage->setStatus(
|
||||
$status,
|
||||
$e->getMessage()
|
||||
|
|
|
|||
Loading…
Reference in a new issue