Copy.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 OCP\Files\File;
  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 Copy 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:copy')
  42. ->setDescription('Copy 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 confirmation and don't output any warnings")
  46. ->addOption('no-target-directory', 'T', InputOption::VALUE_NONE, "When target path is folder, overwrite the folder instead of copying into the folder");
  47. }
  48. public function execute(InputInterface $input, OutputInterface $output): int {
  49. $sourceInput = $input->getArgument('source');
  50. $targetInput = $input->getArgument('target');
  51. $force = $input->getOption('force');
  52. $noTargetDir = $input->getOption('no-target-directory');
  53. $node = $this->fileUtils->getNode($sourceInput);
  54. $targetNode = $this->fileUtils->getNode($targetInput);
  55. if (!$node) {
  56. $output->writeln("<error>file $sourceInput not found</error>");
  57. return 1;
  58. }
  59. $targetParentPath = dirname(rtrim($targetInput, '/'));
  60. $targetParent = $this->fileUtils->getNode($targetParentPath);
  61. if (!$targetParent) {
  62. $output->writeln("<error>Target parent path $targetParentPath doesn't exist</error>");
  63. return 1;
  64. }
  65. $wouldRequireDelete = false;
  66. if ($targetNode) {
  67. if (!$targetNode->isUpdateable()) {
  68. $output->writeln("<error>$targetInput isn't writable</error>");
  69. return 1;
  70. }
  71. if ($targetNode instanceof Folder) {
  72. if ($noTargetDir) {
  73. if (!$force) {
  74. $output->writeln("Warning: <info>$sourceInput</info> is a file, but <info>$targetInput</info> is a folder");
  75. }
  76. $wouldRequireDelete = true;
  77. } else {
  78. $targetInput = $targetNode->getFullPath($node->getName());
  79. $targetNode = $this->fileUtils->getNode($targetInput);
  80. }
  81. } else {
  82. if ($node instanceof Folder) {
  83. if (!$force) {
  84. $output->writeln("Warning: <info>$sourceInput</info> is a folder, but <info>$targetInput</info> is a file");
  85. }
  86. $wouldRequireDelete = true;
  87. }
  88. }
  89. if ($wouldRequireDelete && $targetNode->getInternalPath() === '') {
  90. $output->writeln("<error>Mount root can't be overwritten with a different type</error>");
  91. return 1;
  92. }
  93. if ($wouldRequireDelete && !$targetNode->isDeletable()) {
  94. $output->writeln("<error>$targetInput can't be deleted to be replaced with $sourceInput</error>");
  95. return 1;
  96. }
  97. if (!$force && $targetNode) {
  98. /** @var QuestionHelper $helper */
  99. $helper = $this->getHelper('question');
  100. $question = new ConfirmationQuestion("<info>" . $targetInput . "</info> already exists, overwrite? [y/N] ", false);
  101. if (!$helper->ask($input, $output, $question)) {
  102. return 1;
  103. }
  104. }
  105. }
  106. if ($wouldRequireDelete && $targetNode) {
  107. $targetNode->delete();
  108. }
  109. $node->copy($targetInput);
  110. return 0;
  111. }
  112. }