Move.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 OCP\Files\File;
  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 Move extends Command {
  34. private FileUtils $fileUtils;
  35. public function __construct(FileUtils $fileUtils) {
  36. $this->fileUtils = $fileUtils;
  37. parent::__construct();
  38. }
  39. protected function configure(): void {
  40. $this
  41. ->setName('files:move')
  42. ->setDescription('Move a file or folder')
  43. ->addArgument('source', InputArgument::REQUIRED, "Source file id or path")
  44. ->addArgument('target', InputArgument::REQUIRED, "Target path")
  45. ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for configuration and don't output any warnings");
  46. }
  47. public function execute(InputInterface $input, OutputInterface $output): int {
  48. $sourceInput = $input->getArgument('source');
  49. $targetInput = $input->getArgument('target');
  50. $force = $input->getOption('force');
  51. $node = $this->fileUtils->getNode($sourceInput);
  52. $targetNode = $this->fileUtils->getNode($targetInput);
  53. if (!$node) {
  54. $output->writeln("<error>file $sourceInput not found</error>");
  55. return 1;
  56. }
  57. $targetParentPath = dirname(rtrim($targetInput, '/'));
  58. $targetParent = $this->fileUtils->getNode($targetParentPath);
  59. if (!$targetParent) {
  60. $output->writeln("<error>Target parent path $targetParentPath doesn't exist</error>");
  61. return 1;
  62. }
  63. $wouldRequireDelete = false;
  64. if ($targetNode) {
  65. if (!$targetNode->isUpdateable()) {
  66. $output->writeln("<error>$targetInput already exists and isn't writable</error>");
  67. return 1;
  68. }
  69. if ($node instanceof Folder && $targetNode instanceof File) {
  70. $output->writeln("Warning: <info>$sourceInput</info> is a folder, but <info>$targetInput</info> is a file");
  71. $wouldRequireDelete = true;
  72. }
  73. if ($node instanceof File && $targetNode instanceof Folder) {
  74. $output->writeln("Warning: <info>$sourceInput</info> is a file, but <info>$targetInput</info> is a folder");
  75. $wouldRequireDelete = true;
  76. }
  77. if ($wouldRequireDelete && $targetNode->getInternalPath() === '') {
  78. $output->writeln("<error>Mount root can't be overwritten with a different type</error>");
  79. return 1;
  80. }
  81. if ($wouldRequireDelete && !$targetNode->isDeletable()) {
  82. $output->writeln("<error>$targetInput can't be deleted to be replaced with $sourceInput</error>");
  83. return 1;
  84. }
  85. if (!$force) {
  86. /** @var QuestionHelper $helper */
  87. $helper = $this->getHelper('question');
  88. $question = new ConfirmationQuestion("<info>" . $targetInput . "</info> already exists, overwrite? [y/N] ", false);
  89. if (!$helper->ask($input, $output, $question)) {
  90. return 1;
  91. }
  92. }
  93. }
  94. if ($wouldRequireDelete && $targetNode) {
  95. $targetNode->delete();
  96. }
  97. $node->move($targetInput);
  98. return 0;
  99. }
  100. }