1
0

Delete.php 3.9 KB

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