-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Extract GPS data from EXIF #33511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Extract GPS data from EXIF #33511
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| declare(strict_types=1); | ||
| /** | ||
| * @copyright Copyright 2022 Carl Schwan <[email protected]> | ||
| * @copyright Copyright 2022 Louis Chmn <[email protected]> | ||
| * @license AGPL-3.0-or-later | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
|
|
@@ -24,6 +25,7 @@ | |
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\Db\MultipleObjectsReturnedException; | ||
| use OCP\AppFramework\Db\QBMapper; | ||
| use OCP\AppFramework\Db\Entity; | ||
| use OCP\DB\Exception; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
|
|
@@ -102,4 +104,45 @@ public function clear(int $fileId): void { | |
|
|
||
| $qb->executeStatement(); | ||
| } | ||
|
|
||
| /** | ||
| * Updates an entry in the db from an entity | ||
| * | ||
| * @param Entity $entity the entity that should be created | ||
| * @return Entity the saved entity with the set id | ||
| * @throws Exception | ||
| * @throws \InvalidArgumentException if entity has no id | ||
| */ | ||
| public function update(Entity $entity): Entity { | ||
|
||
| if (!($entity instanceof FileMetadata)) { | ||
| throw new \Exception("Entity should be a FileMetadata entity"); | ||
| } | ||
|
|
||
| // entity needs an id | ||
| $id = $entity->getId(); | ||
| if ($id === null) { | ||
| throw new \InvalidArgumentException('Entity which should be updated has no id'); | ||
| } | ||
|
|
||
| // entity needs an group_name | ||
| $groupName = $entity->getGroupName(); | ||
|
||
| if ($groupName === null) { | ||
| throw new \InvalidArgumentException('Entity which should be updated has no group_name'); | ||
| } | ||
|
|
||
| $idType = $this->getParameterTypeForProperty($entity, 'id'); | ||
| $groupNameType = $this->getParameterTypeForProperty($entity, 'groupName'); | ||
| $metadataValue = $entity->getMetadata(); | ||
| $metadataType = $this->getParameterTypeForProperty($entity, 'metadata'); | ||
|
|
||
| $qb = $this->db->getQueryBuilder(); | ||
|
|
||
| $qb->update($this->tableName) | ||
| ->set('metadata', $qb->createNamedParameter($metadataValue, $metadataType)) | ||
| ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $idType))) | ||
| ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, $groupNameType))) | ||
| ->executeStatement(); | ||
|
|
||
| return $entity; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * @copyright Copyright 2022 Carl Schwan <[email protected]> | ||
| * @copyright Copyright 2022 Louis Chmn <[email protected]> | ||
| * @license AGPL-3.0-or-later | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|
|
||
| namespace OC\Metadata\Provider; | ||
|
|
||
| use OC\Metadata\FileMetadata; | ||
| use OC\Metadata\IMetadataProvider; | ||
| use OCP\Files\File; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class ExifProvider implements IMetadataProvider { | ||
| private LoggerInterface $logger; | ||
|
|
||
| public function __construct( | ||
| LoggerInterface $logger | ||
| ) { | ||
| $this->logger = $logger; | ||
| } | ||
|
|
||
| public static function groupsProvided(): array { | ||
| return ['size']; | ||
| return ['size', 'gps']; | ||
| } | ||
|
|
||
| public static function isAvailable(): bool { | ||
| return extension_loaded('exif'); | ||
| } | ||
|
|
||
| /** @return array{'gps': FileMetadata, 'size': FileMetadata} */ | ||
| public function execute(File $file): array { | ||
| $exifData = []; | ||
| $fileDescriptor = $file->fopen('rb'); | ||
| $data = exif_read_data($fileDescriptor, 'COMPUTED', true); | ||
|
|
||
| $data = null; | ||
| try { | ||
| // Needed to make reading exif data reliable. | ||
| // This is to trigger this condition: https://github.com/php/php-src/blob/d64aa6f646a7b5e58359dc79479860164239580a/main/streams/streams.c#L710 | ||
| // But I don't understand why 1 as a special meaning. | ||
| // Revert right after reading the exif data. | ||
| $oldBufferSize = stream_set_chunk_size($fileDescriptor, 1); | ||
| $data = exif_read_data($fileDescriptor, 'ANY_TAG', true); | ||
artonge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| stream_set_chunk_size($fileDescriptor, $oldBufferSize); | ||
| } catch (\Exception $ex) { | ||
| $this->logger->warning("Couldn't extract metadata for ".$file->getId(), ['exception' => $ex]); | ||
| } | ||
|
|
||
| $size = new FileMetadata(); | ||
| $size->setGroupName('size'); | ||
|
|
@@ -31,29 +74,65 @@ public function execute(File $file): array { | |
| 'width' => $sizeResult[0], | ||
| 'height' => $sizeResult[1], | ||
| ]); | ||
|
|
||
| $exifData['size'] = $size; | ||
| } | ||
| } elseif (array_key_exists('COMPUTED', $data)) { | ||
| if (array_key_exists('Width', $data['COMPUTED']) && array_key_exists('Height', $data['COMPUTED'])) { | ||
| $size->setMetadata([ | ||
| 'width' => $data['COMPUTED']['Width'], | ||
| 'height' => $data['COMPUTED']['Height'], | ||
| ]); | ||
|
|
||
| return [ | ||
| 'size' => $size, | ||
| ]; | ||
| $exifData['size'] = $size; | ||
| } | ||
| } | ||
|
|
||
| if (array_key_exists('COMPUTED', $data) | ||
| && array_key_exists('Width', $data['COMPUTED']) | ||
| && array_key_exists('Height', $data['COMPUTED']) | ||
| if ($data && array_key_exists('GPS', $data) | ||
| && array_key_exists('GPSLatitude', $data['GPS']) && array_key_exists('GPSLatitudeRef', $data['GPS']) | ||
| && array_key_exists('GPSLongitude', $data['GPS']) && array_key_exists('GPSLongitudeRef', $data['GPS']) | ||
| ) { | ||
| $size->setMetadata([ | ||
| 'width' => $data['COMPUTED']['Width'], | ||
| 'height' => $data['COMPUTED']['Height'], | ||
| $gps = new FileMetadata(); | ||
| $gps->setGroupName('gps'); | ||
| $gps->setId($file->getId()); | ||
| $gps->setMetadata([ | ||
| 'latitude' => $this->gpsDegreesToDecimal($data['GPS']['GPSLatitude'], $data['GPS']['GPSLatitudeRef']), | ||
| 'longitude' => $this->gpsDegreesToDecimal($data['GPS']['GPSLongitude'], $data['GPS']['GPSLongitudeRef']), | ||
| ]); | ||
|
|
||
| $exifData['gps'] = $gps; | ||
| } | ||
|
|
||
| return [ | ||
| 'size' => $size, | ||
| ]; | ||
| return $exifData; | ||
| } | ||
|
|
||
| public static function getMimetypesSupported(): string { | ||
| return '/image\/.*/'; | ||
| } | ||
|
|
||
| /** | ||
| * @param array|string $coordinates | ||
|
||
| */ | ||
| private static function gpsDegreesToDecimal($coordinates, ?string $hemisphere): float { | ||
PVince81 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (is_string($coordinates)) { | ||
|
||
| $coordinates = array_map("trim", explode(",", $coordinates)); | ||
| } | ||
|
|
||
| if (count($coordinates) !== 3) { | ||
| throw new \Exception('Invalid coordinate format: ' . json_encode($coordinates)); | ||
| } | ||
|
|
||
| [$degrees, $minutes, $seconds] = array_map(function (string $rawDegree) { | ||
| $parts = explode('/', $rawDegree); | ||
|
|
||
| if ($parts[1] === '0') { | ||
| return 0; | ||
| } | ||
|
|
||
| return floatval($parts[0]) / floatval($parts[1] ?? 1); | ||
| }, $coordinates); | ||
artonge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $sign = ($hemisphere === 'W' || $hemisphere === 'S') ? -1 : 1; | ||
| return $sign * ($degrees + $minutes / 60 + $seconds / 3600); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Psalm
DeprecatedMethod