Size.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. private $config;
  19. private $userManager;
  20. private $commandBus;
  21. public function __construct(
  22. IConfig $config,
  23. IUserManager $userManager,
  24. IBus $commandBus
  25. ) {
  26. parent::__construct();
  27. $this->config = $config;
  28. $this->userManager = $userManager;
  29. $this->commandBus = $commandBus;
  30. }
  31. protected function configure() {
  32. parent::configure();
  33. $this
  34. ->setName('trashbin:size')
  35. ->setDescription('Configure the target trashbin size')
  36. ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'configure the target size for the provided user, if no user is given the default size is configured')
  37. ->addArgument(
  38. 'size',
  39. InputArgument::OPTIONAL,
  40. 'the target size for the trashbin, if not provided the current trashbin size will be returned'
  41. );
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. $user = $input->getOption('user');
  45. $size = $input->getArgument('size');
  46. if ($size) {
  47. $parsedSize = \OC_Helper::computerFileSize($size);
  48. if ($parsedSize === false) {
  49. $output->writeln("<error>Failed to parse input size</error>");
  50. return -1;
  51. }
  52. if ($user) {
  53. $this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize);
  54. $this->commandBus->push(new Expire($user));
  55. } else {
  56. $this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize);
  57. $output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>");
  58. $output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>");
  59. }
  60. } else {
  61. $this->printTrashbinSize($input, $output, $user);
  62. }
  63. return 0;
  64. }
  65. private function printTrashbinSize(InputInterface $input, OutputInterface $output, ?string $user) {
  66. $globalSize = (int)$this->config->getAppValue('files_trashbin', 'trashbin_size', '-1');
  67. if ($globalSize < 0) {
  68. $globalHumanSize = "default (50% of available space)";
  69. } else {
  70. $globalHumanSize = \OC_Helper::humanFileSize($globalSize);
  71. }
  72. if ($user) {
  73. $userSize = (int)$this->config->getUserValue($user, 'files_trashbin', 'trashbin_size', '-1');
  74. if ($userSize < 0) {
  75. $userHumanSize = ($globalSize < 0) ? $globalHumanSize : "default($globalHumanSize)";
  76. } else {
  77. $userHumanSize = \OC_Helper::humanFileSize($userSize);
  78. }
  79. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  80. $output->writeln($userHumanSize);
  81. } else {
  82. $userValue = ($userSize < 0) ? 'default' : $userSize;
  83. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  84. $this->writeArrayInOutputFormat($input, $output, [
  85. 'user_size' => $userValue,
  86. 'global_size' => $globalValue,
  87. 'effective_size' => ($userSize < 0) ? $globalValue : $userValue,
  88. ]);
  89. }
  90. } else {
  91. $users = [];
  92. $this->userManager->callForSeenUsers(function (IUser $user) use (&$users) {
  93. $users[] = $user->getUID();
  94. });
  95. $userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users);
  96. if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
  97. $output->writeln("Default size: $globalHumanSize");
  98. $output->writeln("");
  99. if (count($userValues)) {
  100. $output->writeln("Per-user sizes:");
  101. $this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
  102. return \OC_Helper::humanFileSize($size);
  103. }, $userValues));
  104. } else {
  105. $output->writeln("No per-user sizes configured");
  106. }
  107. } else {
  108. $globalValue = ($globalSize < 0) ? 'default' : $globalSize;
  109. $this->writeArrayInOutputFormat($input, $output, [
  110. 'global_size' => $globalValue,
  111. 'user_sizes' => $userValues,
  112. ]);
  113. }
  114. }
  115. }
  116. }