Put.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 OCP\Files\IRootFolder;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class Put extends Command {
  32. public function __construct(
  33. private FileUtils $fileUtils,
  34. private IRootFolder $rootFolder,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure(): void {
  39. $this
  40. ->setName('files:put')
  41. ->setDescription('Write contents of a file')
  42. ->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN")
  43. ->addArgument('file', InputArgument::REQUIRED, "Target Nextcloud file path to write to or fileid of existing file");
  44. }
  45. public function execute(InputInterface $input, OutputInterface $output): int {
  46. $fileOutput = $input->getArgument('file');
  47. $inputName = $input->getArgument('input');
  48. $node = $this->fileUtils->getNode($fileOutput);
  49. if ($node instanceof Folder) {
  50. $output->writeln("<error>$fileOutput is a folder</error>");
  51. return self::FAILURE;
  52. }
  53. if (!$node and is_numeric($fileOutput)) {
  54. $output->writeln("<error>$fileOutput not found</error>");
  55. return self::FAILURE;
  56. }
  57. $source = ($inputName === null || $inputName === '-') ? STDIN : fopen($inputName, 'r');
  58. if (!$source) {
  59. $output->writeln("<error>Failed to open $inputName</error>");
  60. return self::FAILURE;
  61. }
  62. if ($node instanceof File) {
  63. $target = $node->fopen('w');
  64. if (!$target) {
  65. $output->writeln("<error>Failed to open $fileOutput</error>");
  66. return self::FAILURE;
  67. }
  68. stream_copy_to_stream($source, $target);
  69. } else {
  70. $this->rootFolder->newFile($fileOutput, $source);
  71. }
  72. return self::SUCCESS;
  73. }
  74. }