Delete.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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\NotFoundException;
  10. use OCA\Files_External\Service\GlobalStoragesService;
  11. use OCA\Files_External\Service\UserStoragesService;
  12. use OCP\IUserManager;
  13. use OCP\IUserSession;
  14. use Symfony\Component\Console\Input\ArrayInput;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Question\ConfirmationQuestion;
  20. use Symfony\Component\HttpFoundation\Response;
  21. class Delete extends Base {
  22. public function __construct(
  23. protected GlobalStoragesService $globalService,
  24. protected UserStoragesService $userService,
  25. protected IUserSession $userSession,
  26. protected IUserManager $userManager,
  27. ) {
  28. parent::__construct();
  29. }
  30. protected function configure(): void {
  31. $this
  32. ->setName('files_external:delete')
  33. ->setDescription('Delete an external mount')
  34. ->addArgument(
  35. 'mount_id',
  36. InputArgument::REQUIRED,
  37. 'The id of the mount to edit'
  38. )->addOption(
  39. 'yes',
  40. 'y',
  41. InputOption::VALUE_NONE,
  42. 'Skip confirmation'
  43. );
  44. parent::configure();
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output): int {
  47. $mountId = $input->getArgument('mount_id');
  48. try {
  49. $mount = $this->globalService->getStorage($mountId);
  50. } catch (NotFoundException $e) {
  51. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  52. return Response::HTTP_NOT_FOUND;
  53. }
  54. $noConfirm = $input->getOption('yes');
  55. if (!$noConfirm) {
  56. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  57. $listInput = new ArrayInput([], $listCommand->getDefinition());
  58. $listInput->setOption('output', $input->getOption('output'));
  59. $listCommand->listMounts(null, [$mount], $listInput, $output);
  60. $questionHelper = $this->getHelper('question');
  61. $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);
  62. if (!$questionHelper->ask($input, $output, $question)) {
  63. return self::FAILURE;
  64. }
  65. }
  66. $this->globalService->removeStorage($mountId);
  67. return self::SUCCESS;
  68. }
  69. }