< Summary

Line coverage
96%
Covered lines: 31
Uncovered lines: 1
Coverable lines: 32
Total lines: 1064
Line coverage: 96.8%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
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: get_Order()100%210%
File 2: CanInvoke(...)100%11100%
File 2: InvokeAsync()100%66100%

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/Middlewares/ImageProxyMiddleware.cs

#LineLine coverage
 1using Anichron.Worker.Ingestion.Pipeline;
 2using Anichron.Worker.Ingestion.Proxy;
 3using SixLabors.ImageSharp;
 4using SixLabors.ImageSharp.Processing;
 5using System.IO.Abstractions;
 6
 7namespace Anichron.Worker.Ingestion.Middlewares;
 8
 639internal sealed partial class ImageProxyMiddleware(
 6310    IEnumerable<IImageProxyGenerator> generators,
 6311    BlurhashEncoder blurhashEncoder,
 6312    IUprightImageSource imageSource,
 6313    IProxyDirectoryStrategy proxyDirectoryStrategy,
 6314    ProxyStagingWriter stagingWriter,
 6315    IFileSystem fileSystem,
 6316    ILogger<ImageProxyMiddleware> logger) : IIngestionMiddleware
 17{
 018    public int Order => IngestionOrder.ImageProxy;
 319    public bool CanInvoke(IngestionContext context) => true;
 20
 21    public async Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct)
 6222    {
 23        // Ordering guarantees ContentHashingMiddleware ran first; suppression is safe.
 6224        var proxyDirectoryName = proxyDirectoryStrategy.GetDirectory(context.Config.Id, context.ContentHash!);
 6225        using var image = await imageSource.LoadAsync(context.Item, ct);
 26
 5527        var proxySteps = generators
 5528            .Select(g => new Step(g.Bound, () => WriteProxyAsync(g)))
 5529            .ToArray();
 5530        Step[] allSteps = [.. proxySteps, new Step(blurhashEncoder.Bound, EncodeBlurhashAsync)];
 5531        var orderedSteps = allSteps
 5532            .Select(s => (Step: s, Target: s.Bound.FitTo(image.Size)))
 5533            .OrderByDescending(s => (long)s.Target.Width * s.Target.Height)
 5534            .ToArray();
 49235        foreach (var (step, target) in orderedSteps)
 16436        {
 16437            if (image.Size != target)
 9638                image.Mutate(x => x.Resize(target));
 16439            await step.RunAsync();
 16340        }
 41
 5442        Log.ProxiesGenerated(logger, proxySteps.Length, context.Item.RelativePath);
 5443        await next(context, ct);
 44
 45        Task WriteProxyAsync(IImageProxyGenerator generator)
 46        {
 47            return stagingWriter.WriteAsync(
 48                context,
 49                context.AssetId,
 50                $"{proxyDirectoryName}/{generator.FileName}",
 51                generator.ProxyType,
 52                ProduceAsync,
 53                ct);
 54
 55            async Task ProduceAsync(string temporaryPath, CancellationToken token)
 56            {
 57                using var copy = image.Clone();
 58                var bytes = await generator.GenerateAsync(copy, token);
 59                await fileSystem.File.WriteAllBytesAsync(temporaryPath, bytes, token);
 60            }
 61        }
 62
 63        async Task EncodeBlurhashAsync()
 64        {
 65            using var copy = image.Clone();
 66            context.Blurhash = await blurhashEncoder.EncodeAsync(copy, ct);
 67        }
 5468    }
 69
 70    private sealed record Step(ProxyBound Bound, Func<Task> RunAsync);
 71
 72    private static partial class Log
 73    {
 74        [LoggerMessage(Level = LogLevel.Information, Message = "Generated {Count} proxy files for {RelativePath}.")]
 75        public static partial void ProxiesGenerated(ILogger logger, int count, string relativePath);
 76    }
 77}