< Summary

Line coverage
100%
Covered lines: 91
Uncovered lines: 0
Coverable lines: 91
Total lines: 972
Line coverage: 100%
Branch coverage
97%
Covered branches: 76
Total branches: 78
Branch coverage: 97.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Extraction/RecordedDates.cs

#LineLine coverage
 1using Anichron.Core.Domain;
 2using MetadataExtractor;
 3using MetadataExtractor.Formats.Avi;
 4using MetadataExtractor.Formats.Exif;
 5using MetadataExtractor.Formats.Iptc;
 6using MetadataExtractor.Formats.QuickTime;
 7using MetadataExtractor.Formats.Xmp;
 8using NodaTime;
 9using NodaTime.Text;
 10using XmpCore;
 11using Directory = MetadataExtractor.Directory;
 12
 13namespace 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
 16internal static partial class RecordedDates
 17{
 18    private const int shortestXmpClock = 16;
 19    private const int iptcClock = 6;
 20
 1721    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
 1825    private static LocalDateTimePattern ExifClock => LocalDateTimePattern.CreateWithInvariantCulture("uuuu':'MM':'dd HH'
 1426    private static OffsetPattern ExifOffset => OffsetPattern.CreateWithInvariantCulture("+HH:mm");
 1727    private static LocalDatePattern XmpDay => LocalDatePattern.Iso;
 2228    private static LocalDateTimePattern XmpClock => LocalDateTimePattern.ExtendedIso;
 929    private static OffsetPattern XmpOffset => OffsetPattern.CreateWithInvariantCulture("Z+HH:mm");
 1230    private static LocalDatePattern IptcDay => LocalDatePattern.CreateWithInvariantCulture("uuuuMMdd");
 931    private static LocalTimePattern IptcTime => LocalTimePattern.CreateWithInvariantCulture("HHmmss");
 632    private static OffsetPattern IptcOffset => OffsetPattern.CreateWithInvariantCulture("+HHmm");
 533    private static LocalDateTimePattern AviClock => LocalDateTimePattern.CreateWithInvariantCulture("ddd MMM d HH':'mm':
 1934    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,
 18642        => SourcesOf(mediaType, appleCreationDate).Select(source => source(directories, logger)).FirstOrDefault(date => 
 43
 44    private static Source[] SourcesOf(MediaType mediaType, string? appleCreationDate)
 18645        => mediaType == MediaType.Video
 18646            ? [(_, logger) => AppleCreationDate(appleCreationDate, logger), AviOriginal, MovieHeaderCreated]
 18647            : [ExifOriginal, XmpDateCreated, IptcDateCreated];
 48
 49    private static OffsetDateTimePattern AppleClock(string offset)
 3850        => OffsetDateTimePattern.CreateWithInvariantCulture($"uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<{offset}>");
 51
 52    private static RecordedDate? AppleCreationDate(string? raw, ILogger logger)
 4353    {
 4354        if (raw is null)
 2455            return null;
 1956        if (AppleClocks.Select(pattern => Parsed(pattern, raw)).FirstOrDefault(at => at is not null) is { } at)
 1257            return new RecordedDate.ClockTime(at.LocalDateTime, at.Offset);
 758        if (Parsed(XmpClock, raw) is { } clock)
 259            return new RecordedDate.ClockTime(clock, null);
 60
 561        return Unparseable("Apple", raw, logger);
 4362    }
 63
 64    private static RecordedDate? ExifOriginal(IReadOnlyList<Directory> directories, ILogger logger)
 14365    {
 14366        var subIfd = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
 14367        var raw = subIfd?.GetString(ExifDirectoryBase.TagDateTimeOriginal);
 14368        if (raw is null)
 12569            return null;
 1870        if (Parsed(ExifClock, raw) is not { } at)
 471            return Unparseable("EXIF", raw, logger);
 72
 1473        return new RecordedDate.ClockTime(at, OffsetOf("EXIF", subIfd!.GetString(ExifDirectoryBase.TagTimeZoneOriginal),
 14374    }
 75
 76    private static RecordedDate? XmpDateCreated(IReadOnlyList<Directory> directories, ILogger logger)
 12977    {
 12978        var raw = directories.OfType<XmpDirectory>().FirstOrDefault()?.XmpMeta?.GetPropertyString(XmpConstants.NsPhotosh
 12979        if (raw is null)
 11280            return null;
 1781        if (Parsed(XmpDay, raw) is { } day)
 282            return new RecordedDate.DateAlone(day);
 83
 1584        var (clock, offset) = SplitOffset(raw, shortestXmpClock);
 1585        if (Parsed(XmpClock, clock.Length == shortestXmpClock ? clock + ":00" : clock) is not { } at)
 686            return Unparseable("XMP", raw, logger);
 87
 988        return new RecordedDate.ClockTime(at, OffsetOf("XMP", offset, XmpOffset, logger));
 12989    }
 90
 91    private static RecordedDate? IptcDateCreated(IReadOnlyList<Directory> directories, ILogger logger)
 11892    {
 11893        var iptc = directories.OfType<IptcDirectory>().FirstOrDefault();
 11894        var raw = iptc?.GetString(IptcDirectory.TagDateCreated);
 11895        if (raw is null)
 10696            return null;
 1297        if (Parsed(IptcDay, raw) is not { } day)
 298            return Unparseable("IPTC", raw, logger);
 1099        if (iptc!.GetString(IptcDirectory.TagTimeCreated) is not { } rawTime)
 1100            return new RecordedDate.DateAlone(day);
 101
 9102        var (clock, offset) = SplitOffset(rawTime, iptcClock);
 9103        if (Parsed(IptcTime, clock) is not { } time)
 3104            return Unparseable("IPTC", rawTime, logger);
 105
 6106        return new RecordedDate.ClockTime(day + time, OffsetOf("IPTC", offset, IptcOffset, logger));
 118107    }
 108
 109    private static RecordedDate? AviOriginal(IReadOnlyList<Directory> directories, ILogger logger)
 29110    {
 29111        var raw = directories.OfType<AviDirectory>().FirstOrDefault()?.GetString(AviDirectory.TagDateTimeOriginal);
 29112        if (raw is null)
 24113            return null;
 5114        if (Parsed(AviClock, string.Join(' ', raw.Split([' ', '\n', '\0'], StringSplitOptions.RemoveEmptyEntries))) is n
 2115            return Unparseable("AVI", raw, logger);
 116
 3117        return new RecordedDate.ClockTime(at, null);
 29118    }
 119
 120    private static RecordedDate? MovieHeaderCreated(IReadOnlyList<Directory> directories, ILogger logger)
 26121    {
 26122        var created = directories.OfType<QuickTimeMovieHeaderDirectory>().FirstOrDefault()?.GetObject(QuickTimeMovieHead
 26123        return created is DateTime moment && moment != QuickTimeEpoch
 26124            ? new RecordedDate.Moment(Instant.FromDateTimeUtc(DateTime.SpecifyKind(moment, DateTimeKind.Utc)))
 26125            : null;
 26126    }
 127
 128    private static (string Clock, string? Offset) SplitOffset(string raw, int shortestClock)
 24129        => 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)
 29132    {
 29133        if (raw is null || raw.Trim(' ', ':').Length == 0)
 11134            return null;
 18135        if (Parsed(pattern, raw) is { } offset)
 10136            return offset;
 137
 8138        Log.OffsetParseFailed(logger, source, raw);
 8139        return null;
 29140    }
 141
 142    private static T? Parsed<T>(IPattern<T> pattern, string raw) where T : struct
 133143        => pattern.Parse(raw) is { Success: true } result ? result.Value : null;
 144
 145    private static RecordedDate? Unparseable(string source, string raw, ILogger logger)
 22146    {
 22147        Log.DateParseFailed(logger, source, raw);
 22148        return null;
 22149    }
 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}