|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me> |
| 6 | + * |
| 7 | + * @author Louis Chemineau <louis@chmn.me> |
| 8 | + * |
| 9 | + * @license AGPL-3.0-or-later |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | +namespace OCA\Photos\Command; |
| 26 | + |
| 27 | +use OCP\IConfig; |
| 28 | +use OCP\IUserManager; |
| 29 | +use OCP\Files\IRootFolder; |
| 30 | +use OCP\Files\Folder; |
| 31 | +use OCA\Photos\Service\MediaLocationManager; |
| 32 | +use Symfony\Component\Console\Command\Command; |
| 33 | +use Symfony\Component\Console\Input\InputInterface; |
| 34 | +use Symfony\Component\Console\Input\InputOption; |
| 35 | +use Symfony\Component\Console\Output\OutputInterface; |
| 36 | + |
| 37 | +class MapMediaToLocationCommand extends Command { |
| 38 | + public function __construct( |
| 39 | + private IRootFolder $rootFolder, |
| 40 | + private MediaLocationManager $mediaLocationManager, |
| 41 | + private IConfig $config, |
| 42 | + private IUserManager $userManager, |
| 43 | + ) { |
| 44 | + parent::__construct(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Configure the command |
| 49 | + */ |
| 50 | + protected function configure(): void { |
| 51 | + $this->setName('photos:map-media-to-location') |
| 52 | + ->setDescription('Reverse geocode media coordinates.') |
| 53 | + ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Limit the mapping to a user.', null); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Execute the command |
| 58 | + */ |
| 59 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 60 | + if (!$this->config->getSystemValueBool('enable_file_metadata', true)) { |
| 61 | + throw new \Exception('File metadata is not enabled.'); |
| 62 | + } |
| 63 | + |
| 64 | + $userId = $input->getOption('user'); |
| 65 | + if ($userId === null) { |
| 66 | + $this->scanForAllUsers($output); |
| 67 | + } else { |
| 68 | + $this->scanFilesForUser($userId, $output); |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + private function scanForAllUsers(OutputInterface $output): void { |
| 75 | + $users = $this->userManager->search(''); |
| 76 | + |
| 77 | + $output->writeln("Scanning all users:"); |
| 78 | + foreach ($users as $user) { |
| 79 | + $this->scanFilesForUser($user->getUID(), $output); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private function scanFilesForUser(string $userId, OutputInterface $output): void { |
| 84 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
| 85 | + $output->write(" - Scanning files for $userId"); |
| 86 | + $startTime = time(); |
| 87 | + $count = $this->scanFolder($userFolder); |
| 88 | + $timeElapse = time() - $startTime; |
| 89 | + $output->writeln(" - $count files, $timeElapse sec"); |
| 90 | + } |
| 91 | + |
| 92 | + private function scanFolder(Folder $folder): int { |
| 93 | + $count = 0; |
| 94 | + |
| 95 | + // Do not scan share and other moveable mounts. |
| 96 | + if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) { |
| 97 | + return $count; |
| 98 | + } |
| 99 | + |
| 100 | + foreach ($folder->getDirectoryListing() as $node) { |
| 101 | + if ($node instanceof Folder) { |
| 102 | + $count += $this->scanFolder($node); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (!str_starts_with($node->getMimeType(), 'image')) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + $this->mediaLocationManager->setLocationForFile($node->getId()); |
| 111 | + $count++; |
| 112 | + } |
| 113 | + |
| 114 | + return $count; |
| 115 | + } |
| 116 | +} |
0 commit comments