Space.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Info;
  8. use OCP\Files\Folder;
  9. use OCP\Util;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Space extends Command {
  16. public function __construct(
  17. private FileUtils $fileUtils,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure(): void {
  22. $this
  23. ->setName('info:file:space')
  24. ->setDescription('Summarize space usage of specified folder')
  25. ->addArgument('file', InputArgument::REQUIRED, "File id or path")
  26. ->addOption('count', 'c', InputOption::VALUE_REQUIRED, "Number of items to display", 25)
  27. ->addOption('all', 'a', InputOption::VALUE_NONE, "Display all items");
  28. }
  29. public function execute(InputInterface $input, OutputInterface $output): int {
  30. $fileInput = $input->getArgument('file');
  31. $count = (int)$input->getOption('count');
  32. $all = $input->getOption('all');
  33. $node = $this->fileUtils->getNode($fileInput);
  34. if (!$node) {
  35. $output->writeln("<error>file $fileInput not found</error>");
  36. return 1;
  37. }
  38. $output->writeln($node->getName() . ": <info>" . Util::humanFileSize($node->getSize()) . "</info>");
  39. if ($node instanceof Folder) {
  40. $limits = $all ? [] : array_fill(0, $count - 1, 0);
  41. $this->fileUtils->outputLargeFilesTree($output, $node, '', $limits, $all);
  42. }
  43. return 0;
  44. }
  45. }