Get.php 2.8 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 Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. class Get extends Command {
  30. public function __construct(
  31. private FileUtils $fileUtils,
  32. ) {
  33. parent::__construct();
  34. }
  35. protected function configure(): void {
  36. $this
  37. ->setName('files:get')
  38. ->setDescription('Get the contents of a file')
  39. ->addArgument('file', InputArgument::REQUIRED, "Source file id or Nextcloud path")
  40. ->addArgument('output', InputArgument::OPTIONAL, "Target local file to output to, defaults to STDOUT");
  41. }
  42. public function execute(InputInterface $input, OutputInterface $output): int {
  43. $fileInput = $input->getArgument('file');
  44. $outputName = $input->getArgument('output');
  45. $node = $this->fileUtils->getNode($fileInput);
  46. if (!$node) {
  47. $output->writeln("<error>file $fileInput not found</error>");
  48. return self::FAILURE;
  49. }
  50. if (!($node instanceof File)) {
  51. $output->writeln("<error>$fileInput is a directory</error>");
  52. return self::FAILURE;
  53. }
  54. $isTTY = stream_isatty(STDOUT);
  55. if ($outputName === null && $isTTY && $node->getMimePart() !== 'text') {
  56. $output->writeln([
  57. "<error>Warning: Binary output can mess up your terminal</error>",
  58. " Use <info>occ files:get $fileInput -</info> to output it to the terminal anyway",
  59. " Or <info>occ files:get $fileInput <FILE></info> to save to a file instead"
  60. ]);
  61. return self::FAILURE;
  62. }
  63. $source = $node->fopen('r');
  64. if (!$source) {
  65. $output->writeln("<error>Failed to open $fileInput for reading</error>");
  66. return self::FAILURE;
  67. }
  68. $target = ($outputName === null || $outputName === '-') ? STDOUT : fopen($outputName, 'w');
  69. if (!$target) {
  70. $output->writeln("<error>Failed to open $outputName for reading</error>");
  71. return self::FAILURE;
  72. }
  73. stream_copy_to_stream($source, $target);
  74. return self::SUCCESS;
  75. }
  76. }