Hero-admin: schakelaar Actief, destructieve knoppen rood, slide-kopjes, juiste voorvertoning in het focusvenster, status per foto

- Schakelaar (role=switch) Actief / Niet actief bij de knoppen, gedimde rij; 'Verberg voor schermlezers'; veld 'Beschrijving' binnen een groep 'Slide N'
- Alle foto's zonder complete set tonen 'Wordt verwerkt' (voortgang bij de foto die bezig is, 'in de wachtrij' bij de rest); totaal bovenaan telt ook nog niet opgeslagen foto's
- Per foto 'Verwerking afgerond' (zelfde weergave voor een hergebruikte foto), foto direct bruikbaar; niet-opgeslagen foto's worden zelf ververst (GET /hero/images/{id}), ook in een achtergrondtab
- Focusvenster toont de hele foto (full-preview in elke set, achtergrond-aanvulling voor oude sets) in plaats van de uitgesneden wide-variant (vervorming)
- Prullenbak en 'Wijzigingen weggooien' in de destructieve stijl
- upload-respons: reused (intern)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 23:26:47 +01:00
co-authored by Claude Sonnet 5.5
parent 9f07934f9e
commit 6999f5b2ba
25 changed files with 794 additions and 73 deletions
@@ -95,6 +95,16 @@ public class HeroImagesController(IHeroImageService service, IOptions<MediaOptio
}
}
/// <summary>One image as the admin sees it now; the page asks for the images it uploaded but has not saved yet.</summary>
[HttpGet("{id:guid}")]
[ProducesResponseType<HeroImageDto>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(Guid id, CancellationToken cancellationToken)
{
var image = await service.GetImageAsync(id, cancellationToken);
return image is null ? NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) }) : Ok(image);
}
/// <summary>The formats and limits an upload has to meet on this host (what the validation uses).</summary>
[HttpGet("limits")]
[ProducesResponseType<HeroUploadLimitsDto>(StatusCodes.Status200OK)]
@@ -27,7 +27,9 @@ public record HeroImageDto(
bool Processing = false,
bool Failed = false,
int? RenderDone = null,
int? RenderTotal = null);
int? RenderTotal = null,
string? PreviewSrc = null,
bool Reused = false);
public record HeroButtonDto(string Label, string Target);
@@ -22,6 +22,9 @@ public interface IHeroRepository
/// <summary>Images whose set is missing or does not match their focus, oldest first (tracked).</summary>
Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken);
/// <summary>Images with a complete set made before the full-frame preview existed (tracked).</summary>
Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken);
/// <summary>The image as stored right now, not tracked.</summary>
Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken);
@@ -72,6 +75,9 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
public Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
@@ -43,6 +43,9 @@ public interface IHeroImageService
/// <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>One image as the admin sees it now (including whether it is still being prepared). Null when there is no such image.</summary>
Task<HeroImageDto?> GetImageAsync(Guid imageId, CancellationToken cancellationToken);
/// <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);
@@ -90,7 +93,7 @@ public class HeroImageService(
// The same photo again also gives up the failed state: the renderer tries it again when it is still pending.
signal?.Reset(existing.Id);
signal?.Notify();
return HeroMapping.ToImageDto(existing);
return HeroMapping.ToImageDto(existing) with { Reused = true };
}
await EnsureRoomAsync(admitted.Bytes.Length, cancellationToken);
@@ -161,6 +164,19 @@ public class HeroImageService(
public void NotifyPending() => signal?.Notify();
public async Task<HeroImageDto?> GetImageAsync(Guid imageId, CancellationToken cancellationToken)
{
var image = await repository.GetImageFreshAsync(imageId, cancellationToken);
if (image is null)
{
return null;
}
var failed = IsRenderFailed(imageId);
var progress = RenderProgress(imageId);
return HeroMapping.ToImageDto(image) with { Failed = failed, RenderDone = progress?.Done, RenderTotal = progress?.Total };
}
public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false;
public (int Done, int Total)? RenderProgress(Guid imageId) => signal?.GetProgress(imageId);
@@ -177,7 +193,9 @@ public class HeroImageService(
var image = (await repository.GetImagesNeedingRenderAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id));
if (image is null)
{
return null;
// Nothing to cut: add the full-frame preview to complete sets that were made before it existed.
var old = (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id));
return old is null ? null : await AddPreviewAsync(old, cancellationToken);
}
try
@@ -195,6 +213,40 @@ public class HeroImageService(
}
}
/// <summary>Makes only the preview file of a complete set (one decode, one resize) and adds it to the set where it already is.</summary>
private async Task<Guid?> AddPreviewAsync(HeroImage image, CancellationToken cancellationToken)
{
try
{
using (await gate.EnterAsync(cancellationToken))
{
var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken)
?? throw new ImageRejectedException(ImageRejection.Unavailable, "The stored original is missing.");
using var memory = new MemoryStream();
await using (file.Content)
{
await file.Content.CopyToAsync(memory, cancellationToken);
}
var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.'));
var spec = VariantPlanner.Preview(image.Width, image.Height, processor.SupportedOutput);
var rendered = (await processor.RenderSetAsync(memory.ToArray(), storedFormat, [spec], null, cancellationToken))[0];
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
await store.PutAsync($"{HeroMapping.Area}/{image.SetHash}/{name}", rendered.Bytes, cancellationToken);
image.Variants = [.. image.Variants, new HeroVariant(spec.Ratio.Name, rendered.Width, rendered.Height, spec.Format.MimeType(), name, rendered.Bytes.LongLength)];
}
await repository.SaveChangesAsync(cancellationToken);
logger.LogInformation("Hero image {ImageId}: the full-frame preview was added to its set.", image.Id);
return image.Id;
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Hero image {ImageId}: adding the preview failed.", image.Id);
throw new HeroImageRenderException(image.Id, exception);
}
}
private async Task<Guid?> RenderAsync(HeroImage image, CancellationToken cancellationToken)
{
@@ -283,7 +335,9 @@ public class HeroImageService(
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);
var specs = VariantPlanner.Plan(image.Width, image.Height, ratios, processor.SupportedOutput).ToList();
// The whole picture, for the focus dialog of the admin (never published to a website).
specs.Add(VariantPlanner.Preview(image.Width, image.Height, processor.SupportedOutput));
progress?.Invoke(0, specs.Count);
var rendered = await processor.RenderSetAsync(original, storedFormat, specs, progress, cancellationToken);
@@ -47,6 +47,19 @@ public static class HeroMapping
_ => format.ToString().ToUpperInvariant(),
};
/// <summary>The variants websites and the admin lists use: everything except the full-frame preview.</summary>
public static IEnumerable<HeroVariant> Published(HeroImage image) => image.Variants.Where(v => v.Ratio != VariantPlanner.PreviewRatioName);
/// <summary>The address of the full-frame preview for the focus dialog, or null while the set has none yet.</summary>
public static string? PreviewSrc(HeroImage image)
=> image.Variants.FirstOrDefault(v => v.Ratio == VariantPlanner.PreviewRatioName) is { } preview
? MediaPaths.Url(Area, image.SetHash, preview.FileName)
: null;
/// <summary>A complete, current set that lacks the preview (made before it existed): only that file is added, quietly.</summary>
public static bool NeedsPreview(HeroImage image)
=> !NeedsRender(image) && image.Variants.All(v => v.Ratio != VariantPlanner.PreviewRatioName);
/// <summary>The set hash the image should have at its current focus.</summary>
public static string TargetSetHash(HeroImage image)
=> MediaPaths.SetHash(image.OriginalSha256, [("wide", image.FocusFor("wide")), ("tall", image.FocusFor("tall"))]);
@@ -60,10 +73,10 @@ public static class HeroMapping
/// <summary>The address the plain <c>img</c> uses: the largest WebP of the wide ratio, else the largest WebP, else the first variant.</summary>
public static string SrcFor(HeroImage image)
{
var webp = image.Variants.Where(v => v.Type == ImageFormat.WebP.MimeType()).ToList();
var webp = Published(image).Where(v => v.Type == ImageFormat.WebP.MimeType()).ToList();
var pick = webp.Where(v => v.Ratio == "wide").OrderByDescending(v => v.Width).FirstOrDefault()
?? webp.OrderByDescending(v => v.Width).FirstOrDefault()
?? image.Variants.FirstOrDefault();
?? Published(image).FirstOrDefault();
return pick is null ? string.Empty : MediaPaths.Url(Area, image.SetHash, pick.FileName);
}
@@ -76,8 +89,9 @@ public static class HeroMapping
image.WideFocusX is null ? null : FocusDto.From(image.FocusFor("wide")),
image.TallFocusX is null ? null : FocusDto.From(image.FocusFor("tall")),
SrcFor(image),
image.Variants.Select(v => ToDto(image, v)).ToList(),
NeedsRender(image));
Published(image).Select(v => ToDto(image, v)).ToList(),
NeedsRender(image),
PreviewSrc: PreviewSrc(image));
public static bool IsLive(HeroContent hero, DateTimeOffset now)
=> hero.IsActive
@@ -137,7 +151,7 @@ public static class HeroMapping
image.Height,
FocusDto.From(image.General),
SrcFor(image),
image.Variants.Select(v => ToDto(image, v)).ToList(),
Published(image).Select(v => ToDto(image, v)).ToList(),
byRatio);
}