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

Put::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
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\Object;
25
26
use OCP\Files\IMimeTypeDetector;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Helper\QuestionHelper;
29
use Symfony\Component\Console\Input\InputArgument;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Input\InputOption;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\Console\Question\ConfirmationQuestion;
34
35
class Put extends Command {
36
	private ObjectUtil $objectUtils;
37
	private IMimeTypeDetector $mimeTypeDetector;
38
39
	public function __construct(ObjectUtil $objectUtils, IMimeTypeDetector $mimeTypeDetector) {
40
		$this->objectUtils = $objectUtils;
41
		$this->mimeTypeDetector = $mimeTypeDetector;
42
		parent::__construct();
43
	}
44
45
	protected function configure(): void {
46
		$this
47
			->setName('files:object:put')
48
			->setDescription('Write a file to the object store')
49
			->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN")
50
			->addArgument('object', InputArgument::REQUIRED, "Object to write")
51
			->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket where to store the object, only required in cases where it can't be determined from the config");;
52
	}
53
54
	public function execute(InputInterface $input, OutputInterface $output): int {
55
		$object = $input->getArgument('object');
56
		$inputName = (string)$input->getArgument('input');
57
		$objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
58
		if (!$objectStore) {
59
			return -1;
60
		}
61
62
		if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
63
			$output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>");
64
			$output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely.");
65
			$output->writeln("");
66
67
			/** @var QuestionHelper $helper */
68
			$helper = $this->getHelper('question');
69
			$question = new ConfirmationQuestion("Write to the object anyway? [y/N] ", false);
70
			if (!$helper->ask($input, $output, $question)) {
71
				return -1;
72
			}
73
		}
74
75
		$source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
76
		if (!$source) {
77
			$output->writeln("<error>Failed to open $inputName</error>");
78
			return 1;
79
		}
80
		$objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
81
		return 0;
82
	}
83
84
}
85