Copy.php 4.3 KB

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