1
0

Verify.php 3.8 KB

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