Delete.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  8. use OC\Core\Command\Info\FileUtils;
  9. use OCA\Files_Sharing\SharedStorage;
  10. use OCP\Files\Folder;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Question\ConfirmationQuestion;
  18. class Delete extends Command {
  19. public function __construct(
  20. private FileUtils $fileUtils,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('files:delete')
  27. ->setDescription('Delete a file or folder')
  28. ->addArgument('file', InputArgument::REQUIRED, 'File id or path')
  29. ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for configuration and don't output any warnings");
  30. }
  31. public function execute(InputInterface $input, OutputInterface $output): int {
  32. $fileInput = $input->getArgument('file');
  33. $inputIsId = is_numeric($fileInput);
  34. $force = $input->getOption('force');
  35. $node = $this->fileUtils->getNode($fileInput);
  36. if (!$node) {
  37. $output->writeln("<error>file $fileInput not found</error>");
  38. return self::FAILURE;
  39. }
  40. $deleteConfirmed = $force;
  41. if (!$deleteConfirmed) {
  42. /** @var QuestionHelper $helper */
  43. $helper = $this->getHelper('question');
  44. $storage = $node->getStorage();
  45. if (!$inputIsId && $storage->instanceOfStorage(SharedStorage::class) && $node->getInternalPath() === '') {
  46. /** @var SharedStorage $storage */
  47. [,$user] = explode('/', $fileInput, 3);
  48. $question = new ConfirmationQuestion("<info>$fileInput</info> in a shared file, do you want to unshare the file from <info>$user</info> instead of deleting the source file? [Y/n] ", true);
  49. if ($helper->ask($input, $output, $question)) {
  50. $storage->unshareStorage();
  51. return self::SUCCESS;
  52. } else {
  53. $node = $storage->getShare()->getNode();
  54. $output->writeln('');
  55. }
  56. }
  57. $filesByUsers = $this->fileUtils->getFilesByUser($node);
  58. if (count($filesByUsers) > 1) {
  59. $output->writeln('Warning: the provided file is accessible by more than one user');
  60. $output->writeln(' all of the following users will lose access to the file when deleted:');
  61. $output->writeln('');
  62. foreach ($filesByUsers as $user => $filesByUser) {
  63. $output->writeln($user . ':');
  64. foreach ($filesByUser as $file) {
  65. $output->writeln(' - ' . $file->getPath());
  66. }
  67. }
  68. $output->writeln('');
  69. }
  70. if ($node instanceof Folder) {
  71. $maybeContents = " and all it's contents";
  72. } else {
  73. $maybeContents = '';
  74. }
  75. $question = new ConfirmationQuestion('Delete ' . $node->getPath() . $maybeContents . '? [y/N] ', false);
  76. $deleteConfirmed = $helper->ask($input, $output, $question);
  77. }
  78. if ($deleteConfirmed) {
  79. if ($node->isDeletable()) {
  80. $node->delete();
  81. } else {
  82. $output->writeln('<error>File cannot be deleted, insufficient permissions.</error>');
  83. }
  84. }
  85. return self::SUCCESS;
  86. }
  87. }