< Summary

Line coverage
100%
Covered lines: 58
Uncovered lines: 0
Coverable lines: 58
Total lines: 514
Line coverage: 100%
Branch coverage
97%
Covered branches: 33
Total branches: 34
Branch coverage: 97%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%11100%
File 2: .ctor(...)100%11100%
File 2: PairAsync()100%88100%
File 2: DateFromPhoto(...)100%22100%
File 2: TakeFromOtherPhotosAsync()90%1010100%
File 2: LetGoOfPreviousAsync()100%1414100%
File 2: Add(...)100%11100%

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/PairedMovResolver.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Data.Repository;
 3using Anichron.Core.Domain;
 4using Anichron.Worker.Ingestion.Classification;
 5using Anichron.Worker.Ingestion.Pipeline;
 6using NodaTime;
 7
 8namespace Anichron.Worker.Ingestion;
 9
 13510internal sealed partial class PairedMovResolver(
 13511    IMediaAssetRepository repository,
 13512    StoredContent storedContent,
 13513    IClock clock,
 13514    IGuidFactory guidFactory,
 13515    ILogger<PairedMovResolver> logger)
 16{
 17    /// <summary>
 18    /// Points <paramref name="photo"/> at the item's MOV row, stored or added now, and marks that row a Live Photo's vi
 19    /// every other photo still pointing at a stored one lets go of it. The video <paramref name="photo"/> lets go of is
 20    /// soft-deleted, unless the item's video half retires it.
 21    /// </summary>
 22    /// <param name="context">The classified Live Photo item, which stages the soft-delete count.</param>
 23    /// <param name="photo">The row that becomes the video's photo, dated already.</param>
 24    /// <param name="ct">Cancels the lookups of the other photos and of the previous video.</param>
 25    /// <returns>The MOV row.</returns>
 26    public async Task<MediaAsset> PairAsync(IngestionContext context, MediaAsset photo, CancellationToken ct)
 4827    {
 4828        var video = context.Classification!.Secondary!;
 4829        var stored = video.Row;
 4830        if (stored is not null)
 2731            await TakeFromOtherPhotosAsync(photo, stored, ct);
 32
 4833        var mov = stored ?? Add(context);
 4834        if (stored is null || video.TakesContent)
 3135            DateFromPhoto(mov, photo);
 36
 4837        await LetGoOfPreviousAsync(context, photo, mov, video.Retired, ct);
 4838        photo.PairedAssetId = mov.Id;
 4839        mov.IsLivePhotoVideo = true;
 4840        return mov;
 4841    }
 42
 43    /// <summary>
 44    /// The one rule that dates a Live Photo's video: it takes its photo's whole capture date, and is undated when the p
 45    /// is or when there is no photo row.
 46    /// </summary>
 47    /// <param name="video">The video row.</param>
 48    /// <param name="photo">Its photo's row as it stands in this item.</param>
 4749    public static void DateFromPhoto(MediaAsset video, MediaAsset? photo) => video.Captured = photo?.Captured;
 50
 51    private async Task TakeFromOtherPhotosAsync(MediaAsset photo, MediaAsset video, CancellationToken ct)
 2752    {
 9153        foreach (var other in await repository.FindPhotosOfAsync(video.Id, ct))
 554        {
 555            if (other == photo || other.PairedAssetId != video.Id)
 256                continue;
 57
 358            other.PairedAssetId = null;
 359            if (!other.IsSoftDeleted)
 260                Log.NowAStillPhoto(logger, other.Id, video.Id, photo.Id);
 361        }
 2762    }
 63
 64    private async Task LetGoOfPreviousAsync(
 65        IngestionContext context, MediaAsset photo, MediaAsset mov, MediaAsset? retiredByTheItem, CancellationToken ct)
 4866    {
 4867        if (photo.PairedAssetId is not { } previousId || previousId == mov.Id || previousId == retiredByTheItem?.Id)
 3768            return;
 1169        if (await repository.FindAsync(previousId, ct) is not { IsSoftDeleted: false } previous)
 570            return;
 71
 672        await storedContent.RetireAsync(context, previous, ct);
 673        Log.LetGo(logger, previous.Id, previous.FilePath, photo.Id, mov.Id);
 4874    }
 75
 76    private MediaAsset Add(IngestionContext context)
 2177    {
 2178        var mov = context.Item.SecondaryFile!;
 2179        var asset = new MediaAsset
 2180        {
 2181            Id = guidFactory.NewGuid(),
 2182            StorageConfigId = context.Config.Id,
 2183            FilePath = mov.RelativePath,
 2184            FileName = Path.GetFileName(mov.RelativePath),
 2185            ContentHash = context.SecondaryHash!,
 2186            MediaType = mov.MediaType,
 2187            IsSoftDeleted = false,
 2188            LastSeenOnNas = clock.GetCurrentInstant(),
 2189        };
 2190        repository.Add(asset);
 2191        return asset;
 2192    }
 93
 94    private static partial class Log
 95    {
 96        [LoggerMessage(
 97            Level = LogLevel.Information,
 98            Message = "Asset {PhotoId} is now a still photo: its video, asset {VideoId}, is now the video of asset {NewP
 99        public static partial void NowAStillPhoto(ILogger logger, Guid photoId, Guid videoId, Guid newPhotoId);
 100
 101        [LoggerMessage(
 102            Level = LogLevel.Information,
 103            Message = "Soft-deleted asset {VideoId} at {FilePath}: its photo, asset {PhotoId}, now has another video, as
 104        public static partial void LetGo(ILogger logger, Guid videoId, string filePath, Guid photoId, Guid newVideoId);
 105    }
 106}