Verify.php 4.6 KB

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