Verify.php 4.7 KB

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