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

Put::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 5
c 2
b 1
f 1
nc 1
nop 0
dl 0
loc 6
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 OCP\Files\Folder;
30
use OCP\Files\IRootFolder;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class Put extends Command {
37
	private FileUtils $fileUtils;
38
	private IRootFolder $rootFolder;
39
40
	public function __construct(FileUtils $fileUtils, IRootFolder $rootFolder) {
41
		$this->fileUtils = $fileUtils;
42
		$this->rootFolder = $rootFolder;
43
		parent::__construct();
44
	}
45
46
	protected function configure(): void {
47
		$this
48
			->setName('files:put')
49
			->setDescription('Write contents of a file')
50
			->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN")
51
			->addArgument('file', InputArgument::REQUIRED, "Target Nextcloud file path to write to or fileid of existing file");
52
	}
53
54
	public function execute(InputInterface $input, OutputInterface $output): int {
55
		$fileOutput = $input->getArgument('file');
56
		$inputName = $input->getArgument('input');
57
		$node = $this->fileUtils->getNode($fileOutput);
58
59
		if ($node instanceof Folder) {
60
			$output->writeln("<error>$fileOutput is a folder</error>");
61
			return 1;
62
		}
63
		if (!$node and is_numeric($fileOutput)) {
64
			$output->writeln("<error>$fileOutput not found</error>");
65
			return 1;
66
		}
67
68
		$source = ($inputName === null || $inputName === '-') ? STDIN : fopen($inputName, 'r');
69
		if (!$source) {
70
			$output->writeln("<error>Failed to open $inputName</error>");
71
			return 1;
72
		}
73
		if ($node instanceof File) {
74
			$target = $node->fopen('w');
75
			if (!$target) {
0 ignored issues
show
introduced by
$target is of type false|resource, thus it always evaluated to false.
Loading history...
76
				$output->writeln("<error>Failed to open $fileOutput</error>");
77
				return 1;
78
			}
79
			stream_copy_to_stream($source, $target);
80
		} else {
81
			$this->rootFolder->newFile($fileOutput, $source);
82
		}
83
		return 0;
84
	}
85
86
}
87