Passed
Push — master ( 9e5393...4822f1 )
by Robin
15:36 queued 13s
created

Get::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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;
25
26
27
use OC\Core\Command\Info\FileUtils;
28
use OCP\Files\File;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
class Get extends Command {
35
	private FileUtils $fileUtils;
36
37
	public function __construct(FileUtils $fileUtils) {
38
		$this->fileUtils = $fileUtils;
39
		parent::__construct();
40
	}
41
42
	protected function configure(): void {
43
		$this
44
			->setName('files:get')
45
			->setDescription('Get the contents of a file')
46
			->addArgument('file', InputArgument::REQUIRED, "Source file id or Nextcloud path")
47
			->addArgument('output', InputArgument::OPTIONAL, "Target local file to output to, defaults to STDOUT");
48
	}
49
50
	public function execute(InputInterface $input, OutputInterface $output): int {
51
		$fileInput = $input->getArgument('file');
52
		$outputName = $input->getArgument('output');
53
		$node = $this->fileUtils->getNode($fileInput);
54
55
		if (!$node) {
56
			$output->writeln("<error>file $fileInput not found</error>");
57
			return 1;
58
		}
59
60
		if ($node instanceof File) {
61
			$isTTY = stream_isatty(STDOUT);
62
			if ($outputName === null && $isTTY && $node->getMimePart() !== 'text') {
63
				$output->writeln([
64
					"<error>Warning: Binary output can mess up your terminal</error>",
65
					"         Use <info>occ files:get $fileInput -</info> to output it to the terminal anyway",
66
					"         Or <info>occ files:get $fileInput <FILE></info> to save to a file instead"
67
				]);
68
				return 1;
69
			}
70
			$source = $node->fopen('r');
71
			if (!$source) {
0 ignored issues
show
introduced by
$source is of type false|resource, thus it always evaluated to false.
Loading history...
72
				$output->writeln("<error>Failed to open $fileInput for reading</error>");
73
				return 1;
74
			}
75
			$target = ($outputName === null || $outputName === '-') ? STDOUT : fopen($outputName, 'w');
76
			if (!$target) {
77
				$output->writeln("<error>Failed to open $outputName for reading</error>");
78
				return 1;
79
			}
80
81
			stream_copy_to_stream($source, $target);
82
			return 0;
83
		} else {
84
			$output->writeln("<error>$fileInput is a directory</error>");
85
			return 1;
86
		}
87
	}
88
89
}
90