|
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) { |
|
|
|
|
|
|
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
|
|
|
|