|
| 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 OC\Metadata\MetadataManager; |
| 28 | +use OCP\IUser; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\Files\IRootFolder; |
| 31 | +use OCP\Files\Folder; |
| 32 | +use OCP\BackgroundJob\IJobList; |
| 33 | +use OCA\Photos\Service\ReverseGeoCoderService; |
| 34 | +use OCA\Photos\Service\LocationTagManager; |
| 35 | +use Symfony\Component\Console\Command\Command; |
| 36 | +use Symfony\Component\Console\Input\InputInterface; |
| 37 | +use Symfony\Component\Console\Input\InputOption; |
| 38 | +use Symfony\Component\Console\Output\OutputInterface; |
| 39 | + |
| 40 | +class ReverseGeoCodeMedia extends Command { |
| 41 | + private ReverseGeoCoderService $rgcService; |
| 42 | + private IRootFolder $rootFolder; |
| 43 | + private MetadataManager $metadataManager; |
| 44 | + private LocationTagManager $locationTagManager; |
| 45 | + private IConfig $config; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + ReverseGeoCoderService $rgcService, |
| 49 | + IJobList $jobList, |
| 50 | + IRootFolder $rootFolder, |
| 51 | + MetadataManager $metadataManager, |
| 52 | + LocationTagManager $locationTagManager, |
| 53 | + IConfig $config |
| 54 | + ) { |
| 55 | + parent::__construct(); |
| 56 | + $this->rgcService = $rgcService; |
| 57 | + $this->config = $config; |
| 58 | + $this->jobList = $jobList; |
| 59 | + $this->rootFolder = $rootFolder; |
| 60 | + $this->metadataManager = $metadataManager; |
| 61 | + $this->locationTagManager = $locationTagManager; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Configure the command |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + protected function configure() { |
| 70 | + $this->setName('photos:reverse-geocode-media') |
| 71 | + ->setDescription('Reverse geocode coordinates of users\' media') |
| 72 | + ->addOption('force', 'f', InputOption::VALUE_OPTIONAL, 'Force the download of the reverse geocoding files.', false); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Execute the command |
| 77 | + * |
| 78 | + * @param InputInterface $input |
| 79 | + * @param OutputInterface $output |
| 80 | + * |
| 81 | + * @return int |
| 82 | + */ |
| 83 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 84 | + if (!$this->config->getSystemValueBool('enable_file_metadata', true)) { |
| 85 | + throw new \Exception('File metadata is not enabled.'); |
| 86 | + } |
| 87 | + |
| 88 | + $this->rgcService->init(); |
| 89 | + // TODO: add per user runs. |
| 90 | + $this->scanForAllUsers(); |
| 91 | + |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + private function scanForAllUsers() { |
| 96 | + $users = $this->userManager->search(''); |
| 97 | + |
| 98 | + foreach ($users as $user) { |
| 99 | + $this->scanFilesForUser($user); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private function scanFilesForUser(IUser $user) { |
| 104 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
| 105 | + $this->scanFolder($userFolder); |
| 106 | + } |
| 107 | + |
| 108 | + private function scanFolder(Folder $folder) { |
| 109 | + foreach ($folder->getDirectoryListing() as $node) { |
| 110 | + if ($node instanceof Folder) { |
| 111 | + $this->scanFolder($node); |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (!str_starts_with($node->getMimeType(), 'image')) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + $gpsMetadata = $this->metadataManager->fetchMetadataFor('gps', [$node->getId()])[$node->getId()]; |
| 120 | + [$latitude, $longitude] = $gpsMetadata->getMetadata(); |
| 121 | + $locationId = $this->rgcService->getLocationIdForCoordinates($latitude, $longitude); |
| 122 | + $this->locationTagManager->tagFileWithLocationId($node, $locationId); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments