| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using MetadataExtractor; |
| | | 3 | | using MetadataExtractor.Formats.Avi; |
| | | 4 | | using MetadataExtractor.Formats.Exif; |
| | | 5 | | using MetadataExtractor.Formats.Iptc; |
| | | 6 | | using MetadataExtractor.Formats.QuickTime; |
| | | 7 | | using MetadataExtractor.Formats.Xmp; |
| | | 8 | | using NodaTime; |
| | | 9 | | using NodaTime.Text; |
| | | 10 | | using XmpCore; |
| | | 11 | | using Directory = MetadataExtractor.Directory; |
| | | 12 | | |
| | | 13 | | namespace Anichron.Worker.Ingestion.Extraction; |
| | | 14 | | |
| | | 15 | | /// <summary>Reads the recorded capture date from the sources of a media type, in ADR 0015's order; the first wins.</sum |
| | | 16 | | internal static partial class RecordedDates |
| | | 17 | | { |
| | | 18 | | private const int shortestXmpClock = 16; |
| | | 19 | | private const int iptcClock = 6; |
| | | 20 | | |
| | 17 | 21 | | private static DateTime QuickTimeEpoch => new(1904, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); |
| | | 22 | | |
| | | 23 | | private delegate RecordedDate? Source(IReadOnlyList<Directory> directories, ILogger logger); |
| | | 24 | | |
| | 18 | 25 | | private static LocalDateTimePattern ExifClock => LocalDateTimePattern.CreateWithInvariantCulture("uuuu':'MM':'dd HH' |
| | 14 | 26 | | private static OffsetPattern ExifOffset => OffsetPattern.CreateWithInvariantCulture("+HH:mm"); |
| | 17 | 27 | | private static LocalDatePattern XmpDay => LocalDatePattern.Iso; |
| | 22 | 28 | | private static LocalDateTimePattern XmpClock => LocalDateTimePattern.ExtendedIso; |
| | 9 | 29 | | private static OffsetPattern XmpOffset => OffsetPattern.CreateWithInvariantCulture("Z+HH:mm"); |
| | 12 | 30 | | private static LocalDatePattern IptcDay => LocalDatePattern.CreateWithInvariantCulture("uuuuMMdd"); |
| | 9 | 31 | | private static LocalTimePattern IptcTime => LocalTimePattern.CreateWithInvariantCulture("HHmmss"); |
| | 6 | 32 | | private static OffsetPattern IptcOffset => OffsetPattern.CreateWithInvariantCulture("+HHmm"); |
| | 5 | 33 | | private static LocalDateTimePattern AviClock => LocalDateTimePattern.CreateWithInvariantCulture("ddd MMM d HH':'mm': |
| | 19 | 34 | | private static OffsetDateTimePattern[] AppleClocks => [AppleClock("Z+HH:mm"), AppleClock("+HHmm")]; |
| | | 35 | | |
| | | 36 | | /// <summary>The recorded capture date, or null when no source of the media type yields one.</summary> |
| | | 37 | | /// <param name="directories">What MetadataExtractor read from the file.</param> |
| | | 38 | | /// <param name="appleCreationDate">The raw <c>com.apple.quicktime.creationdate</c>, read by <see cref="AppleCreatio |
| | | 39 | | /// <param name="mediaType">The item's media type, which picks the sources.</param> |
| | | 40 | | /// <param name="logger">Logs a value that does not parse.</param> |
| | | 41 | | public static RecordedDate? Of(IReadOnlyList<Directory> directories, string? appleCreationDate, MediaType mediaType, |
| | 186 | 42 | | => SourcesOf(mediaType, appleCreationDate).Select(source => source(directories, logger)).FirstOrDefault(date => |
| | | 43 | | |
| | | 44 | | private static Source[] SourcesOf(MediaType mediaType, string? appleCreationDate) |
| | 186 | 45 | | => mediaType == MediaType.Video |
| | 186 | 46 | | ? [(_, logger) => AppleCreationDate(appleCreationDate, logger), AviOriginal, MovieHeaderCreated] |
| | 186 | 47 | | : [ExifOriginal, XmpDateCreated, IptcDateCreated]; |
| | | 48 | | |
| | | 49 | | private static OffsetDateTimePattern AppleClock(string offset) |
| | 38 | 50 | | => OffsetDateTimePattern.CreateWithInvariantCulture($"uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<{offset}>"); |
| | | 51 | | |
| | | 52 | | private static RecordedDate? AppleCreationDate(string? raw, ILogger logger) |
| | 43 | 53 | | { |
| | 43 | 54 | | if (raw is null) |
| | 24 | 55 | | return null; |
| | 19 | 56 | | if (AppleClocks.Select(pattern => Parsed(pattern, raw)).FirstOrDefault(at => at is not null) is { } at) |
| | 12 | 57 | | return new RecordedDate.ClockTime(at.LocalDateTime, at.Offset); |
| | 7 | 58 | | if (Parsed(XmpClock, raw) is { } clock) |
| | 2 | 59 | | return new RecordedDate.ClockTime(clock, null); |
| | | 60 | | |
| | 5 | 61 | | return Unparseable("Apple", raw, logger); |
| | 43 | 62 | | } |
| | | 63 | | |
| | | 64 | | private static RecordedDate? ExifOriginal(IReadOnlyList<Directory> directories, ILogger logger) |
| | 143 | 65 | | { |
| | 143 | 66 | | var subIfd = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault(); |
| | 143 | 67 | | var raw = subIfd?.GetString(ExifDirectoryBase.TagDateTimeOriginal); |
| | 143 | 68 | | if (raw is null) |
| | 125 | 69 | | return null; |
| | 18 | 70 | | if (Parsed(ExifClock, raw) is not { } at) |
| | 4 | 71 | | return Unparseable("EXIF", raw, logger); |
| | | 72 | | |
| | 14 | 73 | | return new RecordedDate.ClockTime(at, OffsetOf("EXIF", subIfd!.GetString(ExifDirectoryBase.TagTimeZoneOriginal), |
| | 143 | 74 | | } |
| | | 75 | | |
| | | 76 | | private static RecordedDate? XmpDateCreated(IReadOnlyList<Directory> directories, ILogger logger) |
| | 129 | 77 | | { |
| | 129 | 78 | | var raw = directories.OfType<XmpDirectory>().FirstOrDefault()?.XmpMeta?.GetPropertyString(XmpConstants.NsPhotosh |
| | 129 | 79 | | if (raw is null) |
| | 112 | 80 | | return null; |
| | 17 | 81 | | if (Parsed(XmpDay, raw) is { } day) |
| | 2 | 82 | | return new RecordedDate.DateAlone(day); |
| | | 83 | | |
| | 15 | 84 | | var (clock, offset) = SplitOffset(raw, shortestXmpClock); |
| | 15 | 85 | | if (Parsed(XmpClock, clock.Length == shortestXmpClock ? clock + ":00" : clock) is not { } at) |
| | 6 | 86 | | return Unparseable("XMP", raw, logger); |
| | | 87 | | |
| | 9 | 88 | | return new RecordedDate.ClockTime(at, OffsetOf("XMP", offset, XmpOffset, logger)); |
| | 129 | 89 | | } |
| | | 90 | | |
| | | 91 | | private static RecordedDate? IptcDateCreated(IReadOnlyList<Directory> directories, ILogger logger) |
| | 118 | 92 | | { |
| | 118 | 93 | | var iptc = directories.OfType<IptcDirectory>().FirstOrDefault(); |
| | 118 | 94 | | var raw = iptc?.GetString(IptcDirectory.TagDateCreated); |
| | 118 | 95 | | if (raw is null) |
| | 106 | 96 | | return null; |
| | 12 | 97 | | if (Parsed(IptcDay, raw) is not { } day) |
| | 2 | 98 | | return Unparseable("IPTC", raw, logger); |
| | 10 | 99 | | if (iptc!.GetString(IptcDirectory.TagTimeCreated) is not { } rawTime) |
| | 1 | 100 | | return new RecordedDate.DateAlone(day); |
| | | 101 | | |
| | 9 | 102 | | var (clock, offset) = SplitOffset(rawTime, iptcClock); |
| | 9 | 103 | | if (Parsed(IptcTime, clock) is not { } time) |
| | 3 | 104 | | return Unparseable("IPTC", rawTime, logger); |
| | | 105 | | |
| | 6 | 106 | | return new RecordedDate.ClockTime(day + time, OffsetOf("IPTC", offset, IptcOffset, logger)); |
| | 118 | 107 | | } |
| | | 108 | | |
| | | 109 | | private static RecordedDate? AviOriginal(IReadOnlyList<Directory> directories, ILogger logger) |
| | 29 | 110 | | { |
| | 29 | 111 | | var raw = directories.OfType<AviDirectory>().FirstOrDefault()?.GetString(AviDirectory.TagDateTimeOriginal); |
| | 29 | 112 | | if (raw is null) |
| | 24 | 113 | | return null; |
| | 5 | 114 | | if (Parsed(AviClock, string.Join(' ', raw.Split([' ', '\n', '\0'], StringSplitOptions.RemoveEmptyEntries))) is n |
| | 2 | 115 | | return Unparseable("AVI", raw, logger); |
| | | 116 | | |
| | 3 | 117 | | return new RecordedDate.ClockTime(at, null); |
| | 29 | 118 | | } |
| | | 119 | | |
| | | 120 | | private static RecordedDate? MovieHeaderCreated(IReadOnlyList<Directory> directories, ILogger logger) |
| | 26 | 121 | | { |
| | 26 | 122 | | var created = directories.OfType<QuickTimeMovieHeaderDirectory>().FirstOrDefault()?.GetObject(QuickTimeMovieHead |
| | 26 | 123 | | return created is DateTime moment && moment != QuickTimeEpoch |
| | 26 | 124 | | ? new RecordedDate.Moment(Instant.FromDateTimeUtc(DateTime.SpecifyKind(moment, DateTimeKind.Utc))) |
| | 26 | 125 | | : null; |
| | 26 | 126 | | } |
| | | 127 | | |
| | | 128 | | private static (string Clock, string? Offset) SplitOffset(string raw, int shortestClock) |
| | 24 | 129 | | => raw.LastIndexOfAny(['Z', '+', '-']) is var start && start >= shortestClock ? (raw[..start], raw[start..]) : ( |
| | | 130 | | |
| | | 131 | | private static Offset? OffsetOf(string source, string? raw, OffsetPattern pattern, ILogger logger) |
| | 29 | 132 | | { |
| | 29 | 133 | | if (raw is null || raw.Trim(' ', ':').Length == 0) |
| | 11 | 134 | | return null; |
| | 18 | 135 | | if (Parsed(pattern, raw) is { } offset) |
| | 10 | 136 | | return offset; |
| | | 137 | | |
| | 8 | 138 | | Log.OffsetParseFailed(logger, source, raw); |
| | 8 | 139 | | return null; |
| | 29 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static T? Parsed<T>(IPattern<T> pattern, string raw) where T : struct |
| | 133 | 143 | | => pattern.Parse(raw) is { Success: true } result ? result.Value : null; |
| | | 144 | | |
| | | 145 | | private static RecordedDate? Unparseable(string source, string raw, ILogger logger) |
| | 22 | 146 | | { |
| | 22 | 147 | | Log.DateParseFailed(logger, source, raw); |
| | 22 | 148 | | return null; |
| | 22 | 149 | | } |
| | | 150 | | |
| | | 151 | | private static partial class Log |
| | | 152 | | { |
| | | 153 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Failed to parse {Source} date '{Raw}'.")] |
| | | 154 | | public static partial void DateParseFailed(ILogger logger, string source, string raw); |
| | | 155 | | |
| | | 156 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Failed to parse {Source} offset '{Raw}'; the date keeps no off |
| | | 157 | | public static partial void OffsetParseFailed(ILogger logger, string source, string raw); |
| | | 158 | | } |
| | | 159 | | } |