Size.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Trashbin\Command;
  25. use OC\Core\Command\Base;
  26. use OCP\Command\IBus;
  27. use OCP\IConfig;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class Size extends Base {
  35. private $config;
  36. private $userManager;
  37. private $commandBus;
  38. public function __construct(
  39. IConfig $config,
  40. IUserManager $userManager,
  41. IBus $commandBus
  42. ) {
  43. parent::__construct();
  44. $this->config = $config;
  45. $this->userManager = $userManager;
  46. $this->commandBus = $commandBus;
  47. }
  48. protected function configure() {
  49. parent::configure();
  50. $this
  51. ->setName('trashbin:size')
  52. ->setDescription('Configure the target trashbin size')
  53. ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'configure the target size for the provided user, if no user is given the default size is configured')
  54. ->addArgument(
  55. 'size',
  56. InputArgument::OPTIONAL,
  57. 'the target size for the trashbin, if not provided the current trashbin size will be returned'
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. $user = $input->getOption('user');
  62. $size = $input->getArgument('size');
  63. if ($size) {
  64. $parsedSize = \OC_Helper::computerFileSize($size);
  65. if ($parsedSize === false) {
  66. $output->writeln("<error>Failed to parse input size</error>");
  67. return -1;
  68. }
  69. if ($user) {
  70. $this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize);
  71. $this->commandBus->push(new Expire($user));
  72. } else {
  73. $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize);
  74. $output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>");
  75. $output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>");
  76. }
  77. } else {
  78. $this->printTrashbinSize($input, $output, $user);
  79. }
  80. return 0;
  81. }
  82. private function printTrashbinSize(InputInterface $input, OutputInterface $output, ?string $user) {
  83. $globalSize = (int)$this->config->getAppValue('files_trashbin', 'trashbin_size', '-1');
  84. if ($globalSize < 0) {
  85. $globalHumanSize = "default (50% of available space)";
  86. } else {
  87. $globalHumanSize = \OC_Helper::humanFileSize($globalSize);
  88. }
  89. if ($user) {
  90. $userSize = (int)$this->config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
  91. if ($userSize < 0) {
  92. $userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)";
  93. } else {
  94. $userHumanSize = \OC_Helper::humanFileSize($userSize);
  95. }
  96. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  97. $output->writeln($userHumanSize);
  98. } else {
  99. $userValue = ($userSize < 0) ? 'default' : $userSize;
  100. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  101. $this->writeArrayInOutputFormat($input, $output, [
  102. 'user_size' => $userValue,
  103. 'global_size' => $globalValue,
  104. 'effective_size' => ($userSize < 0) ? $globalValue : $userValue,
  105. ]);
  106. }
  107. } else {
  108. $users = [];
  109. $this->userManager->callForSeenUsers(function (IUser $user) use (&$users) {
  110. $users[] = $user->getUID();
  111. });
  112. $userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users);
  113. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  114. $output->writeln("Default size: $globalHumanSize");
  115. $output->writeln("");
  116. if (count($userValues)) {
  117. $output->writeln("Per-user sizes:");
  118. $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
  119. return \OC_Helper::humanFileSize($size);
  120. }, $userValues));
  121. } else {
  122. $output->writeln("No per-user sizes configured");
  123. }
  124. } else {
  125. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  126. $this->writeArrayInOutputFormat($input, $output, [
  127. 'global_size' => $globalValue,
  128. 'user_sizes' => $userValues,
  129. ]);
  130. }
  131. }
  132. }
  133. }