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

Delete::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
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
use OC\Core\Command\Info\FileUtils;
27
use OCA\Files_Sharing\SharedStorage;
28
use OCP\Files\Folder;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Helper\QuestionHelper;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\Console\Question\ConfirmationQuestion;
36
37
class Delete extends Command {
38
	private FileUtils $fileUtils;
39
40
	public function __construct(FileUtils $fileUtils) {
41
		$this->fileUtils = $fileUtils;
42
		parent::__construct();
43
	}
44
45
	protected function configure(): void {
46
		$this
47
			->setName('files:delete')
48
			->setDescription('Delete a file or folder')
49
			->addArgument('file', InputArgument::REQUIRED, "File id or path")
50
			->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for configuration and don't output any warnings");
51
	}
52
53
	public function execute(InputInterface $input, OutputInterface $output): int {
54
		$fileInput = $input->getArgument('file');
55
		$inputIsId = is_numeric($fileInput);
56
		$force = $input->getOption('force');
57
		$node = $this->fileUtils->getNode($fileInput);
58
59
		if (!$node) {
60
			$output->writeln("<error>file $fileInput not found</error>");
61
			return 1;
62
		}
63
64
		$deleteConfirmed = $force;
65
		if (!$deleteConfirmed) {
66
			/** @var QuestionHelper $helper */
67
			$helper = $this->getHelper('question');
68
			$storage = $node->getStorage();
69
			if (!$inputIsId && $storage->instanceOfStorage(SharedStorage::class) && $node->getInternalPath() === '') {
70
				/** @var SharedStorage $storage */
71
				[,$user] = explode('/', $fileInput, 3);
72
				$question = new ConfirmationQuestion("<info>$fileInput</info> in a shared file, do you want to unshare the file from <info>$user</info> instead of deleting the source file? [Y/n] ", true);
73
				if ($helper->ask($input, $output, $question)) {
74
					$storage->unshareStorage();
75
					return 0;
76
				} else {
77
					$node = $storage->getShare()->getNode();
78
					$output->writeln("");
79
				}
80
			}
81
82
			$filesByUsers = $this->fileUtils->getFilesByUser($node);
83
			if (count($filesByUsers) > 1) {
84
				$output->writeln("Warning: the provided file is accessible by more than one user");
85
				$output->writeln("  all of the following users will lose access to the file when deleted:");
86
				$output->writeln("");
87
				foreach ($filesByUsers as $user => $filesByUser) {
88
					$output->writeln($user . ":");
89
					foreach($filesByUser as $file) {
90
						$output->writeln("  - " . $file->getPath());
91
					}
92
				}
93
				$output->writeln("");
94
			}
95
96
			if ($node instanceof Folder) {
97
				$maybeContents = " and all it's contents";
98
			} else {
99
				$maybeContents = "";
100
			}
101
			$question = new ConfirmationQuestion("Delete " . $node->getPath() . $maybeContents . "? [y/N] ", false);
102
			$deleteConfirmed = $helper->ask($input, $output, $question);
103
		}
104
105
		if ($deleteConfirmed) {
106
			if ($node->isDeletable()) {
107
				$node->delete();
108
			} else {
109
				$output->writeln("<error>File cannot be deleted, insufficient permissions.</error>");
110
			}
111
		}
112
113
		return 0;
114
	}
115
116
}
117