Fase 3b: de Hero bewaart zijn foto's in de bibliotheektabel (schakelaar uit), HeroImages blijft een kopie, controle bij het opstarten, FK-migratie met Down

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 00:10:58 +01:00
co-authored by Claude Sonnet 5.5
parent 53e1e3c8a6
commit 7246efccc1
18 changed files with 957 additions and 11 deletions
@@ -0,0 +1,107 @@
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.");
}
}
}