-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathCleanup.php
86 lines (70 loc) Β· 2.67 KB
/
Cleanup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\GroupFolders\Command\Trashbin;
use OC\Core\Command\Base;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Trash\TrashBackend;
use OCP\App\IAppManager;
use OCP\Server;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class Cleanup extends Base {
private ?TrashBackend $trashBackend = null;
public function __construct(
private readonly FolderManager $folderManager,
) {
parent::__construct();
if (Server::get(IAppManager::class)->isEnabledForUser('files_trashbin')) {
$this->trashBackend = \OCP\Server::get(TrashBackend::class);
}
}
protected function configure(): void {
$this
->setName('groupfolders:trashbin:cleanup')
->setDescription('Empty the Team folder trashbin')
->addArgument('folder_id', InputArgument::OPTIONAL, 'Id of the Team folder')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Skip confirmation');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
if (!$this->trashBackend) {
$output->writeln('<error>files_trashbin is disabled: Team folders trashbin is not available</error>');
return -1;
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$folders = $this->folderManager->getAllFolders();
if ($input->getArgument('folder_id') !== null) {
$folderId = (int)$input->getArgument('folder_id');
foreach ($folders as $folder) {
if ($folder['id'] === $folderId) {
$question = new ConfirmationQuestion('Are you sure you want to empty the trashbin of your Team folder with id ' . $folderId . ', this can not be undone (y/N).', false);
if (!$input->getOption('force') && !$helper->ask($input, $output, $question)) {
return -1;
}
$this->trashBackend->cleanTrashFolder($folder['id']);
return 0;
}
}
$output->writeln('<error>Folder not found: ' . $folderId . '</error>');
return -1;
} else {
$question = new ConfirmationQuestion('Are you sure you want to empty the trashbin of your Team folders, this can not be undone (y/N).', false);
if (!$input->getOption('force') && !$helper->ask($input, $output, $question)) {
return -1;
}
foreach ($folders as $folder) {
$this->trashBackend->cleanTrashFolder($folder['id']);
}
}
return 0;
}
}