| | | 1 | | using MetadataExtractor; |
| | | 2 | | using MetadataExtractor.Formats.Exif; |
| | | 3 | | using MetadataExtractor.Formats.QuickTime; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using Directory = MetadataExtractor.Directory; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Ingestion.Extraction; |
| | | 9 | | |
| | | 10 | | internal static partial class Positions |
| | | 11 | | { |
| | | 12 | | public static (double? Latitude, double? Longitude) Of(IReadOnlyList<Directory> directories, ILogger logger) |
| | 186 | 13 | | { |
| | 186 | 14 | | var gps = directories.OfType<GpsDirectory>().FirstOrDefault(); |
| | 186 | 15 | | if (gps is null) |
| | 181 | 16 | | return OfVideoLocation(directories, logger); |
| | | 17 | | try |
| | 5 | 18 | | { |
| | | 19 | | // GetGeoLocation() returns Nullable<GeoLocation>; the pattern unwraps it. |
| | 5 | 20 | | var location = gps.GetGeoLocation(); |
| | 5 | 21 | | return location is { } coordinates |
| | 5 | 22 | | ? (coordinates.Latitude, coordinates.Longitude) |
| | 5 | 23 | | : (null, null); |
| | | 24 | | } |
| | | 25 | | // Stryker disable once Block |
| | 0 | 26 | | catch (Exception ex) |
| | 0 | 27 | | { |
| | 0 | 28 | | Log.GpsCoordinatesParseFailed(logger, ex); |
| | 0 | 29 | | return (null, null); |
| | | 30 | | } |
| | 186 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static (double? Latitude, double? Longitude) OfVideoLocation(IReadOnlyList<Directory> directories, ILogger l |
| | 181 | 34 | | { |
| | 181 | 35 | | var raw = directories.OfType<QuickTimeMetadataHeaderDirectory>().FirstOrDefault() |
| | 181 | 36 | | ?.GetString(QuickTimeMetadataHeaderDirectory.TagGpsLocation); |
| | 181 | 37 | | if (raw is null) |
| | 165 | 38 | | return (null, null); |
| | 16 | 39 | | if (Iso6709().Match(raw) is not { Success: true } match) |
| | 11 | 40 | | { |
| | 11 | 41 | | Log.Iso6709ParseFailed(logger, raw); |
| | 11 | 42 | | return (null, null); |
| | | 43 | | } |
| | | 44 | | |
| | 5 | 45 | | return (Degrees(match.Groups["latitude"].Value), Degrees(match.Groups["longitude"].Value)); |
| | 181 | 46 | | } |
| | | 47 | | |
| | 10 | 48 | | private static double Degrees(string value) => double.Parse(value, NumberStyles.AllowLeadingSign | NumberStyles.Allo |
| | | 49 | | |
| | | 50 | | [GeneratedRegex(@"^(?<latitude>[+-]\d{2}(\.\d+)?)(?<longitude>[+-]\d{3}(\.\d+)?)([+-]\d+(\.\d+)?)?/$", RegexOptions. |
| | | 51 | | private static partial Regex Iso6709(); |
| | | 52 | | |
| | | 53 | | private static partial class Log |
| | | 54 | | { |
| | | 55 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Failed to parse GPS coordinates from EXIF data.")] |
| | | 56 | | public static partial void GpsCoordinatesParseFailed(ILogger logger, Exception exception); |
| | | 57 | | |
| | | 58 | | [LoggerMessage(Level = LogLevel.Debug, Message = "No position: malformed ISO 6709 location '{Raw}'.")] |
| | | 59 | | public static partial void Iso6709ParseFailed(ILogger logger, string raw); |
| | | 60 | | } |
| | | 61 | | } |