1
0

Delete.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files\Command\Object;
  23. use Symfony\Component\Console\Command\Command;
  24. use Symfony\Component\Console\Helper\QuestionHelper;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. use Symfony\Component\Console\Question\ConfirmationQuestion;
  30. class Delete extends Command {
  31. public function __construct(
  32. private ObjectUtil $objectUtils,
  33. ) {
  34. parent::__construct();
  35. }
  36. protected function configure(): void {
  37. $this
  38. ->setName('files:object:delete')
  39. ->setDescription('Delete an object from the object store')
  40. ->addArgument('object', InputArgument::REQUIRED, "Object to delete")
  41. ->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");
  42. }
  43. public function execute(InputInterface $input, OutputInterface $output): int {
  44. $object = $input->getArgument('object');
  45. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  46. if (!$objectStore) {
  47. return -1;
  48. }
  49. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  50. $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>");
  51. $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");
  52. $output->writeln("");
  53. }
  54. if (!$objectStore->objectExists($object)) {
  55. $output->writeln("<error>Object $object does not exist</error>");
  56. return -1;
  57. }
  58. /** @var QuestionHelper $helper */
  59. $helper = $this->getHelper('question');
  60. $question = new ConfirmationQuestion("Delete $object? [y/N] ", false);
  61. if ($helper->ask($input, $output, $question)) {
  62. $objectStore->deleteObject($object);
  63. }
  64. return self::SUCCESS;
  65. }
  66. }