| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 5 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 6 | | using NodaTime; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Ingestion.Classification; |
| | | 9 | | |
| | 88 | 10 | | internal sealed partial class KnownFileReconciler( |
| | 88 | 11 | | IClock clock, |
| | 88 | 12 | | IMediaAssetRepository repository, |
| | 88 | 13 | | StoredContent storedContent, |
| | 88 | 14 | | IGuidFactory guidFactory, |
| | 88 | 15 | | PairedMovProxyWriter pairedMovProxyWriter, |
| | 88 | 16 | | ILogger<KnownFileReconciler> logger) |
| | | 17 | | { |
| | | 18 | | public Task<bool> ReconcileAsync(IngestionContext context, FileClassification file, string relativePath, Cancellatio |
| | 67 | 19 | | => ReconcileAsync(context, file, relativePath, photo: null, ct); |
| | | 20 | | |
| | | 21 | | public Task<bool> ReconcileSecondaryAsync( |
| | | 22 | | IngestionContext context, FileClassification file, FileClassification photo, string relativePath, CancellationTo |
| | 47 | 23 | | => ReconcileAsync(context, file, relativePath, photo, ct); |
| | | 24 | | |
| | | 25 | | private Task<bool> ReconcileAsync( |
| | 114 | 26 | | IngestionContext context, FileClassification file, string relativePath, FileClassification? photo, CancellationT |
| | 114 | 27 | | { |
| | 27 | 28 | | FileOutcome.Unchanged => Task.FromResult(Refresh(file.Stored!, relativePath)), |
| | 15 | 29 | | FileOutcome.Moved => Task.FromResult(Move(file.Stored!, relativePath)), |
| | 9 | 30 | | FileOutcome.DuplicateCopy => Task.FromResult(SkipDuplicate(file.Stored!, relativePath)), |
| | 7 | 31 | | FileOutcome.OverwrittenByCopy => RetireOverwrittenAsync(context, file.Stored!, file.Retired!, relativePath, |
| | 22 | 32 | | FileOutcome.OverwrittenByMove => TakeMovedContentAsync(context, file, relativePath, photo, ct), |
| | 114 | 33 | | FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged |
| | 9 | 34 | | => TakeVideoContentAsync(context, file.Stored!, relativePath, ct), |
| | 11 | 35 | | FileOutcome.AutoRestored or FileOutcome.AutoRestoredMoved => AutoRestoreAsync(context, file.Stored!, relativ |
| | 7 | 36 | | FileOutcome.LoneLivePhotoVideo => LeaveLivePhotoVideoAsync(context, file.Stored!, file.Retired, relativePath |
| | 7 | 37 | | _ => Task.FromResult(false), |
| | 114 | 38 | | }; |
| | | 39 | | |
| | | 40 | | private bool Refresh(MediaAsset stored, string relativePath) |
| | 27 | 41 | | { |
| | 27 | 42 | | stored.LastSeenOnNas = clock.GetCurrentInstant(); |
| | 27 | 43 | | Log.AlreadyIngested(logger, relativePath); |
| | 27 | 44 | | return true; |
| | 27 | 45 | | } |
| | | 46 | | |
| | | 47 | | private bool Move(MediaAsset stored, string relativePath) |
| | 15 | 48 | | { |
| | 15 | 49 | | Log.Moved(logger, stored.Id, stored.FilePath, relativePath); |
| | 15 | 50 | | MoveTo(stored, relativePath); |
| | 15 | 51 | | stored.LastSeenOnNas = clock.GetCurrentInstant(); |
| | 15 | 52 | | return true; |
| | 15 | 53 | | } |
| | | 54 | | |
| | | 55 | | private bool SkipDuplicate(MediaAsset stored, string relativePath) |
| | 16 | 56 | | { |
| | 16 | 57 | | Log.DuplicateCopy(logger, stored.ContentHash, stored.StorageConfigId, relativePath, stored.FilePath); |
| | 16 | 58 | | return false; |
| | 16 | 59 | | } |
| | | 60 | | |
| | | 61 | | private async Task<bool> RetireOverwrittenAsync( |
| | | 62 | | IngestionContext context, MediaAsset copied, MediaAsset atPath, string relativePath, CancellationToken ct) |
| | 7 | 63 | | { |
| | 7 | 64 | | SkipDuplicate(copied, relativePath); |
| | 7 | 65 | | await storedContent.RetireAsync(context, atPath, ct); |
| | 7 | 66 | | Log.OverwrittenByCopy(logger, atPath.Id, relativePath, copied.FilePath); |
| | 7 | 67 | | return true; |
| | 7 | 68 | | } |
| | | 69 | | |
| | | 70 | | private async Task<bool> LeaveLivePhotoVideoAsync( |
| | | 71 | | IngestionContext context, MediaAsset video, MediaAsset? atPath, string relativePath, CancellationToken ct) |
| | 7 | 72 | | { |
| | 7 | 73 | | if (atPath is null) |
| | 5 | 74 | | { |
| | 5 | 75 | | Log.LeftLivePhotoVideo(logger, relativePath, video.Id); |
| | 5 | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | 2 | 79 | | await storedContent.RetireAsync(context, atPath, ct); |
| | 2 | 80 | | Log.OverwrittenByLivePhotoVideo(logger, atPath.Id, relativePath, video.Id, video.FilePath); |
| | 2 | 81 | | return true; |
| | 7 | 82 | | } |
| | | 83 | | |
| | | 84 | | private async Task<bool> TakeMovedContentAsync( |
| | | 85 | | IngestionContext context, FileClassification file, string relativePath, FileClassification? photo, CancellationT |
| | 22 | 86 | | { |
| | 22 | 87 | | var (atPath, moved) = (file.Stored!, file.Retired!); |
| | 22 | 88 | | await repository.LoadMetadataAsync(moved, ct); |
| | 22 | 89 | | MoveTo(atPath, relativePath); |
| | 22 | 90 | | await storedContent.TakeAsync(context, atPath, AssetContent.Of(moved, [.. moved.ProxyFiles.Select(proxy => Proxy |
| | 22 | 91 | | if (photo is not null) |
| | 7 | 92 | | PairedMovResolver.DateFromPhoto(atPath, photo.Row); |
| | | 93 | | |
| | 22 | 94 | | await storedContent.RetireAsync(context, moved, ct); |
| | 22 | 95 | | Log.OverwrittenByMove(logger, atPath.Id, relativePath, moved.Id, moved.FilePath); |
| | 22 | 96 | | return true; |
| | 22 | 97 | | } |
| | | 98 | | |
| | | 99 | | private async Task<bool> TakeVideoContentAsync( |
| | | 100 | | IngestionContext context, MediaAsset video, string relativePath, CancellationToken ct) |
| | 9 | 101 | | { |
| | 9 | 102 | | Log.VideoContentChanged(logger, video.Id, relativePath, video.ContentHash, context.SecondaryHash!); |
| | 9 | 103 | | MoveTo(video, relativePath); |
| | 9 | 104 | | var proxies = await pairedMovProxyWriter.WriteForNewContentAsync(context, video, ct); |
| | 9 | 105 | | await storedContent.TakeAsync(context, video, new AssetContent(context.SecondaryHash!, null, MediaType.Video, Me |
| | 9 | 106 | | PairedMovResolver.DateFromPhoto(video, context.Classification!.Primary.Row); |
| | 9 | 107 | | return true; |
| | 9 | 108 | | } |
| | | 109 | | |
| | | 110 | | private async Task<bool> AutoRestoreAsync(IngestionContext context, MediaAsset trashed, string relativePath, Cancell |
| | 11 | 111 | | { |
| | 11 | 112 | | MoveTo(trashed, relativePath); |
| | 11 | 113 | | await storedContent.ReactivateAsync(context, trashed, ct); |
| | 11 | 114 | | Log.AutoRestored(logger, trashed.Id, relativePath); |
| | 11 | 115 | | if (context.Classification?.Primary is { Stored: { } photo } primary |
| | 11 | 116 | | && photo.PairedAssetId == trashed.Id |
| | 11 | 117 | | && primary.Outcome is not (FileOutcome.AutoRestored or FileOutcome.AutoRestoredMoved or FileOutcome.AutoRest |
| | 3 | 118 | | { |
| | 3 | 119 | | Log.LivePhotoAgain(logger, photo.Id, trashed.Id, relativePath); |
| | 3 | 120 | | } |
| | | 121 | | |
| | 11 | 122 | | return true; |
| | 11 | 123 | | } |
| | | 124 | | |
| | 23 | 125 | | private ProxyFile ProxyOf(MediaAsset owner, ProxyFile source) => new() |
| | 23 | 126 | | { |
| | 23 | 127 | | Id = guidFactory.NewGuid(), |
| | 23 | 128 | | AssetId = owner.Id, |
| | 23 | 129 | | ProxyType = source.ProxyType, |
| | 23 | 130 | | ProxyPath = source.ProxyPath, |
| | 23 | 131 | | SizeBytes = source.SizeBytes, |
| | 23 | 132 | | CreatedAt = source.CreatedAt, |
| | 23 | 133 | | }; |
| | | 134 | | |
| | | 135 | | private static void MoveTo(MediaAsset asset, string relativePath) |
| | 57 | 136 | | { |
| | 57 | 137 | | asset.FilePath = relativePath; |
| | 57 | 138 | | asset.FileName = Path.GetFileName(relativePath); |
| | 57 | 139 | | } |
| | | 140 | | |
| | | 141 | | private static partial class Log |
| | | 142 | | { |
| | | 143 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Already ingested {RelativePath}, refreshed last seen.")] |
| | | 144 | | public static partial void AlreadyIngested(ILogger logger, string relativePath); |
| | | 145 | | |
| | | 146 | | [LoggerMessage( |
| | | 147 | | Level = LogLevel.Debug, |
| | | 148 | | Message = "Duplicate content {ContentHash} in storage config {StorageConfigId}: {RelativePath} is a copy of |
| | | 149 | | public static partial void DuplicateCopy( |
| | | 150 | | ILogger logger, string contentHash, Guid storageConfigId, string relativePath, string storedPath); |
| | | 151 | | |
| | | 152 | | [LoggerMessage(Level = LogLevel.Information, Message = "Asset {AssetId} moved from {OldPath} to {NewPath}")] |
| | | 153 | | public static partial void Moved(ILogger logger, Guid assetId, string oldPath, string newPath); |
| | | 154 | | |
| | | 155 | | [LoggerMessage( |
| | | 156 | | Level = LogLevel.Information, |
| | | 157 | | Message = "Video asset {AssetId} at {RelativePath} changed content from {OldHash} to {NewHash}, updating it |
| | | 158 | | public static partial void VideoContentChanged(ILogger logger, Guid assetId, string relativePath, string oldHash |
| | | 159 | | |
| | | 160 | | [LoggerMessage( |
| | | 161 | | Level = LogLevel.Information, |
| | | 162 | | Message = "Soft-deleted asset {AssetId} at {RelativePath}: the path now holds a copy of {CopiedPath}.")] |
| | | 163 | | public static partial void OverwrittenByCopy(ILogger logger, Guid assetId, string relativePath, string copiedPat |
| | | 164 | | |
| | | 165 | | [LoggerMessage( |
| | | 166 | | Level = LogLevel.Information, |
| | | 167 | | Message = "Asset {AssetId} at {RelativePath} took the content of asset {MovedId}, moved there from {MovedPat |
| | | 168 | | + "soft-deleted asset {MovedId}.")] |
| | | 169 | | public static partial void OverwrittenByMove(ILogger logger, Guid assetId, string relativePath, Guid movedId, st |
| | | 170 | | |
| | | 171 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Auto-restored asset {AssetId} at {RelativePath}.")] |
| | | 172 | | public static partial void AutoRestored(ILogger logger, Guid assetId, string relativePath); |
| | | 173 | | |
| | | 174 | | [LoggerMessage( |
| | | 175 | | Level = LogLevel.Information, |
| | | 176 | | Message = "Asset {PhotoId} is a Live Photo again: its video, asset {VideoId} at {RelativePath}, was auto-res |
| | | 177 | | public static partial void LivePhotoAgain(ILogger logger, Guid photoId, Guid videoId, string relativePath); |
| | | 178 | | |
| | | 179 | | [LoggerMessage( |
| | | 180 | | Level = LogLevel.Debug, |
| | | 181 | | Message = "Left {RelativePath} alone: it holds the video of a Live Photo, asset {VideoId}, which follows onl |
| | | 182 | | public static partial void LeftLivePhotoVideo(ILogger logger, string relativePath, Guid videoId); |
| | | 183 | | |
| | | 184 | | [LoggerMessage( |
| | | 185 | | Level = LogLevel.Information, |
| | | 186 | | Message = "Soft-deleted asset {AssetId} at {RelativePath}: the path now holds a copy of the video of a Live |
| | | 187 | | + "{VideoId} at {VideoPath}.")] |
| | | 188 | | public static partial void OverwrittenByLivePhotoVideo( |
| | | 189 | | ILogger logger, Guid assetId, string relativePath, Guid videoId, string videoPath); |
| | | 190 | | } |
| | | 191 | | } |