< Summary

Information
Class: Anichron.Worker.Ingestion.Classification.AssetContent
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Classification/StoredContent.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 115
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Of(...)100%22100%
ApplyTo(...)100%22100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Classification/StoredContent.cs

#LineLine coverage
 1using Anichron.Core.Data.Repository;
 2using Anichron.Core.Domain;
 3using Anichron.Worker.Ingestion.Pipeline;
 4using NodaTime;
 5
 6namespace Anichron.Worker.Ingestion.Classification;
 7
 8/// <summary>The content a row takes, from processing a file or from another row.</summary>
 9/// <param name="ContentHash">The content's hash.</param>
 10/// <param name="Captured">The capture date the row takes; null makes it undated.</param>
 11/// <param name="MediaType">The content's media type.</param>
 12/// <param name="Description">What extraction read, owned by the row that takes it.</param>
 13/// <param name="Blurhash">The content's placeholder.</param>
 14/// <param name="Proxies">The content's proxies, owned by the row that takes it.</param>
 10215internal sealed record AssetContent(
 10216    string ContentHash,
 10217    CaptureDate? Captured,
 10218    MediaType MediaType,
 10219    MediaDescription Description,
 10220    string? Blurhash,
 10221    IReadOnlyList<ProxyFile> Proxies)
 22{
 23    /// <summary>The content of <paramref name="source"/>, with a copy of its description, since a row owns its own.</su
 24    /// <param name="source">The row whose content another row takes.</param>
 25    /// <param name="proxies">The proxies the taking row owns.</param>
 26    public static AssetContent Of(MediaAsset source, IReadOnlyList<ProxyFile> proxies)
 2627    {
 2628        var metadata = source.Metadata ?? new Metadata();
 2629        return new(source.ContentHash, source.Captured, source.MediaType, metadata.Description with { }, metadata.Blurha
 2630    }
 31
 32    /// <summary>Makes <paramref name="asset"/> hold this content's fields and metadata.</summary>
 33    /// <param name="asset">The row, with its metadata loaded when it has any.</param>
 34    public void ApplyTo(MediaAsset asset)
 10035    {
 10036        asset.ContentHash = ContentHash;
 10037        asset.Captured = Captured;
 10038        asset.MediaType = MediaType;
 10039        var metadata = asset.Metadata ??= new Metadata { AssetId = asset.Id };
 10040        metadata.Description = Description;
 10041        metadata.Blurhash = Blurhash;
 10042    }
 43}
 44
 45internal sealed partial class StoredContent(
 46    IMediaAssetRepository repository, IBurstRepository bursts, IClock clock, ILogger<StoredContent> logger)
 47{
 48    /// <summary>
 49    /// Makes <paramref name="asset"/> hold <paramref name="content"/>: its content fields, metadata and proxy rows,
 50    /// out of its burst, and active.
 51    /// </summary>
 52    /// <param name="context">The item that stages the auto-restore count.</param>
 53    /// <param name="asset">A tracked row.</param>
 54    /// <param name="content">The content it takes.</param>
 55    /// <param name="ct">Cancels the lookups.</param>
 56    /// <returns>Whether the row was soft-deleted.</returns>
 57    public async Task<bool> TakeAsync(IngestionContext context, MediaAsset asset, AssetContent content, CancellationToke
 58    {
 59        await repository.LoadMetadataAsync(asset, ct);
 60        content.ApplyTo(asset);
 61        ProxyFileRows.Replace(repository, asset, content.Proxies);
 62        asset.BurstId = null;
 63        return await ReactivateAsync(context, asset, ct);
 64    }
 65
 66    /// <summary>Makes the row active and seen now, and clears its burst-checked mark.</summary>
 67    /// <param name="context">The item that stages the auto-restore count.</param>
 68    /// <param name="asset">A tracked row.</param>
 69    /// <param name="ct">Cancels the lookup.</param>
 70    /// <returns>Whether the row was soft-deleted.</returns>
 71    public async Task<bool> ReactivateAsync(IngestionContext context, MediaAsset asset, CancellationToken ct)
 72    {
 73        var wasSoftDeleted = asset.IsSoftDeleted;
 74        if (wasSoftDeleted)
 75            context.StageAutoRestored();
 76        asset.IsSoftDeleted = false;
 77        asset.LastSeenOnNas = clock.GetCurrentInstant();
 78        await bursts.RemoveCheckAsync(asset.Id, ct);
 79        return wasSoftDeleted;
 80    }
 81
 82    /// <summary>
 83    /// Soft-deletes the row and, when it is a Live Photo's photo, its video. A row already soft-deleted is left alone
 84    /// and not counted again.
 85    /// </summary>
 86    /// <param name="context">The item that stages the soft-delete counts.</param>
 87    /// <param name="asset">A tracked row.</param>
 88    /// <param name="ct">Cancels the lookup of the video.</param>
 89    public async Task RetireAsync(IngestionContext context, MediaAsset asset, CancellationToken ct)
 90    {
 91        if (!SoftDelete(context, asset) || asset.PairedAssetId is not { } videoId)
 92            return;
 93
 94        if (await repository.FindAsync(videoId, ct) is { } video && SoftDelete(context, video))
 95            Log.RetiredWithItsPhoto(logger, video.Id, video.FilePath, asset.Id);
 96    }
 97
 98    private static bool SoftDelete(IngestionContext context, MediaAsset asset)
 99    {
 100        if (asset.IsSoftDeleted)
 101            return false;
 102
 103        asset.IsSoftDeleted = true;
 104        context.StageSoftDeleted();
 105        return true;
 106    }
 107
 108    private static partial class Log
 109    {
 110        [LoggerMessage(
 111            Level = LogLevel.Information,
 112            Message = "Soft-deleted asset {VideoId} at {FilePath} with its photo, asset {PhotoId}: it is that Live Photo
 113        public static partial void RetiredWithItsPhoto(ILogger logger, Guid videoId, string filePath, Guid photoId);
 114    }
 115}