diff --git a/SlpModularCms.Ci.slnf b/SlpModularCms.Ci.slnf index 1265755..ac3ee04 100644 --- a/SlpModularCms.Ci.slnf +++ b/SlpModularCms.Ci.slnf @@ -17,6 +17,8 @@ "src\\SlpModularCms.Modules.Offerings.Tests\\SlpModularCms.Modules.Offerings.Tests.csproj", "src\\SlpModularCms.Modules.Hero\\SlpModularCms.Modules.Hero.csproj", "src\\SlpModularCms.Modules.Hero.Tests\\SlpModularCms.Modules.Hero.Tests.csproj", + "src\\SlpModularCms.Modules.Media\\SlpModularCms.Modules.Media.csproj", + "src\\SlpModularCms.Modules.Media.Tests\\SlpModularCms.Modules.Media.Tests.csproj", "src\\SlpModularCms.Modules.Contact\\SlpModularCms.Modules.Contact.csproj", "src\\SlpModularCms.Modules.Contact.Tests\\SlpModularCms.Modules.Contact.Tests.csproj" ] diff --git a/SlpModularCms.sln b/SlpModularCms.sln index 836c7b3..e1ed2e1 100644 --- a/SlpModularCms.sln +++ b/SlpModularCms.sln @@ -50,6 +50,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Hero" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Hero.Tests", "src\SlpModularCms.Modules.Hero.Tests\SlpModularCms.Modules.Hero.Tests.csproj", "{838B2DA6-8B01-4C83-A696-D910AB2BC5D9}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Media", "src\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj", "{729ABD96-ADEB-4352-8030-2F6074B977F0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Media.Tests", "src\SlpModularCms.Modules.Media.Tests\SlpModularCms.Modules.Media.Tests.csproj", "{E697D4E1-FA8D-468C-BC31-81520715E246}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -300,6 +306,30 @@ Global {838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x64.Build.0 = Release|Any CPU {838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x86.ActiveCfg = Release|Any CPU {838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x86.Build.0 = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x64.ActiveCfg = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x64.Build.0 = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x86.ActiveCfg = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x86.Build.0 = Debug|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|Any CPU.Build.0 = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x64.ActiveCfg = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x64.Build.0 = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x86.ActiveCfg = Release|Any CPU + {729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x86.Build.0 = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x64.ActiveCfg = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x64.Build.0 = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x86.ActiveCfg = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x86.Build.0 = Debug|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|Any CPU.Build.0 = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x64.ActiveCfg = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x64.Build.0 = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x86.ActiveCfg = Release|Any CPU + {E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -327,5 +357,7 @@ Global {9EAC8A27-8DB0-4E3B-B144-280882828DE8} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} {4A7E3360-C7D0-4ADF-BC98-79AA912A8867} = {30D8F44D-4B6E-4980-8D1F-29D1A64F438C} {838B2DA6-8B01-4C83-A696-D910AB2BC5D9} = {77DF6642-7863-4D67-BAEE-217EC2D99894} + {729ABD96-ADEB-4352-8030-2F6074B977F0} = {30D8F44D-4B6E-4980-8D1F-29D1A64F438C} + {E697D4E1-FA8D-468C-BC31-81520715E246} = {77DF6642-7863-4D67-BAEE-217EC2D99894} EndGlobalSection EndGlobal diff --git a/aidlc-docs/features/media-library/assumptions-and-open-questions.md b/aidlc-docs/features/media-library/assumptions-and-open-questions.md index 6e4b948..0d1d04c 100644 --- a/aidlc-docs/features/media-library/assumptions-and-open-questions.md +++ b/aidlc-docs/features/media-library/assumptions-and-open-questions.md @@ -61,3 +61,5 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu - **Confirmed by Bryan "as designed"**: 1, 2, 5, 6, 9, 11, 14, 15, 16, 19, 20, 26, 27, 28. - **Still assumptions** (not put to him separately): 17 (language), 18 (what phase A contained, now overtaken), 22 (selectability), 23 (performance), 24 (ASF data), 25 (use definitions); and the older rows 18 to 25 above. Assumptions 29 to 32 follow from his decisions. - Assumption 12 now reads: there IS a trash (decision 12), the earlier "no trash in phase A" no longer applies. Assumption 10: deleting a used photo is possible after a second confirmation (built). Assumption 21: the original file name is stored from now on (built). +| 33 | **Where the copy runs and what it copies** (built 2026-11-17). | A new module `Modules.Media` owns the tables (`MediaAssets`, `MediaUses`); modules offer their data through `IMediaMigrationSource` so the library module does not depend on the Hero. The copy is a verified mirror in one transaction behind `Media:Library:CopyOnStartup` (default false). The use of a hero slide carries both crops (`wide`, `tall`) of its photo as JSON; its set hash is the photo's current set hash. | A copy job triggered from the admin instead of start-up: easier to time, needs an endpoint and a button. | `Modules.Media/Services/MediaLibraryCopier.cs`, `HeroMediaMigrationSource` | +| 34 | **Naming**: entity `MediaAssetUse` (table `MediaUses`), because `MediaUse` already names the read model in Core. | As built. | Rename the table or the record: a small migration. | `Modules.Media/Data` | diff --git a/aidlc-docs/features/media-library/inception/application-design/migration-plan.md b/aidlc-docs/features/media-library/inception/application-design/migration-plan.md index 78329b5..b4bc899 100644 --- a/aidlc-docs/features/media-library/inception/application-design/migration-plan.md +++ b/aidlc-docs/features/media-library/inception/application-design/migration-plan.md @@ -64,10 +64,26 @@ The migration job compares and **aborts** (rolls its transaction back) on any di 3. Back-up first (above), on the test host too. 4. Deploy with the setting **off**, check the verification result in the log, then switch it on, then the smoke test (hero markers, `/theme-init.js`, a sample of media addresses), then Bryan checks the admin. -## What is built first (next step, when this plan is accepted) +## Status (2026-11-17): phase 1 (expand) is built, behind a setting that is OFF -1. A new module `SlpModularCms.Modules.Media` with its own `DbContext`, the tables `MediaAssets` and `MediaUses` and an additive EF migration (no existing table touched), with tests. -2. The copy-and-verify job (idempotent, safe to run twice) behind a setting; unit and in-memory tests, and a test against MariaDB. -3. The library reading from the new tables behind `Media:Library:ReadFromLibrary` (default off). +Built on `feature/media-library-screen`, not yet deployed anywhere: -Nothing in steps 1 to 3 runs without the setting, so a deploy of that code alone changes no data except the new, empty tables. +- New module `SlpModularCms.Modules.Media` (own `MediaDbContext`, tables `MediaAssets` and `MediaUses`, additive migration `InitialCreate`; no existing table is touched). It is referenced by the three hosts, so a deploy of this code creates the two empty tables at start-up and nothing else. +- `IMediaMigrationSource` (Core) and `HeroMediaMigrationSource` (Hero, read-only): the hero's photos (also those in the trash) and slides with their own ids, hashes and set hashes. +- `MediaLibraryCopier`: copies and **verifies** (counts, ids, hash, key, size, set hash) in one transaction; any difference or error rolls the whole copy back and nothing is left. It MIRRORS the source, so running it again gives the same result; rows of other modules are not touched. +- The copy runs only when `Media__Library__CopyOnStartup=true` (default **false**) and a failure is logged, never fatal. +- Tested in memory and against a real MariaDB (migration, copy twice, rollback on a failing copy). + +Nothing reads from the new tables yet (phase 2, the switch, is not built). The library screen still reads the hero through the usage provider. + +### Roll-out on TEST (needs SLS-Main to be told first) + +1. Back-up (database dump and media listing) as above. +2. Deploy the branch with `Media__Library__CopyOnStartup` unset: the two empty tables appear; check the log for the Media migration. +3. Set `Media__Library__CopyOnStartup=true` and restart: the log says `Media library copy verified: N photos and M uses from hero` (or the error and the rollback). +4. Rollback at any point: set it back to false (the tables stay, unused) or drop `MediaUses` and `MediaAssets`. +5. Never on production. + +### Still to build (phases 2 and 3) + +The switch (`Media:Library:ReadFromLibrary`, dual write of new uploads, library reading from the new tables) and the contract phase. They need Bryan's acceptance of the copy on test first. diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj b/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj index 8302f79..623fce0 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj +++ b/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj @@ -31,6 +31,7 @@ + diff --git a/src/SlpModularCms.Api.SlpSoftware/SlpModularCms.Api.SlpSoftware.csproj b/src/SlpModularCms.Api.SlpSoftware/SlpModularCms.Api.SlpSoftware.csproj index 53a41cd..b31dd5f 100644 --- a/src/SlpModularCms.Api.SlpSoftware/SlpModularCms.Api.SlpSoftware.csproj +++ b/src/SlpModularCms.Api.SlpSoftware/SlpModularCms.Api.SlpSoftware.csproj @@ -25,6 +25,7 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs b/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs new file mode 100644 index 0000000..b99fd79 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs @@ -0,0 +1,59 @@ +namespace SlpModularCms.Modules.Media.Data.Entities; + +/// +/// A photo in the library: the original (never served), its default focus and when it was last used or trashed. The ids, +/// hashes and keys are the ones the owning module already uses, so public addresses stay as they are. +/// +public class MediaAsset +{ + public Guid Id { get; set; } + + /// The module that uploaded it (for example hero); the library is shared, this only says where it came from. + public string Module { get; set; } = string.Empty; + + /// SHA-256 of the file as uploaded (hex); the same photo uploaded twice is one record. + 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 DefaultFocusX { get; set; } = 0.5; + public double DefaultFocusY { get; set; } = 0.5; + public double DefaultZoom { get; set; } = 1.0; + + 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; } + + public List Uses { get; set; } = []; +} + +/// +/// One place where a photo is used, with its own crops. The set hash is computed by the module with the existing formula +/// over the photo's hash and these crops. +/// +public class MediaAssetUse +{ + public Guid Id { get; set; } + public Guid AssetId { get; set; } + public MediaAsset? Asset { get; set; } + + public string Module { get; set; } = string.Empty; + + /// For example slide. + public string PlaceKind { get; set; } = string.Empty; + + /// The 1-based position in the place (display order). + public int Position { get; set; } + + public bool Active { get; set; } = true; + public string SetHash { get; set; } = string.Empty; + + /// The crops of this use as JSON: [{ratio, x, y, zoom}]. Empty for a natural-ratio use. + public string Crops { get; set; } = "[]"; +} diff --git a/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs b/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs new file mode 100644 index 0000000..3f85b1d --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore; +using SlpModularCms.Modules.Media.Data.Entities; + +namespace SlpModularCms.Modules.Media.Data; + +public class MediaDbContext(DbContextOptions options) : DbContext(options) +{ + public DbSet MediaAssets => Set(); + public DbSet MediaUses => Set(); + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity(entity => + { + entity.ToTable("MediaAssets"); + entity.HasKey(e => e.Id); + entity.Property(e => e.Module).IsRequired().HasMaxLength(20); + entity.Property(e => e.OriginalSha256).IsRequired().HasMaxLength(64); + entity.HasIndex(e => e.OriginalSha256).IsUnique(); + entity.Property(e => e.OriginalKey).IsRequired().HasMaxLength(200); + entity.Property(e => e.SourceFormat).IsRequired().HasMaxLength(10); + entity.Property(e => e.OriginalFileName).HasMaxLength(200); + entity.HasIndex(e => e.CreatedAt); + entity.HasMany(e => e.Uses).WithOne(u => u.Asset).HasForeignKey(u => u.AssetId).OnDelete(DeleteBehavior.Cascade); + }); + + modelBuilder.Entity(entity => + { + entity.ToTable("MediaUses"); + entity.HasKey(e => e.Id); + entity.Property(e => e.Module).IsRequired().HasMaxLength(20); + entity.Property(e => e.PlaceKind).IsRequired().HasMaxLength(20); + entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16); + entity.Property(e => e.Crops).IsRequired().HasColumnType("longtext"); + entity.HasIndex(e => e.AssetId); + }); + } +} diff --git a/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs b/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs new file mode 100644 index 0000000..8e2a65a --- /dev/null +++ b/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs @@ -0,0 +1,13 @@ +namespace SlpModularCms.Modules.Media; + +/// Settings of the library's own tables (Media:Library:*). Everything is off by default: a deploy of the code alone only adds the empty tables. +public class MediaLibraryOptions +{ + public const string SectionName = "Media:Library"; + + /// + /// Copy the photos and uses of the modules into the library's tables once at start-up and verify the copy + /// (Media__Library__CopyOnStartup=true). Safe to repeat: the copy mirrors the source. Never touches the modules' own data. + /// + public bool CopyOnStartup { get; set; } +} diff --git a/src/SlpModularCms.Modules.Media/MediaModule.cs b/src/SlpModularCms.Modules.Media/MediaModule.cs new file mode 100644 index 0000000..76625e6 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/MediaModule.cs @@ -0,0 +1,72 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Builder; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Hosting; +using SlpModularCms.Core.Modules; +using SlpModularCms.Modules.Media.Data; +using SlpModularCms.Modules.Media.Services; + +namespace SlpModularCms.Modules.Media; + +/// +/// The library's own tables (photos and where they are used). Phase 1 of the migration plan: only the tables, and a copy +/// of the modules' photos behind Media:Library:CopyOnStartup (off by default). Nothing reads from these tables yet. +/// +[ExcludeFromCodeCoverage] +public class MediaModule : IModule +{ + public string Name => "Media"; + public string Version => "1.0.0"; + + public void RegisterServices(IServiceCollection services) + { + services.AddDbContext((serviceProvider, options) => + { + var config = serviceProvider.GetRequiredService(); + options.UseMySQL(config.GetConnectionString("DefaultConnection")!); + options.ReplaceService(); + }); + + services.AddOptions().Configure((options, configuration) => + configuration.GetSection(MediaLibraryOptions.SectionName).Bind(options)); + services.AddScoped(); + services.AddHostedService(); + } + + public void UseModule(IApplicationBuilder app) + { + using var scope = app.ApplicationServices.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.Migrate(); + } +} + +/// Runs the verified copy once at start-up when Media:Library:CopyOnStartup is on; a failure is logged and never stops the host. +public sealed class MediaLibraryCopyService(IServiceScopeFactory scopes, IOptions options, ILogger logger) : IHostedService +{ + public async Task StartAsync(CancellationToken cancellationToken) + { + if (!options.Value.CopyOnStartup) + { + return; + } + + try + { + using var scope = scopes.CreateScope(); + await scope.ServiceProvider.GetRequiredService().CopyAsync(cancellationToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger.LogError(exception, "The media library copy failed and was rolled back; the modules' own data is untouched."); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.Designer.cs b/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.Designer.cs new file mode 100644 index 0000000..902c72b --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.Designer.cs @@ -0,0 +1,154 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SlpModularCms.Modules.Media.Data; + +#nullable disable + +namespace SlpModularCms.Modules.Media.Migrations +{ + [DbContext(typeof(MediaDbContext))] + [Migration("20261117143138_InitialCreate")] + partial class InitialCreate + { + /// + 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.Media.Data.Entities.MediaAsset", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Bytes") + .HasColumnType("bigint"); + + b.Property("CreatedAt") + .HasColumnType("datetime"); + + b.Property("CreatedByUserId") + .HasColumnType("char(36)"); + + b.Property("DefaultFocusX") + .HasColumnType("double"); + + b.Property("DefaultFocusY") + .HasColumnType("double"); + + b.Property("DefaultZoom") + .HasColumnType("double"); + + b.Property("DeletedAt") + .HasColumnType("datetime"); + + b.Property("Height") + .HasColumnType("int"); + + b.Property("LastUsedAt") + .HasColumnType("datetime"); + + b.Property("Module") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("OriginalFileName") + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalSha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("SourceFormat") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("Width") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CreatedAt"); + + b.HasIndex("OriginalSha256") + .IsUnique(); + + b.ToTable("MediaAssets", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Active") + .HasColumnType("tinyint(1)"); + + b.Property("AssetId") + .HasColumnType("char(36)"); + + b.Property("Crops") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Module") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("PlaceKind") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("Position") + .HasColumnType("int"); + + b.Property("SetHash") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.HasKey("Id"); + + b.HasIndex("AssetId"); + + b.ToTable("MediaUses", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b => + { + b.HasOne("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", "Asset") + .WithMany("Uses") + .HasForeignKey("AssetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Asset"); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b => + { + b.Navigation("Uses"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.cs b/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.cs new file mode 100644 index 0000000..1881e9c --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Migrations/20261117143138_InitialCreate.cs @@ -0,0 +1,96 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SlpModularCms.Modules.Media.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "MediaAssets", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + Module = table.Column(type: "varchar(20)", maxLength: 20, nullable: false), + OriginalSha256 = table.Column(type: "varchar(64)", maxLength: 64, nullable: false), + OriginalKey = table.Column(type: "varchar(200)", maxLength: 200, nullable: false), + SourceFormat = table.Column(type: "varchar(10)", maxLength: 10, nullable: false), + Width = table.Column(type: "int", nullable: false), + Height = table.Column(type: "int", nullable: false), + Bytes = table.Column(type: "bigint", nullable: false), + DefaultFocusX = table.Column(type: "double", nullable: false), + DefaultFocusY = table.Column(type: "double", nullable: false), + DefaultZoom = table.Column(type: "double", nullable: false), + CreatedAt = table.Column(type: "datetime", nullable: false), + CreatedByUserId = table.Column(type: "char(36)", nullable: false), + OriginalFileName = table.Column(type: "varchar(200)", maxLength: 200, nullable: true), + LastUsedAt = table.Column(type: "datetime", nullable: true), + DeletedAt = table.Column(type: "datetime", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_MediaAssets", x => x.Id); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "MediaUses", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + AssetId = table.Column(type: "char(36)", nullable: false), + Module = table.Column(type: "varchar(20)", maxLength: 20, nullable: false), + PlaceKind = table.Column(type: "varchar(20)", maxLength: 20, nullable: false), + Position = table.Column(type: "int", nullable: false), + Active = table.Column(type: "tinyint(1)", nullable: false), + SetHash = table.Column(type: "varchar(16)", maxLength: 16, nullable: false), + Crops = table.Column(type: "longtext", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_MediaUses", x => x.Id); + table.ForeignKey( + name: "FK_MediaUses_MediaAssets_AssetId", + column: x => x.AssetId, + principalTable: "MediaAssets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_MediaAssets_CreatedAt", + table: "MediaAssets", + column: "CreatedAt"); + + migrationBuilder.CreateIndex( + name: "IX_MediaAssets_OriginalSha256", + table: "MediaAssets", + column: "OriginalSha256", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_MediaUses_AssetId", + table: "MediaUses", + column: "AssetId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "MediaUses"); + + migrationBuilder.DropTable( + name: "MediaAssets"); + } + } +} diff --git a/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs b/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs new file mode 100644 index 0000000..1d7f5b3 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs @@ -0,0 +1,151 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SlpModularCms.Modules.Media.Data; + +#nullable disable + +namespace SlpModularCms.Modules.Media.Migrations +{ + [DbContext(typeof(MediaDbContext))] + partial class MediaDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Bytes") + .HasColumnType("bigint"); + + b.Property("CreatedAt") + .HasColumnType("datetime"); + + b.Property("CreatedByUserId") + .HasColumnType("char(36)"); + + b.Property("DefaultFocusX") + .HasColumnType("double"); + + b.Property("DefaultFocusY") + .HasColumnType("double"); + + b.Property("DefaultZoom") + .HasColumnType("double"); + + b.Property("DeletedAt") + .HasColumnType("datetime"); + + b.Property("Height") + .HasColumnType("int"); + + b.Property("LastUsedAt") + .HasColumnType("datetime"); + + b.Property("Module") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("OriginalFileName") + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalSha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("SourceFormat") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("Width") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CreatedAt"); + + b.HasIndex("OriginalSha256") + .IsUnique(); + + b.ToTable("MediaAssets", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Active") + .HasColumnType("tinyint(1)"); + + b.Property("AssetId") + .HasColumnType("char(36)"); + + b.Property("Crops") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("Module") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("PlaceKind") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("Position") + .HasColumnType("int"); + + b.Property("SetHash") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.HasKey("Id"); + + b.HasIndex("AssetId"); + + b.ToTable("MediaUses", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b => + { + b.HasOne("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", "Asset") + .WithMany("Uses") + .HasForeignKey("AssetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Asset"); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b => + { + b.Navigation("Uses"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs new file mode 100644 index 0000000..0856f74 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs @@ -0,0 +1,204 @@ +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using SlpModularCms.Core.Media.Library; +using SlpModularCms.Modules.Media.Data; +using SlpModularCms.Modules.Media.Data.Entities; + +namespace SlpModularCms.Modules.Media.Services; + +/// What a copy did and what the verification found. +public sealed record MediaCopyReport(int Assets, int Uses, IReadOnlyList Differences) +{ + public bool Verified => Differences.Count == 0; +} + +public sealed class MediaCopyVerificationException(MediaCopyReport report) + : Exception($"The copy of the media library does not match its source: {string.Join("; ", report.Differences.Take(5))}") +{ + public MediaCopyReport Report { get; } = report; +} + +/// +/// Copies the photos and uses of every module () into the library's own tables and +/// verifies the copy against the source (migration plan, decision 13). The copy MIRRORS the source: re-running it gives +/// the same result, rows of a module that no longer exist at the source are removed from the copy. The modules' own data +/// is never changed. A difference after copying rolls the whole copy back and raises . +/// +public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerable sources, ILogger logger) +{ + private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web); + + public async Task CopyAsync(CancellationToken cancellationToken) + { + var assets = new List(); + var uses = new List(); + var modules = new List(); + foreach (var source in sources) + { + modules.Add(source.Module); + assets.AddRange(await source.ListAssetsAsync(cancellationToken)); + uses.AddRange(await source.ListUsesAsync(cancellationToken)); + } + + var transaction = db.Database.IsRelational() ? await db.Database.BeginTransactionAsync(cancellationToken) : null; + try + { + await MirrorAsync(assets, uses, modules, cancellationToken); + await db.SaveChangesAsync(cancellationToken); + + var report = await VerifyAsync(assets, uses, modules, cancellationToken); + if (!report.Verified) + { + throw new MediaCopyVerificationException(report); + } + + if (transaction is not null) + { + await transaction.CommitAsync(cancellationToken); + } + + logger.LogInformation("Media library copy verified: {Assets} photos and {Uses} uses from {Modules}.", report.Assets, report.Uses, string.Join(", ", modules)); + return report; + } + catch + { + if (transaction is not null) + { + await transaction.RollbackAsync(CancellationToken.None); + } + + db.ChangeTracker.Clear(); + throw; + } + finally + { + if (transaction is not null) + { + await transaction.DisposeAsync(); + } + } + } + + private async Task MirrorAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken) + { + var existingAssets = new Dictionary(); + var existingUses = new Dictionary(); + foreach (var module in modules) + { + // One equality query per module: the MySQL provider cannot translate a list parameter in an IN clause. + foreach (var asset in await db.MediaAssets.Where(a => a.Module == module).ToListAsync(cancellationToken)) + { + existingAssets[asset.Id] = asset; + } + + foreach (var use in await db.MediaUses.Where(u => u.Module == module).ToListAsync(cancellationToken)) + { + existingUses[use.Id] = use; + } + } + + foreach (var snapshot in assets) + { + if (!existingAssets.Remove(snapshot.Id, out var row)) + { + row = new MediaAsset { Id = snapshot.Id }; + db.MediaAssets.Add(row); + } + + row.Module = snapshot.Module; + row.OriginalSha256 = snapshot.OriginalSha256; + row.OriginalKey = snapshot.OriginalKey; + row.SourceFormat = snapshot.SourceFormat; + row.Width = snapshot.Width; + row.Height = snapshot.Height; + row.Bytes = snapshot.Bytes; + row.DefaultFocusX = snapshot.FocusX; + row.DefaultFocusY = snapshot.FocusY; + row.DefaultZoom = snapshot.Zoom; + row.CreatedAt = snapshot.CreatedAt; + row.CreatedByUserId = snapshot.CreatedByUserId; + row.OriginalFileName = snapshot.OriginalFileName; + row.LastUsedAt = snapshot.LastUsedAt; + row.DeletedAt = snapshot.DeletedAt; + } + + // Uses reference their asset; the crops of a use are the crops of its photo (the hero's wide and tall). + var cropsByAsset = assets.ToDictionary(a => a.Id, a => JsonSerializer.Serialize(a.Crops, Json)); + foreach (var snapshot in uses) + { + if (!existingUses.Remove(snapshot.Id, out var row)) + { + row = new MediaAssetUse { Id = snapshot.Id }; + db.MediaUses.Add(row); + } + + row.AssetId = snapshot.AssetId; + row.Module = snapshot.Module; + row.PlaceKind = snapshot.PlaceKind; + row.Position = snapshot.Position; + row.Active = snapshot.Active; + row.SetHash = snapshot.SetHash; + row.Crops = cropsByAsset.TryGetValue(snapshot.AssetId, out var crops) ? crops : "[]"; + } + + // What is left no longer exists at the source. + db.MediaUses.RemoveRange(existingUses.Values); + db.MediaAssets.RemoveRange(existingAssets.Values); + } + + private async Task VerifyAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken) + { + var differences = new List(); + var copiedAssets = new Dictionary(); + var copiedUses = new Dictionary(); + foreach (var module in modules) + { + foreach (var asset in await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken)) + { + copiedAssets[asset.Id] = asset; + } + + foreach (var use in await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken)) + { + copiedUses[use.Id] = use; + } + } + + if (copiedAssets.Count != assets.Count) + { + differences.Add($"photos: source {assets.Count}, copy {copiedAssets.Count}"); + } + + if (copiedUses.Count != uses.Count) + { + differences.Add($"uses: source {uses.Count}, copy {copiedUses.Count}"); + } + + foreach (var asset in assets) + { + if (!copiedAssets.TryGetValue(asset.Id, out var copy)) + { + differences.Add($"photo {asset.Id} is missing in the copy"); + } + else if (copy.OriginalSha256 != asset.OriginalSha256 || copy.OriginalKey != asset.OriginalKey || copy.Bytes != asset.Bytes) + { + differences.Add($"photo {asset.Id}: hash, key or size differs"); + } + } + + foreach (var use in uses) + { + if (!copiedUses.TryGetValue(use.Id, out var copy)) + { + differences.Add($"use {use.Id} is missing in the copy"); + } + else if (copy.SetHash != use.SetHash || copy.AssetId != use.AssetId) + { + differences.Add($"use {use.Id}: set hash or photo differs"); + } + } + + return new MediaCopyReport(copiedAssets.Count, copiedUses.Count, differences); + } +} diff --git a/src/SlpModularCms.Modules.Media/SlpModularCms.Modules.Media.csproj b/src/SlpModularCms.Modules.Media/SlpModularCms.Modules.Media.csproj new file mode 100644 index 0000000..3d21af3 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/SlpModularCms.Modules.Media.csproj @@ -0,0 +1,19 @@ + + + + net10.0 + enable + enable + + + + + <_Parameter1>SlpModularCms.Modules.Media.Tests + + + + + + + +