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:
@@ -114,10 +114,22 @@ public class HeroImageService(
|
||||
TimeProvider? time = null,
|
||||
IIndexHtmlCache? indexCache = null,
|
||||
HeroRenderSignal? signal = null,
|
||||
IMediaLibrarySync? librarySync = null) : IHeroImageService
|
||||
IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = 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 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;
|
||||
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,8 @@ public class HeroMediaCleanup(
|
||||
IOptions<MediaOptions> options,
|
||||
ILogger<HeroMediaCleanup> logger,
|
||||
TimeProvider? time = null,
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null)
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = null)
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
|
||||
@@ -53,6 +54,11 @@ public class HeroMediaCleanup(
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -31,7 +31,8 @@ public class HeroService(
|
||||
TimeProvider? time = null,
|
||||
IIndexHtmlCache? indexCache = null,
|
||||
ITextCatalog? texts = null,
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null) : IHeroService
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = null) : IHeroService
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
|
||||
@@ -156,6 +157,11 @@ 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);
|
||||
|
||||
Reference in New Issue
Block a user