1
0

Delete.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 OCP\DB\QueryBuilder\IQueryBuilder;
  24. use OCP\IDBConnection;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Helper\QuestionHelper;
  27. use Symfony\Component\Console\Input\InputArgument;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Question\ConfirmationQuestion;
  32. class Delete extends Command {
  33. public function __construct(
  34. private ObjectUtil $objectUtils,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure(): void {
  39. $this
  40. ->setName('files:object:delete')
  41. ->setDescription('Delete an object from the object store')
  42. ->addArgument('object', InputArgument::REQUIRED, "Object to delete")
  43. ->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");
  44. }
  45. public function execute(InputInterface $input, OutputInterface $output): int {
  46. $object = $input->getArgument('object');
  47. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  48. if (!$objectStore) {
  49. return -1;
  50. }
  51. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  52. $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>");
  53. $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");
  54. $output->writeln("");
  55. }
  56. if (!$objectStore->objectExists($object)) {
  57. $output->writeln("<error>Object $object does not exist</error>");
  58. return -1;
  59. }
  60. /** @var QuestionHelper $helper */
  61. $helper = $this->getHelper('question');
  62. $question = new ConfirmationQuestion("Delete $object? [y/N] ", false);
  63. if ($helper->ask($input, $output, $question)) {
  64. $objectStore->deleteObject($object);
  65. }
  66. return self::SUCCESS;
  67. }
  68. }