1
0

Verify.php 4.7 KB

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