Passed
Push — master ( 76d448...3ef509 )
by Robin
17:04 queued 13s
created

Get::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 21
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 27
rs 8.9617
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2023 Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files\Command\Object;
25
26
use OCP\Files\File;
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\Input\InputOption;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class Get extends Command {
34
	private ObjectUtil $objectUtils;
35
36
	public function __construct(ObjectUtil $objectUtils) {
37
		$this->objectUtils = $objectUtils;
38
		parent::__construct();
39
	}
40
41
	protected function configure(): void {
42
		$this
43
			->setName('files:object:get')
44
			->setDescription('Get the contents of an object')
45
			->addArgument('object', InputArgument::REQUIRED, "Object to get")
46
			->addArgument('output', InputArgument::REQUIRED, "Target local file to output to, use - for STDOUT")
47
			->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");
48
	}
49
50
	public function execute(InputInterface $input, OutputInterface $output): int {
51
		$object = $input->getArgument('object');
52
		$outputName = $input->getArgument('output');
53
		$objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
54
		if (!$objectStore) {
55
			return 1;
56
		}
57
58
		if (!$objectStore->objectExists($object)) {
59
			$output->writeln("<error>Object $object does not exist</error>");
60
			return 1;
61
		} else {
62
			try {
63
				$source = $objectStore->readObject($object);
64
			} catch (\Exception $e) {
65
				$msg = $e->getMessage();
66
				$output->writeln("<error>Failed to read $object from object store: $msg</error>");
67
				return 1;
68
			}
69
			$target = $outputName === '-' ? STDOUT : fopen($outputName, 'w');
70
			if (!$target) {
71
				$output->writeln("<error>Failed to open $outputName for writing</error>");
72
				return 1;
73
			}
74
75
			stream_copy_to_stream($source, $target);
76
			return 0;
77
		}
78
	}
79
80
}
81