Hero: een gewijzigd focuspunt geldt pas bij het opslaan van de hero (Bryan); het focus-venster houdt de wijziging lokaal
Het eindpunt PUT /hero/images/{id}/focus is vervallen; de focus gaat mee in de opslag (imageFocus) en de varianten worden dan in nieuwe bestanden opnieuw gesneden.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -54,6 +54,12 @@ public class HeroController(IHeroService service, IPublicHeroService publicServi
|
||||
ModelState.AddModelError(exception.Field, exception.Message);
|
||||
return ValidationProblem(ModelState);
|
||||
}
|
||||
catch (ImageRejectedException rejection)
|
||||
{
|
||||
// Cutting the variants for a changed focus needs the stored original and a free processing slot.
|
||||
var status = rejection.Reason == ImageRejection.Busy ? StatusCodes.Status429TooManyRequests : StatusCodes.Status503ServiceUnavailable;
|
||||
return StatusCode(status, new { error = rejection.Code, message = HttpTexts.From(HttpContext).Get(HeroTexts.Image.For(rejection.Reason)) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,24 +95,6 @@ public class HeroImagesController(IHeroImageService service, IOptions<MediaOptio
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}/focus")]
|
||||
[ProducesResponseType<HeroImageDto>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> SetFocus(Guid id, [FromBody] HeroFocusRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await service.SetFocusAsync(id, request, HeroCaller.Id(User), cancellationToken));
|
||||
}
|
||||
catch (HeroImageNotFoundException)
|
||||
{
|
||||
return NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) });
|
||||
}
|
||||
catch (ImageRejectedException rejection)
|
||||
{
|
||||
return Rejected(rejection);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
|
||||
@@ -71,6 +71,9 @@ public record HeroButtonRequest(string? Label, string? Target);
|
||||
|
||||
public record HeroSlideRequest(Guid? Id, Guid ImageId, string? Alt, bool Decorative = false, bool IsActive = true);
|
||||
|
||||
/// <summary>A changed focus of one image; it takes effect when the hero is saved (the admin keeps it locally until then).</summary>
|
||||
public record HeroImageFocusRequest(Guid ImageId, FocusDto Focus, FocusDto? Wide = null, FocusDto? Tall = null);
|
||||
|
||||
public record HeroFocusRequest(FocusDto Focus, FocusDto? Wide = null, FocusDto? Tall = null) : IValidatableObject
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
@@ -101,7 +104,8 @@ public record HeroSaveRequest(
|
||||
bool IsActive,
|
||||
DateTimeOffset? StartsAt,
|
||||
DateTimeOffset? EndsAt,
|
||||
IReadOnlyList<HeroSlideRequest>? Slides) : IValidatableObject
|
||||
IReadOnlyList<HeroSlideRequest>? Slides,
|
||||
IReadOnlyList<HeroImageFocusRequest>? ImageFocus = null) : IValidatableObject
|
||||
{
|
||||
public const int TitleMax = 200;
|
||||
public const int TitleMaxLines = 5;
|
||||
@@ -190,6 +194,20 @@ public record HeroSaveRequest(
|
||||
yield return new ValidationResult(texts.Get(HeroTexts.Validation.EndAfterStart), [nameof(EndsAt)]);
|
||||
}
|
||||
|
||||
var focusEdits = ImageFocus ?? [];
|
||||
for (var i = 0; i < focusEdits.Count; i++)
|
||||
{
|
||||
var edit = focusEdits[i];
|
||||
var valid = edit.ImageId != Guid.Empty
|
||||
&& edit.Focus is not null && edit.Focus.ToSetting().IsValid
|
||||
&& (edit.Wide is null || edit.Wide.ToSetting().IsValid)
|
||||
&& (edit.Tall is null || edit.Tall.ToSetting().IsValid);
|
||||
if (!valid)
|
||||
{
|
||||
yield return new ValidationResult(texts.Get(HeroTexts.Validation.FocusInvalid, args: ("maxZoom", FocusSetting.MaxZoom)), [$"{nameof(ImageFocus)}[{i}]"]);
|
||||
}
|
||||
}
|
||||
|
||||
var slides = Slides ?? [];
|
||||
for (var i = 0; i < slides.Count; i++)
|
||||
{
|
||||
|
||||
@@ -23,8 +23,14 @@ public interface IHeroImageService
|
||||
/// <exception cref="ImageRejectedException">The upload is refused; see the code.</exception>
|
||||
Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Changes where the subject is (general and optionally per ratio) and cuts the variants again around it.</summary>
|
||||
Task<HeroImageDto> SetFocusAsync(Guid imageId, HeroFocusRequest request, Guid callerId, CancellationToken cancellationToken);
|
||||
/// <summary>
|
||||
/// Applies a changed focus (general and optionally per ratio) to a tracked image: when the set changes, the variants are
|
||||
/// cut again around the new point into a NEW set of files (new addresses, nothing live is touched) and the old set is
|
||||
/// noted as retired. Nothing is saved here: the caller saves it together with the hero, so a changed focus only becomes
|
||||
/// visible when the hero is saved. When the hero is not saved the new files are never referenced and the clean-up
|
||||
/// removes them after the retention time.
|
||||
/// </summary>
|
||||
Task StageFocusAsync(HeroImage image, HeroFocusRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Removes an image nothing uses; its files go after the retention time.</summary>
|
||||
Task DeleteAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken);
|
||||
@@ -110,10 +116,8 @@ public class HeroImageService(
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<HeroImageDto> SetFocusAsync(Guid imageId, HeroFocusRequest request, Guid callerId, CancellationToken cancellationToken)
|
||||
public async Task StageFocusAsync(HeroImage image, HeroFocusRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var image = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
|
||||
|
||||
var oldSet = image.SetHash;
|
||||
var general = request.Focus.ToSetting().Normalize();
|
||||
image.FocusX = general.X;
|
||||
@@ -140,12 +144,8 @@ public class HeroImageService(
|
||||
var files = await RenderSetAsync(image, memory.ToArray(), storedFormat, cancellationToken);
|
||||
await store.PutSetAsync(HeroMapping.Area, image.SetHash, files, cancellationToken);
|
||||
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = oldSet, RetiredAt = _time.GetUtcNow() });
|
||||
logger.LogInformation("Hero image {ImageId}: variants cut again for a changed focus (saved with the hero)", image.Id);
|
||||
}
|
||||
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
indexCache?.Invalidate();
|
||||
logger.LogInformation("Hero image {ImageId} focus changed by user {UserId}", imageId, callerId);
|
||||
return HeroMapping.ToImageDto(image);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken)
|
||||
|
||||
@@ -25,6 +25,7 @@ public interface IHeroService
|
||||
|
||||
public class HeroService(
|
||||
IHeroRepository repository,
|
||||
IHeroImageService imageService,
|
||||
IOptions<HeroOptions> options,
|
||||
ILogger<HeroService> logger,
|
||||
TimeProvider? time = null,
|
||||
@@ -42,8 +43,17 @@ public class HeroService(
|
||||
var now = _time.GetUtcNow();
|
||||
var slideRequests = request.Slides ?? [];
|
||||
|
||||
var imageIds = slideRequests.Select(s => s.ImageId).Distinct().ToList();
|
||||
var focusRequests = request.ImageFocus ?? [];
|
||||
var imageIds = slideRequests.Select(s => s.ImageId).Concat(focusRequests.Select(f => f.ImageId)).Distinct().ToList();
|
||||
var images = (await repository.GetImagesAsync(imageIds, cancellationToken)).ToDictionary(i => i.Id);
|
||||
for (var i = 0; i < focusRequests.Count; i++)
|
||||
{
|
||||
if (!images.ContainsKey(focusRequests[i].ImageId))
|
||||
{
|
||||
throw new HeroSaveRejectedException($"ImageFocus[{i}].ImageId", _texts.Get(HeroTexts.Validation.Slide.ImageUnknown));
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < slideRequests.Count; i++)
|
||||
{
|
||||
if (!images.ContainsKey(slideRequests[i].ImageId))
|
||||
@@ -105,6 +115,13 @@ public class HeroService(
|
||||
}
|
||||
}
|
||||
|
||||
// A changed focus is applied only now, together with the hero: the new variants are cut into new files first, then
|
||||
// the hero and the images are saved in one go, so nothing live changes before the hero is saved.
|
||||
foreach (var edit in focusRequests)
|
||||
{
|
||||
await imageService.StageFocusAsync(images[edit.ImageId], new HeroFocusRequest(edit.Focus, edit.Wide, edit.Tall), cancellationToken);
|
||||
}
|
||||
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
indexCache?.Invalidate();
|
||||
|
||||
Reference in New Issue
Block a user