|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2020 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_Trashbin\Command; |
|
25
|
|
|
|
|
26
|
|
|
use OC\Core\Command\Base; |
|
27
|
|
|
use OCP\Command\IBus; |
|
28
|
|
|
use OCP\IConfig; |
|
29
|
|
|
use OCP\IUser; |
|
30
|
|
|
use OCP\IUserManager; |
|
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
|
|
|
|
|
36
|
|
|
class Size extends Base { |
|
37
|
|
|
private $config; |
|
38
|
|
|
private $userManager; |
|
39
|
|
|
private $commandBus; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
IConfig $config, |
|
43
|
|
|
IUserManager $userManager, |
|
44
|
|
|
IBus $commandBus |
|
45
|
|
|
) { |
|
46
|
|
|
parent::__construct(); |
|
47
|
|
|
|
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->userManager = $userManager; |
|
50
|
|
|
$this->commandBus = $commandBus; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function configure() { |
|
54
|
|
|
parent::configure(); |
|
55
|
|
|
$this |
|
56
|
|
|
->setName('trashbin:size') |
|
57
|
|
|
->setDescription('Configure the target trashbin size') |
|
58
|
|
|
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'configure the target size for the provided user, if no user is given the default size is configured') |
|
59
|
|
|
->addArgument( |
|
60
|
|
|
'size', |
|
61
|
|
|
InputArgument::OPTIONAL, |
|
62
|
|
|
'the target size for the trashbin, if not provided the current trashbin size will be returned' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
|
67
|
|
|
$user = $input->getOption('user'); |
|
68
|
|
|
$size = $input->getArgument('size'); |
|
69
|
|
|
|
|
70
|
|
|
if ($size) { |
|
71
|
|
|
$parsedSize = \OC_Helper::computerFileSize($size); |
|
72
|
|
|
if ($parsedSize === false) { |
|
73
|
|
|
$output->writeln("<error>Failed to parse input size</error>"); |
|
74
|
|
|
return -1; |
|
75
|
|
|
} |
|
76
|
|
|
if ($user) { |
|
77
|
|
|
$this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize); |
|
78
|
|
|
$this->commandBus->push(new Expire($user)); |
|
79
|
|
|
} else { |
|
80
|
|
|
$this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize); |
|
81
|
|
|
$output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>"); |
|
82
|
|
|
$output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>"); |
|
83
|
|
|
} |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->printTrashbinSize($input, $output, $user); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function printTrashbinSize(InputInterface $input, OutputInterface $output, ?string $user) { |
|
92
|
|
|
$globalSize = (int)$this->config->getAppValue('files_trashbin', 'trashbin_size', '-1'); |
|
93
|
|
|
if ($globalSize < 0) { |
|
94
|
|
|
$globalHumanSize = "default (50% of available space)"; |
|
95
|
|
|
} else { |
|
96
|
|
|
$globalHumanSize = \OC_Helper::humanFileSize($globalSize); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($user) { |
|
100
|
|
|
$userSize = (int)$this->config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1'); |
|
101
|
|
|
|
|
102
|
|
|
if ($userSize < 0) { |
|
103
|
|
|
$userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)"; |
|
104
|
|
|
} else { |
|
105
|
|
|
$userHumanSize = \OC_Helper::humanFileSize($userSize); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { |
|
109
|
|
|
$output->writeln($userHumanSize); |
|
110
|
|
|
} else { |
|
111
|
|
|
$userValue = ($userSize < 0) ? 'default' : $userSize; |
|
112
|
|
|
$globalValue = ($globalSize < 0) ? 'default' : $globalSize; |
|
113
|
|
|
$this->writeArrayInOutputFormat($input, $output, [ |
|
114
|
|
|
'user_size' => $userValue, |
|
115
|
|
|
'global_size' => $globalValue, |
|
116
|
|
|
'effective_size' => ($userSize < 0) ? $globalValue : $userValue, |
|
117
|
|
|
]); |
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
|
|
$users = []; |
|
121
|
|
|
$this->userManager->callForSeenUsers(function (IUser $user) use (&$users) { |
|
122
|
|
|
$users[] = $user->getUID(); |
|
123
|
|
|
}); |
|
124
|
|
|
$userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users); |
|
125
|
|
|
|
|
126
|
|
|
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) { |
|
127
|
|
|
$output->writeln("Default size: $globalHumanSize"); |
|
128
|
|
|
$output->writeln(""); |
|
129
|
|
|
if (count($userValues)) { |
|
130
|
|
|
$output->writeln("Per-user sizes:"); |
|
131
|
|
|
$this->writeArrayInOutputFormat($input, $output, array_map(function ($size) { |
|
132
|
|
|
return \OC_Helper::humanFileSize($size); |
|
133
|
|
|
}, $userValues)); |
|
134
|
|
|
} else { |
|
135
|
|
|
$output->writeln("No per-user sizes configured"); |
|
136
|
|
|
} |
|
137
|
|
|
} else { |
|
138
|
|
|
$globalValue = ($globalSize < 0) ? 'default' : $globalSize; |
|
139
|
|
|
$this->writeArrayInOutputFormat($input, $output, [ |
|
140
|
|
|
'global_size' => $globalValue, |
|
141
|
|
|
'user_sizes' => $userValues, |
|
142
|
|
|
]); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|