migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,141 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\IGroupManager;
use OCP\IUserManager;
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 {
public function __construct(
protected GlobalStoragesService $globalService,
private IUserManager $userManager,
private IGroupManager $groupManager,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:applicable')
->setDescription('Manage applicable users and groups for a mount')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'The id of the mount to edit'
)->addOption(
'add-user',
'',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'user to add as applicable'
)->addOption(
'remove-user',
'',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'user to remove as applicable'
)->addOption(
'add-group',
'',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'group to add as applicable'
)->addOption(
'remove-group',
'',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'group to remove as applicable'
)->addOption(
'remove-all',
'',
InputOption::VALUE_NONE,
'Set the mount to be globally applicable'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$mountId = $input->getArgument('mount_id');
try {
$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 Response::HTTP_NOT_FOUND;
}
if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
$output->writeln('<error>Can\'t change applicables on personal mounts</error>');
return self::FAILURE;
}
$addUsers = $input->getOption('add-user');
$removeUsers = $input->getOption('remove-user');
$addGroups = $input->getOption('add-group');
$removeGroups = $input->getOption('remove-group');
$applicableUsers = $mount->getApplicableUsers();
$applicableGroups = $mount->getApplicableGroups();
if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) {
foreach ($addUsers as $addUser) {
if (!$this->userManager->userExists($addUser)) {
$output->writeln('<error>User "' . $addUser . '" not found</error>');
return Response::HTTP_NOT_FOUND;
}
}
foreach ($addGroups as $addGroup) {
if (!$this->groupManager->groupExists($addGroup)) {
$output->writeln('<error>Group "' . $addGroup . '" not found</error>');
return Response::HTTP_NOT_FOUND;
}
}
if ($input->getOption('remove-all')) {
$applicableUsers = [];
$applicableGroups = [];
} else {
$applicableUsers = array_unique(array_merge($applicableUsers, $addUsers));
$applicableUsers = array_values(array_diff($applicableUsers, $removeUsers));
$applicableGroups = array_unique(array_merge($applicableGroups, $addGroups));
$applicableGroups = array_values(array_diff($applicableGroups, $removeGroups));
}
$mount->setApplicableUsers($applicableUsers);
$mount->setApplicableGroups($applicableGroups);
$this->globalService->updateStorage($mount);
}
$this->writeArrayInOutputFormat($input, $output, [
'users' => $applicableUsers,
'groups' => $applicableGroups
]);
return self::SUCCESS;
}
}
@@ -0,0 +1,120 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\Backend\Backend;
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Service\BackendService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Backends extends Base {
public function __construct(
private BackendService $backendService,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:backends')
->setDescription('Show available authentication and storage backends')
->addArgument(
'type',
InputArgument::OPTIONAL,
'only show backends of a certain type. Possible values are "authentication" or "storage"'
)->addArgument(
'backend',
InputArgument::OPTIONAL,
'only show information of a specific backend'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$authBackends = $this->backendService->getAuthMechanisms();
$storageBackends = $this->backendService->getBackends();
$data = [
'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends),
'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends)
];
$type = $input->getArgument('type');
$backend = $input->getArgument('backend');
if ($type) {
if (!isset($data[$type])) {
$output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
return self::FAILURE;
}
$data = $data[$type];
if ($backend) {
if (!isset($data[$backend])) {
$output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
return self::FAILURE;
}
$data = $data[$backend];
}
}
$this->writeArrayInOutputFormat($input, $output, $data);
return self::SUCCESS;
}
private function serializeAuthBackend(\JsonSerializable $backend): array {
$data = $backend->jsonSerialize();
$result = [
'name' => $data['name'],
'identifier' => $data['identifier'],
'configuration' => $this->formatConfiguration($data['configuration'])
];
if ($backend instanceof Backend) {
$result['storage_class'] = $backend->getStorageClass();
$authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
$result['supported_authentication_backends'] = array_keys($authBackends);
$authConfig = array_map(function (AuthMechanism $auth) {
return $this->serializeAuthBackend($auth)['configuration'];
}, $authBackends);
$result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
}
return $result;
}
/**
* @param DefinitionParameter[] $parameters
* @return string[]
*/
private function formatConfiguration(array $parameters): array {
$configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
});
return array_map(function (DefinitionParameter $parameter) {
return $parameter->getTypeName();
}, $configuration);
}
}
@@ -0,0 +1,113 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Ardinis <Ardinis@users.noreply.github.com>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
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 {
public function __construct(
protected GlobalStoragesService $globalService,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:config')
->setDescription('Manage backend configuration for a mount')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'The id of the mount to edit'
)->addArgument(
'key',
InputArgument::REQUIRED,
'key of the config option to set/get'
)->addArgument(
'value',
InputArgument::OPTIONAL,
'value to set the config option to, when no value is provided the existing value will be printed'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$mountId = $input->getArgument('mount_id');
$key = $input->getArgument('key');
try {
$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 Response::HTTP_NOT_FOUND;
}
$value = $input->getArgument('value');
if ($value !== null) {
$this->setOption($mount, $key, $value, $output);
} else {
$this->getOption($mount, $key, $output);
}
return self::SUCCESS;
}
/**
* @param string $key
*/
protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
if ($key === 'mountpoint' || $key === 'mount_point') {
$value = $mount->getMountPoint();
} else {
$value = $mount->getBackendOption($key);
}
if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly
$value = json_encode($value);
}
$output->writeln($value);
}
/**
* @param string $key
* @param string $value
*/
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;
}
if ($key === 'mountpoint' || $key === 'mount_point') {
$mount->setMountPoint($value);
} else {
$mount->setBackendOption($key, $value);
}
$this->globalService->updateStorage($mount);
}
}
@@ -0,0 +1,201 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OC\Files\Filesystem;
use OC\User\NoUserException;
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\Backend\Backend;
use OCA\Files_External\Lib\DefinitionParameter;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\GlobalStoragesService;
use OCA\Files_External\Service\StoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\IUserManager;
use OCP\IUserSession;
use Symfony\Component\Console\Input\ArrayInput;
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 {
public function __construct(
private GlobalStoragesService $globalService,
private UserStoragesService $userService,
private IUserManager $userManager,
private IUserSession $userSession,
private BackendService $backendService,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:create')
->setDescription('Create a new mount configuration')
->addOption(
'user',
'',
InputOption::VALUE_OPTIONAL,
'user to add the mount configuration for, if not set the mount will be added as system mount'
)
->addArgument(
'mount_point',
InputArgument::REQUIRED,
'mount point for the new mount'
)
->addArgument(
'storage_backend',
InputArgument::REQUIRED,
'storage backend identifier for the new mount, see `occ files_external:backends` for possible values'
)
->addArgument(
'authentication_backend',
InputArgument::REQUIRED,
'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values'
)
->addOption(
'config',
'c',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Mount configuration option in key=value format'
)
->addOption(
'dry',
'',
InputOption::VALUE_NONE,
'Don\'t save the created mount, only list the new mount'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$user = (string)$input->getOption('user');
$mountPoint = $input->getArgument('mount_point');
$storageIdentifier = $input->getArgument('storage_backend');
$authIdentifier = $input->getArgument('authentication_backend');
$configInput = $input->getOption('config');
$storageBackend = $this->backendService->getBackend($storageIdentifier);
$authBackend = $this->backendService->getAuthMechanism($authIdentifier);
if (!Filesystem::isValidPath($mountPoint)) {
$output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
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 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 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 self::FAILURE;
}
$config = [];
foreach ($configInput as $configOption) {
if (!str_contains($configOption, '=')) {
$output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
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 self::FAILURE;
}
$config[$key] = $value;
}
$mount = new StorageConfig();
$mount->setMountPoint($mountPoint);
$mount->setBackend($storageBackend);
$mount->setAuthMechanism($authBackend);
$mount->setBackendOptions($config);
if ($user) {
if (!$this->userManager->userExists($user)) {
$output->writeln('<error>User "' . $user . '" not found</error>');
return self::FAILURE;
}
$mount->setApplicableUsers([$user]);
}
if ($input->getOption('dry')) {
$this->showMount($user, $mount, $input, $output);
} else {
$this->getStorageService($user)->addStorage($mount);
if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
$output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>');
} else {
$output->writeln((string)$mount->getId());
}
}
return self::SUCCESS;
}
private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool {
$params = array_merge($storageBackend->getParameters(), $authBackend->getParameters());
foreach ($params as $param) {
/** @var DefinitionParameter $param */
if ($param->getName() === $key) {
if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) {
$value = ($value === 'true');
}
return true;
}
}
return false;
}
private function showMount(string $user, StorageConfig $mount, InputInterface $input, OutputInterface $output): void {
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
$listInput->setOption('output', $input->getOption('output'));
$listInput->setOption('show-password', true);
$listCommand->listMounts($user, [$mount], $listInput, $output);
}
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;
}
}
@@ -0,0 +1,95 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\GlobalStoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\IUserManager;
use OCP\IUserSession;
use Symfony\Component\Console\Input\ArrayInput;
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\Console\Question\ConfirmationQuestion;
use Symfony\Component\HttpFoundation\Response;
class Delete extends Base {
public function __construct(
protected GlobalStoragesService $globalService,
protected UserStoragesService $userService,
protected IUserSession $userSession,
protected IUserManager $userManager,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:delete')
->setDescription('Delete an external mount')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'The id of the mount to edit'
)->addOption(
'yes',
'y',
InputOption::VALUE_NONE,
'Skip confirmation'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$mountId = $input->getArgument('mount_id');
try {
$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 Response::HTTP_NOT_FOUND;
}
$noConfirm = $input->getOption('yes');
if (!$noConfirm) {
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
$listInput->setOption('output', $input->getOption('output'));
$listCommand->listMounts(null, [$mount], $listInput, $output);
$questionHelper = $this->getHelper('question');
$question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
if (!$questionHelper->ask($input, $output, $question)) {
return self::FAILURE;
}
}
$this->globalService->removeStorage($mountId);
return self::SUCCESS;
}
}
@@ -0,0 +1,59 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Export extends ListCommand {
protected function configure(): void {
$this
->setName('files_external:export')
->setDescription('Export mount configurations')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'user id to export the personal mounts for, if no user is provided admin mounts will be exported'
)->addOption(
'all',
'a',
InputOption::VALUE_NONE,
'show both system wide mounts and all personal mounts'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
$listInput->setArgument('user_id', $input->getArgument('user_id'));
$listInput->setOption('all', $input->getOption('all'));
$listInput->setOption('output', 'json_pretty');
$listInput->setOption('show-password', true);
$listInput->setOption('full', true);
$listCommand->execute($listInput, $output);
return self::SUCCESS;
}
}
@@ -0,0 +1,196 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OC\User\NoUserException;
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;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Import extends Base {
public function __construct(
private GlobalStoragesService $globalService,
private UserStoragesService $userService,
private IUserSession $userSession,
private IUserManager $userManager,
private ImportLegacyStoragesService $importLegacyStorageService,
private BackendService $backendService,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:import')
->setDescription('Import mount configurations')
->addOption(
'user',
'',
InputOption::VALUE_OPTIONAL,
'user to add the mount configurations for, if not set the mount will be added as system mount'
)
->addArgument(
'path',
InputArgument::REQUIRED,
'path to a json file containing the mounts to import, use "-" to read from stdin'
)
->addOption(
'dry',
'',
InputOption::VALUE_NONE,
'Don\'t save the imported mounts, only list the new mounts'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$user = (string) $input->getOption('user');
$path = $input->getArgument('path');
if ($path === '-') {
$json = file_get_contents('php://stdin');
} else {
if (!file_exists($path)) {
$output->writeln('<error>File not found: ' . $path . '</error>');
return self::FAILURE;
}
$json = file_get_contents($path);
}
if (!is_string($json) || strlen($json) < 2) {
$output->writeln('<error>Error while reading json</error>');
return self::FAILURE;
}
$data = json_decode($json, true);
if (!is_array($data)) {
$output->writeln('<error>Error while parsing json</error>');
return self::FAILURE;
}
$isLegacy = isset($data['user']) || isset($data['group']);
if ($isLegacy) {
$this->importLegacyStorageService->setData($data);
$mounts = $this->importLegacyStorageService->getAllStorages();
foreach ($mounts as $mount) {
if ($mount->getBackendOption('password') === false) {
$output->writeln('<error>Failed to decrypt password</error>');
return self::FAILURE;
}
}
} else {
if (!isset($data[0])) { //normalize to an array of mounts
$data = [$data];
}
$mounts = array_map([$this, 'parseData'], $data);
}
if ($user) {
// ensure applicables are correct for personal mounts
foreach ($mounts as $mount) {
$mount->setApplicableGroups([]);
$mount->setApplicableUsers([$user]);
}
}
$storageService = $this->getStorageService($user);
$existingMounts = $storageService->getAllStorages();
foreach ($mounts as $mount) {
foreach ($existingMounts as $existingMount) {
if (
$existingMount->getMountPoint() === $mount->getMountPoint() &&
$existingMount->getApplicableGroups() === $mount->getApplicableGroups() &&
$existingMount->getApplicableUsers() === $mount->getApplicableUsers() &&
$existingMount->getBackendOptions() === $mount->getBackendOptions()
) {
$output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>");
return self::FAILURE;
}
}
}
if ($input->getOption('dry')) {
if (count($mounts) === 0) {
$output->writeln('<error>No mounts to be imported</error>');
return self::FAILURE;
}
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
$listInput->setOption('output', $input->getOption('output'));
$listInput->setOption('show-password', true);
$listCommand->listMounts($user, $mounts, $listInput, $output);
} else {
foreach ($mounts as $mount) {
$storageService->addStorage($mount);
}
}
return self::SUCCESS;
}
private function parseData(array $data): StorageConfig {
$mount = new StorageConfig($data['mount_id']);
$mount->setMountPoint($data['mount_point']);
$mount->setBackend($this->getBackendByClass($data['storage']));
$authBackend = $this->backendService->getAuthMechanism($data['authentication_type']);
$mount->setAuthMechanism($authBackend);
$mount->setBackendOptions($data['configuration']);
$mount->setMountOptions($data['options']);
$mount->setApplicableUsers($data['applicable_users'] ?? []);
$mount->setApplicableGroups($data['applicable_groups'] ?? []);
return $mount;
}
private function getBackendByClass(string $className) {
$backends = $this->backendService->getBackends();
foreach ($backends as $backend) {
if ($backend->getStorageClass() === $className) {
return $backend;
}
}
}
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;
}
}
@@ -0,0 +1,257 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Kesselberg <mail@danielkesselberg.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
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;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ListCommand extends Base {
public const ALL = -1;
public function __construct(
protected GlobalStoragesService $globalService,
protected UserStoragesService $userService,
protected IUserSession $userSession,
protected IUserManager $userManager,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:list')
->setDescription('List configured admin or personal mounts')
->addArgument(
'user_id',
InputArgument::OPTIONAL,
'user id to list the personal mounts for, if no user is provided admin mounts will be listed'
)->addOption(
'show-password',
'',
InputOption::VALUE_NONE,
'show passwords and secrets'
)->addOption(
'full',
null,
InputOption::VALUE_NONE,
'don\'t truncate long values in table output'
)->addOption(
'all',
'a',
InputOption::VALUE_NONE,
'show both system wide mounts and all personal mounts'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
/** @var StorageConfig[] $mounts */
if ($input->getOption('all')) {
$mounts = $this->globalService->getStorageForAllUsers();
$userId = self::ALL;
} else {
$userId = (string)$input->getArgument('user_id');
$storageService = $this->getStorageService($userId);
$mounts = $storageService->getAllStorages();
}
$this->listMounts($userId, $mounts, $input, $output);
return self::SUCCESS;
}
/**
* @param ?string|ListCommand::ALL $userId
* @param StorageConfig[] $mounts
*/
public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output): void {
$outputType = $input->getOption('output');
if (count($mounts) === 0) {
if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$output->writeln('[]');
} else {
if ($userId === self::ALL) {
$output->writeln("<info>No mounts configured</info>");
} elseif ($userId) {
$output->writeln("<info>No mounts configured by $userId</info>");
} else {
$output->writeln("<info>No admin mounts configured</info>");
}
}
return;
}
$headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options'];
if (!$userId || $userId === self::ALL) {
$headers[] = 'Applicable Users';
$headers[] = 'Applicable Groups';
}
if ($userId === self::ALL) {
$headers[] = 'Type';
}
if (!$input->getOption('show-password')) {
$hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key'];
foreach ($mounts as $mount) {
$config = $mount->getBackendOptions();
foreach ($config as $key => $value) {
if (in_array($key, $hideKeys)) {
$mount->setBackendOption($key, '***');
}
}
}
}
if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$keys = array_map(function ($header) {
return strtolower(str_replace(' ', '_', $header));
}, $headers);
$pairs = array_map(function (StorageConfig $config) use ($keys, $userId) {
$values = [
$config->getId(),
$config->getMountPoint(),
$config->getBackend()->getStorageClass(),
$config->getAuthMechanism()->getIdentifier(),
$config->getBackendOptions(),
$config->getMountOptions()
];
if (!$userId || $userId === self::ALL) {
$values[] = $config->getApplicableUsers();
$values[] = $config->getApplicableGroups();
}
if ($userId === self::ALL) {
$values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal';
}
return array_combine($keys, $values);
}, $mounts);
if ($outputType === self::OUTPUT_FORMAT_JSON) {
$output->writeln(json_encode(array_values($pairs)));
} else {
$output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT));
}
} else {
$full = $input->getOption('full');
$defaultMountOptions = [
'encrypt' => true,
'previews' => true,
'filesystem_check_changes' => 1,
'enable_sharing' => false,
'encoding_compatibility' => false,
'readonly' => false,
];
$rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) {
$storageConfig = $config->getBackendOptions();
$keys = array_keys($storageConfig);
$values = array_values($storageConfig);
if (!$full) {
$values = array_map(function ($value) {
if (is_string($value) && strlen($value) > 32) {
return substr($value, 0, 6) . '...' . substr($value, -6, 6);
} else {
return $value;
}
}, $values);
}
$configStrings = array_map(function ($key, $value) {
return $key . ': ' . json_encode($value);
}, $keys, $values);
$configString = implode(', ', $configStrings);
$mountOptions = $config->getMountOptions();
// hide defaults
foreach ($mountOptions as $key => $value) {
if ($value === $defaultMountOptions[$key]) {
unset($mountOptions[$key]);
}
}
$keys = array_keys($mountOptions);
$values = array_values($mountOptions);
$optionsStrings = array_map(function ($key, $value) {
return $key . ': ' . json_encode($value);
}, $keys, $values);
$optionsString = implode(', ', $optionsStrings);
$values = [
$config->getId(),
$config->getMountPoint(),
$config->getBackend()->getText(),
$config->getAuthMechanism()->getText(),
$configString,
$optionsString
];
if (!$userId || $userId === self::ALL) {
$applicableUsers = implode(', ', $config->getApplicableUsers());
$applicableGroups = implode(', ', $config->getApplicableGroups());
if ($applicableUsers === '' && $applicableGroups === '') {
$applicableUsers = 'All';
}
$values[] = $applicableUsers;
$values[] = $applicableGroups;
}
if ($userId === self::ALL) {
$values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal';
}
return $values;
}, $mounts);
$table = new Table($output);
$table->setHeaders($headers);
$table->setRows($rows);
$table->render();
}
}
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;
}
}
@@ -0,0 +1,342 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
*
* @author Ari Selseng <ari@selseng.net>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_External\Command;
use Doctrine\DBAL\Exception\DriverException;
use OC\Core\Command\Base;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Notify\IChange;
use OCP\Files\Notify\INotifyHandler;
use OCP\Files\Notify\IRenameChange;
use OCP\Files\Storage\INotifyStorage;
use OCP\Files\Storage\IStorage;
use OCP\Files\StorageNotAvailableException;
use OCP\IDBConnection;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Notify extends Base {
public function __construct(
private GlobalStoragesService $globalService,
private IDBConnection $connection,
private LoggerInterface $logger,
private IUserManager $userManager
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:notify')
->setDescription('Listen for active update notifications for a configured external mount')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'the mount id of the mount to listen to'
)->addOption(
'user',
'u',
InputOption::VALUE_REQUIRED,
'The username for the remote mount (required only for some mount configuration that don\'t store credentials)'
)->addOption(
'password',
'p',
InputOption::VALUE_REQUIRED,
'The password for the remote mount (required only for some mount configuration that don\'t store credentials)'
)->addOption(
'path',
'',
InputOption::VALUE_REQUIRED,
'The directory in the storage to listen for updates in',
'/'
)->addOption(
'no-self-check',
'',
InputOption::VALUE_NONE,
'Disable self check on startup'
)->addOption(
'dry-run',
'',
InputOption::VALUE_NONE,
'Don\'t make any changes, only log detected changes'
);
parent::configure();
}
private function getUserOption(InputInterface $input): ?string {
if ($input->getOption('user')) {
return (string)$input->getOption('user');
}
return $_ENV['NOTIFY_USER'] ?? $_SERVER['NOTIFY_USER'] ?? null;
}
private function getPasswordOption(InputInterface $input): ?string {
if ($input->getOption('password')) {
return (string)$input->getOption('password');
}
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 self::FAILURE;
}
$noAuth = false;
$userOption = $this->getUserOption($input);
$passwordOption = $this->getPasswordOption($input);
// if only the user is provided, we get the user object to pass along to the auth backend
// this allows using saved user credentials
$user = ($userOption && !$passwordOption) ? $this->userManager->get($userOption) : null;
try {
$authBackend = $mount->getAuthMechanism();
$authBackend->manipulateStorageConfig($mount, $user);
} catch (InsufficientDataForMeaningfulAnswerException $e) {
$noAuth = true;
} catch (StorageNotAvailableException $e) {
$noAuth = true;
}
if ($userOption) {
$mount->setBackendOption('user', $userOption);
}
if ($passwordOption) {
$mount->setBackendOption('password', $passwordOption);
}
try {
$backend = $mount->getBackend();
$backend->manipulateStorageConfig($mount, $user);
} catch (InsufficientDataForMeaningfulAnswerException $e) {
$noAuth = true;
} catch (StorageNotAvailableException $e) {
$noAuth = true;
}
try {
$storage = $this->createStorage($mount);
} catch (\Exception $e) {
$output->writeln('<error>Error while trying to create storage</error>');
if ($noAuth) {
$output->writeln('<error>Username and/or password required</error>');
}
return self::FAILURE;
}
if (!$storage instanceof INotifyStorage) {
$output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>');
return self::FAILURE;
}
$dryRun = $input->getOption('dry-run');
if ($dryRun && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
}
$path = trim($input->getOption('path'), '/');
$notifyHandler = $storage->notify($path);
if (!$input->getOption('no-self-check')) {
$this->selfTest($storage, $notifyHandler, $output);
}
$notifyHandler->listen(function (IChange $change) use ($mount, $output, $dryRun) {
$this->logUpdate($change, $output);
if ($change instanceof IRenameChange) {
$this->markParentAsOutdated($mount->getId(), $change->getTargetPath(), $output, $dryRun);
}
$this->markParentAsOutdated($mount->getId(), $change->getPath(), $output, $dryRun);
});
return self::SUCCESS;
}
private function createStorage(StorageConfig $mount): IStorage {
$class = $mount->getBackend()->getStorageClass();
return new $class($mount->getBackendOptions());
}
private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $dryRun): void {
$parent = ltrim(dirname($path), '/');
if ($parent === '.') {
$parent = '';
}
try {
$storages = $this->getStorageIds($mountId, $parent);
} catch (DriverException $ex) {
$this->logger->warning('Error while trying to find correct storage ids.', ['exception' => $ex]);
$this->connection = $this->reconnectToDatabase($this->connection, $output);
$output->writeln('<info>Needed to reconnect to the database</info>');
$storages = $this->getStorageIds($mountId, $path);
}
if (count($storages) === 0) {
$output->writeln(" no users found with access to '$parent', skipping", OutputInterface::VERBOSITY_VERBOSE);
return;
}
$users = array_map(function (array $storage) {
return $storage['user_id'];
}, $storages);
$output->writeln(" marking '$parent' as outdated for " . implode(', ', $users), OutputInterface::VERBOSITY_VERBOSE);
$storageIds = array_map(function (array $storage) {
return intval($storage['storage_id']);
}, $storages);
$storageIds = array_values(array_unique($storageIds));
if ($dryRun) {
$output->writeln(" dry-run: skipping database write");
} else {
$result = $this->updateParent($storageIds, $parent);
if ($result === 0) {
//TODO: Find existing parent further up the tree in the database and register that folder instead.
$this->logger->info('Failed updating parent for "' . $path . '" while trying to register change. It may not exist in the filecache.');
}
}
}
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();
if ($change instanceof IRenameChange) {
$text .= ' to ' . $change->getTargetPath();
}
$output->writeln($text, OutputInterface::VERBOSITY_VERBOSE);
}
private function getStorageIds(int $mountId, string $path): array {
$pathHash = md5(trim((string)\OC_Util::normalizeUnicode($path), '/'));
$qb = $this->connection->getQueryBuilder();
return $qb
->select('storage_id', 'user_id')
->from('mounts', 'm')
->innerJoin('m', 'filecache', 'f', $qb->expr()->eq('m.storage_id', 'f.storage'))
->where($qb->expr()->eq('mount_id', $qb->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter($pathHash, IQueryBuilder::PARAM_STR)))
->execute()
->fetchAll();
}
private function updateParent(array $storageIds, string $parent): int {
$pathHash = md5(trim((string)\OC_Util::normalizeUnicode($parent), '/'));
$qb = $this->connection->getQueryBuilder();
return $qb
->update('filecache')
->set('size', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
->where($qb->expr()->in('storage', $qb->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY, ':storage_ids')))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter($pathHash, IQueryBuilder::PARAM_STR)))
->executeStatement();
}
private function reconnectToDatabase(IDBConnection $connection, OutputInterface $output): IDBConnection {
try {
$connection->close();
} catch (\Exception $ex) {
$this->logger->warning('Error while disconnecting from DB', ['exception' => $ex]);
$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
}
$connected = false;
while (!$connected) {
try {
$connected = $connection->connect();
} catch (\Exception $ex) {
$this->logger->warning('Error while re-connecting to database', ['exception' => $ex]);
$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
sleep(60);
}
}
return $connection;
}
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");
return;
}
$storage->mkdir('/.nc_test_folder');
$storage->file_put_contents('/.nc_test_folder/subfile.txt', 'test content');
usleep(100 * 1000); //time for all changes to be processed
$changes = $notifyHandler->getChanges();
$storage->unlink('/.nc_test_file.txt');
$storage->unlink('/.nc_test_folder/subfile.txt');
$storage->rmdir('/.nc_test_folder');
usleep(100 * 1000); //time for all changes to be processed
$notifyHandler->getChanges(); // flush
$foundRootChange = false;
$foundSubfolderChange = false;
foreach ($changes as $change) {
if ($change->getPath() === '/.nc_test_file.txt' || $change->getPath() === '.nc_test_file.txt') {
$foundRootChange = true;
} elseif ($change->getPath() === '/.nc_test_folder/subfile.txt' || $change->getPath() === '.nc_test_folder/subfile.txt') {
$foundSubfolderChange = true;
}
}
if ($foundRootChange && $foundSubfolderChange) {
$output->writeln('<info>Self-test successful</info>', OutputInterface::VERBOSITY_VERBOSE);
} elseif ($foundRootChange) {
$output->writeln('<error>Error while running self-test, change is subfolder not detected</error>');
} else {
$output->writeln('<error>Error while running self-test, no changes detected</error>');
}
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OCA\Files_External\Lib\StorageConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
class Option extends Config {
protected function configure(): void {
$this
->setName('files_external:option')
->setDescription('Manage mount options for a mount')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'The id of the mount to edit'
)->addArgument(
'key',
InputArgument::REQUIRED,
'key of the mount option to set/get'
)->addArgument(
'value',
InputArgument::OPTIONAL,
'value to set the mount option to, when no value is provided the existing value will be printed'
);
}
/**
* @param string $key
*/
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);
}
$output->writeln($value);
}
/**
* @param string $key
* @param string $value
*/
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
$decoded = json_decode($value, true);
if (!is_null($decoded)) {
$value = $decoded;
}
$mount->setMountOption($key, $value);
$this->globalService->updateStorage($mount);
}
}
@@ -0,0 +1,138 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Command;
use OC\Core\Command\Base;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\NotFoundException;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\Files\StorageNotAvailableException;
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 {
public function __construct(
protected GlobalStoragesService $globalService,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('files_external:verify')
->setDescription('Verify mount configuration')
->addArgument(
'mount_id',
InputArgument::REQUIRED,
'The id of the mount to check'
)->addOption(
'config',
'c',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$mountId = $input->getArgument('mount_id');
$configInput = $input->getOption('config');
try {
$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 Response::HTTP_NOT_FOUND;
}
$this->updateStorageStatus($mount, $configInput, $output);
$this->writeArrayInOutputFormat($input, $output, [
'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()),
'code' => $mount->getStatus(),
'message' => $mount->getStatusMessage()
]);
return self::SUCCESS;
}
private function manipulateStorageConfig(StorageConfig $storage): void {
$authMechanism = $storage->getAuthMechanism();
$authMechanism->manipulateStorageConfig($storage);
$backend = $storage->getBackend();
$backend->manipulateStorageConfig($storage);
}
private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output): void {
try {
try {
$this->manipulateStorageConfig($storage);
} catch (InsufficientDataForMeaningfulAnswerException $e) {
if (count($configInput) === 0) { // extra config options might solve the error
throw $e;
}
}
foreach ($configInput as $configOption) {
if (!strpos($configOption, '=')) {
$output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
return;
}
[$key, $value] = explode('=', $configOption, 2);
$storage->setBackendOption($key, $value);
}
$backend = $storage->getBackend();
// update status (can be time-consuming)
$storage->setStatus(
\OCA\Files_External\MountConfig::getBackendStatus(
$backend->getStorageClass(),
$storage->getBackendOptions(),
false
)
);
} catch (InsufficientDataForMeaningfulAnswerException $e) {
$status = $e->getCode() ?: StorageNotAvailableException::STATUS_INDETERMINATE;
$storage->setStatus(
$status,
$e->getMessage()
);
} catch (StorageNotAvailableException $e) {
$storage->setStatus(
$e->getCode(),
$e->getMessage()
);
} catch (\Exception $e) {
// FIXME: convert storage exceptions to StorageNotAvailableException
$storage->setStatus(
StorageNotAvailableException::STATUS_ERROR,
get_class($e) . ': ' . $e->getMessage()
);
}
}
}