| | | 1 | | using MetadataExtractor; |
| | | 2 | | using MetadataExtractor.Formats.Exif.Makernotes; |
| | | 3 | | using System.IO.Abstractions; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Worker.Bursts; |
| | | 6 | | |
| | | 7 | | /// <summary>What burst detection reads from a photo's original.</summary> |
| | | 8 | | /// <param name="CameraBurstId">Apple's BurstUUID, or null when the camera wrote none.</param> |
| | | 9 | | internal sealed record OriginalPhotoFacts(string? CameraBurstId) |
| | | 10 | | { |
| | | 11 | | internal static OriginalPhotoFacts Unknown { get; } = new(CameraBurstId: null); |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | internal interface IOriginalPhotoReader |
| | | 15 | | { |
| | | 16 | | /// <summary>Reads the original once; an unreadable one is logged and yields no facts.</summary> |
| | | 17 | | /// <param name="absolutePath">The original's path in the container.</param> |
| | | 18 | | OriginalPhotoFacts Read(string absolutePath); |
| | | 19 | | } |
| | | 20 | | |
| | 3 | 21 | | internal sealed partial class OriginalPhotoReader(IFileSystem fileSystem, ILogger<OriginalPhotoReader> logger) |
| | | 22 | | : IOriginalPhotoReader |
| | | 23 | | { |
| | | 24 | | public OriginalPhotoFacts Read(string absolutePath) |
| | 2 | 25 | | { |
| | | 26 | | try |
| | 2 | 27 | | { |
| | 2 | 28 | | using var stream = fileSystem.File.OpenRead(absolutePath); |
| | 1 | 29 | | return Describe(ImageMetadataReader.ReadMetadata(stream)); |
| | | 30 | | } |
| | 1 | 31 | | catch (Exception ex) |
| | 1 | 32 | | { |
| | 1 | 33 | | Log.Unreadable(logger, absolutePath, ex); |
| | 1 | 34 | | return OriginalPhotoFacts.Unknown; |
| | | 35 | | } |
| | 2 | 36 | | } |
| | | 37 | | |
| | | 38 | | internal static OriginalPhotoFacts Describe(IReadOnlyList<MetadataExtractor.Directory> directories) |
| | 5 | 39 | | { |
| | 5 | 40 | | var burstId = directories.OfType<AppleMakernoteDirectory>().FirstOrDefault()?.GetString(AppleMakernoteDirectory. |
| | 5 | 41 | | return new(string.IsNullOrWhiteSpace(burstId) ? null : burstId); |
| | 5 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static partial class Log |
| | | 45 | | { |
| | | 46 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Could not read {Path} for burst detection; it is grouped by |
| | | 47 | | public static partial void Unreadable(ILogger logger, string path, Exception exception); |
| | | 48 | | } |
| | | 49 | | } |