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:
2026-11-16 22:23:15 +01:00
co-authored by Claude Sonnet 5.5
parent 9d647bb049
commit 1b542dee98
22 changed files with 567 additions and 54 deletions
@@ -40,6 +40,9 @@ public interface IHeroImageService
/// </summary>
void ApplyFocus(HeroImage image, HeroFocusRequest request);
/// <summary>The variant the renderer is busy with: (done, total), or null when the image is not being cut right now.</summary>
(int Done, int Total)? RenderProgress(Guid imageId);
/// <summary>True when the background renderer gave up on the image after a few failed tries (until a retry or a restart).</summary>
bool IsRenderFailed(Guid imageId);
@@ -160,6 +163,8 @@ public class HeroImageService(
public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false;
public (int Done, int Total)? RenderProgress(Guid imageId) => signal?.GetProgress(imageId);
public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken)
{
_ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
@@ -181,8 +186,13 @@ public class HeroImageService(
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Hero image {ImageId}: cutting the variants failed.", image.Id);
throw new HeroImageRenderException(image.Id, exception);
}
finally
{
signal?.ClearProgress(image.Id);
}
}
private async Task<Guid?> RenderAsync(HeroImage image, CancellationToken cancellationToken)
@@ -193,6 +203,7 @@ public class HeroImageService(
Dictionary<string, byte[]> files;
List<HeroVariant> variants;
var watch = System.Diagnostics.Stopwatch.StartNew();
logger.LogInformation("Hero image {ImageId}: cutting the variants starts ({Width}x{Height}, {Bytes} bytes, {Reason}).", image.Id, image.Width, image.Height, image.Bytes, image.Variants.Count == 0 ? "new" : "focus changed");
using (await gate.EnterAsync(cancellationToken))
{
var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken)
@@ -204,7 +215,13 @@ public class HeroImageService(
}
var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.'));
(files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, cancellationToken);
var lastStep = watch.Elapsed;
(files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, (done, total) =>
{
signal?.SetProgress(image.Id, done, total);
logger.LogInformation("Hero image {ImageId}: variant {Done} of {Total} in {Milliseconds:0} ms.", image.Id, done, total, (watch.Elapsed - lastStep).TotalMilliseconds);
lastStep = watch.Elapsed;
}, cancellationToken);
}
await store.PutSetAsync(HeroMapping.Area, target, files, cancellationToken);
@@ -260,22 +277,25 @@ public class HeroImageService(
assign((f.X, f.Y, f.Zoom));
}
/// <summary>Cuts and encodes every planned variant of the image at its current focus. Returns the files by name and the variant list.</summary>
private async Task<(Dictionary<string, byte[]> Files, List<HeroVariant> Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, CancellationToken cancellationToken)
/// <summary>Cuts and encodes every planned variant of the image at its current focus (one decode for the whole set).</summary>
private async Task<(Dictionary<string, byte[]> Files, List<HeroVariant> Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, Action<int, int>? progress, CancellationToken cancellationToken)
{
var ratios = new[] { Ratio.Wide, Ratio.Tall }
.Select(r => new RatioPlan(r, image.FocusFor(r.Name), VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = VariantPlanner.Plan(image.Width, image.Height, ratios, processor.SupportedOutput);
progress?.Invoke(0, specs.Count);
var rendered = await processor.RenderSetAsync(original, storedFormat, specs, progress, cancellationToken);
var files = new Dictionary<string, byte[]>();
var variants = new List<HeroVariant>();
foreach (var spec in specs)
for (var i = 0; i < specs.Count; i++)
{
var rendered = await processor.RenderAsync(original, storedFormat, spec, cancellationToken);
var spec = specs[i];
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
files[name] = rendered.Bytes;
variants.Add(new HeroVariant(spec.Ratio.Name, rendered.Width, rendered.Height, spec.Format.MimeType(), name, rendered.Bytes.LongLength));
files[name] = rendered[i].Bytes;
variants.Add(new HeroVariant(spec.Ratio.Name, rendered[i].Width, rendered[i].Height, spec.Format.MimeType(), name, rendered[i].Bytes.LongLength));
}
return (files, variants);