Get.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Object;
  23. use OCP\Files\File;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Input\InputArgument;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. class Get extends Command {
  30. public function __construct(
  31. private ObjectUtil $objectUtils,
  32. ) {
  33. parent::__construct();
  34. }
  35. protected function configure(): void {
  36. $this
  37. ->setName('files:object:get')
  38. ->setDescription('Get the contents of an object')
  39. ->addArgument('object', InputArgument::REQUIRED, "Object to get")
  40. ->addArgument('output', InputArgument::REQUIRED, "Target local file to output to, use - for STDOUT")
  41. ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to get the object from, only required in cases where it can't be determined from the config");
  42. }
  43. public function execute(InputInterface $input, OutputInterface $output): int {
  44. $object = $input->getArgument('object');
  45. $outputName = $input->getArgument('output');
  46. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  47. if (!$objectStore) {
  48. return self::FAILURE;
  49. }
  50. if (!$objectStore->objectExists($object)) {
  51. $output->writeln("<error>Object $object does not exist</error>");
  52. return self::FAILURE;
  53. }
  54. try {
  55. $source = $objectStore->readObject($object);
  56. } catch (\Exception $e) {
  57. $msg = $e->getMessage();
  58. $output->writeln("<error>Failed to read $object from object store: $msg</error>");
  59. return self::FAILURE;
  60. }
  61. $target = $outputName === '-' ? STDOUT : fopen($outputName, 'w');
  62. if (!$target) {
  63. $output->writeln("<error>Failed to open $outputName for writing</error>");
  64. return self::FAILURE;
  65. }
  66. stream_copy_to_stream($source, $target);
  67. return self::SUCCESS;
  68. }
  69. }