Space.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\Core\Command\Info;
  23. use OCP\Files\Folder;
  24. use OCP\Util;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class Space extends Command {
  31. private FileUtils $fileUtils;
  32. public function __construct(FileUtils $fileUtils) {
  33. $this->fileUtils = $fileUtils;
  34. parent::__construct();
  35. }
  36. protected function configure(): void {
  37. $this
  38. ->setName('info:file:space')
  39. ->setDescription('Summarize space usage of specified folder')
  40. ->addArgument('file', InputArgument::REQUIRED, "File id or path")
  41. ->addOption('count', 'c', InputOption::VALUE_REQUIRED, "Number of items to display", 25)
  42. ->addOption('all', 'a', InputOption::VALUE_NONE, "Display all items");
  43. }
  44. public function execute(InputInterface $input, OutputInterface $output): int {
  45. $fileInput = $input->getArgument('file');
  46. $count = (int)$input->getOption('count');
  47. $all = $input->getOption('all');
  48. $node = $this->fileUtils->getNode($fileInput);
  49. if (!$node) {
  50. $output->writeln("<error>file $fileInput not found</error>");
  51. return 1;
  52. }
  53. $output->writeln($node->getName() . ": <info>" . Util::humanFileSize($node->getSize()) . "</info>");
  54. if ($node instanceof Folder) {
  55. $limits = $all ? [] : array_fill(0, $count - 1, 0);
  56. $this->fileUtils->outputLargeFilesTree($output, $node, '', $limits, $all);
  57. }
  58. return 0;
  59. }
  60. }