| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 6 | | using NodaTime; |
| | | 7 | | using System.IO.Abstractions; |
| | | 8 | | |
| | | 9 | | namespace Anichron.Worker.Ingestion.Middlewares; |
| | | 10 | | |
| | 17 | 11 | | internal sealed partial class PersistenceMiddleware( |
| | 17 | 12 | | IMediaAssetRepository repository, |
| | 17 | 13 | | IUnitOfWork unitOfWork, |
| | 17 | 14 | | IFileSystem fileSystem, |
| | 17 | 15 | | IClock clock, |
| | 17 | 16 | | IGuidFactory guidFactory, |
| | 17 | 17 | | ILogger<PersistenceMiddleware> logger) : IIngestionMiddleware |
| | | 18 | | { |
| | 0 | 19 | | public int Order => IngestionOrder.Persistence; |
| | 0 | 20 | | public bool CanInvoke(IngestionContext context) => true; |
| | | 21 | | |
| | | 22 | | public async Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct) |
| | 17 | 23 | | { |
| | 17 | 24 | | var asset = BuildAsset(context); |
| | | 25 | | |
| | 17 | 26 | | if (context.Item.SecondaryFile is { } secondary) |
| | 5 | 27 | | { |
| | | 28 | | // Ordering guarantees ContentHashingMiddleware ran first and set SecondaryHash; suppression is safe. |
| | 5 | 29 | | var secondaryAsset = BuildSecondaryAsset(secondary, context.SecondaryHash!, context); |
| | 5 | 30 | | asset.PairedAssetId = secondaryAsset.Id; |
| | 5 | 31 | | repository.Add(secondaryAsset); |
| | 5 | 32 | | } |
| | | 33 | | |
| | 17 | 34 | | repository.Add(asset); |
| | 17 | 35 | | await unitOfWork.SaveChangesAsync(ct); |
| | 17 | 36 | | context.Asset = asset; |
| | 17 | 37 | | Log.Persisted(logger, context.Item.RelativePath, asset.Id); |
| | 17 | 38 | | await next(context, ct); |
| | 17 | 39 | | } |
| | | 40 | | |
| | | 41 | | private MediaAsset BuildAsset(IngestionContext context) |
| | 17 | 42 | | { |
| | 17 | 43 | | var relativePath = context.Item.RelativePath; |
| | 17 | 44 | | var mediaType = context.Item.PrimaryMediaType; |
| | 17 | 45 | | var exif = context.Exif!; |
| | 17 | 46 | | var dateCaptured = exif.DateCaptured ?? FallbackDate(context.Item.AbsolutePath); |
| | | 47 | | |
| | 17 | 48 | | return new MediaAsset |
| | 17 | 49 | | { |
| | 17 | 50 | | Id = context.AssetId, |
| | 17 | 51 | | StorageConfigId = context.Config.Id, |
| | 17 | 52 | | FilePath = relativePath, |
| | 17 | 53 | | FileName = Path.GetFileName(relativePath), |
| | 17 | 54 | | ContentHash = context.ContentHash!, |
| | 17 | 55 | | DateCaptured = dateCaptured, |
| | 17 | 56 | | Month = dateCaptured.Month, |
| | 17 | 57 | | Day = dateCaptured.Day, |
| | 17 | 58 | | Year = dateCaptured.Year, |
| | 17 | 59 | | MediaType = mediaType, |
| | 17 | 60 | | IsSoftDeleted = false, |
| | 17 | 61 | | LastSeenOnNas = clock.GetCurrentInstant(), |
| | 17 | 62 | | ProxyFiles = [.. context.ProxyFiles], |
| | 17 | 63 | | Metadata = new Metadata |
| | 17 | 64 | | { |
| | 17 | 65 | | Width = exif.Width, |
| | 17 | 66 | | Height = exif.Height, |
| | 17 | 67 | | OrientationDegrees = exif.OrientationDegrees, |
| | 17 | 68 | | Latitude = exif.Latitude, |
| | 17 | 69 | | Longitude = exif.Longitude, |
| | 17 | 70 | | CameraMake = exif.CameraMake, |
| | 17 | 71 | | CameraModel = exif.CameraModel, |
| | 17 | 72 | | LensModel = exif.LensModel, |
| | 17 | 73 | | DurationInSeconds = exif.DurationInSeconds, |
| | 17 | 74 | | }, |
| | 17 | 75 | | }; |
| | 17 | 76 | | } |
| | | 77 | | |
| | | 78 | | private MediaAsset BuildSecondaryAsset(SecondaryFileDescriptor secondary, string hash, IngestionContext context) |
| | 5 | 79 | | { |
| | 5 | 80 | | var exif = context.Exif!; |
| | 5 | 81 | | var dateCaptured = exif.DateCaptured ?? FallbackDate(secondary.AbsolutePath); |
| | | 82 | | |
| | 5 | 83 | | return new MediaAsset |
| | 5 | 84 | | { |
| | 5 | 85 | | Id = guidFactory.NewGuid(), |
| | 5 | 86 | | StorageConfigId = context.Config.Id, |
| | 5 | 87 | | FilePath = secondary.RelativePath, |
| | 5 | 88 | | FileName = Path.GetFileName(secondary.RelativePath), |
| | 5 | 89 | | ContentHash = hash, |
| | 5 | 90 | | DateCaptured = dateCaptured, |
| | 5 | 91 | | Month = dateCaptured.Month, |
| | 5 | 92 | | Day = dateCaptured.Day, |
| | 5 | 93 | | Year = dateCaptured.Year, |
| | 5 | 94 | | MediaType = secondary.MediaType, |
| | 5 | 95 | | IsSoftDeleted = false, |
| | 5 | 96 | | LastSeenOnNas = clock.GetCurrentInstant(), |
| | 5 | 97 | | }; |
| | 5 | 98 | | } |
| | | 99 | | |
| | | 100 | | private LocalDateTime FallbackDate(string absolutePath) |
| | 1 | 101 | | { |
| | 1 | 102 | | var lastWrite = fileSystem.FileInfo.New(absolutePath).LastWriteTime; |
| | 1 | 103 | | return LocalDateTime.FromDateTime(lastWrite); |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static partial class Log |
| | | 107 | | { |
| | | 108 | | [LoggerMessage(Level = LogLevel.Information, Message = "Persisted {RelativePath} as asset {AssetId}")] |
| | | 109 | | public static partial void Persisted(ILogger logger, string relativePath, Guid assetId); |
| | | 110 | | } |
| | | 111 | | } |