|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\Files_External\Command; |
| 25 | + |
| 26 | +use OC\Files\Cache\Scanner; |
| 27 | +use OCA\Files_External\Service\GlobalStoragesService; |
| 28 | +use OCP\IUserManager; |
| 29 | +use Symfony\Component\Console\Helper\Table; |
| 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 | + |
| 35 | +class Scan extends StorageAuthBase { |
| 36 | + protected float $execTime = 0; |
| 37 | + protected int $foldersCounter = 0; |
| 38 | + protected int $filesCounter = 0; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + GlobalStoragesService $globalService, |
| 42 | + IUserManager $userManager |
| 43 | + ) { |
| 44 | + parent::__construct($globalService, $userManager); |
| 45 | + } |
| 46 | + |
| 47 | + protected function configure(): void { |
| 48 | + $this |
| 49 | + ->setName('files_external:scan') |
| 50 | + ->setDescription('Scan an external storage for changed files') |
| 51 | + ->addArgument( |
| 52 | + 'mount_id', |
| 53 | + InputArgument::REQUIRED, |
| 54 | + 'the mount id of the mount to scan' |
| 55 | + )->addOption( |
| 56 | + 'user', |
| 57 | + 'u', |
| 58 | + InputOption::VALUE_REQUIRED, |
| 59 | + 'The username for the remote mount (required only for some mount configuration that don\'t store credentials)' |
| 60 | + )->addOption( |
| 61 | + 'password', |
| 62 | + 'p', |
| 63 | + InputOption::VALUE_REQUIRED, |
| 64 | + 'The password for the remote mount (required only for some mount configuration that don\'t store credentials)' |
| 65 | + )->addOption( |
| 66 | + 'path', |
| 67 | + '', |
| 68 | + InputOption::VALUE_OPTIONAL, |
| 69 | + 'The path in the storage to scan', |
| 70 | + '' |
| 71 | + ); |
| 72 | + parent::configure(); |
| 73 | + } |
| 74 | + |
| 75 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 76 | + [, $storage] = $this->createStorage($input, $output); |
| 77 | + if ($storage === null) { |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + $path = $input->getOption('path'); |
| 82 | + |
| 83 | + $this->execTime = -microtime(true); |
| 84 | + |
| 85 | + /** @var Scanner $scanner */ |
| 86 | + $scanner = $storage->getScanner(); |
| 87 | + |
| 88 | + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFile', function (string $path) use ($output) { |
| 89 | + $output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE); |
| 90 | + ++$this->filesCounter; |
| 91 | + $this->abortIfInterrupted(); |
| 92 | + }); |
| 93 | + |
| 94 | + $scanner->listen('\OC\Files\Cache\Scanner', 'scanFolder', function (string $path) use ($output) { |
| 95 | + $output->writeln("\tFolder\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE); |
| 96 | + ++$this->foldersCounter; |
| 97 | + $this->abortIfInterrupted(); |
| 98 | + }); |
| 99 | + |
| 100 | + $scanner->scan($path); |
| 101 | + |
| 102 | + $this->presentStats($output); |
| 103 | + |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param OutputInterface $output |
| 109 | + */ |
| 110 | + protected function presentStats(OutputInterface $output): void { |
| 111 | + // Stop the timer |
| 112 | + $this->execTime += microtime(true); |
| 113 | + |
| 114 | + $headers = [ |
| 115 | + 'Folders', 'Files', 'Elapsed time' |
| 116 | + ]; |
| 117 | + |
| 118 | + $this->showSummary($headers, [], $output); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Shows a summary of operations |
| 123 | + * |
| 124 | + * @param string[] $headers |
| 125 | + * @param string[] $rows |
| 126 | + * @param OutputInterface $output |
| 127 | + */ |
| 128 | + protected function showSummary(array $headers, array $rows, OutputInterface $output): void { |
| 129 | + $niceDate = $this->formatExecTime(); |
| 130 | + if (!$rows) { |
| 131 | + $rows = [ |
| 132 | + $this->foldersCounter, |
| 133 | + $this->filesCounter, |
| 134 | + $niceDate, |
| 135 | + ]; |
| 136 | + } |
| 137 | + $table = new Table($output); |
| 138 | + $table |
| 139 | + ->setHeaders($headers) |
| 140 | + ->setRows([$rows]); |
| 141 | + $table->render(); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /** |
| 146 | + * Formats microtime into a human readable format |
| 147 | + * |
| 148 | + * @return string |
| 149 | + */ |
| 150 | + protected function formatExecTime(): string { |
| 151 | + $secs = round($this->execTime); |
| 152 | + # convert seconds into HH:MM:SS form |
| 153 | + return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60); |
| 154 | + } |
| 155 | +} |
0 commit comments