| | | 1 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 2 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 3 | | using SixLabors.ImageSharp; |
| | | 4 | | using SixLabors.ImageSharp.Processing; |
| | | 5 | | using System.IO.Abstractions; |
| | | 6 | | |
| | | 7 | | namespace Anichron.Worker.Ingestion.Middlewares; |
| | | 8 | | |
| | 63 | 9 | | internal sealed partial class ImageProxyMiddleware( |
| | 63 | 10 | | IEnumerable<IImageProxyGenerator> generators, |
| | 63 | 11 | | BlurhashEncoder blurhashEncoder, |
| | 63 | 12 | | IUprightImageSource imageSource, |
| | 63 | 13 | | IProxyDirectoryStrategy proxyDirectoryStrategy, |
| | 63 | 14 | | ProxyStagingWriter stagingWriter, |
| | 63 | 15 | | IFileSystem fileSystem, |
| | 63 | 16 | | ILogger<ImageProxyMiddleware> logger) : IIngestionMiddleware |
| | | 17 | | { |
| | 0 | 18 | | public int Order => IngestionOrder.ImageProxy; |
| | 3 | 19 | | public bool CanInvoke(IngestionContext context) => true; |
| | | 20 | | |
| | | 21 | | public async Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct) |
| | 62 | 22 | | { |
| | | 23 | | // Ordering guarantees ContentHashingMiddleware ran first; suppression is safe. |
| | 62 | 24 | | var proxyDirectoryName = proxyDirectoryStrategy.GetDirectory(context.Config.Id, context.ContentHash!); |
| | 62 | 25 | | using var image = await imageSource.LoadAsync(context.Item, ct); |
| | | 26 | | |
| | 55 | 27 | | var proxySteps = generators |
| | 55 | 28 | | .Select(g => new Step(g.Bound, () => WriteProxyAsync(g))) |
| | 55 | 29 | | .ToArray(); |
| | 55 | 30 | | Step[] allSteps = [.. proxySteps, new Step(blurhashEncoder.Bound, EncodeBlurhashAsync)]; |
| | 55 | 31 | | var orderedSteps = allSteps |
| | 55 | 32 | | .Select(s => (Step: s, Target: s.Bound.FitTo(image.Size))) |
| | 55 | 33 | | .OrderByDescending(s => (long)s.Target.Width * s.Target.Height) |
| | 55 | 34 | | .ToArray(); |
| | 492 | 35 | | foreach (var (step, target) in orderedSteps) |
| | 164 | 36 | | { |
| | 164 | 37 | | if (image.Size != target) |
| | 96 | 38 | | image.Mutate(x => x.Resize(target)); |
| | 164 | 39 | | await step.RunAsync(); |
| | 163 | 40 | | } |
| | | 41 | | |
| | 54 | 42 | | Log.ProxiesGenerated(logger, proxySteps.Length, context.Item.RelativePath); |
| | 54 | 43 | | 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 | | } |
| | 54 | 68 | | } |
| | | 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 | | } |