using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using SlpModularCms.Modules.Hero.Data; using SlpModularCms.Modules.Hero.Data.Entities; namespace SlpModularCms.Modules.Hero.Services; /// Keeps HeroImages a complete copy of the hero's photos while they live in the library's table (phase 3b). public interface IHeroLegacyImageMirror { /// Does nothing unless the photos live in the library. Never throws: the library rows are the truth then. Task SyncAsync(CancellationToken cancellationToken); } public sealed class HeroLegacyImageMirror(HeroDbContext db, ILogger 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; } } /// /// Run at start-up when the photos are to live in the library: the library rows must already be a complete copy of /// HeroImages (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. /// 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(); 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."); } } }