Verify.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  10. use OCA\Files_External\Lib\StorageConfig;
  11. use OCA\Files_External\NotFoundException;
  12. use OCA\Files_External\Service\GlobalStoragesService;
  13. use OCP\Files\StorageNotAvailableException;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\HttpFoundation\Response;
  19. class Verify extends Base {
  20. public function __construct(
  21. protected GlobalStoragesService $globalService,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('files_external:verify')
  28. ->setDescription('Verify mount configuration')
  29. ->addArgument(
  30. 'mount_id',
  31. InputArgument::REQUIRED,
  32. 'The id of the mount to check'
  33. )->addOption(
  34. 'config',
  35. 'c',
  36. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  37. 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails'
  38. );
  39. parent::configure();
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output): int {
  42. $mountId = $input->getArgument('mount_id');
  43. $configInput = $input->getOption('config');
  44. try {
  45. $mount = $this->globalService->getStorage($mountId);
  46. } catch (NotFoundException $e) {
  47. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  48. return Response::HTTP_NOT_FOUND;
  49. }
  50. $this->updateStorageStatus($mount, $configInput, $output);
  51. $this->writeArrayInOutputFormat($input, $output, [
  52. 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()),
  53. 'code' => $mount->getStatus(),
  54. 'message' => $mount->getStatusMessage()
  55. ]);
  56. return self::SUCCESS;
  57. }
  58. private function manipulateStorageConfig(StorageConfig $storage): void {
  59. $authMechanism = $storage->getAuthMechanism();
  60. $authMechanism->manipulateStorageConfig($storage);
  61. $backend = $storage->getBackend();
  62. $backend->manipulateStorageConfig($storage);
  63. }
  64. private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output): void {
  65. try {
  66. try {
  67. $this->manipulateStorageConfig($storage);
  68. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  69. if (count($configInput) === 0) { // extra config options might solve the error
  70. throw $e;
  71. }
  72. }
  73. foreach ($configInput as $configOption) {
  74. if (!strpos($configOption, '=')) {
  75. $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
  76. return;
  77. }
  78. [$key, $value] = explode('=', $configOption, 2);
  79. $storage->setBackendOption($key, $value);
  80. }
  81. $backend = $storage->getBackend();
  82. // update status (can be time-consuming)
  83. $storage->setStatus(
  84. \OCA\Files_External\MountConfig::getBackendStatus(
  85. $backend->getStorageClass(),
  86. $storage->getBackendOptions(),
  87. false
  88. )
  89. );
  90. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  91. $status = $e->getCode() ?: StorageNotAvailableException::STATUS_INDETERMINATE;
  92. $storage->setStatus(
  93. $status,
  94. $e->getMessage()
  95. );
  96. } catch (StorageNotAvailableException $e) {
  97. $storage->setStatus(
  98. $e->getCode(),
  99. $e->getMessage()
  100. );
  101. } catch (\Exception $e) {
  102. // FIXME: convert storage exceptions to StorageNotAvailableException
  103. $storage->setStatus(
  104. StorageNotAvailableException::STATUS_ERROR,
  105. get_class($e) . ': ' . $e->getMessage()
  106. );
  107. }
  108. }
  109. }