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:
@@ -112,6 +112,37 @@ public class HeroImage
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The same photo as <see cref="HeroImage"/>, always stored in <c>HeroImages</c>. Only used while the photos live in the
|
||||
/// library (phase 3b): then <c>HeroImages</c> is kept up to date from the library rows so the switch can be turned back.
|
||||
/// </summary>
|
||||
public class LegacyHeroImage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string OriginalSha256 { get; set; } = string.Empty;
|
||||
public string OriginalKey { get; set; } = string.Empty;
|
||||
public string SourceFormat { get; set; } = string.Empty;
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public long Bytes { get; set; }
|
||||
public double FocusX { get; set; } = 0.5;
|
||||
public double FocusY { get; set; } = 0.5;
|
||||
public double Zoom { get; set; } = 1.0;
|
||||
public double? WideFocusX { get; set; }
|
||||
public double? WideFocusY { get; set; }
|
||||
public double? WideZoom { get; set; }
|
||||
public double? TallFocusX { get; set; }
|
||||
public double? TallFocusY { get; set; }
|
||||
public double? TallZoom { get; set; }
|
||||
public string SetHash { get; set; } = string.Empty;
|
||||
public List<HeroVariant> Variants { get; set; } = [];
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public Guid CreatedByUserId { get; set; }
|
||||
public string? OriginalFileName { get; set; }
|
||||
public DateTimeOffset? LastUsedAt { get; set; }
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>One file of the current set: the ratio, its pixel size, the type and the file name inside the set.</summary>
|
||||
public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes);
|
||||
|
||||
|
||||
@@ -2,11 +2,18 @@ using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(options)
|
||||
public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage? storage = null) : DbContext(options)
|
||||
{
|
||||
/// <summary>True while the photos live in the library's table (phase 3b); see <see cref="HeroStorage"/>.</summary>
|
||||
public bool PhotosInLibrary { get; } = storage?.PhotosInLibrary ?? false;
|
||||
|
||||
/// <summary>The photos as they are always kept in <c>HeroImages</c> (only mapped while <see cref="PhotosInLibrary"/> is on).</summary>
|
||||
public DbSet<LegacyHeroImage> LegacyHeroImages => Set<LegacyHeroImage>();
|
||||
|
||||
public DbSet<HeroContent> Heroes => Set<HeroContent>();
|
||||
public DbSet<HeroSlide> HeroSlides => Set<HeroSlide>();
|
||||
public DbSet<HeroImage> HeroImages => Set<HeroImage>();
|
||||
@@ -86,6 +93,8 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(
|
||||
v => v.ToList()));
|
||||
});
|
||||
|
||||
ConfigureStorage(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<HeroRetiredFile>(entity =>
|
||||
{
|
||||
entity.ToTable("HeroRetiredFiles");
|
||||
@@ -95,4 +104,65 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(
|
||||
entity.Property(e => e.RetiredAt).IsRequired();
|
||||
});
|
||||
}
|
||||
|
||||
private void ConfigureStorage(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (!PhotosInLibrary)
|
||||
{
|
||||
modelBuilder.Ignore<LegacyHeroImage>();
|
||||
return;
|
||||
}
|
||||
|
||||
// The hero's photos are rows of the library's table, module "hero". The library owns that table and its migrations.
|
||||
modelBuilder.Entity<HeroImage>(entity =>
|
||||
{
|
||||
entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations());
|
||||
entity.Property<string>(ModuleColumn).HasMaxLength(20).IsRequired().HasDefaultValue(HeroMapping.Area);
|
||||
entity.Property(e => e.FocusX).HasColumnName("DefaultFocusX");
|
||||
entity.Property(e => e.FocusY).HasColumnName("DefaultFocusY");
|
||||
entity.Property(e => e.Zoom).HasColumnName("DefaultZoom");
|
||||
entity.HasQueryFilter(e => EF.Property<string>(e, ModuleColumn) == HeroMapping.Area);
|
||||
});
|
||||
|
||||
// And HeroImages stays a complete copy, so switching back loses nothing.
|
||||
modelBuilder.Entity<LegacyHeroImage>(entity =>
|
||||
{
|
||||
entity.ToTable("HeroImages");
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
|
||||
entity.Property(e => e.Variants)
|
||||
.HasConversion(
|
||||
variants => JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null),
|
||||
json => JsonSerializer.Deserialize<List<HeroVariant>>(json, (JsonSerializerOptions?)null) ?? new List<HeroVariant>())
|
||||
.HasColumnType("longtext");
|
||||
});
|
||||
}
|
||||
|
||||
public const string ModuleColumn = "Module";
|
||||
|
||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||
{
|
||||
MarkNewPhotosAsHero();
|
||||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
|
||||
{
|
||||
MarkNewPhotosAsHero();
|
||||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>A photo the hero adds to the library's table belongs to the hero module.</summary>
|
||||
private void MarkNewPhotosAsHero()
|
||||
{
|
||||
if (!PhotosInLibrary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entry in ChangeTracker.Entries<HeroImage>().Where(e => e.State == EntityState.Added))
|
||||
{
|
||||
entry.Property(ModuleColumn).CurrentValue = HeroMapping.Area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Where the hero's photos are stored (media library phase 3b). Off (default): the hero's own table <c>HeroImages</c>. On
|
||||
/// (<c>Media__Library__HeroPhotosInLibrary=true</c>): the library's <c>MediaAssets</c> table (rows of module <c>hero</c>), with
|
||||
/// <c>HeroImages</c> kept up to date as a safety net so the switch can go back. The choice is made when the application starts.
|
||||
/// </summary>
|
||||
public sealed record HeroStorage(bool PhotosInLibrary);
|
||||
|
||||
/// <summary>EF builds the model once per key: the two storage modes have different models.</summary>
|
||||
public sealed class HeroModelCacheKeyFactory : IModelCacheKeyFactory
|
||||
{
|
||||
public object Create(DbContext context, bool designTime)
|
||||
=> (context.GetType(), context is HeroDbContext { PhotosInLibrary: true }, designTime);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -26,12 +28,21 @@ public class HeroModule : IModule
|
||||
|
||||
public void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
// Where the photos are stored (media library phase 3b): off = HeroImages, on = the library's MediaAssets table.
|
||||
services.AddSingleton(serviceProvider => new HeroStorage(serviceProvider.GetRequiredService<IConfiguration>().GetValue<bool>("Media:Library:HeroPhotosInLibrary")));
|
||||
services.AddDbContext<HeroDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
||||
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
options.ReplaceService<IModelCacheKeyFactory, HeroModelCacheKeyFactory>();
|
||||
if (serviceProvider.GetRequiredService<HeroStorage>().PhotosInLibrary)
|
||||
{
|
||||
// The model differs from the migrations snapshot on purpose (the photos' table belongs to the library).
|
||||
options.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
|
||||
}
|
||||
});
|
||||
services.AddScoped<IHeroLegacyImageMirror, HeroLegacyImageMirror>();
|
||||
|
||||
services.AddCmsTextsFrom(typeof(HeroModule).Assembly);
|
||||
services.AddOptions<HeroOptions>().Configure<IConfiguration>((options, configuration) =>
|
||||
@@ -63,5 +74,6 @@ public class HeroModule : IModule
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
|
||||
db.Database.Migrate();
|
||||
HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+291
@@ -0,0 +1,291 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
[DbContext(typeof(HeroDbContext))]
|
||||
[Migration("20261117230241_DropSlideImageForeignKey")]
|
||||
partial class DropSlideImageForeignKey
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.9")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Button1Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button1Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Button2Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button2Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Display")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<string>("Language")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<Guid>("LastModifiedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Lead")
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<string>("Tagline")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<DateTimeOffset>("UpdatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Language")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Heroes", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<long>("Bytes")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<Guid>("CreatedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<double>("FocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("FocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("OriginalFileName")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalKey")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalSha256")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<string>("SourceFormat")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<double?>("TallFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<string>("Variants")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<double?>("WideFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<int>("Width")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Zoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("HeroImages", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Kind")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<DateTimeOffset>("RetiredAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("HeroRetiredFiles", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Alt")
|
||||
.IsRequired()
|
||||
.HasMaxLength(600)
|
||||
.HasColumnType("varchar(600)");
|
||||
|
||||
b.Property<bool>("Decorative")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<double?>("FocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("FocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<Guid>("HeroId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("ImageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<int>("SortOrder")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double?>("TallFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<string>("Variants")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<double?>("WideFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("Zoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ImageId");
|
||||
|
||||
b.HasIndex("HeroId", "SortOrder");
|
||||
|
||||
b.ToTable("HeroSlides", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
|
||||
.WithMany("Slides")
|
||||
.HasForeignKey("HeroId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
|
||||
.WithMany()
|
||||
.HasForeignKey("ImageId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Image");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Navigation("Slides");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class DropSlideImageForeignKey : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
// Phase 3b of the media library: a slide's photo can live in the library's MediaAssets instead of HeroImages, so the
|
||||
// database no longer ties a slide to HeroImages. The service layer already checks that the photo exists and that a
|
||||
// photo in use is never removed. The model snapshot is deliberately unchanged (the relation stays in the EF model, only
|
||||
// the constraint in the database goes), so this migration is reversible and later migrations see no difference.
|
||||
migrationBuilder.DropForeignKey(name: "FK_HeroSlides_HeroImages_ImageId", table: "HeroSlides");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_HeroSlides_HeroImages_ImageId",
|
||||
table: "HeroSlides",
|
||||
column: "ImageId",
|
||||
principalTable: "HeroImages",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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