1
0

Delete.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Command\Object;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Helper\QuestionHelper;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Question\ConfirmationQuestion;
  15. class Delete extends Command {
  16. public function __construct(
  17. private ObjectUtil $objectUtils,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure(): void {
  22. $this
  23. ->setName('files:object:delete')
  24. ->setDescription('Delete an object from the object store')
  25. ->addArgument('object', InputArgument::REQUIRED, "Object to delete")
  26. ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to delete the object from, only required in cases where it can't be determined from the config");
  27. }
  28. public function execute(InputInterface $input, OutputInterface $output): int {
  29. $object = $input->getArgument('object');
  30. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  31. if (!$objectStore) {
  32. return -1;
  33. }
  34. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  35. $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>");
  36. $output->writeln(" Note: use <info>occ files:delete $fileId</info> to delete the file cleanly or <info>occ info:file $fileId</info> for more information about the file");
  37. $output->writeln("");
  38. }
  39. if (!$objectStore->objectExists($object)) {
  40. $output->writeln("<error>Object $object does not exist</error>");
  41. return -1;
  42. }
  43. /** @var QuestionHelper $helper */
  44. $helper = $this->getHelper('question');
  45. $question = new ConfirmationQuestion("Delete $object? [y/N] ", false);
  46. if ($helper->ask($input, $output, $question)) {
  47. $objectStore->deleteObject($object);
  48. }
  49. return self::SUCCESS;
  50. }
  51. }