Hero: grote foto's in de achtergrond robuust en sneller, voortgang en opnieuw proberen; admin zonder inline script
- Een decode voor de hele set, per breedte eenmaal geschaald voor WebP en AVIF (6000x4000: 21,1 s -> 6,4 s per set) - Eigen limiet per set (Media:RenderSetTimeoutSeconds 600), harde limiet rond een foto, de renderer stopt nooit door een uitzondering, een mislukte foto wacht tot de volgende ronde en houdt de rij niet op - Logging per foto (start, per variant, einde, fout); voortgang per variant in de admin; nieuwe uitsnede na focuswijziging zichtbaar (oude afbeelding blijft, shimmer); knop Opnieuw proberen - Thema-script als /theme-init.js (CSP script-src 'self'), test en smoke-controle op inline scripts Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,8 +38,11 @@ public sealed class MagickImageProcessor : IImageProcessor
|
||||
public Task<RenderedImage> RenderAsync(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken)
|
||||
=> RunAsync(() => Render(data, declared, variant), cancellationToken);
|
||||
|
||||
public Task<IReadOnlyList<RenderedImage>> RenderSetAsync(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
|
||||
=> RunAsync(() => RenderSet(data, declared, variants, progress, cancellationToken), cancellationToken, _options.RenderSetTimeoutSeconds);
|
||||
|
||||
public Task<byte[]?> StripMetadataAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken)
|
||||
=> RunAsync(() => StripMetadata(data, declared), cancellationToken);
|
||||
=> RunAsync(() => StripMetadata(data, declared), cancellationToken, _options.StripTimeoutSeconds);
|
||||
|
||||
public async Task SelfTestAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -75,12 +78,12 @@ public sealed class MagickImageProcessor : IImageProcessor
|
||||
|
||||
// ----- work done on a pool thread with a time limit (the native limit below stops a runaway decode) ------------
|
||||
|
||||
private async Task<T> RunAsync<T>(Func<T> work, CancellationToken cancellationToken)
|
||||
private async Task<T> RunAsync<T>(Func<T> work, CancellationToken cancellationToken, int? timeoutSeconds = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await Task.Run(work, cancellationToken)
|
||||
.WaitAsync(TimeSpan.FromSeconds(Math.Max(1, _options.DecodeTimeoutSeconds)), cancellationToken);
|
||||
.WaitAsync(TimeSpan.FromSeconds(Math.Max(1, timeoutSeconds ?? _options.DecodeTimeoutSeconds)), cancellationToken);
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
@@ -133,33 +136,115 @@ public sealed class MagickImageProcessor : IImageProcessor
|
||||
|
||||
private RenderedImage Render(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant)
|
||||
{
|
||||
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
|
||||
using var image = ReadStrict(data, settings);
|
||||
using var image = DecodeOriented(data, declared);
|
||||
return Cut(image, variant);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One decode for the whole set, and as little repeated work as possible: per crop the widths are cut from large to
|
||||
/// small, each from the previous (smaller input than the full picture), and a width is resized once for all of its
|
||||
/// formats (WebP and AVIF are encoded from the same pixels).
|
||||
/// </summary>
|
||||
private IReadOnlyList<RenderedImage> RenderSet(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
|
||||
{
|
||||
using var image = DecodeOriented(data, declared);
|
||||
var results = new RenderedImage?[variants.Count];
|
||||
var done = 0;
|
||||
|
||||
foreach (var group in variants.Select((spec, index) => (spec, index)).GroupBy(x => (x.spec.Ratio.Name, x.spec.Crop)))
|
||||
{
|
||||
var crop = group.Key.Crop;
|
||||
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|
||||
|| crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height)
|
||||
{
|
||||
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
|
||||
}
|
||||
|
||||
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
|
||||
IMagickImage<byte> current = image.Clone();
|
||||
try
|
||||
{
|
||||
current.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
|
||||
current.ResetPage();
|
||||
ToSrgb(current);
|
||||
current.Strip();
|
||||
|
||||
foreach (var width in group.GroupBy(x => x.spec.Width).OrderByDescending(g => g.Key))
|
||||
{
|
||||
// A cancelled or timed-out job stops at the next width; the native library itself cannot be interrupted.
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var first = width.First().spec;
|
||||
if (current.Width != (uint)first.Width || current.Height != (uint)first.Height)
|
||||
{
|
||||
current.Resize(new MagickGeometry((uint)first.Width, (uint)first.Height) { IgnoreAspectRatio = true });
|
||||
}
|
||||
|
||||
foreach (var (spec, index) in width)
|
||||
{
|
||||
results[index] = Encode(current, spec);
|
||||
progress?.Invoke(++done, variants.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
current.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
return results.Select(r => r!).ToList();
|
||||
}
|
||||
|
||||
/// <summary>Writes already cut and resized pixels in the format and quality of the variant.</summary>
|
||||
private static RenderedImage Encode(IMagickImage<byte> pixels, VariantSpec variant)
|
||||
{
|
||||
pixels.Quality = (uint)variant.Quality;
|
||||
if (variant.Format == ImageFormat.Avif)
|
||||
{
|
||||
pixels.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
pixels.Write(stream, ToMagick(variant.Format));
|
||||
return new RenderedImage(stream.ToArray(), (int)pixels.Width, (int)pixels.Height, variant.Format);
|
||||
}
|
||||
|
||||
private static MagickImage DecodeOriented(ReadOnlyMemory<byte> data, ImageFormat declared)
|
||||
{
|
||||
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
|
||||
var image = ReadStrict(data, settings);
|
||||
image.AutoOrient();
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>Crops a copy of the decoded picture, resizes and encodes it.</summary>
|
||||
private static RenderedImage Cut(IMagickImage<byte> source, VariantSpec variant)
|
||||
{
|
||||
var crop = variant.Crop;
|
||||
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|
||||
|| crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height)
|
||||
|| crop.X + crop.Width > source.Width || crop.Y + crop.Height > source.Height)
|
||||
{
|
||||
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
|
||||
}
|
||||
|
||||
image.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
|
||||
image.ResetPage();
|
||||
image.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true });
|
||||
ToSrgb(image);
|
||||
image.Strip();
|
||||
image.Quality = (uint)variant.Quality;
|
||||
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
|
||||
using var part = source.Clone();
|
||||
part.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
|
||||
part.ResetPage();
|
||||
part.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true });
|
||||
ToSrgb(part);
|
||||
part.Strip();
|
||||
part.Quality = (uint)variant.Quality;
|
||||
|
||||
var format = ToMagick(variant.Format);
|
||||
if (variant.Format == ImageFormat.Avif)
|
||||
{
|
||||
image.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
|
||||
part.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
image.Write(stream, format);
|
||||
return new RenderedImage(stream.ToArray(), (int)image.Width, (int)image.Height, variant.Format);
|
||||
part.Write(stream, format);
|
||||
return new RenderedImage(stream.ToArray(), (int)part.Width, (int)part.Height, variant.Format);
|
||||
}
|
||||
|
||||
private static byte[]? StripMetadata(ReadOnlyMemory<byte> data, ImageFormat declared)
|
||||
@@ -227,7 +312,7 @@ public sealed class MagickImageProcessor : IImageProcessor
|
||||
return image;
|
||||
}
|
||||
|
||||
private static void ToSrgb(MagickImage image)
|
||||
private static void ToSrgb(IMagickImage<byte> image)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -257,7 +342,8 @@ public sealed class MagickImageProcessor : IImageProcessor
|
||||
ResourceLimits.Width = (ulong)Math.Max(1, options.MaxSide);
|
||||
ResourceLimits.Height = (ulong)Math.Max(1, options.MaxSide);
|
||||
ResourceLimits.Memory = 1UL * 1024 * 1024 * 1024;
|
||||
ResourceLimits.Time = (ulong)Math.Max(1, options.DecodeTimeoutSeconds);
|
||||
// The library's own clock runs per picture from its creation: it must not end a set that the job timeout allows.
|
||||
ResourceLimits.Time = (ulong)Math.Max(1, Math.Max(options.DecodeTimeoutSeconds, Math.Max(options.StripTimeoutSeconds, options.RenderSetTimeoutSeconds)) + 30);
|
||||
_limitsSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user