1
0

Copy.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 OCP\Files\Folder;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Helper\QuestionHelper;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. class Copy extends Command {
  18. private FileUtils $fileUtils;
  19. public function __construct(FileUtils $fileUtils) {
  20. $this->fileUtils = $fileUtils;
  21. parent::__construct();
  22. }
  23. protected function configure(): void {
  24. $this
  25. ->setName('files:copy')
  26. ->setDescription('Copy a file or folder')
  27. ->addArgument('source', InputArgument::REQUIRED, 'Source file id or path')
  28. ->addArgument('target', InputArgument::REQUIRED, 'Target path')
  29. ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for confirmation and don't output any warnings")
  30. ->addOption('no-target-directory', 'T', InputOption::VALUE_NONE, 'When target path is folder, overwrite the folder instead of copying into the folder');
  31. }
  32. public function execute(InputInterface $input, OutputInterface $output): int {
  33. $sourceInput = $input->getArgument('source');
  34. $targetInput = $input->getArgument('target');
  35. $force = $input->getOption('force');
  36. $noTargetDir = $input->getOption('no-target-directory');
  37. $node = $this->fileUtils->getNode($sourceInput);
  38. $targetNode = $this->fileUtils->getNode($targetInput);
  39. if (!$node) {
  40. $output->writeln("<error>file $sourceInput not found</error>");
  41. return 1;
  42. }
  43. $targetParentPath = dirname(rtrim($targetInput, '/'));
  44. $targetParent = $this->fileUtils->getNode($targetParentPath);
  45. if (!$targetParent) {
  46. $output->writeln("<error>Target parent path $targetParentPath doesn't exist</error>");
  47. return 1;
  48. }
  49. $wouldRequireDelete = false;
  50. if ($targetNode) {
  51. if (!$targetNode->isUpdateable()) {
  52. $output->writeln("<error>$targetInput isn't writable</error>");
  53. return 1;
  54. }
  55. if ($targetNode instanceof Folder) {
  56. if ($noTargetDir) {
  57. if (!$force) {
  58. $output->writeln("Warning: <info>$sourceInput</info> is a file, but <info>$targetInput</info> is a folder");
  59. }
  60. $wouldRequireDelete = true;
  61. } else {
  62. $targetInput = $targetNode->getFullPath($node->getName());
  63. $targetNode = $this->fileUtils->getNode($targetInput);
  64. }
  65. } else {
  66. if ($node instanceof Folder) {
  67. if (!$force) {
  68. $output->writeln("Warning: <info>$sourceInput</info> is a folder, but <info>$targetInput</info> is a file");
  69. }
  70. $wouldRequireDelete = true;
  71. }
  72. }
  73. if ($wouldRequireDelete && $targetNode->getInternalPath() === '') {
  74. $output->writeln("<error>Mount root can't be overwritten with a different type</error>");
  75. return 1;
  76. }
  77. if ($wouldRequireDelete && !$targetNode->isDeletable()) {
  78. $output->writeln("<error>$targetInput can't be deleted to be replaced with $sourceInput</error>");
  79. return 1;
  80. }
  81. if (!$force && $targetNode) {
  82. /** @var QuestionHelper $helper */
  83. $helper = $this->getHelper('question');
  84. $question = new ConfirmationQuestion('<info>' . $targetInput . '</info> already exists, overwrite? [y/N] ', false);
  85. if (!$helper->ask($input, $output, $question)) {
  86. return 1;
  87. }
  88. }
  89. }
  90. if ($wouldRequireDelete && $targetNode) {
  91. $targetNode->delete();
  92. }
  93. $node->copy($targetInput);
  94. return 0;
  95. }
  96. }