< Summary

Line coverage
100%
Covered lines: 114
Uncovered lines: 0
Coverable lines: 114
Total lines: 829
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
Branch coverage: 100%
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/Classification/KnownFileReconciler.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Data.Repository;
 3using Anichron.Core.Domain;
 4using Anichron.Worker.Ingestion.Pipeline;
 5using Anichron.Worker.Ingestion.Proxy;
 6using NodaTime;
 7
 8namespace Anichron.Worker.Ingestion.Classification;
 9
 8810internal sealed partial class KnownFileReconciler(
 8811    IClock clock,
 8812    IMediaAssetRepository repository,
 8813    StoredContent storedContent,
 8814    IGuidFactory guidFactory,
 8815    PairedMovProxyWriter pairedMovProxyWriter,
 8816    ILogger<KnownFileReconciler> logger)
 17{
 18    public Task<bool> ReconcileAsync(IngestionContext context, FileClassification file, string relativePath, Cancellatio
 6719        => ReconcileAsync(context, file, relativePath, photo: null, ct);
 20
 21    public Task<bool> ReconcileSecondaryAsync(
 22        IngestionContext context, FileClassification file, FileClassification photo, string relativePath, CancellationTo
 4723        => ReconcileAsync(context, file, relativePath, photo, ct);
 24
 25    private Task<bool> ReconcileAsync(
 11426        IngestionContext context, FileClassification file, string relativePath, FileClassification? photo, CancellationT
 11427        {
 2728            FileOutcome.Unchanged => Task.FromResult(Refresh(file.Stored!, relativePath)),
 1529            FileOutcome.Moved => Task.FromResult(Move(file.Stored!, relativePath)),
 930            FileOutcome.DuplicateCopy => Task.FromResult(SkipDuplicate(file.Stored!, relativePath)),
 731            FileOutcome.OverwrittenByCopy => RetireOverwrittenAsync(context, file.Stored!, file.Retired!, relativePath, 
 2232            FileOutcome.OverwrittenByMove => TakeMovedContentAsync(context, file, relativePath, photo, ct),
 11433            FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged
 934                => TakeVideoContentAsync(context, file.Stored!, relativePath, ct),
 1135            FileOutcome.AutoRestored or FileOutcome.AutoRestoredMoved => AutoRestoreAsync(context, file.Stored!, relativ
 736            FileOutcome.LoneLivePhotoVideo => LeaveLivePhotoVideoAsync(context, file.Stored!, file.Retired, relativePath
 737            _ => Task.FromResult(false),
 11438        };
 39
 40    private bool Refresh(MediaAsset stored, string relativePath)
 2741    {
 2742        stored.LastSeenOnNas = clock.GetCurrentInstant();
 2743        Log.AlreadyIngested(logger, relativePath);
 2744        return true;
 2745    }
 46
 47    private bool Move(MediaAsset stored, string relativePath)
 1548    {
 1549        Log.Moved(logger, stored.Id, stored.FilePath, relativePath);
 1550        MoveTo(stored, relativePath);
 1551        stored.LastSeenOnNas = clock.GetCurrentInstant();
 1552        return true;
 1553    }
 54
 55    private bool SkipDuplicate(MediaAsset stored, string relativePath)
 1656    {
 1657        Log.DuplicateCopy(logger, stored.ContentHash, stored.StorageConfigId, relativePath, stored.FilePath);
 1658        return false;
 1659    }
 60
 61    private async Task<bool> RetireOverwrittenAsync(
 62        IngestionContext context, MediaAsset copied, MediaAsset atPath, string relativePath, CancellationToken ct)
 763    {
 764        SkipDuplicate(copied, relativePath);
 765        await storedContent.RetireAsync(context, atPath, ct);
 766        Log.OverwrittenByCopy(logger, atPath.Id, relativePath, copied.FilePath);
 767        return true;
 768    }
 69
 70    private async Task<bool> LeaveLivePhotoVideoAsync(
 71        IngestionContext context, MediaAsset video, MediaAsset? atPath, string relativePath, CancellationToken ct)
 772    {
 773        if (atPath is null)
 574        {
 575            Log.LeftLivePhotoVideo(logger, relativePath, video.Id);
 576            return false;
 77        }
 78
 279        await storedContent.RetireAsync(context, atPath, ct);
 280        Log.OverwrittenByLivePhotoVideo(logger, atPath.Id, relativePath, video.Id, video.FilePath);
 281        return true;
 782    }
 83
 84    private async Task<bool> TakeMovedContentAsync(
 85        IngestionContext context, FileClassification file, string relativePath, FileClassification? photo, CancellationT
 2286    {
 2287        var (atPath, moved) = (file.Stored!, file.Retired!);
 2288        await repository.LoadMetadataAsync(moved, ct);
 2289        MoveTo(atPath, relativePath);
 2290        await storedContent.TakeAsync(context, atPath, AssetContent.Of(moved, [.. moved.ProxyFiles.Select(proxy => Proxy
 2291        if (photo is not null)
 792            PairedMovResolver.DateFromPhoto(atPath, photo.Row);
 93
 2294        await storedContent.RetireAsync(context, moved, ct);
 2295        Log.OverwrittenByMove(logger, atPath.Id, relativePath, moved.Id, moved.FilePath);
 2296        return true;
 2297    }
 98
 99    private async Task<bool> TakeVideoContentAsync(
 100        IngestionContext context, MediaAsset video, string relativePath, CancellationToken ct)
 9101    {
 9102        Log.VideoContentChanged(logger, video.Id, relativePath, video.ContentHash, context.SecondaryHash!);
 9103        MoveTo(video, relativePath);
 9104        var proxies = await pairedMovProxyWriter.WriteForNewContentAsync(context, video, ct);
 9105        await storedContent.TakeAsync(context, video, new AssetContent(context.SecondaryHash!, null, MediaType.Video, Me
 9106        PairedMovResolver.DateFromPhoto(video, context.Classification!.Primary.Row);
 9107        return true;
 9108    }
 109
 110    private async Task<bool> AutoRestoreAsync(IngestionContext context, MediaAsset trashed, string relativePath, Cancell
 11111    {
 11112        MoveTo(trashed, relativePath);
 11113        await storedContent.ReactivateAsync(context, trashed, ct);
 11114        Log.AutoRestored(logger, trashed.Id, relativePath);
 11115        if (context.Classification?.Primary is { Stored: { } photo } primary
 11116            && photo.PairedAssetId == trashed.Id
 11117            && primary.Outcome is not (FileOutcome.AutoRestored or FileOutcome.AutoRestoredMoved or FileOutcome.AutoRest
 3118        {
 3119            Log.LivePhotoAgain(logger, photo.Id, trashed.Id, relativePath);
 3120        }
 121
 11122        return true;
 11123    }
 124
 23125    private ProxyFile ProxyOf(MediaAsset owner, ProxyFile source) => new()
 23126    {
 23127        Id = guidFactory.NewGuid(),
 23128        AssetId = owner.Id,
 23129        ProxyType = source.ProxyType,
 23130        ProxyPath = source.ProxyPath,
 23131        SizeBytes = source.SizeBytes,
 23132        CreatedAt = source.CreatedAt,
 23133    };
 134
 135    private static void MoveTo(MediaAsset asset, string relativePath)
 57136    {
 57137        asset.FilePath = relativePath;
 57138        asset.FileName = Path.GetFileName(relativePath);
 57139    }
 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}