Get.php 2.6 KB

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