diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index bd373ef..dc34038 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -30,6 +30,11 @@ on: type: choice options: [unchanged, 'off', compare, 'on'] default: unchanged + media_library_hero_photos: + description: 'Hero photos in the media library on the TEST deploys (Media__Library__HeroPhotosInLibrary in shared/env): unchanged, on or off. Needs the copy and the library check first.' + type: choice + options: [unchanged, 'on', 'off'] + default: unchanged pull_request: types: [opened, synchronize, reopened] push: @@ -573,6 +578,7 @@ jobs: media_library_copy: ${{ github.event.inputs.media_library_copy || 'unchanged' }} media_library_dual_write: ${{ github.event.inputs.media_library_dual_write || 'unchanged' }} media_library_read: ${{ github.event.inputs.media_library_read || 'unchanged' }} + media_library_hero_photos: ${{ github.event.inputs.media_library_hero_photos || 'unchanged' }} # Production deploy ONLY on an explicit workflow_dispatch run with deploy_sls_production checked, and # only when that run is on a release/* branch (same rule as ASF) — never reachable from a plain push @@ -703,6 +709,7 @@ jobs: media_library_copy: ${{ github.event.inputs.media_library_copy || 'unchanged' }} media_library_dual_write: ${{ github.event.inputs.media_library_dual_write || 'unchanged' }} media_library_read: ${{ github.event.inputs.media_library_read || 'unchanged' }} + media_library_hero_photos: ${{ github.event.inputs.media_library_hero_photos || 'unchanged' }} # --- Smoke tests of the deployed compositions (anonymous requests, see scripts/smoke-site.sh) --- # The CI tests the master CMS; this is the check that the composition of each site that was just deployed to test diff --git a/.gitea/workflows/deploy-scp.yaml b/.gitea/workflows/deploy-scp.yaml index fad9fc5..4207513 100644 --- a/.gitea/workflows/deploy-scp.yaml +++ b/.gitea/workflows/deploy-scp.yaml @@ -52,6 +52,11 @@ on: required: false type: string default: unchanged + media_library_hero_photos: + description: 'unchanged | on | off: sets Media__Library__HeroPhotosInLibrary (phase 3b: the hero photos live in the library table) in shared/env before the restart. TEST only, never production.' + required: false + type: string + default: unchanged jobs: deploy: @@ -311,6 +316,34 @@ jobs: fi echo "Media__Library__ReadFromLibrary is now: $(rssh "grep '^Media__Library__ReadFromLibrary=' $ENV_FILE || echo '(not set = off)'")" + # The hero-photos storage switch of phase 3b (same rules as the switches above). `on` makes the hero keep its photos in the + # library's MediaAssets table (HeroImages stays a copy so it can go back); the application REFUSES to start when the library + # does not yet hold every hero photo, so run the copy first and check it with the library check. + - name: Set the media library hero photos switch (test only) + if: inputs.media_library_hero_photos != 'unchanged' + env: + SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} + SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} + SSH_PORT: ${{ secrets.PI_MAIN_PORT }} + SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} + run: | + MODE="${{ inputs.media_library_hero_photos }}" + if [ "${{ inputs.environment }}" != "test" ]; then + echo "The hero photos switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 + exit 1 + fi + if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then + echo "media_library_hero_photos must be unchanged, on or off (got: $MODE)." >&2 + exit 1 + fi + ENV_FILE="${{ inputs.deploy_path }}/shared/env" + if [ "$MODE" = "on" ]; then + rssh "touch $ENV_FILE && sed -i '/^Media__Library__HeroPhotosInLibrary=/d' $ENV_FILE && echo 'Media__Library__HeroPhotosInLibrary=true' >> $ENV_FILE" + else + rssh "touch $ENV_FILE && sed -i '/^Media__Library__HeroPhotosInLibrary=/d' $ENV_FILE" + fi + echo "Media__Library__HeroPhotosInLibrary is now: $(rssh "grep '^Media__Library__HeroPhotosInLibrary=' $ENV_FILE || echo '(not set = off)'")" + # Atomic release switch (FR-06, D-27): `ln -sfn` replaces the `current` symlink target in a # single filesystem operation, so there is no moment where `current` points at a half-written # directory. The process is then restarted so it picks up the new assemblies — a running .NET 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 2341bd3..2ce64dd 100644 --- a/aidlc-docs/features/media-library/assumptions-and-open-questions.md +++ b/aidlc-docs/features/media-library/assumptions-and-open-questions.md @@ -81,3 +81,4 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu | 50 | **Step C reads the library screen's data from the library tables, not the hero admin** (2026-11-17). | The read switch (`off`/`compare`/`on`) changes where the library screen gets photos, trash and uses; the hero's admin and public endpoints keep their own tables because they need the variants, which move in phase 3. Thumbnails come from the hero. `compare` only logs and reports (`GET media-library/read-check`); `on` falls back to the hero's tables when the library cannot be read. | Serve the hero admin from the library too: needs the variants (and cut sets) in the library tables first. | `HeroMediaUsageProvider`, `IMediaLibraryReader`, `MediaLibraryReader`, `MediaReadComparer` | | 51 | **"Controle bibliotheek" panel in the library screen** (SLS-Main for Bryan, 2026-11-17). | A small panel under the storage meter, for administrators (the whole screen is admin only), asks `GET media-library/read-check` only when its button is clicked and says in plain words (nl/en) whether the library tables match each module or lists the differences, plus the state of the three switches (copy, write-through, reading off/compare/on; the response now carries `copyOnStartup` and `dualWrite`). It changes nothing. Always shown, not only on test. | Show it only when the read switch is not off: Bryan could then not check before switching. | `LibraryCheck.tsx`, `MediaLibraryController.ReadCheck`, `IMediaLibrarySwitchState` | | 52 | **Phase 3a: the library tables hold what the hero needs to publish** (2026-11-18). | Additive migration `AddHeroPhotoColumns` of the Media module: `MediaAssets.SetHash`, `MediaAssets.Variants` (the module's own JSON), the wide/tall overrides of the photo's crop, and `MediaUses.Variants`. The copy, the write-through and the verification fill and compare them; the read check shows a difference in them. Nothing reads them to publish yet (that is 3b). | Store the variants relationally: more tables for no gain while only the hero publishes from here. | `MediaEntities`, `MediaLibraryCopier`, `HeroMediaMigrationSource` | +| 53 | **Phase 3b maps the hero's photos onto the library table, behind a switch, with HeroImages kept as a copy** (2026-11-18). | `Media__Library__HeroPhotosInLibrary` (default off, test only via the workflow input `media_library_hero_photos`). On: the hero's photo entity is the library's `MediaAssets` rows of module `hero`; `HeroImages` is mirrored after every change so the switch can go back; the application refuses to start if the library lacks a photo or its set/variants; the foreign key slide -> `HeroImages` is dropped by a reversible Hero migration. No data is deleted in 3b; dropping `HeroImages` is step 3c. | Move the data in one go and drop the old table at once: cannot be switched back. | `HeroDbContext.ConfigureStorage`, `HeroLegacyImageMirror`, `HeroPhotoStorageGuard`, migration `DropSlideImageForeignKey` | diff --git a/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md b/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md index 97faeb0..4958fcf 100644 --- a/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md +++ b/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md @@ -1,6 +1,6 @@ # Phase 3: the library becomes the truth, the old Hero tables and the switches go -Status: plan only (2026-11-17). **Nothing in this plan is built or run.** Rules from Bryan, relayed by SLS-Main: build it only after phase 2 is verified on test; dropping the old tables only on TEST, with a back-up and rollback plan and a notice to SLS-Main first; never on production without a separate go from Bryan; no PR for the phase-2/3 branch before #22 is merged. +Status (2026-11-18): steps 3a and 3b are BUILT on `feature/media-library-phase2` (3b behind a switch that is off by default); step 3c (dropping the old table and the switches) is NOT built and NOT run. Rules from Bryan, relayed by SLS-Main: build it only after phase 2 is verified on test; dropping the old tables only on TEST, with a back-up and rollback plan and a notice to SLS-Main first; never on production without a separate go from Bryan; no PR for the phase-2/3 branch before #22 is merged. ## Where phase 2 leaves us @@ -41,3 +41,22 @@ Public addresses (`/media/hero//...`), ids and hashes stay as they are - 3c is a separate release after a quiet period on test (assumption: at least a week), not part of the first rollout of phase 2. - The clean-up job of the Hero (`HeroMediaCleanup`) moves to the library in 3b, so other modules can use the same retention rules. + +## As built: 3a and 3b (2026-11-18) + +**3a, expand (Media migration `AddHeroPhotoColumns`, additive):** `MediaAssets` gets `SetHash`, `Variants` (the hero's own JSON), `WideFocusX/Y/Zoom`, `TallFocusX/Y/Zoom`; `MediaUses` gets `Variants`. The verified copy, the write-through and the read check fill and compare them (a difference in the set or variants shows in the library check). + +**3b, the switch (Hero migration `DropSlideImageForeignKey` + switch `Media__Library__HeroPhotosInLibrary`, default off):** +- The migration drops only the foreign key `FK_HeroSlides_HeroImages_ImageId` (a slide may point at a photo that is only in `MediaAssets`). It has a `Down` that puts it back. The model snapshot is deliberately unchanged. The service layer already checks that a photo exists and is not removed while a slide uses it. +- With the switch **on**, the Hero's photo entity is mapped to the library table: rows of `MediaAssets` with module `hero` (columns `DefaultFocusX/Y/Zoom`, a `Module` column set to `hero`, a query filter on it). Everything else in the hero (services, renderer, public mapping, clean-up) is unchanged and runs on those rows. The library owns that table and its migrations; the hero's context excludes it from its own. +- `HeroImages` stays a **complete copy** while the switch is on (`HeroLegacyImageMirror` after every saved change), so switching back loses nothing. +- **Start-up guard:** with the switch on, the application refuses to start (clear message in the log) unless the library holds every photo of `HeroImages` with the same hash, set and variants. So the order is fixed: copy first, library check, then the switch. +- Public addresses, ids and hashes are unchanged; the golden files are untouched. + +**Order on test (each step a deploy, SLS-Main tells Bryan):** +1. Deploy with `media_library_copy=on` (and `media_library_dual_write=on`): the migrations run and the copy fills the new columns for the existing photos. Open "Controle bibliotheek": it must say that the library matches the hero (it now also compares set and variants). +2. Deploy with `media_library_hero_photos=on`. If the guard refuses to start, the log says why; set the switch back to off. +3. Check the hero admin (upload, crop a slide, save, trash and restore) and the public site; the library check still matches. +4. Rollback at any time: `media_library_hero_photos=off` (the application reads `HeroImages` again, a complete copy). To undo the foreign-key migration too: the `Down` of `DropSlideImageForeignKey`. Last resort: the restore point of the test sites. + +**Step 3c (not built):** drop `HeroImages` and the legacy mirror, remove the three media switches and the copier, make the library table the only home. Needs: Bryan's verification of 3b on test, a notice to SLS-Main with the exact migration, a fresh restore point; test only. diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs new file mode 100644 index 0000000..ba7a6c3 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs @@ -0,0 +1,185 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.Extensions.Logging.Abstractions; +using MySql.Data.MySqlClient; +using SlpModularCms.Core.Hosting; +using SlpModularCms.Modules.Hero.Data; +using SlpModularCms.Modules.Hero.Data.Entities; +using SlpModularCms.Modules.Hero.Services; +using SlpModularCms.Modules.Media.Data; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// +/// Phase 3b against a real MariaDB in a database of its own: both modules' migrations, then the hero in library storage mode +/// really writes and reads rows of MediaAssets, a slide can point at a photo that is only there (the foreign key to HeroImages +/// is gone), HeroImages is kept a copy, and the foreign key comes back with the migration's Down. Runs when +/// ConnectionStrings__DefaultConnection is set (CI sets it); otherwise it does nothing. +/// +public class HeroPhotosInLibraryMariaDbTests +{ + private static string? ConnectionString() + { + var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection"); + return string.IsNullOrWhiteSpace(baseString) + ? null + : new MySqlConnectionStringBuilder(baseString) { Database = "hero_phase3_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString; + } + + private static HeroDbContext OpenHero(string connectionString, bool inLibrary) + => new( + new DbContextOptionsBuilder() + .UseMySQL(connectionString) + .ReplaceService() + .ReplaceService() + .ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)) + .Options, + new HeroStorage(inLibrary)); + + private static MediaDbContext OpenMedia(string connectionString) + => new(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().Options); + + private static HeroImage Photo(string sha, string set = "0123456789abcdef") + => new() + { + Id = Guid.NewGuid(), + OriginalSha256 = sha, + OriginalKey = "originals/" + sha[..8] + ".jpg", + SourceFormat = "jpg", + Width = 3000, + Height = 2000, + Bytes = 2_000_000, + FocusX = 0.4, + FocusY = 0.6, + Zoom = 1.5, + WideFocusX = 0.1, + SetHash = set, + Variants = [new HeroVariant("wide", 480, 270, "image/webp", "wide-480.webp", 1234)], + CreatedAt = new DateTimeOffset(2026, 11, 17, 12, 0, 0, TimeSpan.Zero), + CreatedByUserId = Guid.NewGuid(), + OriginalFileName = "zomer.jpg", + }; + + [Fact] + public async Task The_hero_in_library_mode_writes_and_reads_rows_of_MediaAssets_and_keeps_HeroImages_a_copy() + { + var connection = ConnectionString(); + if (connection is null) + { + return; + } + + try + { + await using (var hero = OpenHero(connection, inLibrary: false)) + { + await hero.Database.MigrateAsync(); + } + + await using (var media = OpenMedia(connection)) + { + await media.Database.MigrateAsync(); + } + + await using var db = OpenHero(connection, inLibrary: true); + var photo = Photo(new string('b', 64)); + db.HeroImages.Add(photo); + var content = new HeroContent { Id = Guid.NewGuid(), Title = "Titel", UpdatedAt = DateTimeOffset.UtcNow, LastModifiedByUserId = Guid.NewGuid() }; + content.Slides.Add(new HeroSlide { Id = Guid.NewGuid(), HeroId = content.Id, ImageId = photo.Id, Alt = "een", SortOrder = 0 }); + db.Heroes.Add(content); + await db.SaveChangesAsync(); + + // The row is in the library's table, as module hero, with the hero's focus in the library's column names. + await using (var check = OpenMedia(connection)) + { + var row = await check.MediaAssets.AsNoTracking().SingleAsync(a => a.Id == photo.Id); + (row.Module, row.DefaultFocusX, row.DefaultFocusY, row.DefaultZoom, row.WideFocusX, row.SetHash, row.OriginalFileName).Should().Be(("hero", 0.4, 0.6, 1.5, 0.1, "0123456789abcdef", "zomer.jpg")); + row.Variants.Should().Contain("wide-480.webp"); + } + + // The slide points at a photo that is only in MediaAssets: the foreign key to HeroImages is gone, and it reads back. + (await db.HeroImages.CountAsync()).Should().Be(1); + var hero2 = await new Repositories.HeroRepository(OpenHero(connection, inLibrary: true)).GetReadOnlyAsync(HeroLanguages.Default, CancellationToken.None); + hero2!.Slides.Single().Image!.Id.Should().Be(photo.Id); + + // A change goes to the library row; HeroImages is brought up to date by the mirror and nothing else wrote to it. + photo.FocusX = 0.9; + await db.SaveChangesAsync(); + await new HeroLegacyImageMirror(db, NullLogger.Instance).SyncAsync(CancellationToken.None); + await using (var legacy = OpenHero(connection, inLibrary: true)) + { + var copy = await legacy.LegacyHeroImages.AsNoTracking().SingleAsync(); + (copy.Id, copy.FocusX, copy.SetHash).Should().Be((photo.Id, 0.9, "0123456789abcdef")); + copy.Variants.Should().ContainSingle(); + } + + await HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None); + + // The same data, read the old way (the switch off), is complete because of the mirror: switching back loses nothing. + await using var off = OpenHero(connection, inLibrary: false); + var old = await off.HeroImages.AsNoTracking().SingleAsync(); + (old.Id, old.FocusX, old.OriginalFileName).Should().Be((photo.Id, 0.9, "zomer.jpg")); + } + finally + { + await DropAsync(connection); + } + } + + [Fact] + public async Task The_foreign_key_migration_can_be_undone() + { + var connection = ConnectionString(); + if (connection is null) + { + return; + } + + try + { + await using var db = OpenHero(connection, inLibrary: false); + await db.Database.MigrateAsync(); + (await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().Be(0); + + var migrator = db.GetService(); + await migrator.MigrateAsync("20261117071410_RemoveHeroScheduleAndSwitch"); + (await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().NotBe(0); + } + finally + { + await DropAsync(connection); + } + } + + private static async Task Count(HeroDbContext db, string constraint) + { + await using var command = db.Database.GetDbConnection().CreateCommand(); + if (command.Connection!.State != System.Data.ConnectionState.Open) + { + await command.Connection.OpenAsync(); + } + + command.CommandText = $"SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = '{constraint}'"; + return Convert.ToInt32(await command.ExecuteScalarAsync()); + } + + private static async Task DropAsync(string? connectionString) + { + if (connectionString is null) + { + return; + } + + var builder = new MySqlConnectionStringBuilder(connectionString); + var database = builder.Database; + builder.Database = string.Empty; + await using var connection = new MySqlConnection(builder.ConnectionString); + await connection.OpenAsync(); + await using var command = connection.CreateCommand(); + command.CommandText = $"DROP DATABASE IF EXISTS `{database}`"; + await command.ExecuteNonQueryAsync(); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs new file mode 100644 index 0000000..b35043f --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs @@ -0,0 +1,111 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using SlpModularCms.Modules.Hero.Data; +using SlpModularCms.Modules.Hero.Data.Entities; +using SlpModularCms.Modules.Hero.Models; +using SlpModularCms.Modules.Hero.Services; +using Xunit; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// Phase 3b: the hero's photos live in the library's table (rows of module "hero"), and HeroImages stays a complete copy. +public class HeroPhotosInLibraryTests +{ + private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides); + + [Fact] + public void The_model_maps_the_photos_to_the_library_table_only_when_the_switch_is_on() + { + using var off = new HeroTestRig(); + using var on = new HeroTestRig(photosInLibrary: true); + + off.Db.Model.FindEntityType(typeof(HeroImage))!.GetTableName().Should().Be("HeroImages"); + off.Db.Model.FindEntityType(typeof(LegacyHeroImage)).Should().BeNull(); + + var photo = on.Db.Model.FindEntityType(typeof(HeroImage))!; + photo.GetTableName().Should().Be("MediaAssets"); + photo.FindProperty(nameof(HeroImage.FocusX))!.GetColumnName().Should().Be("DefaultFocusX"); + photo.FindProperty(nameof(HeroImage.Zoom))!.GetColumnName().Should().Be("DefaultZoom"); + photo.FindProperty(HeroDbContext.ModuleColumn).Should().NotBeNull(); + on.Db.Model.FindEntityType(typeof(LegacyHeroImage))!.GetTableName().Should().Be("HeroImages"); + } + + [Fact] + public async Task Everything_the_hero_does_with_a_photo_works_and_HeroImages_stays_a_complete_copy() + { + using var rig = new HeroTestRig(photosInLibrary: true); + var image = await rig.Upload(HeroTestRig.Photo()); + var id = Guid.Parse(image.Id); + var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, id, "een"), new HeroSlideRequest(null, id, "twee")), HeroTestRig.Admin, CancellationToken.None); + await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.3, 0.4, 1.4), new FocusDto(0.2, 0.2, 1.1), null), HeroTestRig.Admin, CancellationToken.None); + await rig.Images.SetSlideFocusAsync(Guid.Parse(saved.Slides[1].Id), new HeroFocusRequest(new FocusDto(0.9, 0.9, 1.2)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + var other = await rig.Upload(HeroTestRig.Photo(seed: 5)); + await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(other.Id), HeroTestRig.Admin, CancellationToken.None); + + var library = await rig.Db.HeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync(); + var legacy = await rig.Db.LegacyHeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync(); + + library.Should().HaveCount(2); + legacy.Should().HaveCount(2); + library.Select(i => i.Id).Should().Equal(legacy.Select(i => i.Id)); + foreach (var (l, c) in library.Zip(legacy)) + { + (l.OriginalSha256, l.SetHash, l.FocusX, l.WideFocusX, l.DeletedAt, l.OriginalFileName).Should().Be((c.OriginalSha256, c.SetHash, c.FocusX, c.WideFocusX, c.DeletedAt, c.OriginalFileName)); + l.Variants.Should().Equal(c.Variants); + } + + library.Single(i => i.Id == id).Variants.Should().NotBeEmpty(); + var publicHero = (await rig.Public.GetActiveAsync(CancellationToken.None))!; + publicHero.Slides.Should().HaveCount(2); + publicHero.Slides[0].Image.Focus.Should().Be(new FocusDto(0.3, 0.4, 1.4)); + } + + [Fact] + public async Task The_clean_up_removes_a_trashed_photo_from_both_tables() + { + using var rig = new HeroTestRig(photosInLibrary: true); + var image = await rig.Upload(HeroTestRig.Photo()); + await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None); + rig.Time.Now = rig.Time.Now.AddDays(60); + + await rig.Cleanup.RunOnceAsync(CancellationToken.None); + + (await rig.Db.HeroImages.CountAsync()).Should().Be(0); + (await rig.Db.LegacyHeroImages.CountAsync()).Should().Be(0); + } + + [Fact] + public async Task The_start_up_check_passes_for_a_complete_copy_and_refuses_when_the_library_misses_a_photo_or_its_variants() + { + using var rig = new HeroTestRig(photosInLibrary: true); + var image = await rig.Upload(HeroTestRig.Photo()); + await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None); + + // A photo that exists in HeroImages only (the copy was not run). + rig.Db.LegacyHeroImages.Add(new LegacyHeroImage { Id = Guid.NewGuid(), OriginalSha256 = new string('a', 64), OriginalKey = "originals/a.jpg", SourceFormat = "jpg", SetHash = "0123456789abcdef", CreatedAt = DateTimeOffset.UtcNow }); + await rig.Db.SaveChangesAsync(); + var missing = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None); + (await missing.Should().ThrowAsync()).Which.Message.Should().Contain("is not in the library").And.Contain("copy"); + + // And a photo whose variants differ (a copy made before the variants were stored). + rig.Db.LegacyHeroImages.RemoveRange(rig.Db.LegacyHeroImages.Where(i => i.OriginalSha256 == new string('a', 64))); + var photo = await rig.Db.HeroImages.SingleAsync(i => i.Id == Guid.Parse(image.Id)); + photo.Variants = []; + await rig.Db.SaveChangesAsync(); + var differs = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None); + (await differs.Should().ThrowAsync()).Which.Message.Should().Contain("differs from the library"); + } + + [Fact] + public async Task With_the_switch_off_nothing_is_mirrored_and_the_check_does_nothing() + { + using var rig = new HeroTestRig(); + await rig.Upload(HeroTestRig.Photo()); + + await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None); + await rig.LegacyMirror.SyncAsync(CancellationToken.None); + + (await rig.Db.HeroImages.CountAsync()).Should().Be(1); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs index 14e1898..6f25f18 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs @@ -39,12 +39,15 @@ public sealed class HeroTestRig : IDisposable public HeroMediaCleanup Cleanup { get; } public HeroOptions HeroOptions { get; } = new(); - public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null) + public IHeroLegacyImageMirror LegacyMirror { get; } + + public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, bool photosInLibrary = false) { HeroOptions.MaxPublishedSlides = maxPublishedSlides; Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 }; var mediaOptions = Options.Create(Media); - Db = new HeroDbContext(new DbContextOptionsBuilder().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options); + Db = new HeroDbContext(new DbContextOptionsBuilder().UseInMemoryDatabase("hero-" + Guid.NewGuid()).ReplaceService().Options, new HeroStorage(photosInLibrary)); + LegacyMirror = new HeroLegacyImageMirror(Db, NullLogger.Instance); Repository = new HeroRepository(Db); Store = new FileSystemMediaStore(mediaOptions); Processor = new MagickImageProcessor(mediaOptions, NullLogger.Instance); @@ -52,10 +55,10 @@ public sealed class HeroTestRig : IDisposable Status.SetDirectory(true); var gate = new MediaProcessingGate(mediaOptions); var guard = new MediaUploadGuard(Processor, gate, Status, mediaOptions); - Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger.Instance, Time, IndexCache, Signal, librarySync); - Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger.Instance, Time, IndexCache, null, librarySync); + Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger.Instance, Time, IndexCache, Signal, librarySync, LegacyMirror); + Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger.Instance, Time, IndexCache, null, librarySync, LegacyMirror); Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time); - Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger.Instance, Time, librarySync); + Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger.Instance, Time, librarySync, LegacyMirror); } /// A photo-like fixture: the left half red, the right half blue, with a camera tag. diff --git a/src/SlpModularCms.Modules.Hero.Tests/SlpModularCms.Modules.Hero.Tests.csproj b/src/SlpModularCms.Modules.Hero.Tests/SlpModularCms.Modules.Hero.Tests.csproj index 3d3e2ea..5b5426f 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/SlpModularCms.Modules.Hero.Tests.csproj +++ b/src/SlpModularCms.Modules.Hero.Tests/SlpModularCms.Modules.Hero.Tests.csproj @@ -33,6 +33,7 @@ + diff --git a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs index 5eb885e..f5520c3 100644 --- a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs +++ b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs @@ -112,6 +112,37 @@ public class HeroImage }; } +/// +/// The same photo as , always stored in HeroImages. Only used while the photos live in the +/// library (phase 3b): then HeroImages is kept up to date from the library rows so the switch can be turned back. +/// +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 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; } +} + /// One file of the current set: the ratio, its pixel size, the type and the file name inside the set. public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes); diff --git a/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs b/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs index 6942f69..60f6c7c 100644 --- a/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs +++ b/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs @@ -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 options) : DbContext(options) +public class HeroDbContext(DbContextOptions options, HeroStorage? storage = null) : DbContext(options) { + /// True while the photos live in the library's table (phase 3b); see . + public bool PhotosInLibrary { get; } = storage?.PhotosInLibrary ?? false; + + /// The photos as they are always kept in HeroImages (only mapped while is on). + public DbSet LegacyHeroImages => Set(); + public DbSet Heroes => Set(); public DbSet HeroSlides => Set(); public DbSet HeroImages => Set(); @@ -86,6 +93,8 @@ public class HeroDbContext(DbContextOptions options) : DbContext( v => v.ToList())); }); + ConfigureStorage(modelBuilder); + modelBuilder.Entity(entity => { entity.ToTable("HeroRetiredFiles"); @@ -95,4 +104,65 @@ public class HeroDbContext(DbContextOptions options) : DbContext( entity.Property(e => e.RetiredAt).IsRequired(); }); } + + private void ConfigureStorage(ModelBuilder modelBuilder) + { + if (!PhotosInLibrary) + { + modelBuilder.Ignore(); + return; + } + + // The hero's photos are rows of the library's table, module "hero". The library owns that table and its migrations. + modelBuilder.Entity(entity => + { + entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations()); + entity.Property(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(e, ModuleColumn) == HeroMapping.Area); + }); + + // And HeroImages stays a complete copy, so switching back loses nothing. + modelBuilder.Entity(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>(json, (JsonSerializerOptions?)null) ?? new List()) + .HasColumnType("longtext"); + }); + } + + public const string ModuleColumn = "Module"; + + public override int SaveChanges(bool acceptAllChangesOnSuccess) + { + MarkNewPhotosAsHero(); + return base.SaveChanges(acceptAllChangesOnSuccess); + } + + public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) + { + MarkNewPhotosAsHero(); + return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + } + + /// A photo the hero adds to the library's table belongs to the hero module. + private void MarkNewPhotosAsHero() + { + if (!PhotosInLibrary) + { + return; + } + + foreach (var entry in ChangeTracker.Entries().Where(e => e.State == EntityState.Added)) + { + entry.Property(ModuleColumn).CurrentValue = HeroMapping.Area; + } + } } diff --git a/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs b/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs new file mode 100644 index 0000000..6b0b7c8 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs @@ -0,0 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace SlpModularCms.Modules.Hero.Data; + +/// +/// Where the hero's photos are stored (media library phase 3b). Off (default): the hero's own table HeroImages. On +/// (Media__Library__HeroPhotosInLibrary=true): the library's MediaAssets table (rows of module hero), with +/// HeroImages kept up to date as a safety net so the switch can go back. The choice is made when the application starts. +/// +public sealed record HeroStorage(bool PhotosInLibrary); + +/// EF builds the model once per key: the two storage modes have different models. +public sealed class HeroModelCacheKeyFactory : IModelCacheKeyFactory +{ + public object Create(DbContext context, bool designTime) + => (context.GetType(), context is HeroDbContext { PhotosInLibrary: true }, designTime); +} diff --git a/src/SlpModularCms.Modules.Hero/HeroModule.cs b/src/SlpModularCms.Modules.Hero/HeroModule.cs index 33609a3..79e0025 100644 --- a/src/SlpModularCms.Modules.Hero/HeroModule.cs +++ b/src/SlpModularCms.Modules.Hero/HeroModule.cs @@ -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().GetValue("Media:Library:HeroPhotosInLibrary"))); services.AddDbContext((serviceProvider, options) => { var config = serviceProvider.GetRequiredService(); options.UseMySQL(config.GetConnectionString("DefaultConnection")!); options.ReplaceService(); + options.ReplaceService(); + if (serviceProvider.GetRequiredService().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(); services.AddCmsTextsFrom(typeof(HeroModule).Assembly); services.AddOptions().Configure((options, configuration) => @@ -63,5 +74,6 @@ public class HeroModule : IModule using var scope = app.ApplicationServices.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); db.Database.Migrate(); + HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None).GetAwaiter().GetResult(); } } diff --git a/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.Designer.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.Designer.cs new file mode 100644 index 0000000..731ca51 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.Designer.cs @@ -0,0 +1,291 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Button1Label") + .HasMaxLength(100) + .HasColumnType("varchar(100)"); + + b.Property("Button1Target") + .HasMaxLength(500) + .HasColumnType("varchar(500)"); + + b.Property("Button2Label") + .HasMaxLength(100) + .HasColumnType("varchar(100)"); + + b.Property("Button2Target") + .HasMaxLength(500) + .HasColumnType("varchar(500)"); + + b.Property("Display") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("Language") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("LastModifiedByUserId") + .HasColumnType("char(36)"); + + b.Property("Lead") + .HasMaxLength(1000) + .HasColumnType("varchar(1000)"); + + b.Property("Tagline") + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("varchar(1000)"); + + b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Bytes") + .HasColumnType("bigint"); + + b.Property("CreatedAt") + .HasColumnType("datetime"); + + b.Property("CreatedByUserId") + .HasColumnType("char(36)"); + + b.Property("DeletedAt") + .HasColumnType("datetime"); + + b.Property("FocusX") + .HasColumnType("double"); + + b.Property("FocusY") + .HasColumnType("double"); + + b.Property("Height") + .HasColumnType("int"); + + b.Property("LastUsedAt") + .HasColumnType("datetime"); + + 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("SetHash") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.Property("SourceFormat") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("TallFocusX") + .HasColumnType("double"); + + b.Property("TallFocusY") + .HasColumnType("double"); + + b.Property("TallZoom") + .HasColumnType("double"); + + b.Property("Variants") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + + b.Property("Width") + .HasColumnType("int"); + + b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("RetiredAt") + .HasColumnType("datetime"); + + b.HasKey("Id"); + + b.ToTable("HeroRetiredFiles", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Alt") + .IsRequired() + .HasMaxLength(600) + .HasColumnType("varchar(600)"); + + b.Property("Decorative") + .HasColumnType("tinyint(1)"); + + b.Property("FocusX") + .HasColumnType("double"); + + b.Property("FocusY") + .HasColumnType("double"); + + b.Property("HeroId") + .HasColumnType("char(36)"); + + b.Property("ImageId") + .HasColumnType("char(36)"); + + b.Property("IsActive") + .HasColumnType("tinyint(1)"); + + b.Property("SetHash") + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.Property("SortOrder") + .HasColumnType("int"); + + b.Property("TallFocusX") + .HasColumnType("double"); + + b.Property("TallFocusY") + .HasColumnType("double"); + + b.Property("TallZoom") + .HasColumnType("double"); + + b.Property("Variants") + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + + b.Property("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 + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.cs new file mode 100644 index 0000000..df45151 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117230241_DropSlideImageForeignKey.cs @@ -0,0 +1,33 @@ +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SlpModularCms.Modules.Hero.Migrations +{ + /// + public partial class DropSlideImageForeignKey : Migration + { + /// + 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"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddForeignKey( + name: "FK_HeroSlides_HeroImages_ImageId", + table: "HeroSlides", + column: "ImageId", + principalTable: "HeroImages", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs index fa145e4..4eed113 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs @@ -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 { /// Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws. - 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; diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs b/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs new file mode 100644 index 0000000..9d66dc9 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs @@ -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; + +/// 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."); + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs index 1a9feb0..0ddde60 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs @@ -19,7 +19,8 @@ public class HeroMediaCleanup( IOptions options, ILogger 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); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs index c8a4ec0..5245edf 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs @@ -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);