Fase 3c (code, nog niet uitgerold): foto's altijd in de bibliotheektabel, HeroImages-migratie met controle, schakelaars en hulpmiddelen weg

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 00:21:58 +01:00
co-authored by Claude Sonnet 5.5
parent 7246efccc1
commit d9e57425c2
52 changed files with 792 additions and 2537 deletions
@@ -113,24 +113,8 @@ public class HeroImageService(
ILogger<HeroImageService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
HeroRenderSignal? signal = null,
IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null) : IHeroImageService
HeroRenderSignal? signal = null) : IHeroImageService
{
/// <summary>Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws.</summary>
private async Task SyncLibraryAsync()
{
if (legacyMirror is not null)
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
if (librarySync is not null)
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);
}
}
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null)
@@ -153,7 +137,6 @@ 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();
@@ -193,7 +176,6 @@ 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);
@@ -240,7 +222,6 @@ public class HeroImageService(
ApplyFocus(image, request);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Reset(imageId);
signal?.Notify();
@@ -261,7 +242,6 @@ 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();
@@ -285,7 +265,6 @@ public class HeroImageService(
}
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
signal?.Reset(slideId);
indexCache?.Invalidate();
@@ -475,7 +454,6 @@ 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;
@@ -529,7 +507,6 @@ 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;
@@ -548,7 +525,6 @@ 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);
}
@@ -572,7 +548,6 @@ 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);
@@ -597,7 +572,6 @@ public class HeroImageService(
var (setHash, originalKey) = (image.SetHash, image.OriginalKey);
repository.RemoveImage(image);
await repository.SaveChangesAsync(cancellationToken);
await SyncLibraryAsync();
if (setHash.Length > 0 && !setShared)
{
@@ -624,7 +598,6 @@ 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);
@@ -1,107 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>Keeps <c>HeroImages</c> a complete copy of the hero's photos while they live in the library's table (phase 3b).</summary>
public interface IHeroLegacyImageMirror
{
/// <summary>Does nothing unless the photos live in the library. Never throws: the library rows are the truth then.</summary>
Task SyncAsync(CancellationToken cancellationToken);
}
public sealed class HeroLegacyImageMirror(HeroDbContext db, ILogger<HeroLegacyImageMirror> logger) : IHeroLegacyImageMirror
{
public async Task SyncAsync(CancellationToken cancellationToken)
{
if (!db.PhotosInLibrary)
{
return;
}
try
{
var library = await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken);
var legacy = (await db.LegacyHeroImages.ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
foreach (var photo in library)
{
if (!legacy.Remove(photo.Id, out var row))
{
row = new LegacyHeroImage { Id = photo.Id };
db.LegacyHeroImages.Add(row);
}
Copy(photo, row);
}
db.LegacyHeroImages.RemoveRange(legacy.Values);
await db.SaveChangesAsync(cancellationToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Keeping HeroImages up to date with the library photos failed; the library rows are the truth, the next change repairs it.");
}
}
internal static void Copy(HeroImage from, LegacyHeroImage to)
{
to.OriginalSha256 = from.OriginalSha256;
to.OriginalKey = from.OriginalKey;
to.SourceFormat = from.SourceFormat;
to.Width = from.Width;
to.Height = from.Height;
to.Bytes = from.Bytes;
(to.FocusX, to.FocusY, to.Zoom) = (from.FocusX, from.FocusY, from.Zoom);
(to.WideFocusX, to.WideFocusY, to.WideZoom) = (from.WideFocusX, from.WideFocusY, from.WideZoom);
(to.TallFocusX, to.TallFocusY, to.TallZoom) = (from.TallFocusX, from.TallFocusY, from.TallZoom);
to.SetHash = from.SetHash;
to.Variants = [.. from.Variants];
to.CreatedAt = from.CreatedAt;
to.CreatedByUserId = from.CreatedByUserId;
to.OriginalFileName = from.OriginalFileName;
to.LastUsedAt = from.LastUsedAt;
to.DeletedAt = from.DeletedAt;
}
}
/// <summary>
/// Run at start-up when the photos are to live in the library: the library rows must already be a complete copy of
/// <c>HeroImages</c> (same ids, hashes, sets and variants), otherwise the application refuses to start with a clear message
/// instead of showing a hero with missing photos. Fix: run the verified copy first (Media__Library__CopyOnStartup) and check
/// the library check, then switch the storage.
/// </summary>
public static class HeroPhotoStorageGuard
{
public static async Task VerifyAsync(HeroDbContext db, CancellationToken cancellationToken)
{
if (!db.PhotosInLibrary)
{
return;
}
var library = (await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
var problems = new List<string>();
foreach (var legacy in await db.LegacyHeroImages.AsNoTracking().ToListAsync(cancellationToken))
{
if (!library.TryGetValue(legacy.Id, out var photo))
{
problems.Add($"photo {legacy.Id} is not in the library");
}
else if (photo.OriginalSha256 != legacy.OriginalSha256 || photo.SetHash != legacy.SetHash || HeroMediaMigrationSource.Json(photo.Variants) != HeroMediaMigrationSource.Json(legacy.Variants))
{
problems.Add($"photo {legacy.Id} differs from the library (hash, set or variants)");
}
}
if (problems.Count > 0)
{
throw new InvalidOperationException(
"Media__Library__HeroPhotosInLibrary is on, but the library does not hold every hero photo as HeroImages does: "
+ string.Join("; ", problems.Take(5))
+ (problems.Count > 5 ? $" (and {problems.Count - 5} more)" : string.Empty)
+ ". Run the media library copy first (Media__Library__CopyOnStartup=true), check it with the library check, then switch the storage.");
}
}
}
@@ -18,9 +18,7 @@ public class HeroMediaCleanup(
IMediaStore store,
IOptions<MediaOptions> options,
ILogger<HeroMediaCleanup> logger,
TimeProvider? time = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null)
TimeProvider? time = null)
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
@@ -44,25 +42,14 @@ 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 (legacyMirror is not null && (unattached.Count > 0 || trashedGone))
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
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);
@@ -1,87 +0,0 @@
using SlpModularCms.Core.Media.Library;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Repositories;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>
/// Offers the hero's photos (also those in the trash) and slides to the media library's copy, with the same ids, hashes and
/// set hashes the hero uses. Read-only: the hero's tables are never changed by the copy.
/// </summary>
public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrationSource
{
public string Module => HeroMapping.Area;
public async Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken)
=> (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).Select(ToSnapshot).ToList();
public async Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken)
{
var images = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id);
var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken);
var uses = new List<MediaUseSnapshot>();
foreach (var hero in slides.GroupBy(s => s.HeroId))
{
var ordered = hero.OrderBy(s => s.SortOrder).ThenBy(s => s.Id).ToList();
for (var index = 0; index < ordered.Count; index++)
{
var slide = ordered[index];
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)];
}
}
var variantsJson = slide.HasOwnCrop && slide.Variants is { Count: > 0 } ? Json(slide.Variants) : null;
uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash, crops, variantsJson));
}
}
return uses;
}
private MediaAssetSnapshot ToSnapshot(HeroImage i)
=> new(
i.Id,
Module,
i.OriginalSha256,
i.OriginalKey,
i.SourceFormat,
i.Width,
i.Height,
i.Bytes,
i.FocusX,
i.FocusY,
i.Zoom,
i.SetHash,
[Crop("wide", i), Crop("tall", i)],
i.CreatedAt,
i.CreatedByUserId,
i.OriginalFileName,
i.LastUsedAt,
i.DeletedAt,
Json(i.Variants),
i.WideFocusX,
i.WideFocusY,
i.WideZoom,
i.TallFocusX,
i.TallFocusY,
i.TallZoom);
/// <summary>The variants as the hero stores them (the same serializer as its database column), so they can go back unchanged.</summary>
public static string Json(IEnumerable<HeroVariant> variants) => System.Text.Json.JsonSerializer.Serialize(variants.ToList(), (System.Text.Json.JsonSerializerOptions?)null);
private static MediaCropSnapshot Crop(string ratio, HeroImage image)
{
var focus = image.FocusFor(ratio);
return new MediaCropSnapshot(ratio, focus.X, focus.Y, focus.Zoom);
}
}
@@ -1,4 +1,3 @@
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Library;
using SlpModularCms.Modules.Hero.Data.Entities;
@@ -10,59 +9,21 @@ namespace SlpModularCms.Modules.Hero.Services;
/// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the
/// hero), and moves them to the trash and back for it. The files are removed by <see cref="HeroMediaCleanup"/> after the trash period.
/// </summary>
public class HeroMediaUsageProvider(
IHeroRepository repository,
IHeroImageService images,
IMediaLibraryReader? reader = null,
ILogger<HeroMediaUsageProvider>? logger = null) : IMediaUsageProvider, IMediaReadVerifier
public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider
{
public const string SlideKind = "slide";
public string Module => HeroMapping.Area;
private MediaReadMode Mode => reader?.Mode ?? MediaReadMode.Off;
public async Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken)
{
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: false, cancellationToken) is { } fromLibrary)
{
return fromLibrary;
}
await CompareInTheLogAsync(cancellationToken);
return (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
}
=> (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
public async Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken)
{
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: true, cancellationToken) is { } fromLibrary)
{
return fromLibrary;
}
return (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
}
=> (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
public async Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken)
{
var wanted = photoIds.ToHashSet();
if (Mode == MediaReadMode.On)
{
try
{
return (await reader!.ListUsesAsync(Module, cancellationToken))
.Where(u => wanted.Contains(u.AssetId))
.GroupBy(u => u.AssetId)
.SelectMany(g => g.OrderBy(u => u.Position))
.Select(u => new MediaUse(Module, u.AssetId, u.PlaceKind, u.Position, u.Active))
.ToList();
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger?.LogError(exception, "Reading the hero uses from the media library failed; the hero's own tables are used instead.");
}
}
var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken);
// The position of a slide is its place in its hero, in display order (1-based), also for slides that are off.
@@ -83,64 +44,6 @@ public class HeroMediaUsageProvider(
return uses;
}
/// <summary>
/// The photos of the library tables (identity, size, name, dates), with the thumbnail from the hero (the variants are not in
/// the library tables until phase 3). Null when the library cannot be read: the hero's own tables are used then.
/// </summary>
private async Task<IReadOnlyList<MediaPhoto>?> TryReadPhotosAsync(bool trash, CancellationToken cancellationToken)
{
try
{
var assets = (await reader!.ListAssetsAsync(Module, cancellationToken)).Where(a => (a.DeletedAt is not null) == trash).ToList();
var own = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id);
var photos = assets.Select(a => new MediaPhoto(
Module, a.Id, a.Bytes, a.Width, a.Height, a.CreatedAt, own.TryGetValue(a.Id, out var image) ? Thumbnail(image) : null, a.OriginalFileName, a.LastUsedAt, a.DeletedAt));
return (trash ? photos.OrderByDescending(p => p.DeletedAt) : photos.OrderByDescending(p => p.CreatedAt)).ToList();
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger?.LogError(exception, "Reading the hero photos from the media library failed; the hero's own tables are used instead.");
return null;
}
}
/// <summary>In compare mode: every difference between the hero's tables and the library tables is a warning in the log.</summary>
private async Task CompareInTheLogAsync(CancellationToken cancellationToken)
{
if (Mode != MediaReadMode.Compare)
{
return;
}
try
{
var check = await CheckAsync(cancellationToken);
foreach (var difference in check.Differences.Take(20))
{
logger?.LogWarning("Media library read check (hero): {Difference}", difference);
}
if (check.Differences.Count > 20)
{
logger?.LogWarning("Media library read check (hero): {More} more differences not listed.", check.Differences.Count - 20);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger?.LogError(exception, "The media library read check of the hero failed.");
}
}
public async Task<MediaReadCheck> CheckAsync(CancellationToken cancellationToken)
{
var source = new HeroMediaMigrationSource(repository);
var ownAssets = await source.ListAssetsAsync(cancellationToken);
var ownUses = await source.ListUsesAsync(cancellationToken);
var libraryAssets = await reader!.ListAssetsAsync(Module, cancellationToken);
var libraryUses = await reader.ListUsesAsync(Module, cancellationToken);
return new MediaReadCheck(Module, Mode, ownAssets.Count, ownUses.Count, MediaReadComparer.Compare(ownAssets, ownUses, libraryAssets, libraryUses));
}
public async Task<int?> TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken)
{
try
@@ -30,9 +30,7 @@ public class HeroService(
ILogger<HeroService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
ITextCatalog? texts = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null) : IHeroService
ITextCatalog? texts = null) : IHeroService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
@@ -157,15 +155,6 @@ public class HeroService(
indexCache?.Invalidate();
imageService.NotifyPending();
if (legacyMirror is not null)
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
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);