Put.php 2.7 KB

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