1
0

Verify.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Command;
  26. use OC\Core\Command\Base;
  27. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  28. use OCA\Files_External\Lib\StorageConfig;
  29. use OCA\Files_External\NotFoundException;
  30. use OCA\Files_External\Service\GlobalStoragesService;
  31. use OCP\Files\StorageNotAvailableException;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. use Symfony\Component\HttpFoundation\Response;
  37. class Verify extends Base {
  38. public function __construct(
  39. protected GlobalStoragesService $globalService,
  40. ) {
  41. parent::__construct();
  42. }
  43. protected function configure(): void {
  44. $this
  45. ->setName('files_external:verify')
  46. ->setDescription('Verify mount configuration')
  47. ->addArgument(
  48. 'mount_id',
  49. InputArgument::REQUIRED,
  50. 'The id of the mount to check'
  51. )->addOption(
  52. 'config',
  53. 'c',
  54. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  55. 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails'
  56. );
  57. parent::configure();
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $mountId = $input->getArgument('mount_id');
  61. $configInput = $input->getOption('config');
  62. try {
  63. $mount = $this->globalService->getStorage($mountId);
  64. } catch (NotFoundException $e) {
  65. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  66. return Response::HTTP_NOT_FOUND;
  67. }
  68. $this->updateStorageStatus($mount, $configInput, $output);
  69. $this->writeArrayInOutputFormat($input, $output, [
  70. 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()),
  71. 'code' => $mount->getStatus(),
  72. 'message' => $mount->getStatusMessage()
  73. ]);
  74. return self::SUCCESS;
  75. }
  76. private function manipulateStorageConfig(StorageConfig $storage): void {
  77. $authMechanism = $storage->getAuthMechanism();
  78. $authMechanism->manipulateStorageConfig($storage);
  79. $backend = $storage->getBackend();
  80. $backend->manipulateStorageConfig($storage);
  81. }
  82. private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output): void {
  83. try {
  84. try {
  85. $this->manipulateStorageConfig($storage);
  86. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  87. if (count($configInput) === 0) { // extra config options might solve the error
  88. throw $e;
  89. }
  90. }
  91. foreach ($configInput as $configOption) {
  92. if (!strpos($configOption, '=')) {
  93. $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
  94. return;
  95. }
  96. [$key, $value] = explode('=', $configOption, 2);
  97. $storage->setBackendOption($key, $value);
  98. }
  99. $backend = $storage->getBackend();
  100. // update status (can be time-consuming)
  101. $storage->setStatus(
  102. \OCA\Files_External\MountConfig::getBackendStatus(
  103. $backend->getStorageClass(),
  104. $storage->getBackendOptions(),
  105. false
  106. )
  107. );
  108. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  109. $status = $e->getCode() ?: StorageNotAvailableException::STATUS_INDETERMINATE;
  110. $storage->setStatus(
  111. $status,
  112. $e->getMessage()
  113. );
  114. } catch (StorageNotAvailableException $e) {
  115. $storage->setStatus(
  116. $e->getCode(),
  117. $e->getMessage()
  118. );
  119. } catch (\Exception $e) {
  120. // FIXME: convert storage exceptions to StorageNotAvailableException
  121. $storage->setStatus(
  122. StorageNotAvailableException::STATUS_ERROR,
  123. get_class($e) . ': ' . $e->getMessage()
  124. );
  125. }
  126. }
  127. }