Size.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Trashbin\Command;
  8. use OC\Core\Command\Base;
  9. use OCP\Command\IBus;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. use OCP\IUserManager;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class Size extends Base {
  18. public function __construct(
  19. private IConfig $config,
  20. private IUserManager $userManager,
  21. private IBus $commandBus,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure() {
  26. parent::configure();
  27. $this
  28. ->setName('trashbin:size')
  29. ->setDescription('Configure the target trashbin size')
  30. ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'configure the target size for the provided user, if no user is given the default size is configured')
  31. ->addArgument(
  32. 'size',
  33. InputArgument::OPTIONAL,
  34. 'the target size for the trashbin, if not provided the current trashbin size will be returned'
  35. );
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $user = $input->getOption('user');
  39. $size = $input->getArgument('size');
  40. if ($size) {
  41. $parsedSize = \OC_Helper::computerFileSize($size);
  42. if ($parsedSize === false) {
  43. $output->writeln('<error>Failed to parse input size</error>');
  44. return -1;
  45. }
  46. if ($user) {
  47. $this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize);
  48. $this->commandBus->push(new Expire($user));
  49. } else {
  50. $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize);
  51. $output->writeln('<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>');
  52. $output->writeln('<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>');
  53. }
  54. } else {
  55. $this->printTrashbinSize($input, $output, $user);
  56. }
  57. return 0;
  58. }
  59. private function printTrashbinSize(InputInterface $input, OutputInterface $output, ?string $user) {
  60. $globalSize = (int)$this->config->getAppValue('files_trashbin', 'trashbin_size', '-1');
  61. if ($globalSize < 0) {
  62. $globalHumanSize = 'default (50% of available space)';
  63. } else {
  64. $globalHumanSize = \OC_Helper::humanFileSize($globalSize);
  65. }
  66. if ($user) {
  67. $userSize = (int)$this->config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
  68. if ($userSize < 0) {
  69. $userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)";
  70. } else {
  71. $userHumanSize = \OC_Helper::humanFileSize($userSize);
  72. }
  73. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  74. $output->writeln($userHumanSize);
  75. } else {
  76. $userValue = ($userSize < 0) ? 'default' : $userSize;
  77. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  78. $this->writeArrayInOutputFormat($input, $output, [
  79. 'user_size' => $userValue,
  80. 'global_size' => $globalValue,
  81. 'effective_size' => ($userSize < 0) ? $globalValue : $userValue,
  82. ]);
  83. }
  84. } else {
  85. $users = [];
  86. $this->userManager->callForSeenUsers(function (IUser $user) use (&$users): void {
  87. $users[] = $user->getUID();
  88. });
  89. $userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users);
  90. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  91. $output->writeln("Default size: $globalHumanSize");
  92. $output->writeln('');
  93. if (count($userValues)) {
  94. $output->writeln('Per-user sizes:');
  95. $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
  96. return \OC_Helper::humanFileSize($size);
  97. }, $userValues));
  98. } else {
  99. $output->writeln('No per-user sizes configured');
  100. }
  101. } else {
  102. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  103. $this->writeArrayInOutputFormat($input, $output, [
  104. 'global_size' => $globalValue,
  105. 'user_sizes' => $userValues,
  106. ]);
  107. }
  108. }
  109. }
  110. }