Fase 2 stap A: write-through van de Hero naar de bibliotheek (schakelaar uit), per-slide uitsnede en set in de bibliotheek

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 23:05:35 +01:00
co-authored by Claude Sonnet 5.5
parent ed0b6ad61c
commit 369bf73666
18 changed files with 332 additions and 15 deletions
@@ -113,8 +113,12 @@ public class HeroImageService(
ILogger<HeroImageService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
HeroRenderSignal? signal = null) : IHeroImageService
HeroRenderSignal? signal = null,
IMediaLibrarySync? librarySync = null) : IHeroImageService
{
/// <summary>Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws.</summary>
private Task SyncLibraryAsync() => librarySync?.SyncAsync(HeroMapping.Area, CancellationToken.None) ?? Task.CompletedTask;
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null)
@@ -137,6 +141,7 @@ public class HeroImageService(
if (existing is not null && await FilesExistAsync(existing, cancellationToken))
{
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
// 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();
@@ -176,6 +181,7 @@ public class HeroImageService(
}
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Notify();
logger.LogInformation("Hero image {ImageId} {Action} by user {UserId} ({Width}x{Height}); its variants are cut in the background", image.Id, existing is null ? "uploaded" : "repaired", callerId, image.Width, image.Height);
@@ -222,6 +228,7 @@ public class HeroImageService(
ApplyFocus(image, request);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Reset(imageId);
signal?.Notify();
@@ -242,6 +249,7 @@ public class HeroImageService(
Apply(request.Wide, v => (slide.WideFocusX, slide.WideFocusY, slide.WideZoom) = v);
Apply(request.Tall, v => (slide.TallFocusX, slide.TallFocusY, slide.TallZoom) = v);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Reset(slideId);
signal?.Notify();
@@ -265,6 +273,7 @@ public class HeroImageService(
}
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Reset(slideId);
indexCache?.Invalidate();
@@ -454,6 +463,7 @@ public class HeroImageService(
}
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
indexCache?.Invalidate();
logger.LogInformation("Hero slide {SlideId}: {Variants} variants cut in {Seconds:0.0} s", slide.Id, variants.Count, watch.Elapsed.TotalSeconds);
return slide.Id;
@@ -507,6 +517,7 @@ public class HeroImageService(
}
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
indexCache?.Invalidate();
logger.LogInformation("Hero image {ImageId}: {Variants} variants cut in {Seconds:0.0} s", image.Id, variants.Count, watch.Elapsed.TotalSeconds);
return image.Id;
@@ -525,6 +536,7 @@ public class HeroImageService(
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = now });
repository.RemoveImage(image);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
logger.LogInformation("Hero image {ImageId} removed by user {UserId}", imageId, callerId);
}
@@ -548,6 +560,7 @@ public class HeroImageService(
image.DeletedAt = now;
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
indexCache?.Invalidate();
logger.LogInformation("Hero image {ImageId} moved to the trash by user {UserId} ({Slides} slides removed)", imageId, callerId, slides.Count);
@@ -572,6 +585,7 @@ public class HeroImageService(
var (setHash, originalKey) = (image.SetHash, image.OriginalKey);
repository.RemoveImage(image);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
if (setHash.Length > 0 && !setShared)
{
@@ -598,6 +612,7 @@ public class HeroImageService(
image.DeletedAt = null;
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Notify();
logger.LogInformation("Hero image {ImageId} restored from the trash by user {UserId}", imageId, callerId);
@@ -18,7 +18,8 @@ public class HeroMediaCleanup(
IMediaStore store,
IOptions<MediaOptions> options,
ILogger<HeroMediaCleanup> logger,
TimeProvider? time = null)
TimeProvider? time = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null)
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
@@ -42,14 +43,20 @@ public class HeroMediaCleanup(
// 1b. Photos that have been in the trash for the trash period: their files go now (they are retired, immediately due).
var trashCutoff = now.AddDays(-Math.Max(0, options.Value.TrashDays));
var trashedGone = false;
foreach (var image in await repository.GetTrashedBeforeAsync(trashCutoff, cancellationToken))
{
trashedGone = true;
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) });
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) });
repository.RemoveImage(image);
}
await repository.SaveChangesAsync(cancellationToken);
if (librarySync is not null && (unattached.Count > 0 || trashedGone))
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);
}
// 2. Retired files whose retention has passed, unless an image uses them again.
var referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken);
@@ -26,8 +26,21 @@ public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrat
for (var index = 0; index < ordered.Count; index++)
{
var slide = ordered[index];
var setHash = images.TryGetValue(slide.ImageId, out var image) ? image.SetHash : string.Empty;
uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash));
var setHash = string.Empty;
IReadOnlyList<MediaCropSnapshot>? crops = null;
if (images.TryGetValue(slide.ImageId, out var image))
{
// The slide as it shows the photo: its own crop and set when it has them (crop per use), else the photo's.
slide.Image = image;
var shown = HeroMapping.Effective(slide) ?? image;
setHash = shown.SetHash;
if (slide.HasOwnCrop)
{
crops = [Crop("wide", shown), Crop("tall", shown)];
}
}
uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash, crops));
}
}
@@ -30,7 +30,8 @@ public class HeroService(
ILogger<HeroService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
ITextCatalog? texts = null) : IHeroService
ITextCatalog? texts = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null) : IHeroService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
@@ -155,6 +156,11 @@ public class HeroService(
indexCache?.Invalidate();
imageService.NotifyPending();
if (librarySync is not null)
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);
}
logger.LogInformation("Hero {HeroId} {Action} by user {LastModifiedByUserId} ({Slides} slides)", hero.Id, created ? "Created" : "Updated", callerId, keep.Count);
return WithFailures(HeroMapping.ToAdmin(hero, now, options.Value.MaxPublishedSlides));