| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Anichron.Worker.Ingestion.Extraction; |
| | | 3 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 4 | | using MetadataExtractor; |
| | | 5 | | using MetadataExtractor.Formats.Exif; |
| | | 6 | | using System.IO.Abstractions; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Ingestion.Middlewares; |
| | | 9 | | |
| | 191 | 10 | | internal sealed partial class MetadataExtractionMiddleware( |
| | 191 | 11 | | IFileSystem fileSystem, |
| | 191 | 12 | | ILogger<MetadataExtractionMiddleware> logger) : IIngestionMiddleware |
| | | 13 | | { |
| | 0 | 14 | | public int Order => IngestionOrder.MetadataExtraction; |
| | 1 | 15 | | public bool CanInvoke(IngestionContext context) => true; |
| | | 16 | | |
| | | 17 | | public Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct) |
| | 58 | 18 | | { |
| | 58 | 19 | | context.Extracted = ExtractFrom(context.Item.AbsolutePath, context.Item.PrimaryMediaType); |
| | 58 | 20 | | return next(context, ct); |
| | 58 | 21 | | } |
| | | 22 | | |
| | | 23 | | private ExtractedMedia ExtractFrom(string absolutePath, MediaType mediaType) |
| | 58 | 24 | | { |
| | | 25 | | try |
| | 58 | 26 | | { |
| | 58 | 27 | | using var stream = fileSystem.File.OpenRead(absolutePath); |
| | 57 | 28 | | var directories = ImageMetadataReader.ReadMetadata(stream); |
| | 54 | 29 | | return Extract(directories, mediaType == MediaType.Video ? AppleCreationDateOf(stream, absolutePath) : null, |
| | | 30 | | } |
| | 4 | 31 | | catch (Exception ex) when (BrokenFile.Rejected(ex)) |
| | 3 | 32 | | { |
| | 3 | 33 | | Log.BrokenFileUnreadable(logger, absolutePath, ex.Message); |
| | 3 | 34 | | return ExtractedMedia.Empty; |
| | | 35 | | } |
| | 1 | 36 | | catch (Exception ex) |
| | 1 | 37 | | { |
| | 1 | 38 | | Log.ExtractionFailed(logger, absolutePath, ex); |
| | 1 | 39 | | return ExtractedMedia.Empty; |
| | | 40 | | } |
| | 58 | 41 | | } |
| | | 42 | | |
| | | 43 | | private string? AppleCreationDateOf(Stream stream, string absolutePath) |
| | 15 | 44 | | { |
| | | 45 | | try |
| | 15 | 46 | | { |
| | 15 | 47 | | stream.Position = 0; |
| | 15 | 48 | | return AppleCreationDates.Read(stream); |
| | | 49 | | } |
| | 1 | 50 | | catch (Exception ex) |
| | 1 | 51 | | { |
| | 1 | 52 | | Log.AppleCreationDateUnreadable(logger, absolutePath, ex); |
| | 1 | 53 | | return null; |
| | | 54 | | } |
| | 15 | 55 | | } |
| | | 56 | | |
| | | 57 | | internal ExtractedMedia Extract(IReadOnlyList<MetadataExtractor.Directory> directories, string? appleCreationDate, M |
| | 186 | 58 | | { |
| | 186 | 59 | | var cameraInfo = directories.OfType<ExifIfd0Directory>().FirstOrDefault(); |
| | 186 | 60 | | var captureDetails = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault(); |
| | 186 | 61 | | var (width, height, orientation) = Frames.Of(directories); |
| | 186 | 62 | | var (latitude, longitude) = Positions.Of(directories, logger); |
| | | 63 | | |
| | 186 | 64 | | return new ExtractedMedia( |
| | 186 | 65 | | RecordedDates.Of(directories, appleCreationDate, mediaType, logger), |
| | 186 | 66 | | new MediaDescription( |
| | 186 | 67 | | width, |
| | 186 | 68 | | height, |
| | 186 | 69 | | orientation, |
| | 186 | 70 | | latitude, |
| | 186 | 71 | | longitude, |
| | 186 | 72 | | cameraInfo?.GetString(ExifDirectoryBase.TagMake), |
| | 186 | 73 | | cameraInfo?.GetString(ExifDirectoryBase.TagModel), |
| | 186 | 74 | | captureDetails?.GetString(ExifDirectoryBase.TagLensModel), |
| | 186 | 75 | | Durations.Of(directories))); |
| | 186 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static partial class Log |
| | | 79 | | { |
| | | 80 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Failed to extract metadata from {Path}.")] |
| | | 81 | | public static partial void ExtractionFailed(ILogger logger, string path, Exception exception); |
| | | 82 | | |
| | | 83 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Failed to extract metadata from {Path}: {Reason}")] |
| | | 84 | | public static partial void BrokenFileUnreadable(ILogger logger, string path, string reason); |
| | | 85 | | |
| | | 86 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Failed to read the Apple creation date of {Path}; the video is |
| | | 87 | | public static partial void AppleCreationDateUnreadable(ILogger logger, string path, Exception exception); |
| | | 88 | | } |
| | | 89 | | } |