diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index 19eac84..dc34038 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -20,6 +20,21 @@ on: type: choice options: [unchanged, 'on', 'off'] default: unchanged + media_library_dual_write: + description: 'Media library write-through on the TEST deploys (Media__Library__DualWrite in shared/env): unchanged, on or off' + type: choice + options: [unchanged, 'on', 'off'] + default: unchanged + media_library_read: + description: 'Media library read switch on the TEST deploys (Media__Library__ReadFromLibrary in shared/env): unchanged, off, compare or 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: @@ -561,6 +576,9 @@ jobs: health_check_url: ${{ vars.HEALTH_CHECK_URL_TEST }} run_db_backup: false 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 @@ -689,6 +707,9 @@ jobs: health_check_url: ${{ vars.HEALTH_CHECK_URL_ASF_TEST }} run_db_backup: false 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 9b5c873..4207513 100644 --- a/.gitea/workflows/deploy-scp.yaml +++ b/.gitea/workflows/deploy-scp.yaml @@ -42,6 +42,21 @@ on: required: false type: string default: unchanged + media_library_dual_write: + description: 'unchanged | on | off: sets Media__Library__DualWrite (the write-through of phase 2) in shared/env before the restart. TEST only, never production.' + required: false + type: string + default: unchanged + media_library_read: + description: 'unchanged | off | compare | on: sets Media__Library__ReadFromLibrary (the read switch of phase 2) in shared/env before the restart. TEST only, never production.' + 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: @@ -247,6 +262,88 @@ jobs: fi echo "Media__Library__CopyOnStartup is now: $(rssh "grep '^Media__Library__CopyOnStartup=' $ENV_FILE || echo '(not set = off)'")" + # The write-through switch of phase 2 (same rules as the copy switch above: only this one key of shared/env, only on test, + # only on, off or unchanged). `on` makes every change of the hero also land in the library's tables. + - name: Set the media library write-through switch (test only) + if: inputs.media_library_dual_write != '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_dual_write }}" + if [ "${{ inputs.environment }}" != "test" ]; then + echo "The media library write-through switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 + exit 1 + fi + if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then + echo "media_library_dual_write 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__DualWrite=/d' $ENV_FILE && echo 'Media__Library__DualWrite=true' >> $ENV_FILE" + else + rssh "touch $ENV_FILE && sed -i '/^Media__Library__DualWrite=/d' $ENV_FILE" + fi + echo "Media__Library__DualWrite is now: $(rssh "grep '^Media__Library__DualWrite=' $ENV_FILE || echo '(not set = off)'")" + + # The read switch of phase 2 (same rules as the switches above): compare = the library tables are read too and differences are + # logged and shown by GET media-library/read-check; on = the library screen reads the library tables (needs the write-through). + - name: Set the media library read switch (test only) + if: inputs.media_library_read != '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_read }}" + if [ "${{ inputs.environment }}" != "test" ]; then + echo "The media library read switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 + exit 1 + fi + if [ "$MODE" != "on" ] && [ "$MODE" != "off" ] && [ "$MODE" != "compare" ]; then + echo "media_library_read must be unchanged, off, compare or on (got: $MODE)." >&2 + exit 1 + fi + ENV_FILE="${{ inputs.deploy_path }}/shared/env" + if [ "$MODE" = "off" ]; then + rssh "touch $ENV_FILE && sed -i '/^Media__Library__ReadFromLibrary=/d' $ENV_FILE" + else + rssh "touch $ENV_FILE && sed -i '/^Media__Library__ReadFromLibrary=/d' $ENV_FILE && echo 'Media__Library__ReadFromLibrary=$MODE' >> $ENV_FILE" + 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 511d9b2..2ce64dd 100644 --- a/aidlc-docs/features/media-library/assumptions-and-open-questions.md +++ b/aidlc-docs/features/media-library/assumptions-and-open-questions.md @@ -74,4 +74,11 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu | 43 | **"Slide toevoegen" replaces the per-slide library button** (Bryan, 2026-11-17, after seeing it on test; replaces assumption 36's per-slide button). | The per-slide "Kies uit bibliotheek" is gone. The existing add button is "Slide toevoegen" and opens the shared picker: choose a library photo or upload one on the spot (it also lands in the library, ML-17); one photo makes one NEW slide. A slide's photo cannot be changed or replaced: remove the slide and add another. Dropping files on the section still adds one slide per file (same upload as before). The crop per slide stays a goal (phase 2). | Keep a per-slide change button: Bryan said no. | `HeroPage.addSlideWithPhoto`, `LibraryPickerDialog` | | 44 | **Tiles or list in the library** (Bryan, 2026-11-17). | The library and the trash show the photos as tiles (grid of thumbnails, selectable, same usage and links) by default, with a switch Tiles / List next to the filters. The choice is remembered in the browser (`localStorage`, guarded; it falls back to tiles). The clean-up dialog keeps its list. The picker already is a grid. | Remember the choice on the server per user: needs a user-settings store. | `PhotoSelectionList` (`layout`), `lib/layoutPreference.ts`, `MediaLibraryPage` | | 45 | **Media screen details** (Bryan, 2026-11-17): bulk bar keeps its height; the whole tile selects; smaller tiles; 0 days in the clean-up; no time-basis choice. | The bar is an invisible placeholder of the same height (h-9) while nothing is selected. A click on a tile selects it (shift for a range); links, buttons and the box itself do their own thing. One more tile per row at every width. The clean-up slider goes from 0: 0 days means every photo that is not in use right now qualifies at the next clean-up (preview and confirmation unchanged, with a note at 0). The period always counts from the last use (assumption 29); there was no separate checkbox for the time basis in the clean-up (the only checkbox is "Alleen ongebruikte foto's", a filter, which stays because it protects photos in use), so nothing was removed. | If Bryan meant "Alleen ongebruikte foto's": removing it would put photos in use in the clean-up (counted from their upload date). | `BulkActionBar`, `PhotoSelectionList`, `CleanupDialog`, `CleanupSelection.MinDays` | -| 46 | **Compact tiles in the clean-up and the delete confirmation** (product owner request, 2026-11-17). | `PhotoSelectionList` has a third layout `compact` (3/4/6/8 columns, square small thumbnails, name and a short usage line on one truncated line each, full name, date, size and usage in the tooltip; checkbox chip, whole-tile select, select all and the bulk bar unchanged). The clean-up dialog uses it. The "places that lose the photo" list in the second step of `DeleteDialog` is a read-only compact grid of the same kind. `PurgeDialog` shows no photo list (only a count), so it is unchanged; the library tiles and the ImagePicker are unchanged. | Long row lists again in the dialogs (much scrolling, little overview). | `PhotoSelectionList.tsx` (`layout="compact"`), `CleanupDialog.tsx`, `DeleteDialog.tsx` (`delete-places`) | +| 46 | **Phase 2 is built in three switchable steps; the Hero tables stay the truth until phase 3** (2026-11-17, design on `feature/media-library-phase2`). | A write-through to the library tables (`Media:Library:DualWrite`), a nullable crop and set per slide (additive; null = today's behaviour and addresses), and a read switch with a compare mode (`Media:Library:ReadFromLibrary`). All switches off by default, test only, rollback by switching off. Details and back-up/rollback plan: `inception/application-design/phase2-design.md`. | Replace the Hero tables in one go: cannot be rolled back by a switch. | `phase2-design.md` | +| 47 | **Crop per slide (step B), built 2026-11-17.** | A slide can have its own crop (nullable columns on `HeroSlides`, migration `AddSlideCrop`, additive); null = the slide uses its photo's crop and set, so no existing address changes. The crop is edited per slide (`PUT/DELETE hero/slides/{id}/focus`, applied at once); a crop equal to the photo's shares the photo's set; equal crops share one set. A slide that is not saved yet cannot be cropped (the button says so): its crop would have no slide to belong to. The "crop is shared" hint of assumption 36 is gone. The photo's own crop stays the default and is no longer edited from the screen (the endpoint remains). | Let an unsaved slide carry its crop in the save request: more code on both sides. | `HeroSlide`, `HeroMapping.Effective`, `HeroImageService.RenderSlideAsync`, `HeroPage` | +| 48 | **Step A (write-through) mirrors the whole module after each change** (2026-11-17). | Simplest consistent form: the same verified copier runs for the `hero` module after every saved change, one at a time, only with `Media:Library:DualWrite` on (default off); a failure never fails the Hero request. At this size (a few dozen photos) a full mirror is cheap and cannot drift from the copy. | Per-change incremental writes: more code, more ways to drift. | `IMediaLibrarySync`, `MediaLibrarySync`, `HeroImageService.SyncLibraryAsync` | +| 49 | **Compact tiles in the clean-up and the delete confirmation** (product owner request, 2026-11-17). | `PhotoSelectionList` has a third layout `compact` (3/4/6/8 columns, square small thumbnails, name and a short usage line on one truncated line each, full name, date, size and usage in the tooltip; checkbox chip, whole-tile select, select all and the bulk bar unchanged). The clean-up dialog uses it. The "places that lose the photo" list in the second step of `DeleteDialog` is a read-only compact grid of the same kind. `PurgeDialog` shows no photo list (only a count), so it is unchanged; the library tiles and the ImagePicker are unchanged. | Long row lists again in the dialogs (much scrolling, little overview). | `PhotoSelectionList.tsx` (`layout="compact"`), `CleanupDialog.tsx`, `DeleteDialog.tsx` (`delete-places`) | +| 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/phase2-design.md b/aidlc-docs/features/media-library/inception/application-design/phase2-design.md new file mode 100644 index 0000000..04de739 --- /dev/null +++ b/aidlc-docs/features/media-library/inception/application-design/phase2-design.md @@ -0,0 +1,110 @@ +# Phase 2: the Hero works through the library, and a crop per use + +Status (2026-11-17): steps B (crop per slide), A (write-through) and C (read switch) are built on this branch, switches off by default; phase 3 is not built. Branch `feature/media-library-phase2` (no PR until #22 is merged). Test only; every switch is OFF by default; SLS-Main is told before every test deploy; phase 3 (dropping the old tables) only after phase 2 is verified on test and never on production without a separate go from Bryan. + +## What Bryan wants + +1. A photo on two slides can have **its own crop per slide** (per-use-crop.md). +2. The Hero **reads and writes through the library** instead of owning the photo data (migration-plan.md, phase 2). +3. The public addresses (`/media/hero//-.`), ids and hashes do not change for anything that exists today; the golden files stay as they are. + +## The idea in one paragraph + +Until phase 3 the Hero's own tables (`HeroImages`, `HeroSlides`) stay the **source of truth** and the safety net. Phase 2 adds, in three independent steps, (A) a **write-through** so every change the Hero makes to a photo or a slide lands in the library tables too, (B) a **crop per slide** with its own set, and (C) a **read switch** so the Hero and the library screen can read the photos from the library tables. Each step has its own switch, can be rolled back by switching it off, and none of them removes or rewrites data of the old tables. Phase 3 is then only: flip the authority and drop the old columns/tables. + +```mermaid +flowchart LR + subgraph Hero module + HS[HeroService / HeroImageService] + HT[(HeroImages, HeroSlides)] + end + subgraph Media module + W[IMediaLibraryWriter] + MT[(MediaAssets, MediaUses)] + end + HS -->|always| HT + HS -->|"A: DualWrite on"| W --> MT + MT -->|"C: ReadFromLibrary on"| HS + classDef hero fill:#9ae6b4,stroke:#2f855a,color:#000; + classDef media fill:#63b3ed,stroke:#2b6cb0,color:#000; + class HS,HT hero; + class W,MT media; +``` + +Text alternative: the Hero always writes its own tables; with step A on it also writes the library tables; with step C on it reads photos back from the library tables. + +## Step A: write-through (`Media:Library:DualWrite`, default off) + +- New Core interface `IMediaLibraryWriter` (Upsert asset, Upsert/replace the uses of a module, Trash, Restore, Purge). Implemented in the Media module on its own `MediaDbContext`; the Hero module depends only on the Core interface (as it does for `IMediaUsageProvider`). With the switch off or no implementation registered it is a no-op. +- The Hero calls it after each successful change: upload (new or restored photo), focus change, trash, restore, purge, saving the hero (the slides become the photo's uses, with position, active flag and set hash), the renderer finishing a set (new set hash). +- Everything a Hero change writes is also exactly what the verified copy writes: the copier is reused as the **backfill and the check** (`CopyOnStartup` stays; running it after dual write was on must report the same counts). A write to the library that fails is logged and **never fails the Hero request** (the Hero tables are the truth); the next copy repairs the difference. +- Tests: in-memory and MariaDB; after each Hero operation the library tables equal what the copier would produce. + +## Step B: a crop per slide (always on once built; additive and nullable) + +Data (additive migration of the Hero, all nullable, no existing column changes): + +| Table | New columns | Meaning | +|---|---|---| +| `HeroSlides` | `FocusX/Y/Zoom`, `WideFocus*`, `TallFocus*` (all nullable) | The slide's own crop. **Null = no override: the slide uses its photo's crop and set exactly as today.** | +| `HeroSlides` | `SetHash` (nullable), `Variants` (JSON, nullable) | The slide's own set, only when it has an override and the set is complete. | + +Behaviour: + +- **Set per (photo, crop).** The set hash uses the existing formula `MediaPaths.SetHash(originalSha256, crops)`, so a slide with an override whose crop equals the photo's crop gets the **same hash and shares the photo's files**; two slides with the same crop share one set. A slide without override keeps the photo's set and hash: **no existing address changes.** +- **Renderer.** `RenderNextPendingAsync` also looks at slides with an override whose `SetHash` differs from the hash of their crop (or have no variants), and cuts that set the same way it cuts a photo's set (same progress, retries and "old set stays live until the new one is complete"). A set that nothing references any more goes to `HeroRetiredFiles` like today; a set shared by photo and slide is only retired when neither uses it (`HeroRepository` already has the shared-files check, extended to slides). +- **Public mapping.** `HeroMapping` picks, per slide, the slide's own set when it has a complete one, else the photo's set. Slides without override map byte-for-byte as before (golden files unchanged). +- **Admin.** The focus dialog is opened **per slide** and edits that slide's crop (`PUT hero/slides/{slideId}/focus`, applies at once, "Wordt verwerkt…" progress like now). "Reset to the photo's crop" clears the override. The "crop is shared" hint disappears; the photo's own crop stays as the default for new slides. The existing `PUT hero/images/{id}/focus` keeps working (sets the photo's default). +- **Library.** Step A mirrors the slide's crop and set into `MediaUses.Crops` and `MediaUses.SetHash`. +- Tests: two slides, one photo, different crops give two sets and two addresses; same crop gives one set; no override gives today's hash; the old set is live until the new one is complete; trash/purge with shared sets; golden files unchanged. + +## Step C: read switch (`Media:Library:ReadFromLibrary`, default off) + +- The library screen (`IMediaUsageProvider` data) and the Hero's photo lookups (`GetImage`, the admin DTOs, the trash/purge checks) read the photo rows from `MediaAssets` and `MediaUses` when the switch is on; the Hero's own tables are still written (step A) so switching off is instant and loses nothing. +- **Compare mode** first: with `ReadFromLibrary=compare` the Hero still answers from its own tables but logs a warning for every photo or slide where the library row differs. Bryan/SLS-Main look at the log on test before `on` is used. +- Tests: the same Hero tests run twice (switch off and on) and give the same answers. + +## Back-up and rollback plan (test hosts) + +Before the first deploy of each step (A, B, C) on a test host: + +1. **Restore point of the test sites (made by Bryan, 2026-11-17 21:37, confirmed by Pi-Main):** `/mnt/storage1/backups/test-herstelpunt/2026-11-17-213757/` (readable by root only) with `db-SlpSoftwareTest.sql.gz`, `db-AndreaSlopFotografieTest.sql.gz`, `media.tar.gz` (both media directories) and `SHA256SUMS`. Made with `sudo bash /home/claude/server-ops/drafts/2026-11-17-test-herstelpunt/test-herstelpunt.sh`. Restoring is step 22b in `drafts/2026-11-17-test-herstelpunt/README.md` on the Pi, done by Bryan (not tested yet). Take a new one before the next step if a lot changed. +2. **Row counts** noted: `HeroImages`, `HeroSlides`, `HeroRetiredFiles`, `MediaAssets`, `MediaUses`. +3. **Sample of public addresses** (ten `/media/hero//...` URLs and the hero markers) requested before and after; same bytes. + +| Step | What can go wrong | Rollback | +|---|---|---| +| A write-through | Library rows differ from the Hero's | Switch `DualWrite` off; run the copy once (mirror) to repair, or ignore: the Hero never read them. | +| B crop per slide | A wrong crop or set for a slide | Clear the override of the slide in the admin ("reset"), or set the slide's new columns to null (`UPDATE HeroSlides SET FocusX=NULL ...`); the photo's own set is untouched. The migration is additive; the EF `Down` drops only the new columns. | +| C read switch | The library answers differ | Switch `ReadFromLibrary` off (instant; the Hero tables are complete because of A). | +| After all steps | Anything | Restore the dump of the step (if one was taken). The media directory is never changed destructively: new sets are new directories, old sets stay until the retention time passes. | + +Switches are set through the existing deploy workflow input path (the same way as `media_library_copy`; test only) or `shared/env`, never on production. + +## Order of work and what is reported + +1. **Now:** this design and the rollback plan pushed for Bryan to read (this file). +2. **B first** (it is what Bryan sees; independent of A): backend (migration, entity, renderer, mapping, endpoint, tests) then the frontend (per-slide focus dialog), reported with a test-deploy request, no switch needed because it is additive and null = unchanged. +3. **A**, then the copy check on test. +4. **C** in compare mode, then on. +5. Phase 3 only afterwards and only on test, with its own plan and notice. + +## Assumptions made here (also in assumptions-and-open-questions.md) + +- Test data is no longer assumed to be rebuildable: there is a recorded restore point (above). The Hero tables stay authoritative until phase 3; "reads and writes through the library" is reached in steps A and C behind switches, not by replacing the tables in one go (a one-go replacement cannot be rolled back by a switch). +- The crop per slide lives in the slide (nullable) in the Hero until phase 3 and is mirrored to `MediaUses.Crops`; moving the authority to `MediaUses` is part of phase 3. + +## Step A as built (2026-11-17) + +- `IMediaLibrarySync` (Core; default `NoMediaLibrarySync`) is called by the Hero after every saved change: upload (also the same photo again), focus of a photo, focus and reset of a slide, trash, restore, purge, delete, saving the hero, the renderer finishing a set (photo or slide) and the clean-up. The Media module replaces it with `MediaLibrarySync`, which runs the verified copier for the `hero` module only, one at a time, **only when `Media__Library__DualWrite=true`**, and never throws (a failure is logged; the next change or the start-up copy repairs it). The Hero's own tables are never touched by it. +- The slide as it is shown is what lands in the library: for a slide with its own crop the use gets the slide's set hash and crops (`MediaUses.SetHash`, `MediaUses.Crops`). +- Switch through the deploy workflow like the copy: dispatch input `media_library_dual_write` (unchanged, on, off; test only). +- **What Bryan can check on test with the switch on:** make any change in the Hero or the library (upload, crop a slide, save, trash, restore); then in the database `SELECT COUNT(*) FROM MediaAssets / MediaUses` equals `HeroImages` / `HeroSlides`, and for a slide with its own crop `MediaUses.SetHash` equals `HeroSlides.SetHash`. The log has no line "The write-through of hero to the media library failed". With the switch off nothing changes at all. + +## Step C as built (2026-11-17) + +- Switch `Media__Library__ReadFromLibrary` = `off` (default) | `compare` | `on`; deploy workflow input `media_library_read` (unchanged, off, compare, on; test only). +- **What is read from the library tables:** what the library screen shows of the hero (photos, trash, where each photo is used): identity, size, name, dates and the uses with position and on/off. The thumbnail address still comes from the hero (the variants are not in the library tables until phase 3), and the hero's own admin and public endpoints keep reading their own tables: they need the variants. Writing (trash, restore, purge, crops) always goes to the hero's own tables, and with the write-through on, on to the library. +- **compare:** the answer stays the hero's own, but the library tables are read too; every difference is a warning in the log (`Media library read check (hero): ...`) when the library screen is opened, and `GET media-library/read-check` (administrators) lists them for all modules in words (`matches`, `differences`), whatever the switch says, so it can be used before `on`. +- **on:** the library tables answer; when they cannot be read the hero's own tables answer and the error is logged. `on` needs `DualWrite=on`, otherwise the library screen shows stale data. +- **What Bryan can check on test:** (1) with `DualWrite=on` and `ReadFromLibrary=compare`, open the library screen and call `/api/v1/media-library/read-check` (or look at the log): `matches: true` for the hero means the library tables are a faithful mirror; (2) then `ReadFromLibrary=on`: the library screen looks exactly the same (same photos, trash, uses, thumbnails); change something (upload, trash, restore, crop a slide) and the screen follows; (3) switch back to `off` at any moment, nothing is lost. 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 new file mode 100644 index 0000000..4958fcf --- /dev/null +++ b/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md @@ -0,0 +1,62 @@ +# Phase 3: the library becomes the truth, the old Hero tables and the switches go + +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 + +- The Hero tables (`HeroImages`, `HeroSlides`) are the truth. The library tables (`MediaAssets`, `MediaUses`) mirror them (write-through, `DualWrite`) and the library screen can read them (`ReadFromLibrary`). +- The library tables do **not** hold what the Hero needs to publish: the variants of a set (`Variants`), the set hash of the photo, and the slide crops and sets as columns. So the Hero admin and the public hero still read the Hero tables. + +## What phase 3 does + +| Step | What | Reversible? | +|---|---|---| +| 3a Expand | Add to the library tables what the Hero needs: `MediaAssets.SetHash`, `MediaAssets.Variants` (JSON), the photo's crops as columns (not only the default focus), `MediaUses.Variants` (the use's own set) and its crop columns (today only the JSON in `Crops`). Additive migration of the Media module. The write-through fills them; a verified backfill copies existing rows. | Yes: drop the new columns; nothing reads them yet. | +| 3b Switch | The Hero reads and writes its photos and slides through a library store (`IMediaLibraryStore`, Core interface, implemented by the Media module) instead of `HeroImages`/`HeroSlides`: upload, crops, renderer, trash, restore, purge, public mapping, clean-up. Behind a setting (`Media:Library:HeroUsesLibrary`, default off) with the old code path kept for one release; compare mode first (both paths run, differences are logged). | Yes: switch off; the old tables are still complete as long as the write-through to them (the reverse mirror) is on. | +| 3c Contract | A separate migration drops `HeroImages`, the photo columns of `HeroSlides` and `HeroRetiredFiles` (or moves it), and the code removes the three switches (`CopyOnStartup`, `DualWrite`, `ReadFromLibrary`), the copier and the mirror. | **Only from a back-up.** TEST only until Bryan gives a separate go. | + +Public addresses (`/media/hero//...`), ids and hashes stay as they are in every step; the golden files must not change. + +## Prerequisites (the gate before 3b and 3c) + +1. Phase 2 verified on test by Bryan: `GET media-library/read-check` says `matches: true`, the library screen with `ReadFromLibrary=on` looks the same, crops per slide work (assumptions 47-50). +2. A restore point of the test sites taken right before each step (the script of Pi-Main, `test-herstelpunt.sh`; path recorded in `phase2-design.md`), plus the row counts and a sample of ten public addresses before and after. +3. SLS-Main is told before every deploy of 3a, 3b and 3c. + +## Tests to write before building 3b (can be written now against the current code) + +- The Hero service tests run through an `IHeroPhotoStore` abstraction twice, once on the Hero tables and once on an in-memory library store, and must give the same answers (upload, same photo twice, crop per slide, trash, restore, purge, clean-up, public mapping, golden files). +- A migration test on MariaDB for 3a (additive, backfill verified, rollback with `Down`). +- A contract test for 3c on MariaDB: after the drop, every Hero endpoint still answers the same (smoke test of the public hero and the admin). + +## Rollback per step + +| Step | Rollback | +|---|---| +| 3a | `Down` of the Media migration (drops the new columns). | +| 3b | `HeroUsesLibrary=off`; the old tables are complete because the old path was kept in sync. | +| 3c | Restore the database dump of the restore point, then deploy the previous release; the media directory is never changed destructively (retired sets stay for the retention time). | + +## Open points for Bryan (assumed until he says otherwise) + +- 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/docs/hero-module.md b/docs/hero-module.md index a5fca83..068f5cc 100644 --- a/docs/hero-module.md +++ b/docs/hero-module.md @@ -26,7 +26,7 @@ De admin toont alleen een badge Live of Niet live. | Instelling | Betekenis | |---|---| | `Media__Directory` | De map voor de afbeeldingen, **buiten de release** en buiten de webroot. Zonder deze instelling (of als de map niet beschrijfbaar is) staan uploads uit; bestaande bestanden en de publieke hero blijven werken. Niet in een committed appsettings-bestand. | -| `Media__MaxUploadBytes` (10 MB), `Media__MaxPixels` (64 miljoen), `Media__MaxParallelJobs` (2), `Media__DecodeTimeoutSeconds` (20), `Media__MaxTotalBytes` (2 GB), `Media__RetentionDays` (7), `Media__TrashDays` (standaard 7: bewaartijd van foto's in de prullenbak van de mediabibliotheek), `Media__AutoCleanupUnattached` (standaard `true`; `false` bewaart ongebruikte uploads tot iemand ze verwijdert, voor de mediabibliotheek) | Grenzen en bewaartijd; standaardwaarden zijn bedoeld om te blijven. | +| `Media__MaxUploadBytes` (10 MB), `Media__MaxPixels` (64 miljoen), `Media__MaxParallelJobs` (2), `Media__DecodeTimeoutSeconds` (20), `Media__MaxTotalBytes` (2 GB), `Media__RetentionDays` (7), `Media__TrashDays` (standaard 7: bewaartijd van foto's in de prullenbak van de mediabibliotheek), `Media__AutoCleanupUnattached` (standaard `false`: ongebruikte uploads blijven bewaard tot iemand ze in de mediabibliotheek verwijdert; `true` ruimt ze na de bewaartijd automatisch op) | Grenzen en bewaartijd; standaardwaarden zijn bedoeld om te blijven. | | `Hero__TextOnlyRegion` | `true` voor een site waarvan de hero geen afbeeldingen heeft (SLP Software; staat in de committed instellingen van die site): een hero zonder slides wordt dan als tekst (titelregels, lead, knoppen) in de eerste HTML geschreven. Uit (standaard, ook voor ASF): een hero zonder slides laat de pagina ongemoeid. | | `Hero__TitlePrefix` | Merktekst die in de tekst-only-regio als eerste, visueel te verbergen deel van de h1 wordt geschreven (``), zodat crawlers en schermlezers de volledige titel lezen. SLP Software: `"SLP Software: "` (committed instelling); leeg (standaard, ook ASF) schrijft niets. De site verbergt de klasse zelf visueel. | | `Media__RenderSetTimeoutSeconds` (600), `Media__StripTimeoutSeconds` (60) | De tijd die het knippen van alle varianten van één foto in de achtergrond mag duren, en het verwijderen van metadata bij een upload. Ruim gekozen: een grote foto op een Raspberry Pi kost minuten. | diff --git a/frontend/src/features/hero/components/CropThumbnail.tsx b/frontend/src/features/hero/components/CropThumbnail.tsx index 7a3aef9..64c3566 100644 --- a/frontend/src/features/hero/components/CropThumbnail.tsx +++ b/frontend/src/features/hero/components/CropThumbnail.tsx @@ -12,6 +12,9 @@ interface CropThumbnailProps { edit?: HeroFocusRequest; onClick: () => void; testId: string; + /** The crop cannot be changed now (for example the slide is not saved yet); `hint` says why. */ + disabled?: boolean; + hint?: string; } /** @@ -20,7 +23,7 @@ interface CropThumbnailProps { * icon is always there, so it can be found on a touch screen as well. After "Apply" in the dialog the thumbnail shows * the new crop right away (the same calculation as the host) with a clear "not saved yet" note. */ -export function CropThumbnail({ image, savedSrc, edit, onClick, testId }: CropThumbnailProps) { +export function CropThumbnail({ image, savedSrc, edit, onClick, testId, disabled = false, hint }: CropThumbnailProps) { const { t } = useTranslation(); const local = edit && image.previewSrc ? localCrop(image, edit) : null; @@ -28,8 +31,10 @@ export function CropThumbnail({ image, savedSrc, edit, onClick, testId }: CropTh + )} diff --git a/frontend/src/features/hero/mocks/handlers.ts b/frontend/src/features/hero/mocks/handlers.ts index 74db8d7..e1ec046 100644 --- a/frontend/src/features/hero/mocks/handlers.ts +++ b/frontend/src/features/hero/mocks/handlers.ts @@ -58,6 +58,7 @@ export const resetMockHero = (): void => { uploadFailure = null; retried = []; focusRequests = []; + resetSlideRequests = []; recutting = new Set(); }; @@ -80,9 +81,11 @@ export const getRetriedImages = (): string[] => retried; export const getSavedHeroRequests = (): HeroSaveRequest[] => saved; -let focusRequests: { imageId: string; body: HeroFocusRequest }[] = []; +let focusRequests: { imageId: string; slideId?: string; body: HeroFocusRequest }[] = []; +let resetSlideRequests: string[] = []; +export const getResetSlideRequests = (): string[] => resetSlideRequests; /** The crops sent at once with PUT hero/images/{id}/focus (no save of the hero involved). */ -export const getFocusRequests = (): { imageId: string; body: HeroFocusRequest }[] => focusRequests; +export const getFocusRequests = (): { imageId: string; slideId?: string; body: HeroFocusRequest }[] => focusRequests; export const failNextUpload = (failure: { status: number; error: string; message: string } | null): void => { uploadFailure = failure; @@ -123,6 +126,29 @@ export const heroHandlers = [ return HttpResponse.json(hero); }), + // A crop for one slide (crop per use): the slide gets its own crop and shows the photo as processing. + http.put(`${API_BASE}/api/v1/hero/slides/:id/focus`, async ({ params, request }) => { + const slideId = String(params.id); + const body = (await request.json()) as HeroFocusRequest; + const slide = hero.slides.find((s) => s.id === slideId); + if (!slide) return new HttpResponse(null, { status: 404 }); + focusRequests.push({ imageId: slide.imageId, slideId, body }); + const updated = { ...slide, ownCrop: true, image: { ...slide.image, focus: body.focus, wideFocus: body.wide ?? null, tallFocus: body.tall ?? null, processing: true } }; + recutting.add(slide.imageId); + hero = { ...hero, slides: hero.slides.map((s) => (s.id === slideId ? updated : s)) }; + return HttpResponse.json(updated); + }), + + http.delete(`${API_BASE}/api/v1/hero/slides/:id/focus`, ({ params }) => { + const slideId = String(params.id); + const slide = hero.slides.find((s) => s.id === slideId); + if (!slide) return new HttpResponse(null, { status: 404 }); + resetSlideRequests.push(slideId); + const updated = { ...slide, ownCrop: false, image: { ...slide.image, processing: false } }; + hero = { ...hero, slides: hero.slides.map((s) => (s.id === slideId ? updated : s)) }; + return HttpResponse.json(updated); + }), + http.put(`${API_BASE}/api/v1/hero/images/:id/focus`, async ({ params, request }) => { const id = String(params.id); const body = (await request.json()) as HeroFocusRequest; diff --git a/frontend/src/features/hero/pages/HeroPage.test.tsx b/frontend/src/features/hero/pages/HeroPage.test.tsx index bdcb874..a48c170 100644 --- a/frontend/src/features/hero/pages/HeroPage.test.tsx +++ b/frontend/src/features/hero/pages/HeroPage.test.tsx @@ -5,7 +5,7 @@ import { http, HttpResponse } from 'msw'; import { server } from '@/mocks/server'; import { API_BASE } from '@/features/auth/mocks/fixtures'; import { renderApp, mockAuthenticated } from '@/test/utils'; -import { emptyHero, failNextUpload, finishMockRecut, getRetriedImages, getFocusRequests, +import { emptyHero, failNextUpload, finishMockRecut, getRetriedImages, getFocusRequests, getResetSlideRequests, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers'; import { _resetSetupStatusCache } from '@/router'; @@ -309,7 +309,7 @@ describe('Focus dialog', () => { await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument()); await waitFor(() => expect(getFocusRequests()).toHaveLength(1)); - expect(getFocusRequests()[0]).toEqual({ + expect(getFocusRequests()[0]).toMatchObject({ imageId: '10000000-0000-0000-0000-000000000001', body: { focus: { x: 0.3, y: 0.5, zoom: 1 }, wide: null, tall: { x: 0.8, y: 0.5, zoom: 2 } }, }); @@ -909,7 +909,7 @@ describe('Each photo shows by itself when its processing is finished', () => { expect(screen.getByTestId('hero-slide-0-done')).toHaveAttribute('role', 'status'); expect(screen.getByTestId('hero-slide-0-done')).toHaveAttribute('aria-live', 'polite'); expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument(); - expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled(); + expect(screen.getByTestId('hero-slide-0-focus')).toBeInTheDocument(); // the crop button is there (disabled until the slide is saved) // The note names the slide for a screen reader, and goes away by itself after a few seconds. expect(note.textContent).toMatch(/Slide 1: (verwerking afgerond|processing finished)/); await waitFor(() => expect(screen.queryByTestId('hero-slide-0-done-text')).not.toBeInTheDocument(), { timeout: 8000 }); @@ -924,7 +924,7 @@ describe('Each photo shows by itself when its processing is finished', () => { expect(await screen.findByTestId('hero-slide-0-done-text', {}, T)).toHaveTextContent(/Verwerking afgerond|Processing finished/); expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument(); - expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled(); + expect(screen.getByTestId('hero-slide-0-focus')).toBeInTheDocument(); // the crop button is there (disabled until the slide is saved) expect(screen.queryByText(/stond al|already/i)).not.toBeInTheDocument(); }); @@ -982,7 +982,7 @@ describe('Each photo shows by itself when its processing is finished', () => { finished.add(ids[0]); await screen.findByTestId('hero-slide-0-done-text', {}, { timeout: 12000 }); - expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled(); + expect(screen.getByTestId('hero-slide-0-focus')).toBeInTheDocument(); // the crop button is there (disabled until the slide is saved) expect(screen.getByTestId('hero-slide-1-processing')).toBeInTheDocument(); expect(screen.queryByTestId('hero-slide-1-focus')).not.toBeInTheDocument(); expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/1 van 2|1 of 2/); @@ -1297,15 +1297,67 @@ describe('Add a slide with a photo from the library or an upload', () => { expect(getSavedHeroRequests()[0].slides.map((x) => x.imageId)).toEqual(['10000000-0000-0000-0000-000000000001', '10000000-0000-0000-0000-000000000002', '10000000-0000-0000-0000-000000000002']); }); - it('tells that the crop is shared when the same photo is in more than one slide', async () => { + it('gives every slide its own crop: the same photo on two slides is cropped per slide', async () => { const one = slide(1); - setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [one, { ...slide(2), imageId: one.imageId, image: one.image }, slide(3)] }); - + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [one, { ...slide(2), imageId: one.imageId, image: one.image }] }); await openPage(); - expect(screen.getByTestId('hero-slide-0-shared-crop')).toBeInTheDocument(); - expect(screen.getByTestId('hero-slide-1-shared-crop')).toBeInTheDocument(); - expect(screen.queryByTestId('hero-slide-2-shared-crop')).not.toBeInTheDocument(); + await userEvent.click(screen.getByTestId('hero-slide-1-focus')); + await screen.findByTestId('focus-dialog'); + fireEvent.change(screen.getByTestId('focus-x'), { target: { value: '20' } }); + await userEvent.click(screen.getByTestId('focus-save')); + + await waitFor(() => expect(getFocusRequests()).toHaveLength(1)); + expect(getFocusRequests()[0].slideId).toBe(slide(2).id); + expect(await screen.findByTestId('hero-slide-1-recut', {}, T)).toBeInTheDocument(); + expect(screen.getByTestId('hero-slide-0-focus')).toBeInTheDocument(); + expect(screen.queryByTestId('hero-slide-0-recut')).not.toBeInTheDocument(); + expect(screen.queryByTestId('hero-slide-0-shared-crop')).not.toBeInTheDocument(); + }); + + it('shows the reset button always: it applies at once and keeps the dialog open for a slide with its own crop', async () => { + const one = slide(1); + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...one, ownCrop: true }, slide(2)] }); + await openPage(); + + await userEvent.click(screen.getByTestId('hero-slide-0-focus')); + const dialog = await screen.findByTestId('focus-dialog'); + expect(within(dialog).getByTestId('focus-reset')).toBeEnabled(); + expect(within(dialog).queryByTestId('focus-apply-hint')).not.toBeInTheDocument(); + await userEvent.click(within(dialog).getByTestId('focus-reset')); + + await waitFor(() => expect(getResetSlideRequests()).toEqual([one.id])); + expect(screen.getByTestId('focus-dialog')).toBeInTheDocument(); + await waitFor(() => expect(within(screen.getByTestId('focus-dialog')).getByTestId('focus-reset')).toBeDisabled()); + }); + + it('shows the reset button disabled for a slide that uses the crop of its photo, and Cancel changes nothing', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + await openPage(); + + await userEvent.click(screen.getByTestId('hero-slide-0-focus')); + const dialog = await screen.findByTestId('focus-dialog'); + + expect(within(dialog).getByTestId('focus-reset')).toBeVisible(); + expect(within(dialog).getByTestId('focus-reset')).toBeDisabled(); + fireEvent.change(within(dialog).getByTestId('focus-x'), { target: { value: '10' } }); + await userEvent.click(within(dialog).getByTestId('focus-cancel')); + await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument()); + expect(getFocusRequests()).toHaveLength(0); + expect(getResetSlideRequests()).toHaveLength(0); + }); + + it('asks to save a new slide first before its crop can be changed', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + server.use(http.get('*/media-library/photos', () => HttpResponse.json({ photos: [], modules: ['hero'] }))); + await openPage(); + await userEvent.click(screen.getByTestId('hero-upload-button')); + const dialog = await screen.findByTestId('library-picker', {}, T); + await userEvent.upload(within(dialog).getByTestId('picker-file'), new File([new Uint8Array(100)], 'nieuw.jpg', { type: 'image/jpeg' })); + await waitFor(() => expect(screen.queryByTestId('library-picker')).not.toBeInTheDocument()); + + expect(await screen.findByTestId('hero-slide-1-save-first')).toBeInTheDocument(); + expect(screen.queryByTestId('hero-slide-0-save-first')).not.toBeInTheDocument(); }); it('uploads a new photo on the spot with the hero upload and puts it in the slide', async () => { diff --git a/frontend/src/features/hero/pages/HeroPage.tsx b/frontend/src/features/hero/pages/HeroPage.tsx index 86bf78a..f787b29 100644 --- a/frontend/src/features/hero/pages/HeroPage.tsx +++ b/frontend/src/features/hero/pages/HeroPage.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useRef, useState } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useQueries, useQueryClient } from '@tanstack/react-query'; import { useFieldArray, useForm, useWatch } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; @@ -27,7 +27,7 @@ import { UnsavedChangesGuard } from '../components/UnsavedChangesGuard'; import { UploadProgress, type UploadItem } from '../components/UploadProgress'; import { mediaUrl } from '../lib/media'; import { heroFormSchema, type HeroFormData } from '../schemas/hero'; -import { heroImageQuery, useHero, useHeroUploadLimits, useSaveHero, useSetHeroImageFocus, useUploadHeroImage } from '../services/hooks'; +import { heroImageQuery, useHero, useHeroUploadLimits, useResetSlideFocus, useSaveHero, useSetSlideFocus, useUploadHeroImage } from '../services/hooks'; import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest } from '../services/types'; const textareaClass = @@ -147,9 +147,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { const nextUploadKey = useRef(0); const limits = useHeroUploadLimits().data; const [localImages, setLocalImages] = useState>({}); - const [focusImageId, setFocusImageId] = useState(null); + // The slide whose crop is being edited (a crop belongs to the slide, not to the photo). + const [focusSlideIndex, setFocusSlideIndex] = useState(null); // A new crop is applied at once (no save needed): the server cuts the new variants in the background. - const setImageFocus = useSetHeroImageFocus(); + const setSlideFocus = useSetSlideFocus(); + const resetSlideFocus = useResetSlideFocus(); // The photos of the current upload batch, for the counter "x of y processed": only the photos uploaded together, not // every photo of the hero. A new batch starts when the previous one is finished. const [batch, setBatch] = useState([]); @@ -168,7 +170,8 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { reset, } = useForm({ resolver: zodResolver(heroFormSchema), mode: 'onTouched', shouldFocusError: false, defaultValues: toForm(hero) }); const slides = useFieldArray({ control, name: 'slides' }); - const watchedSlides = useWatch({ control, name: 'slides' }) ?? []; + const watchedSlidesRaw = useWatch({ control, name: 'slides' }); + const watchedSlides = useMemo(() => watchedSlidesRaw ?? [], [watchedSlidesRaw]); // The two tabs share one form, one Save button and one "unsaved changes" state. The chosen tab is remembered for this // browser session. @@ -219,10 +222,24 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { const images = useMemo(() => { const map: Record = { ...localImages }; for (const image of polled) if (image) map[image.id] = image; - for (const s of hero.slides) map[s.imageId] = s.image; + // A slide with a crop of its own shows another version of the photo: it never replaces the photo's own entry. + for (const s of hero.slides) if (!s.ownCrop || !(s.imageId in map)) map[s.imageId] = s.image; return map; }, [hero.slides, localImages, polled]); + // The photo as a slide shows it: for a saved slide the server's version (its own crop and set), else the photo itself. + const savedSlideOf = useCallback( + (index: number) => { + const id = watchedSlides[index]?.id; + return id ? hero.slides.find((s) => s.id === id && s.imageId === watchedSlides[index]?.imageId) : undefined; + }, + [watchedSlides, hero.slides], + ); + const imageOfSlide = useCallback( + (index: number): HeroImageDto | undefined => savedSlideOf(index)?.image ?? images[watchedSlides[index]?.imageId ?? slides.fields[index]?.imageId ?? ''], + [savedSlideOf, images, watchedSlides, slides.fields], + ); + // Per photo: when its processing is done it shows "Processing finished" for a few seconds, and can be worked on at once. // A photo that was ready at once (the same photo was already in the CMS) shows the same note, without a placeholder. const [justDone, setJustDone] = useState>({}); @@ -238,7 +255,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { }; useEffect(() => { for (const field of slides.fields) { - const image = images[field.imageId]; + const image = imageOfSlide(slides.fields.indexOf(field)); if (!image) continue; if (image.processing) { wasProcessing.current[image.id] = true; @@ -247,7 +264,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { markDone(image.id); } } - }, [images, slides.fields]); + }, [images, slides.fields, imageOfSlide]); // The form is compared with the last saved state (react-hook-form keeps it), so a change that is undone no longer counts. // Photos still being prepared on the server and the background refresh are not changes of the administrator. @@ -286,15 +303,16 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { } }; - // A new crop goes to the server at once; the page then shows the photo as processing until the new variants are ready. - const applyFocus = (imageId: string, request: HeroFocusRequest) => { + // A new crop for ONE slide goes to the server at once; the slide then shows its photo as processing until its own variants are ready. + const applyFocus = (slideIndex: number, request: HeroFocusRequest) => { + const slideId = watchedSlides[slideIndex]?.id; + if (!slideId) return; setServerError(null); - setImageFocus.mutate( - { imageId, request }, + setSlideFocus.mutate( + { slideId, request }, { - onSuccess: (image) => { - setLocalImages((current) => ({ ...current, [image.id]: image })); - setRecutIds((current) => [...new Set([...current, image.id])]); + onSuccess: (slide) => { + setRecutIds((current) => [...new Set([...current, slide.imageId])]); }, onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))), }, @@ -552,7 +570,8 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { f.id)} strategy={verticalListSortingStrategy}>
    {slides.fields.map((field, index) => { - const image = images[field.imageId]; + const image = imageOfSlide(index); + const savedSlide = savedSlideOf(index); const decorative = watchedSlides[index]?.decorative ?? false; return ( @@ -565,16 +584,18 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { setFocusImageId(field.imageId)} + onClick={() => setFocusSlideIndex(index)} testId={`hero-slide-${index}-focus`} + disabled={!savedSlide} + hint={t('hero.slides.saveFirst')} /> ) : (
    ?
    )} - {watchedSlides.some((other, otherIndex) => otherIndex !== index && other?.imageId === field.imageId) && ( -

    - {t('hero.library.sharedCrop')} + {!savedSlide && ( +

    + {t('hero.slides.saveFirst')}

    )} @@ -666,9 +687,14 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { limitsText={limits ? t('hero.slides.limits', { formats: limits.formats.join(', '), size: Math.round(limits.maxUploadBytes / (1024 * 1024)), megapixels: limits.maxMegapixels }) : undefined} /> setFocusImageId(null)} + image={focusSlideIndex !== null ? imageOfSlide(focusSlideIndex) ?? null : null} + onApply={(_imageId, request) => focusSlideIndex !== null && applyFocus(focusSlideIndex, request)} + onClose={() => setFocusSlideIndex(null)} + canReset={focusSlideIndex !== null && Boolean(savedSlideOf(focusSlideIndex)?.ownCrop)} + onReset={() => { + const slideId = focusSlideIndex !== null ? watchedSlides[focusSlideIndex]?.id : undefined; + if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) }); + }} /> ); diff --git a/frontend/src/features/hero/services/hooks.ts b/frontend/src/features/hero/services/hooks.ts index 34cf3ba..9065dd6 100644 --- a/frontend/src/features/hero/services/hooks.ts +++ b/frontend/src/features/hero/services/hooks.ts @@ -1,6 +1,6 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api-client'; -import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest, HeroUploadLimits } from './types'; +import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest, HeroSlideAdminDto, HeroUploadLimits } from './types'; export const HERO_KEY = ['hero']; @@ -73,6 +73,23 @@ export const heroImageQuery = (imageId: string) => ({ * A new crop for an image, applied at once without saving the hero (Bryan, 2026-11-17): the server cuts the new variants * in the background and the image switches to them when they are complete. The answer is the image, now processing. */ +export function useSetSlideFocus() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: ({ slideId, request }) => api.put(`/api/v1/hero/slides/${slideId}/focus`, request), + onSuccess: () => queryClient.invalidateQueries({ queryKey: HERO_KEY }), + }); +} + +/** Takes the slide's own crop away: it uses the crop of its photo again. */ +export function useResetSlideFocus() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: (slideId) => api.delete(`/api/v1/hero/slides/${slideId}/focus`), + onSuccess: () => queryClient.invalidateQueries({ queryKey: HERO_KEY }), + }); +} + export function useSetHeroImageFocus() { const queryClient = useQueryClient(); return useMutation({ diff --git a/frontend/src/features/hero/services/types.ts b/frontend/src/features/hero/services/types.ts index d573e04..b107309 100644 --- a/frontend/src/features/hero/services/types.ts +++ b/frontend/src/features/hero/services/types.ts @@ -48,7 +48,10 @@ export interface HeroSlideAdminDto { decorative: boolean; isActive: boolean; sortOrder: number; + /** The photo as THIS slide shows it (its own crop and set when `ownCrop`); its id is always the photo's id. */ image: HeroImageDto; + /** The slide has a crop of its own (crop per use); otherwise it uses its photo's crop. */ + ownCrop?: boolean; } export interface HeroAdminDto { diff --git a/frontend/src/features/media/components/LibraryCheck.tsx b/frontend/src/features/media/components/LibraryCheck.tsx new file mode 100644 index 0000000..d058dff --- /dev/null +++ b/frontend/src/features/media/components/LibraryCheck.tsx @@ -0,0 +1,70 @@ +import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { CheckCircle2, ListChecks, TriangleAlert } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { useReadCheck } from '../services/hooks'; + +/** + * "Library check": a small panel for administrators that compares the library's own tables with the modules (the hero) in + * plain words and shows the state of the migration switches (copy, write-through, reading). It only asks the server when the + * button is clicked; it changes nothing. + */ +export function LibraryCheck() { + const { t } = useTranslation(); + const [asked, setAsked] = useState(false); + const check = useReadCheck(asked); + + return ( +
    +
    +

    + {t('media.check.title')} +

    + +
    +

    {t('media.check.hint')}

    + {check.isError && ( +

    + {t('media.check.error')} +

    + )} + {check.data && ( +
    +

    + {t('media.check.switches', { + copy: check.data.copyOnStartup ? t('media.check.on') : t('media.check.off'), + write: check.data.dualWrite ? t('media.check.on') : t('media.check.off'), + read: t(`media.check.read.${check.data.mode}`), + })} +

    + {check.data.modules.length === 0 &&

    {t('media.check.noModules')}

    } + {check.data.modules.map((module) => ( +
    + {module.matches ? ( +

    +

    + ) : ( +
    +

    +

    +
      + {module.differences.map((difference) => ( +
    • {difference}
    • + ))} +
    +
    + )} +
    + ))} +
    + )} +
    + ); +} diff --git a/frontend/src/features/media/mocks/handlers.ts b/frontend/src/features/media/mocks/handlers.ts index f56cd28..f94a417 100644 --- a/frontend/src/features/media/mocks/handlers.ts +++ b/frontend/src/features/media/mocks/handlers.ts @@ -1,5 +1,5 @@ import { http, HttpResponse } from 'msw'; -import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types'; +import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types'; const daysAgo = (days: number) => new Date(Date.now() - days * 86_400_000).toISOString(); @@ -81,6 +81,9 @@ export const mediaHandlers = [ ...mediaTrashHandlers, http.get('*/media-library/photos', () => HttpResponse.json({ photos: seedPhotos(), modules: ['hero'] } satisfies MediaLibraryDto)), http.get('*/media-library/storage', () => HttpResponse.json(seedStorage())), + http.get('*/media-library/read-check', () => + HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: true, differences: [] }], copyOnStartup: false, dualWrite: false } satisfies MediaReadCheckResponse), + ), http.get('*/media-library/cleanup-preview', ({ request }) => { const url = new URL(request.url); const days = Number(url.searchParams.get('days') ?? 7); diff --git a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx index c1d008e..9ae89d7 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx @@ -339,6 +339,52 @@ describe('MediaLibraryPage', () => { expect(within(dialog).getByTestId('cleanup-confirm')).toBeInTheDocument(); }); + it('checks the library against the hero in plain words, with the state of the switches, only when asked', async () => { + let calls = 0; + server.use( + http.get('*/media-library/read-check', () => { + calls += 1; + return HttpResponse.json({ mode: 'compare', modules: [{ module: 'hero', mode: 'compare', photos: 11, uses: 8, matches: true, differences: [] }], copyOnStartup: true, dualWrite: true }); + }), + ); + renderPage(); + await screen.findByTestId('photo-selection-list'); + expect(calls).toBe(0); + expect(screen.queryByTestId('library-check-result')).not.toBeInTheDocument(); + + await userEvent.click(screen.getByTestId('library-check-run')); + + expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek komt overeen met Hero (11 foto'); + expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: aan; schrijven naar de bibliotheek: aan; lezen uit de bibliotheek: vergelijken (alleen loggen)'); + expect(calls).toBe(1); + }); + + it('lists the differences when the library does not match', async () => { + server.use( + http.get('*/media-library/read-check', () => + HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: false, differences: ['photo 1 is missing in the library', 'use 2 differs'] }], copyOnStartup: false, dualWrite: false }), + ), + ); + renderPage(); + await screen.findByTestId('photo-selection-list'); + + await userEvent.click(screen.getByTestId('library-check-run')); + + expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek wijkt af van Hero: 2 verschillen.'); + expect(within(screen.getByTestId('library-check-hero-differences')).getAllByRole('listitem')).toHaveLength(2); + expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: uit; schrijven naar de bibliotheek: uit; lezen uit de bibliotheek: uit'); + }); + + it('says so when the check cannot be run', async () => { + server.use(http.get('*/media-library/read-check', () => new HttpResponse(null, { status: 500 }))); + renderPage(); + await screen.findByTestId('photo-selection-list'); + + await userEvent.click(screen.getByTestId('library-check-run')); + + expect(await screen.findByTestId('library-check-error')).toHaveTextContent('De controle kon niet worden uitgevoerd.'); + }); + it('shows an error when the library cannot be loaded', async () => { server.use(http.get('*/media-library/photos', () => new HttpResponse(null, { status: 500 }))); renderPage(); diff --git a/frontend/src/features/media/pages/MediaLibraryPage.tsx b/frontend/src/features/media/pages/MediaLibraryPage.tsx index 5bf5208..6e53f98 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.tsx @@ -7,6 +7,7 @@ import { CleanupDialog } from '../components/CleanupDialog'; import { DeleteDialog } from '../components/DeleteDialog'; import { PurgeDialog } from '../components/PurgeDialog'; import { PhotoSelectionList, type PhotoUse } from '../components/PhotoSelectionList'; +import { LibraryCheck } from '../components/LibraryCheck'; import { StorageMeter } from '../components/StorageMeter'; import { useLayoutPreference } from '../lib/layoutPreference'; import { emptySelection, prune, type Selection } from '../lib/selection'; @@ -90,6 +91,7 @@ export function MediaLibraryPage() { {storage.data && } +
    {(['library', 'trash'] as const).map((value) => ( diff --git a/frontend/src/features/media/services/hooks.ts b/frontend/src/features/media/services/hooks.ts index 055d3d3..d8f8cf4 100644 --- a/frontend/src/features/media/services/hooks.ts +++ b/frontend/src/features/media/services/hooks.ts @@ -1,6 +1,6 @@ import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api-client'; -import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types'; +import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types'; export const MEDIA_KEY = ['media-library']; @@ -22,6 +22,17 @@ export function useMediaStorage() { }); } +/** Compares the library tables with the modules and tells the state of the migration switches. Only asked for when the person clicks. */ +export function useReadCheck(enabled: boolean) { + return useQuery({ + queryKey: [...MEDIA_KEY, 'read-check'], + queryFn: () => api.get('/api/v1/media-library/read-check'), + enabled, + staleTime: 0, + gcTime: 0, + }); +} + /** What a clean-up with these settings would select. A preview: nothing is deleted. */ export function useCleanupPreview(days: number, onlyUnused: boolean, enabled: boolean) { return useQuery({ diff --git a/frontend/src/features/media/services/types.ts b/frontend/src/features/media/services/types.ts index 12b9177..cdef54e 100644 --- a/frontend/src/features/media/services/types.ts +++ b/frontend/src/features/media/services/types.ts @@ -29,6 +29,23 @@ export interface MediaLibraryDto { export type MediaStorageLevel = 'ok' | 'warning' | 'full'; +export interface MediaReadCheckModuleDto { + module: string; + mode: string; + photos: number; + uses: number; + matches: boolean; + differences: string[]; +} + +/** The check of the library tables against the modules, plus the state of the migration switches. */ +export interface MediaReadCheckResponse { + mode: 'off' | 'compare' | 'on'; + modules: MediaReadCheckModuleDto[]; + copyOnStartup: boolean; + dualWrite: boolean; +} + export interface MediaStorageDto { usedBytes: number; /** 0 means no limit. */ diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index d807996..ec158d2 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -645,8 +645,7 @@ "library": { "title": "Add slide", "description": "Choose a photo from the media library or upload a new one; it then lands in the library too. Every slide has one photo. A slide's photo is not changed: remove the slide and add a new one.", - "group": "Photo for the new slide", - "sharedCrop": "This photo is in more slides; the crop applies to all of them." + "group": "Photo for the new slide" }, "slides": { "title": "Images", @@ -676,7 +675,8 @@ "number": "Slide {{n}}", "done": "Processing finished", "doneFor": "Slide {{n}}: processing finished", - "cropEdit": "Adjust crop" + "cropEdit": "Adjust crop", + "saveFirst": "Save the slide first to change its crop." }, "processing": { "count": "{{done}} of {{total}} photos processed", @@ -709,7 +709,8 @@ "tall": "Portrait preview" }, "apply": "Apply", - "previewPending": "The preview of this photo is being prepared. Close this window and try again in a few seconds." + "previewPending": "The preview of this photo is being prepared. Close this window and try again in a few seconds.", + "reset": "Use the photo’s crop again" }, "errors": { "titleRequired": "A title is required.", @@ -901,6 +902,25 @@ "irreversible": "This cannot be undone.", "partial_one": "{{count}} photo could not be deleted. The rest was deleted for good.", "partial_other": "{{count}} photos could not be deleted. The rest was deleted for good." + }, + "check": { + "title": "Library check", + "run": "Check", + "running": "Checking…", + "hint": "Compares the library’s tables with the hero and shows the state of the switches. Nothing is changed.", + "error": "The check could not be run.", + "switches": "State of the switches: copy at start-up: {{copy}}; writing to the library: {{write}}; reading from the library: {{read}}.", + "on": "on", + "off": "off", + "read": { + "off": "off", + "compare": "compare (log only)", + "on": "on" + }, + "noModules": "Nothing to check.", + "matches": "The library matches {{module}} ({{photos}} photos, {{uses}} places).", + "differs_one": "The library differs from {{module}}: {{count}} difference.", + "differs_other": "The library differs from {{module}}: {{count}} differences." } } } diff --git a/frontend/src/i18n/locales/nl/translation.json b/frontend/src/i18n/locales/nl/translation.json index 806730b..bdaa0d1 100644 --- a/frontend/src/i18n/locales/nl/translation.json +++ b/frontend/src/i18n/locales/nl/translation.json @@ -645,8 +645,7 @@ "library": { "title": "Slide toevoegen", "description": "Kies een foto uit de mediabibliotheek of upload een nieuwe foto; die komt dan ook in de bibliotheek. Elke slide heeft één foto. Een foto wijzig je niet op een slide: verwijder de slide en voeg een nieuwe toe.", - "group": "Foto voor de nieuwe slide", - "sharedCrop": "Deze foto staat in meer slides; de uitsnede geldt voor allemaal." + "group": "Foto voor de nieuwe slide" }, "slides": { "title": "Afbeeldingen", @@ -676,7 +675,8 @@ "number": "Slide {{n}}", "done": "Verwerking afgerond", "doneFor": "Slide {{n}}: verwerking afgerond", - "cropEdit": "Uitsnede aanpassen" + "cropEdit": "Uitsnede aanpassen", + "saveFirst": "Sla de slide eerst op om de uitsnede aan te passen." }, "processing": { "count": "{{done}} van {{total}} foto’s verwerkt", @@ -709,7 +709,8 @@ "tall": "Voorbeeld staand" }, "apply": "Toepassen", - "previewPending": "Het voorbeeld van deze foto wordt klaargezet. Sluit dit venster en probeer het over een paar seconden opnieuw." + "previewPending": "Het voorbeeld van deze foto wordt klaargezet. Sluit dit venster en probeer het over een paar seconden opnieuw.", + "reset": "Terug naar de uitsnede van de foto" }, "errors": { "titleRequired": "Een titel is verplicht.", @@ -901,6 +902,25 @@ "irreversible": "Dit kan niet ongedaan worden gemaakt.", "partial_one": "{{count}} foto kon niet worden verwijderd. De rest is definitief verwijderd.", "partial_other": "{{count}} foto's konden niet worden verwijderd. De rest is definitief verwijderd." + }, + "check": { + "title": "Controle bibliotheek", + "run": "Controleren", + "running": "Bezig met controleren…", + "hint": "Vergelijkt de tabellen van de bibliotheek met de Hero en toont de stand van de schakelaars. Er wordt niets gewijzigd.", + "error": "De controle kon niet worden uitgevoerd.", + "switches": "Stand van de schakelaars: kopie bij het opstarten: {{copy}}; schrijven naar de bibliotheek: {{write}}; lezen uit de bibliotheek: {{read}}.", + "on": "aan", + "off": "uit", + "read": { + "off": "uit", + "compare": "vergelijken (alleen loggen)", + "on": "aan" + }, + "noModules": "Geen onderdelen om te controleren.", + "matches": "De bibliotheek komt overeen met {{module}} ({{photos}} foto’s, {{uses}} plekken).", + "differs_one": "De bibliotheek wijkt af van {{module}}: {{count}} verschil.", + "differs_other": "De bibliotheek wijkt af van {{module}}: {{count}} verschillen." } } } diff --git a/openapi/slpsoftware.v1.json b/openapi/slpsoftware.v1.json index e65cc6b..86f681a 100644 --- a/openapi/slpsoftware.v1.json +++ b/openapi/slpsoftware.v1.json @@ -927,6 +927,10 @@ "isActive": { "type": "boolean" }, + "ownCrop": { + "default": false, + "type": "boolean" + }, "sortOrder": { "format": "int32", "pattern": "^-?(?:0|[1-9]\\d*)$", @@ -1256,6 +1260,76 @@ }, "type": "object" }, + "MediaReadCheckDto": { + "properties": { + "differences": { + "items": { + "type": "string" + }, + "type": "array" + }, + "matches": { + "type": "boolean" + }, + "mode": { + "type": "string" + }, + "module": { + "type": "string" + }, + "photos": { + "format": "int32", + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ] + }, + "uses": { + "format": "int32", + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "integer", + "string" + ] + } + }, + "required": [ + "module", + "mode", + "photos", + "uses", + "matches", + "differences" + ], + "type": "object" + }, + "MediaReadCheckResponse": { + "properties": { + "copyOnStartup": { + "default": false, + "type": "boolean" + }, + "dualWrite": { + "default": false, + "type": "boolean" + }, + "mode": { + "type": "string" + }, + "modules": { + "items": { + "$ref": "#/components/schemas/MediaReadCheckDto" + }, + "type": "array" + } + }, + "required": [ + "mode", + "modules" + ], + "type": "object" + }, "MediaRestoreRequest": { "properties": { "ids": { @@ -3683,6 +3757,164 @@ ] } }, + "/api/v1/hero/slides/{id}/focus": { + "delete": { + "parameters": [ + { + "in": "path", + "name": "id", + "required": true, + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + } + }, + "description": "OK" + }, + "404": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + }, + "description": "Not Found" + } + }, + "tags": [ + "HeroSlides" + ] + }, + "put": { + "parameters": [ + { + "in": "path", + "name": "id", + "required": true, + "schema": { + "format": "uuid", + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/*+json": { + "schema": { + "$ref": "#/components/schemas/HeroFocusRequest" + } + }, + "application/json": { + "schema": { + "$ref": "#/components/schemas/HeroFocusRequest" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/HeroFocusRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/HeroSlideAdminDto" + } + } + }, + "description": "OK" + }, + "400": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + }, + "description": "Bad Request" + }, + "404": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + }, + "description": "Not Found" + } + }, + "tags": [ + "HeroSlides" + ] + } + }, "/api/v1/media-library/cleanup-preview": { "get": { "parameters": [ @@ -3902,6 +4134,35 @@ ] } }, + "/api/v1/media-library/read-check": { + "get": { + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MediaReadCheckResponse" + } + }, + "text/json": { + "schema": { + "$ref": "#/components/schemas/MediaReadCheckResponse" + } + }, + "text/plain": { + "schema": { + "$ref": "#/components/schemas/MediaReadCheckResponse" + } + } + }, + "description": "OK" + } + }, + "tags": [ + "MediaLibrary" + ] + } + }, "/api/v1/media-library/restore": { "post": { "requestBody": { @@ -4457,6 +4718,9 @@ { "name": "HeroImages" }, + { + "name": "HeroSlides" + }, { "name": "Auth" }, diff --git a/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs b/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs index 161cf50..f26fd49 100644 --- a/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs +++ b/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs @@ -103,4 +103,37 @@ public class MediaLibraryControllerTests result.Should().Be(new MediaStorageDto(0, 0, "ok", 80)); } + + [Fact] + public async Task The_read_check_lists_what_each_module_found_and_the_mode() + { + var library = Substitute.For(); + library.Modules.Returns(["hero"]); + var verifier = Substitute.For(); + verifier.CheckAsync(Arg.Any()).Returns(new MediaReadCheck("hero", MediaReadMode.Compare, 3, 2, ["photo x is missing in the library"])); + var reader = Substitute.For(); + reader.Mode.Returns(MediaReadMode.Compare); + var switches = Substitute.For(); + switches.Current.Returns(new MediaLibrarySwitches(true, true, MediaReadMode.Compare)); + + var result = await new MediaLibraryController(library, new Clock(), null, [verifier], reader, switches).ReadCheck(CancellationToken.None); + + var response = result.Should().BeOfType().Subject.Value.Should().BeOfType().Subject; + response.Mode.Should().Be("compare"); + response.CopyOnStartup.Should().BeTrue(); + response.DualWrite.Should().BeTrue(); + response.Modules.Should().ContainSingle().Which.Should().Match(m => m.Module == "hero" && m.Photos == 3 && m.Uses == 2 && !m.Matches && m.Differences.Count == 1); + } + + [Fact] + public async Task The_read_check_without_modules_says_off_and_nothing() + { + var library = Substitute.For(); + + var result = await new MediaLibraryController(library, new Clock()).ReadCheck(CancellationToken.None); + + var response = result.Should().BeOfType().Subject.Value.Should().BeOfType().Subject; + response.Mode.Should().Be("off"); + response.Modules.Should().BeEmpty(); + } } diff --git a/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs b/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs index c199a95..7a0855e 100644 --- a/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs +++ b/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs @@ -87,6 +87,9 @@ public static class ServiceCollectionExtensions services.TryAddSingleton(TimeProvider.System); // The media library reads from the usage providers of whichever modules are loaded (none is fine). services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddScoped(); + services.TryAddSingleton(); services.TryAddSingleton(sp => new ResetCooldown(sp.GetRequiredService(), TimeSpan.FromMinutes(sp.GetRequiredService>().Value.CooldownMinutes))); services.AddScoped(); diff --git a/src/SlpModularCms.Core/Media/ImageProcessing.cs b/src/SlpModularCms.Core/Media/ImageProcessing.cs index 0c6c5b8..ade584b 100644 --- a/src/SlpModularCms.Core/Media/ImageProcessing.cs +++ b/src/SlpModularCms.Core/Media/ImageProcessing.cs @@ -68,11 +68,10 @@ public sealed class MediaOptions /// /// Whether photos that were uploaded but never used are removed automatically after . - /// The media library keeps everything until a person removes it, so set this to false (Media__AutoCleanupUnattached=false) - /// once the library can show and delete photos. Retired files of photos a person removed stay housekeeping either way. - /// Default true keeps today's behaviour (assumption 3 of the media-library list). + /// The media library keeps everything until a person removes it, so the default is false: unused uploads stay until someone deletes them in the library. + /// Set Media__AutoCleanupUnattached=true to get the old behaviour back (removal after the retention time). Retired files of photos a person removed stay housekeeping either way. /// - public bool AutoCleanupUnattached { get; set; } = true; + public bool AutoCleanupUnattached { get; set; } = false; /// How long a photo stays recoverable in the trash before its files are removed. public int TrashDays { get; set; } = 7; diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs b/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs index 6b01c28..9a6b8d8 100644 --- a/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs +++ b/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs @@ -20,6 +20,14 @@ public sealed record MediaPhotoDto( DateTimeOffset? UnusedSince = null, DateTimeOffset? DeletedAt = null); +/// The result of comparing a module's own tables with the library tables (read switch, phase 2 step C). +public sealed record MediaReadCheckDto(string Module, string Mode, int Photos, int Uses, bool Matches, IReadOnlyList Differences); + +/// The read switch: off, compare or on. +/// The copy switch (start-up copy into the library tables). +/// The write-through switch. +public sealed record MediaReadCheckResponse(string Mode, IReadOnlyList Modules, bool CopyOnStartup = false, bool DualWrite = false); + public sealed record MediaLibraryDto(IReadOnlyList Photos, IReadOnlyList Modules); public sealed record MediaStorageDto(long UsedBytes, long LimitBytes, string Level, int WarningPercent); @@ -49,8 +57,27 @@ public sealed record MediaTrashResponse(IReadOnlyList Results [ApiController] [Route("media-library")] [Authorize(Policy = "AdminOnly")] -public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null) : ControllerBase +public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null, IEnumerable? verifiers = null, IMediaLibraryReader? reader = null, IMediaLibrarySwitchState? switches = null) : ControllerBase { + /// + /// Compares what each module holds with what the library's own tables hold and lists every difference. Read-only; works + /// whatever the read switch says, so it can be used before the switch is turned on. + /// + [HttpGet("read-check")] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task ReadCheck(CancellationToken cancellationToken) + { + var results = new List(); + foreach (var verifier in verifiers ?? []) + { + var check = await verifier.CheckAsync(cancellationToken); + results.Add(new MediaReadCheckDto(check.Module, check.Mode.ToString().ToLowerInvariant(), check.Photos, check.Uses, check.Matches, check.Differences.Take(50).ToList())); + } + + var state = switches?.Current ?? new MediaLibrarySwitches(false, false, reader?.Mode ?? MediaReadMode.Off); + return Ok(new MediaReadCheckResponse(state.ReadFromLibrary.ToString().ToLowerInvariant(), results, state.CopyOnStartup, state.DualWrite)); + } + [HttpGet("photos")] [ProducesResponseType(StatusCodes.Status200OK)] public async Task Photos(CancellationToken cancellationToken) diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs b/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs new file mode 100644 index 0000000..3abbb9d --- /dev/null +++ b/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs @@ -0,0 +1,130 @@ +namespace SlpModularCms.Core.Media.Library; + +/// Where the media library screen reads the photos of a module from (migration phase 2, step C). +public enum MediaReadMode +{ + /// The module's own tables (the default, and the truth until phase 3). + Off, + + /// Still the module's own tables, but the library tables are read too and every difference is logged and reported. + Compare, + + /// The library tables. Needs the write-through (Media:Library:DualWrite) to be current. + On, +} + +/// Reads a module's photos and uses back from the library's own tables. +public interface IMediaLibraryReader +{ + MediaReadMode Mode { get; } + + Task> ListAssetsAsync(string module, CancellationToken cancellationToken); + + Task> ListUsesAsync(string module, CancellationToken cancellationToken); +} + +/// The default: no library tables to read, so everything stays with the module. +public sealed class NoMediaLibraryReader : IMediaLibraryReader +{ + public MediaReadMode Mode => MediaReadMode.Off; + + public Task> ListAssetsAsync(string module, CancellationToken cancellationToken) => Task.FromResult>([]); + + public Task> ListUsesAsync(string module, CancellationToken cancellationToken) => Task.FromResult>([]); +} + +/// The current state of the migration switches (for the check in the library screen). +public sealed record MediaLibrarySwitches(bool CopyOnStartup, bool DualWrite, MediaReadMode ReadFromLibrary); + +/// Tells the current state of the migration switches. +public interface IMediaLibrarySwitchState +{ + MediaLibrarySwitches Current { get; } +} + +/// The default: no library tables, so every switch is off. +public sealed class NoMediaLibrarySwitchState : IMediaLibrarySwitchState +{ + public MediaLibrarySwitches Current => new(false, false, MediaReadMode.Off); +} + +/// What a comparison of a module's own tables with the library tables found. Differences is empty when they agree. +public sealed record MediaReadCheck(string Module, MediaReadMode Mode, int Photos, int Uses, IReadOnlyList Differences) +{ + public bool Matches => Differences.Count == 0; +} + +/// A module that can compare its own data with the library tables (used by GET media-library/read-check). +public interface IMediaReadVerifier +{ + string Module { get; } + + Task CheckAsync(CancellationToken cancellationToken); +} + +/// Compares what a module holds with what the library tables hold, by id, and lists every difference in words. +public static class MediaReadComparer +{ + public static IReadOnlyList Compare( + IReadOnlyList ownAssets, + IReadOnlyList ownUses, + IReadOnlyList libraryAssets, + IReadOnlyList libraryUses) + { + var differences = new List(); + var library = libraryAssets.ToDictionary(a => a.Id); + foreach (var own in ownAssets) + { + if (!library.Remove(own.Id, out var copy)) + { + differences.Add($"photo {own.Id} is missing in the library"); + continue; + } + + if (own.OriginalSha256 != copy.OriginalSha256 || own.OriginalKey != copy.OriginalKey || own.Bytes != copy.Bytes) + { + differences.Add($"photo {own.Id}: hash, key or size differs"); + } + + if (own.SetHash != copy.SetHash || own.VariantsJson != copy.VariantsJson) + { + differences.Add($"photo {own.Id}: set hash or variants differ"); + } + + if ((own.DeletedAt is null) != (copy.DeletedAt is null)) + { + differences.Add($"photo {own.Id}: trash state differs"); + } + + if (own.OriginalFileName != copy.OriginalFileName) + { + differences.Add($"photo {own.Id}: file name differs"); + } + + if (own.LastUsedAt?.ToUnixTimeSeconds() != copy.LastUsedAt?.ToUnixTimeSeconds()) + { + differences.Add($"photo {own.Id}: last used differs"); + } + } + + differences.AddRange(library.Keys.Select(id => $"photo {id} is in the library but not in the module")); + + var libraryUsesById = libraryUses.ToDictionary(u => u.Id); + foreach (var own in ownUses) + { + if (!libraryUsesById.Remove(own.Id, out var copy)) + { + differences.Add($"use {own.Id} is missing in the library"); + continue; + } + + if (own.AssetId != copy.AssetId || own.Position != copy.Position || own.Active != copy.Active || own.SetHash != copy.SetHash || own.VariantsJson != copy.VariantsJson) + { + differences.Add($"use {own.Id}: photo, position, active flag or set hash differs"); + } + } + + differences.AddRange(libraryUsesById.Keys.Select(id => $"use {id} is in the library but not in the module")); + return differences; + } +} diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs b/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs new file mode 100644 index 0000000..6fd0bc5 --- /dev/null +++ b/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs @@ -0,0 +1,19 @@ +namespace SlpModularCms.Core.Media.Library; + +/// +/// Write-through of a module's photos and uses into the media library's own tables (migration phase 2, step A). A module +/// calls it after every change it saved to its OWN tables, which stay the truth until phase 3. It never throws and never +/// changes the module's data: a failure is logged by the implementation and the next sync (or the verified copy) repairs it. +/// With the switch Media:Library:DualWrite off (the default) it does nothing. +/// +public interface IMediaLibrarySync +{ + /// Mirrors everything the module offers through its into the library tables. + Task SyncAsync(string module, CancellationToken cancellationToken); +} + +/// The default: the Media module is not there or the switch is off, so nothing is written. +public sealed class NoMediaLibrarySync : IMediaLibrarySync +{ + public Task SyncAsync(string module, CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs b/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs index eba1c7c..0a7eea6 100644 --- a/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs +++ b/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs @@ -7,6 +7,8 @@ public sealed record MediaCropSnapshot(string Ratio, double X, double Y, double /// A photo as a module holds it today, for copying into the library's own tables. Same ids, hashes and set hashes as the /// module uses, so no public address changes (migration plan, decision 13). /// +// Phase 3: the snapshots also carry what the module needs to publish the photo from the library tables: the variants of the +// current set (opaque JSON, the module's own format) and the wide and tall overrides of the photo's crop (null = none). public sealed record MediaAssetSnapshot( Guid Id, string Module, @@ -25,10 +27,17 @@ public sealed record MediaAssetSnapshot( Guid CreatedByUserId, string? OriginalFileName, DateTimeOffset? LastUsedAt, - DateTimeOffset? DeletedAt); + DateTimeOffset? DeletedAt, + string? VariantsJson = null, + double? WideFocusX = null, + double? WideFocusY = null, + double? WideZoom = null, + double? TallFocusX = null, + double? TallFocusY = null, + double? TallZoom = null); -/// One place a photo is used, as a module holds it today. -public sealed record MediaUseSnapshot(Guid Id, Guid AssetId, string Module, string PlaceKind, int Position, bool Active, string SetHash); +/// One place a photo is used, as a module holds it today. Crops are the crops of THIS use (crop per use); null = the crops of its photo. +public sealed record MediaUseSnapshot(Guid Id, Guid AssetId, string Module, string PlaceKind, int Position, bool Active, string SetHash, IReadOnlyList? Crops = null, string? VariantsJson = null); /// /// What a module offers so the library can copy its photos and uses into its own tables. Read-only: the module's data is diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs new file mode 100644 index 0000000..4bd3a7f --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs @@ -0,0 +1,94 @@ +using FluentAssertions; +using NSubstitute; +using SlpModularCms.Core.Media.Library; +using SlpModularCms.Modules.Hero.Data.Entities; +using SlpModularCms.Modules.Hero.Models; +using SlpModularCms.Modules.Hero.Services; +using Xunit; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// Phase 2, step A: the hero tells the media library after every change it saved (the write-through). +public class HeroLibrarySyncTests +{ + private static (HeroTestRig Rig, IMediaLibrarySync Sync) Rig() + { + var sync = Substitute.For(); + return (new HeroTestRig(librarySync: sync), sync); + } + + private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides); + + [Fact] + public async Task Every_change_of_a_photo_or_a_slide_is_written_through_for_the_hero_module() + { + var (rig, sync) = Rig(); + using var _r = rig; + var image = await rig.Upload(HeroTestRig.Photo()); + var id = Guid.Parse(image.Id); + var afterUpload = sync.ReceivedCalls().Count(); + afterUpload.Should().BeGreaterThan(0, "the upload and the rendering of its variants both wrote through"); + + var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, id, "een")), HeroTestRig.Admin, CancellationToken.None); + var slideId = Guid.Parse(saved.Slides[0].Id); + await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.2, 0.3, 1.5)), HeroTestRig.Admin, CancellationToken.None); + await rig.Images.SetSlideFocusAsync(slideId, new HeroFocusRequest(new FocusDto(0.6, 0.6, 1.2)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + await rig.Images.ResetSlideFocusAsync(slideId, HeroTestRig.Admin, CancellationToken.None); + await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(id, HeroTestRig.Admin, CancellationToken.None); + await rig.Images.RestoreAsync(id, HeroTestRig.Admin, CancellationToken.None); + + await sync.Received().SyncAsync("hero", Arg.Any()); + sync.ReceivedCalls().Count().Should().BeGreaterThan(afterUpload + 5); + sync.ReceivedCalls().Select(c => c.GetArguments()[0]).Should().OnlyContain(m => (string)m! == "hero"); + } + + [Fact] + public async Task Without_a_write_through_nothing_changes_for_the_hero() + { + using var rig = new HeroTestRig(); + + var image = await rig.Upload(HeroTestRig.Photo()); + + image.Processing.Should().BeFalse(); + } + + [Fact] + public async Task The_migration_source_gives_a_slide_with_its_own_crop_its_own_set_and_crops() + { + using var rig = new HeroTestRig(); + 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.SetSlideFocusAsync(Guid.Parse(saved.Slides[1].Id), new HeroFocusRequest(new FocusDto(0.1, 0.9, 1.4)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + + var uses = await new HeroMediaMigrationSource(rig.Repository).ListUsesAsync(CancellationToken.None); + + var photoSet = rig.Db.HeroImages.Single(i => i.Id == id).SetHash; + var first = uses.Single(u => u.Position == 1); + var second = uses.Single(u => u.Position == 2); + first.SetHash.Should().Be(photoSet); + first.Crops.Should().BeNull("a slide without its own crop uses the crops of its photo"); + second.SetHash.Should().NotBe(photoSet).And.Be(rig.Db.HeroSlides.Single(s => s.Id == Guid.Parse(saved.Slides[1].Id)).SetHash); + second.Crops.Should().NotBeNull(); + second.Crops!.Single(c => c.Ratio == "wide").X.Should().BeApproximately(0.1, 0.0001); + } + [Fact] + public async Task The_migration_source_gives_the_variants_in_the_format_the_hero_stores_them_and_the_overrides_of_the_crop() + { + using var rig = new HeroTestRig(); + var image = await rig.Upload(HeroTestRig.Photo()); + var id = Guid.Parse(image.Id); + await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.5, 0.5, 1), new FocusDto(0.2, 0.3, 1.5), null), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + + var asset = (await new HeroMediaMigrationSource(rig.Repository).ListAssetsAsync(CancellationToken.None)).Single(); + + var stored = rig.Db.HeroImages.Single(i => i.Id == id); + asset.SetHash.Should().Be(stored.SetHash); + System.Text.Json.JsonSerializer.Deserialize>(asset.VariantsJson!).Should().Equal(stored.Variants); + (asset.WideFocusX, asset.WideFocusY, asset.WideZoom).Should().Be((0.2, 0.3, 1.5)); + (asset.TallFocusX, asset.TallFocusY, asset.TallZoom).Should().Be((null, null, null)); + } +} 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/HeroReadFromLibraryTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroReadFromLibraryTests.cs new file mode 100644 index 0000000..fca74c3 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroReadFromLibraryTests.cs @@ -0,0 +1,124 @@ +using FluentAssertions; +using Microsoft.Extensions.Logging.Abstractions; +using SlpModularCms.Core.Media.Library; +using SlpModularCms.Modules.Hero.Models; +using SlpModularCms.Modules.Hero.Services; +using Xunit; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// Phase 2, step C: the library screen reads the hero's photos from the library tables (on), or compares (compare). +public class HeroReadFromLibraryTests +{ + private sealed class FakeReader(MediaReadMode mode, List assets, List uses, bool fails = false) : IMediaLibraryReader + { + public MediaReadMode Mode => mode; + + public Task> ListAssetsAsync(string module, CancellationToken cancellationToken) + => fails ? throw new InvalidOperationException("library down") : Task.FromResult>(assets.ToList()); + + public Task> ListUsesAsync(string module, CancellationToken cancellationToken) + => fails ? throw new InvalidOperationException("library down") : Task.FromResult>(uses.ToList()); + } + + private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides); + + /// A hero with two photos, one in a slide, one not; the library tables made equal to it. + private static async Task<(HeroTestRig Rig, List Assets, List Uses, Guid Used, Guid Unused)> ArrangeAsync() + { + var rig = new HeroTestRig(); + var used = Guid.Parse((await rig.Upload(HeroTestRig.Photo())).Id); + var unused = Guid.Parse((await rig.Upload(HeroTestRig.Photo(seed: 7))).Id); + await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, used, "een")), HeroTestRig.Admin, CancellationToken.None); + var source = new HeroMediaMigrationSource(rig.Repository); + return (rig, (await source.ListAssetsAsync(CancellationToken.None)).ToList(), (await source.ListUsesAsync(CancellationToken.None)).ToList(), used, unused); + } + + private static HeroMediaUsageProvider Provider(HeroTestRig rig, IMediaLibraryReader? reader) => new(rig.Repository, rig.Images, reader, NullLogger.Instance); + + [Fact] + public async Task With_the_switch_off_the_heros_own_tables_answer_and_the_library_is_not_asked() + { + var (rig, assets, uses, used, _) = await ArrangeAsync(); + using var _r = rig; + assets.RemoveAll(a => a.Id == used); + + var photos = await Provider(rig, new FakeReader(MediaReadMode.Off, assets, uses, fails: true)).ListPhotosAsync(CancellationToken.None); + + photos.Should().HaveCount(2); + } + + [Fact] + public async Task On_answers_from_the_library_tables_with_the_thumbnail_of_the_hero() + { + var (rig, assets, uses, used, unused) = await ArrangeAsync(); + using var _r = rig; + // The library knows a file name the hero does not (proof that the answer comes from the library) and lacks the unused photo. + assets[assets.FindIndex(a => a.Id == used)] = assets.Single(a => a.Id == used) with { OriginalFileName = "uit-de-bibliotheek.jpg" }; + assets.RemoveAll(a => a.Id == unused); + var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses)); + + var photos = await provider.ListPhotosAsync(CancellationToken.None); + + photos.Should().ContainSingle().Which.Should().Match(p => p.Id == used && p.OriginalFileName == "uit-de-bibliotheek.jpg" && p.ThumbnailUrl != null && p.ThumbnailUrl.StartsWith("/media/hero/")); + (await provider.ListUsesAsync([used, unused], CancellationToken.None)).Should().ContainSingle().Which.Should().Match(u => u.PhotoId == used && u.Position == 1 && u.PlaceKind == "slide"); + } + + [Fact] + public async Task On_splits_the_library_into_photos_and_trash_by_the_deleted_date() + { + var (rig, assets, uses, used, unused) = await ArrangeAsync(); + using var _r = rig; + assets[assets.FindIndex(a => a.Id == unused)] = assets.Single(a => a.Id == unused) with { DeletedAt = DateTimeOffset.UtcNow }; + var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses)); + + (await provider.ListPhotosAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(used); + (await provider.ListTrashAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(unused); + } + + [Fact] + public async Task On_falls_back_to_the_heros_own_tables_when_the_library_cannot_be_read() + { + var (rig, assets, uses, _, _) = await ArrangeAsync(); + using var _r = rig; + var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses, fails: true)); + + (await provider.ListPhotosAsync(CancellationToken.None)).Should().HaveCount(2); + (await provider.ListUsesAsync([Guid.NewGuid()], CancellationToken.None)).Should().BeEmpty(); + (await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty(); + } + + [Fact] + public async Task Compare_still_answers_from_the_heros_tables_and_the_check_lists_every_difference() + { + var (rig, assets, uses, used, unused) = await ArrangeAsync(); + using var _r = rig; + assets.RemoveAll(a => a.Id == unused); + assets[assets.FindIndex(a => a.Id == used)] = assets.Single(a => a.Id == used) with { Bytes = 1 }; + uses[0] = uses[0] with { Position = 9 }; + var provider = Provider(rig, new FakeReader(MediaReadMode.Compare, assets, uses)); + + (await provider.ListPhotosAsync(CancellationToken.None)).Should().HaveCount(2, "compare never changes the answer"); + var check = await provider.CheckAsync(CancellationToken.None); + + check.Matches.Should().BeFalse(); + check.Mode.Should().Be(MediaReadMode.Compare); + check.Photos.Should().Be(2); + check.Uses.Should().Be(1); + check.Differences.Should().Contain(d => d.Contains(unused.ToString()) && d.Contains("missing in the library")); + check.Differences.Should().Contain(d => d.Contains(used.ToString()) && d.Contains("hash, key or size")); + check.Differences.Should().Contain(d => d.StartsWith("use ") && d.Contains("position")); + } + + [Fact] + public async Task The_check_agrees_when_the_library_is_a_mirror_of_the_hero() + { + var (rig, assets, uses, _, _) = await ArrangeAsync(); + using var _r = rig; + + var check = await Provider(rig, new FakeReader(MediaReadMode.Compare, assets, uses)).CheckAsync(CancellationToken.None); + + check.Matches.Should().BeTrue(); + check.Differences.Should().BeEmpty(); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroServiceTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroServiceTests.cs index ddf15ba..8949477 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroServiceTests.cs +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroServiceTests.cs @@ -447,6 +447,7 @@ public class HeroMediaCleanupTests public async Task AnImageNobodyUsed_IsRetiredAndRemoved_AfterTheRetentionTime() { using var rig = new HeroTestRig(); + rig.Media.AutoCleanupUnattached = true; // the default is off (the library keeps uploads); this tests the opt-in clean-up await rig.Upload(HeroTestRig.Photo()); await rig.Cleanup.RunOnceAsync(CancellationToken.None); diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroSlideCropTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroSlideCropTests.cs new file mode 100644 index 0000000..6b7ce58 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroSlideCropTests.cs @@ -0,0 +1,175 @@ +using FluentAssertions; +using SlpModularCms.Modules.Hero.Models; +using SlpModularCms.Modules.Hero.Services; +using Xunit; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// A crop per slide (phase 2, step B): the same photo can have another crop on each slide (Bryan, 2026-11-17). +public class HeroSlideCropTests +{ + private static HeroSaveRequest Request(params HeroSlideRequest[] slides) + => new("Titel", null, null, "slideshow", [], slides); + + private static async Task<(HeroTestRig Rig, Guid ImageId, Guid SlideA, Guid SlideB)> TwoSlidesOnOnePhotoAsync() + { + var rig = new HeroTestRig(); + var image = await rig.Upload(HeroTestRig.Photo()); + var imageId = Guid.Parse(image.Id); + var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, imageId, "een"), new HeroSlideRequest(null, imageId, "twee")), HeroTestRig.Admin, CancellationToken.None); + return (rig, imageId, Guid.Parse(saved.Slides[0].Id), Guid.Parse(saved.Slides[1].Id)); + } + + [Fact] + public async Task A_slide_without_a_crop_of_its_own_maps_exactly_like_its_photo() + { + var (rig, imageId, slideA, _) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + + var admin = await rig.Service.GetAsync(CancellationToken.None); + var photoSet = rig.Db.HeroImages.Single(i => i.Id == imageId).SetHash; + + admin.Slides.Should().OnlyContain(s => !s.OwnCrop); + var publicHero = (await rig.Public.GetActiveAsync(CancellationToken.None))!; + publicHero.Slides.Should().HaveCount(2); + publicHero.Slides.SelectMany(s => s.Image.Variants).Should().OnlyContain(v => v.Url.Contains(photoSet)); + slideA.Should().NotBeEmpty(); + } + + [Fact] + public async Task Two_slides_with_one_photo_and_different_crops_get_two_sets_and_two_addresses() + { + var (rig, imageId, slideA, slideB) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + var photoSet = rig.Db.HeroImages.Single(i => i.Id == imageId).SetHash; + + var first = await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.1, 0.2, 1.5)), HeroTestRig.Admin, CancellationToken.None); + first.Should().NotBeNull(); + first!.OwnCrop.Should().BeTrue(); + first.Image.Processing.Should().BeTrue("its own set still has to be cut"); + first.Image.Focus.Should().Be(new FocusDto(0.1, 0.2, 1.5)); + + // Until the slide's set is complete the photo's set stays live. + (await rig.Public.GetActiveAsync(CancellationToken.None))!.Slides.SelectMany(s => s.Image.Variants).Should().OnlyContain(v => v.Url.Contains(photoSet)); + + await rig.Images.SetSlideFocusAsync(slideB, new HeroFocusRequest(new FocusDto(0.8, 0.7, 1.2)), HeroTestRig.Admin, CancellationToken.None); + (await rig.RenderPending()).Should().Be(2); + + var slides = rig.Db.HeroSlides.OrderBy(s => s.SortOrder).ToList(); + slides[0].SetHash.Should().NotBeNullOrEmpty().And.NotBe(photoSet); + slides[1].SetHash.Should().NotBeNullOrEmpty().And.NotBe(photoSet).And.NotBe(slides[0].SetHash); + + var publicHero = (await rig.Public.GetActiveAsync(CancellationToken.None))!; + publicHero.Slides[0].Image.Variants.Should().OnlyContain(v => v.Url.Contains(slides[0].SetHash!)); + publicHero.Slides[1].Image.Variants.Should().OnlyContain(v => v.Url.Contains(slides[1].SetHash!)); + publicHero.Slides[0].Image.Focus.Should().Be(new FocusDto(0.1, 0.2, 1.5)); + rig.Db.HeroImages.Single(i => i.Id == imageId).SetHash.Should().Be(photoSet, "the photo's own set and crop are untouched"); + + var admin = await rig.Service.GetAsync(CancellationToken.None); + admin.Slides.Select(s => s.Image.Processing).Should().AllBeEquivalentTo(false); + admin.Slides.Select(s => s.Image.Focus).Should().BeEquivalentTo([new FocusDto(0.1, 0.2, 1.5), new FocusDto(0.8, 0.7, 1.2)], o => o.WithStrictOrdering()); + } + + [Fact] + public async Task The_same_crop_on_two_slides_shares_one_set_and_a_crop_equal_to_the_photos_shares_its_set() + { + var (rig, imageId, slideA, slideB) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + var photo = rig.Db.HeroImages.Single(i => i.Id == imageId); + var photoSet = photo.SetHash; + + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.3, 0.3, 1.3)), HeroTestRig.Admin, CancellationToken.None); + await rig.Images.SetSlideFocusAsync(slideB, new HeroFocusRequest(new FocusDto(0.3, 0.3, 1.3)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + var slides = rig.Db.HeroSlides.OrderBy(s => s.SortOrder).ToList(); + slides[0].SetHash.Should().Be(slides[1].SetHash, "the set hash is computed from the photo and the crop, so equal crops are one set"); + + // A crop equal to the photo's own crop needs no cutting at all: it shares the photo's set. + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(photo.FocusX, photo.FocusY, photo.Zoom)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + rig.Db.HeroSlides.Single(s => s.Id == slideA).SetHash.Should().Be(photoSet); + } + + [Fact] + public async Task The_old_set_of_a_slide_stays_live_until_the_new_one_is_complete() + { + var (rig, _, slideA, _) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.2, 0.2, 1.4)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + var firstSet = rig.Db.HeroSlides.Single(s => s.Id == slideA).SetHash; + + var again = await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.6, 0.6, 1.1)), HeroTestRig.Admin, CancellationToken.None); + + again!.Image.Processing.Should().BeTrue(); + rig.Db.HeroSlides.Single(s => s.Id == slideA).SetHash.Should().Be(firstSet); + (await rig.Public.GetActiveAsync(CancellationToken.None))!.Slides[0].Image.Variants.Should().OnlyContain(v => v.Url.Contains(firstSet!)); + + await rig.RenderPending(); + rig.Db.HeroSlides.Single(s => s.Id == slideA).SetHash.Should().NotBe(firstSet); + } + + [Fact] + public async Task Resetting_a_slide_uses_the_photos_crop_and_set_again() + { + var (rig, imageId, slideA, _) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + var photoSet = rig.Db.HeroImages.Single(i => i.Id == imageId).SetHash; + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.2, 0.2, 1.4)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + + var reset = await rig.Images.ResetSlideFocusAsync(slideA, HeroTestRig.Admin, CancellationToken.None); + + reset!.OwnCrop.Should().BeFalse(); + reset.Image.Processing.Should().BeFalse(); + var slide = rig.Db.HeroSlides.Single(s => s.Id == slideA); + slide.HasOwnCrop.Should().BeFalse(); + slide.SetHash.Should().BeNull(); + (await rig.Public.GetActiveAsync(CancellationToken.None))!.Slides[0].Image.Variants.Should().OnlyContain(v => v.Url.Contains(photoSet)); + } + + [Fact] + public async Task Saving_the_hero_keeps_the_crop_of_a_slide_and_another_photo_drops_it() + { + var (rig, imageId, slideA, slideB) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.2, 0.2, 1.4)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + var other = await rig.Upload(HeroTestRig.Photo(seed: 9)); + + await rig.Service.SaveAsync(Request(new HeroSlideRequest(slideA, imageId, "een, aangepast"), new HeroSlideRequest(slideB, Guid.Parse(other.Id), "twee")), HeroTestRig.Admin, CancellationToken.None); + rig.Db.HeroSlides.Single(s => s.Id == slideA).HasOwnCrop.Should().BeTrue("saving the texts does not touch the crop"); + + await rig.Service.SaveAsync(Request(new HeroSlideRequest(slideA, Guid.Parse(other.Id), "een"), new HeroSlideRequest(slideB, Guid.Parse(other.Id), "twee")), HeroTestRig.Admin, CancellationToken.None); + rig.Db.HeroSlides.Single(s => s.Id == slideA).HasOwnCrop.Should().BeFalse("a crop made for another photo means nothing"); + } + + [Fact] + public async Task An_unknown_slide_gives_null() + { + using var rig = new HeroTestRig(); + var request = new HeroFocusRequest(new FocusDto(0.5, 0.5, 1)); + + (await rig.Images.SetSlideFocusAsync(Guid.NewGuid(), request, HeroTestRig.Admin, CancellationToken.None)).Should().BeNull(); + (await rig.Images.ResetSlideFocusAsync(Guid.NewGuid(), HeroTestRig.Admin, CancellationToken.None)).Should().BeNull(); + } + + [Fact] + public async Task The_clean_up_keeps_the_set_of_a_slide_and_a_slide_set_nobody_uses_goes_after_the_retention() + { + var (rig, _, slideA, _) = await TwoSlidesOnOnePhotoAsync(); + using var _r = rig; + await rig.Images.SetSlideFocusAsync(slideA, new HeroFocusRequest(new FocusDto(0.2, 0.2, 1.4)), HeroTestRig.Admin, CancellationToken.None); + await rig.RenderPending(); + var slideSet = rig.Db.HeroSlides.Single(s => s.Id == slideA).SetHash!; + rig.Time.Now = rig.Time.Now.AddDays(30); + + await rig.Cleanup.RunOnceAsync(CancellationToken.None); + (await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Select(x => x.SetHash).Should().Contain(slideSet, "a slide uses it"); + + await rig.Images.ResetSlideFocusAsync(slideA, HeroTestRig.Admin, CancellationToken.None); + rig.Time.Now = rig.Time.Now.AddDays(30); + await rig.Cleanup.RunOnceAsync(CancellationToken.None); + (await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Select(x => x.SetHash).Should().NotContain(slideSet); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs index cb4a26a..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) + 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); - Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger.Instance, Time, IndexCache); + 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); + 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/Controllers/HeroControllers.cs b/src/SlpModularCms.Modules.Hero/Controllers/HeroControllers.cs index e1d05f9..625fb46 100644 --- a/src/SlpModularCms.Modules.Hero/Controllers/HeroControllers.cs +++ b/src/SlpModularCms.Modules.Hero/Controllers/HeroControllers.cs @@ -180,6 +180,37 @@ public class HeroImagesController(IHeroImageService service, IOptionsThe crop of one slide (crop per use). Administrators only. +[ApiController] +[Route("hero/slides")] +[Authorize(Policy = "AdminOnly")] +public class HeroSlidesController(IHeroImageService service) : ControllerBase +{ + /// + /// A crop for this slide only, applied at once without saving the hero. The same photo on another slide keeps its own + /// crop. Answers with the slide as the admin sees it (processing while its set is being cut). + /// + [HttpPut("{id:guid}/focus")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task SetFocus(Guid id, [FromBody] HeroFocusRequest request, CancellationToken cancellationToken) + { + var slide = await service.SetSlideFocusAsync(id, request, HeroCaller.Id(User), cancellationToken); + return slide is null ? NotFound(new { error = "not_found", message = HttpTexts.From(HttpContext).Get(HeroTexts.Image.NotFound) }) : Ok(slide); + } + + /// Takes the slide's own crop away: it uses the crop of its photo again. + [HttpDelete("{id:guid}/focus")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task ResetFocus(Guid id, CancellationToken cancellationToken) + { + var slide = await service.ResetSlideFocusAsync(id, HeroCaller.Id(User), cancellationToken); + return slide is null ? NotFound(new { error = "not_found", message = HttpTexts.From(HttpContext).Get(HeroTexts.Image.NotFound) }) : Ok(slide); + } +} + internal static class HeroCaller { public static Guid Id(ClaimsPrincipal user) diff --git a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs index 5c825b3..f5520c3 100644 --- a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs +++ b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs @@ -34,6 +34,34 @@ public class HeroSlide public bool Decorative { get; set; } public int SortOrder { get; set; } public bool IsActive { get; set; } = true; + + // The slide's OWN crop (phase 2, crop per use). All null = no override: the slide uses its photo's crop and set exactly + // as before. FocusX/FocusY/Zoom set = the slide has its own crop (the wide and tall overrides are optional on top). + public double? FocusX { get; set; } + public double? FocusY { get; set; } + public double? Zoom { get; set; } + 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; } + + /// The set cut for the slide's own crop; empty until it is complete (the photo's set stays live until then). + public string? SetHash { get; set; } + public List? Variants { get; set; } + + /// True when the slide has a crop of its own. + public bool HasOwnCrop => FocusX is not null && FocusY is not null; + + public void ClearOwnCrop() + { + (FocusX, FocusY, Zoom) = (null, null, null); + (WideFocusX, WideFocusY, WideZoom) = (null, null, null); + (TallFocusX, TallFocusY, TallZoom) = (null, null, null); + SetHash = null; + Variants = null; + } } /// A stored image: the original (never served), where the subject is, and the current set of variants. @@ -84,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 8bc1846..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(); @@ -44,6 +51,17 @@ public class HeroDbContext(DbContextOptions options) : DbContext( // Not unique: re-ordering updates several rows in one save and must not trip over itself. entity.HasIndex(e => new { e.HeroId, e.SortOrder }); entity.HasIndex(e => e.ImageId); + entity.Ignore(e => e.HasOwnCrop); + entity.Property(e => e.SetHash).HasMaxLength(16); + entity.Property(e => e.Variants) + .HasConversion( + variants => variants == null ? null : JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null), + json => json == null ? null : JsonSerializer.Deserialize>(json, (JsonSerializerOptions?)null)) + .HasColumnType("longtext") + .Metadata.SetValueComparer(new ValueComparer?>( + (a, b) => (a ?? new List()).SequenceEqual(b ?? new List()), + v => v == null ? 0 : v.Aggregate(0, (hash, item) => HashCode.Combine(hash, item.GetHashCode())), + v => v == null ? null : v.ToList())); // Restrict: an image that a slide still uses cannot be deleted (the service answers 409 first). entity.HasOne(e => e.Image).WithMany().HasForeignKey(e => e.ImageId).OnDelete(DeleteBehavior.Restrict); @@ -75,6 +93,8 @@ public class HeroDbContext(DbContextOptions options) : DbContext( v => v.ToList())); }); + ConfigureStorage(modelBuilder); + modelBuilder.Entity(entity => { entity.ToTable("HeroRetiredFiles"); @@ -84,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 d6db1a2..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) => @@ -45,7 +56,9 @@ public class HeroModule : IModule services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(provider => provider.GetRequiredService()); + services.AddScoped(provider => provider.GetRequiredService()); services.AddScoped(); services.AddScoped(); services.AddScoped(); @@ -61,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/20261117202757_AddSlideCrop.Designer.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117202757_AddSlideCrop.Designer.cs new file mode 100644 index 0000000..53eb562 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117202757_AddSlideCrop.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("20261117202757_AddSlideCrop")] + partial class AddSlideCrop + { + /// + 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/20261117202757_AddSlideCrop.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117202757_AddSlideCrop.cs new file mode 100644 index 0000000..966814d --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117202757_AddSlideCrop.cs @@ -0,0 +1,129 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SlpModularCms.Modules.Hero.Migrations +{ + /// + public partial class AddSlideCrop : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "FocusX", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "FocusY", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "SetHash", + table: "HeroSlides", + type: "varchar(16)", + maxLength: 16, + nullable: true); + + migrationBuilder.AddColumn( + name: "TallFocusX", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "TallFocusY", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "TallZoom", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "Variants", + table: "HeroSlides", + type: "longtext", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideFocusX", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideFocusY", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideZoom", + table: "HeroSlides", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "Zoom", + table: "HeroSlides", + type: "double", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "FocusX", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "FocusY", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "SetHash", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "TallFocusX", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "TallFocusY", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "TallZoom", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "Variants", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "WideFocusX", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "WideFocusY", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "WideZoom", + table: "HeroSlides"); + + migrationBuilder.DropColumn( + name: "Zoom", + table: "HeroSlides"); + } + } +} 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/Migrations/HeroDbContextModelSnapshot.cs b/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs index 822eb07..5803ae9 100644 --- a/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs +++ b/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs @@ -206,6 +206,12 @@ namespace SlpModularCms.Modules.Hero.Migrations b.Property("Decorative") .HasColumnType("tinyint(1)"); + b.Property("FocusX") + .HasColumnType("double"); + + b.Property("FocusY") + .HasColumnType("double"); + b.Property("HeroId") .HasColumnType("char(36)"); @@ -215,9 +221,37 @@ namespace SlpModularCms.Modules.Hero.Migrations 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"); diff --git a/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs b/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs index 1bf836b..b23941d 100644 --- a/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs +++ b/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs @@ -33,7 +33,8 @@ public record HeroImageDto( public record HeroButtonDto(string Label, string Target); -public record HeroSlideAdminDto(string Id, string ImageId, string Alt, bool Decorative, bool IsActive, int SortOrder, HeroImageDto Image); +/// A slide for the admin. is the photo as THIS slide shows it (its own crop and set when ); its Id is always the photo's id. +public record HeroSlideAdminDto(string Id, string ImageId, string Alt, bool Decorative, bool IsActive, int SortOrder, HeroImageDto Image, bool OwnCrop = false); /// What the admin edits. When no hero exists yet is null and the fields hold the defaults. public record HeroAdminDto( diff --git a/src/SlpModularCms.Modules.Hero/Repositories/HeroRepository.cs b/src/SlpModularCms.Modules.Hero/Repositories/HeroRepository.cs index 9aa01c1..d485d55 100644 --- a/src/SlpModularCms.Modules.Hero/Repositories/HeroRepository.cs +++ b/src/SlpModularCms.Modules.Hero/Repositories/HeroRepository.cs @@ -48,6 +48,15 @@ public interface IHeroRepository Task> GetImagesNeedingRenderAsync(CancellationToken cancellationToken); + /// One slide with its photo, tracked (a new crop for the slide); null when there is none or its photo is in the trash. + Task GetSlideAsync(Guid id, CancellationToken cancellationToken); + + /// The slide as stored right now, with its photo, not tracked. + Task GetSlideFreshAsync(Guid id, CancellationToken cancellationToken); + + /// Slides with a crop of their own whose set is missing or does not match that crop, in hero order (tracked). + Task> GetSlidesNeedingRenderAsync(CancellationToken cancellationToken); + /// Images with a complete set made before the full-frame preview existed (tracked). Task> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken); @@ -133,6 +142,18 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository public async Task> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken) => (await context.HeroImages.Where(i => i.DeletedAt == null).ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList(); + public Task GetSlideAsync(Guid id, CancellationToken cancellationToken) + => context.HeroSlides.Include(s => s.Image).FirstOrDefaultAsync(s => s.Id == id && s.Image != null && s.Image.DeletedAt == null, cancellationToken); + + public Task GetSlideFreshAsync(Guid id, CancellationToken cancellationToken) + => context.HeroSlides.AsNoTracking().Include(s => s.Image).FirstOrDefaultAsync(s => s.Id == id && s.Image != null && s.Image.DeletedAt == null, cancellationToken); + + public async Task> GetSlidesNeedingRenderAsync(CancellationToken cancellationToken) + => (await context.HeroSlides.Include(s => s.Image).Where(s => s.FocusX != null && s.FocusY != null).ToListAsync(cancellationToken)) + .Where(s => s.Image is { DeletedAt: null } && HeroMapping.SlideNeedsRender(s)) + .OrderBy(s => s.SortOrder) + .ToList(); + public Task GetImageFreshAsync(Guid id, CancellationToken cancellationToken) => context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null, cancellationToken); @@ -154,7 +175,12 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository } public async Task> GetReferencedSetHashesAsync(CancellationToken cancellationToken) - => (await context.HeroImages.Select(i => i.SetHash).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal); + { + // The sets of the photos and the own sets of slides with a crop of their own. + var sets = (await context.HeroImages.Select(i => i.SetHash).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal); + sets.UnionWith((await context.HeroSlides.Select(s => s.SetHash).ToListAsync(cancellationToken)).Where(h => !string.IsNullOrEmpty(h))!); + return sets; + } public async Task> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken) => (await context.HeroImages.Select(i => i.OriginalKey).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs index 80eca6d..4eed113 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs @@ -54,6 +54,16 @@ public interface IHeroImageService /// Task SetFocusAsync(Guid imageId, HeroFocusRequest request, Guid callerId, CancellationToken cancellationToken); + /// + /// A crop for ONE slide (crop per use): the same photo can have another crop on another slide. Applied at once, without + /// saving the hero; the slide's own set is cut in the background and the slide switches to it when it is complete (until + /// then it shows the photo's set). Returns the slide as the admin sees it (processing), or null when there is no such slide. + /// + Task SetSlideFocusAsync(Guid slideId, HeroFocusRequest request, Guid callerId, CancellationToken cancellationToken); + + /// Takes the slide's own crop away: it uses the photo's crop and set again. Null when there is no such slide. + Task ResetSlideFocusAsync(Guid slideId, Guid callerId, CancellationToken cancellationToken); + /// One image as the admin sees it now (including whether it is still being prepared). Null when there is no such image. Task GetImageAsync(Guid imageId, CancellationToken cancellationToken); @@ -103,8 +113,24 @@ public class HeroImageService( ILogger logger, TimeProvider? time = null, IIndexHtmlCache? indexCache = null, - HeroRenderSignal? signal = null) : IHeroImageService + HeroRenderSignal? signal = null, + 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 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; public async Task UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null) @@ -127,6 +153,7 @@ public class HeroImageService( if (existing is not null && await FilesExistAsync(existing, cancellationToken)) { await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); // The same photo again also gives up the failed state: the renderer tries it again when it is still pending. signal?.Reset(existing.Id); signal?.Notify(); @@ -166,6 +193,7 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); signal?.Notify(); logger.LogInformation("Hero image {ImageId} {Action} by user {UserId} ({Width}x{Height}); its variants are cut in the background", image.Id, existing is null ? "uploaded" : "repaired", callerId, image.Width, image.Height); @@ -212,6 +240,7 @@ public class HeroImageService( ApplyFocus(image, request); await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); signal?.Reset(imageId); signal?.Notify(); @@ -219,6 +248,69 @@ public class HeroImageService( return await GetImageAsync(imageId, cancellationToken); } + public async Task SetSlideFocusAsync(Guid slideId, HeroFocusRequest request, Guid callerId, CancellationToken cancellationToken) + { + var slide = await repository.GetSlideAsync(slideId, cancellationToken); + if (slide is null) + { + return null; + } + + var general = request.Focus.ToSetting().Normalize(); + (slide.FocusX, slide.FocusY, slide.Zoom) = (general.X, general.Y, general.Zoom); + Apply(request.Wide, v => (slide.WideFocusX, slide.WideFocusY, slide.WideZoom) = v); + Apply(request.Tall, v => (slide.TallFocusX, slide.TallFocusY, slide.TallZoom) = v); + await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); + signal?.Reset(slideId); + signal?.Notify(); + + logger.LogInformation("Hero slide {SlideId} got a crop of its own from user {UserId}; its variants are cut in the background", slideId, callerId); + return await GetSlideAsync(slideId, cancellationToken); + } + + public async Task ResetSlideFocusAsync(Guid slideId, Guid callerId, CancellationToken cancellationToken) + { + var slide = await repository.GetSlideAsync(slideId, cancellationToken); + if (slide is null) + { + return null; + } + + var oldSet = slide.SetHash; + slide.ClearOwnCrop(); + if (!string.IsNullOrEmpty(oldSet) && oldSet != slide.Image!.SetHash) + { + repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = oldSet, RetiredAt = _time.GetUtcNow() }); + } + + await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); + signal?.Reset(slideId); + indexCache?.Invalidate(); + + logger.LogInformation("Hero slide {SlideId} uses the crop of its photo again (reset by user {UserId})", slideId, callerId); + return await GetSlideAsync(slideId, cancellationToken); + } + + private async Task GetSlideAsync(Guid slideId, CancellationToken cancellationToken) + { + var slide = await repository.GetSlideFreshAsync(slideId, cancellationToken); + if (slide is null) + { + return null; + } + + var dto = HeroMapping.ToSlideAdmin(slide); + if (!slide.HasOwnCrop) + { + return dto; + } + + var progress = RenderProgress(slideId); + return dto with { Image = dto.Image with { Failed = IsRenderFailed(slideId), RenderDone = progress?.Done, RenderTotal = progress?.Total } }; + } + public async Task GetImageAsync(Guid imageId, CancellationToken cancellationToken) { var image = await repository.GetImageFreshAsync(imageId, cancellationToken); @@ -238,19 +330,43 @@ public class HeroImageService( public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken) { - _ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException(); + // The id is a photo's or the id of a slide with a crop of its own (both are renderer keys). + if (await repository.GetImageAsync(imageId, cancellationToken) is null && await repository.GetSlideAsync(imageId, cancellationToken) is null) + { + throw new HeroImageNotFoundException(); + } + signal?.Reset(imageId); signal?.Notify(); } public async Task<(int Total, int NeedRender, int NeedPreview)> GetRenderBacklogAsync(CancellationToken cancellationToken) - => (await repository.CountImagesAsync(cancellationToken), (await repository.GetImagesNeedingRenderAsync(cancellationToken)).Count, (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).Count); + => (await repository.CountImagesAsync(cancellationToken), (await repository.GetImagesNeedingRenderAsync(cancellationToken)).Count + (await repository.GetSlidesNeedingRenderAsync(cancellationToken)).Count, (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).Count); public async Task RenderNextPendingAsync(IReadOnlySet skip, CancellationToken cancellationToken) { var image = (await repository.GetImagesNeedingRenderAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id)); if (image is null) { + // Then the slides that have a crop of their own. + var slide = (await repository.GetSlidesNeedingRenderAsync(cancellationToken)).FirstOrDefault(s => !skip.Contains(s.Id)); + if (slide is not null) + { + try + { + return await RenderSlideAsync(slide, cancellationToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger.LogError(exception, "Hero slide {SlideId}: cutting the variants failed.", slide.Id); + throw new HeroImageRenderException(slide.Id, exception); + } + finally + { + signal?.ClearProgress(slide.Id); + } + } + // Nothing to cut: add the full-frame preview to complete sets that were made before it existed. var old = (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id)); return old is null ? null : await AddPreviewAsync(old, cancellationToken); @@ -305,6 +421,66 @@ public class HeroImageService( } } + /// Cuts the set of a slide's own crop. A crop equal to the photo's own crop shares the photo's set (same hash, no cutting). + private async Task RenderSlideAsync(HeroSlide slide, CancellationToken cancellationToken) + { + var photo = slide.Image!; + var effective = HeroMapping.Effective(slide)!; + var target = HeroMapping.TargetSetHash(effective); + var oldSet = slide.SetHash ?? string.Empty; + + if (target == photo.SetHash && photo.Variants.Count > 0) + { + slide.SetHash = target; + slide.Variants = [.. photo.Variants]; + await repository.SaveChangesAsync(cancellationToken); + indexCache?.Invalidate(); + logger.LogInformation("Hero slide {SlideId}: its crop equals the crop of its photo, so it shares the photo's set.", slide.Id); + return slide.Id; + } + + var watch = System.Diagnostics.Stopwatch.StartNew(); + logger.LogInformation("Hero slide {SlideId}: cutting the variants of its own crop starts ({Width}x{Height}).", slide.Id, photo.Width, photo.Height); + Dictionary files; + List variants; + using (await gate.EnterAsync(cancellationToken)) + { + var file = await store.OpenReadAsync(photo.OriginalKey, cancellationToken) + ?? throw new ImageRejectedException(ImageRejection.Unavailable, "The stored original is missing."); + using var memory = new MemoryStream(); + await using (file.Content) + { + await file.Content.CopyToAsync(memory, cancellationToken); + } + + var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(photo.OriginalKey).TrimStart('.')); + (files, variants) = await RenderSetAsync(effective, memory.ToArray(), storedFormat, (done, total) => signal?.SetProgress(slide.Id, done, total), cancellationToken); + } + + await store.PutSetAsync(HeroMapping.Area, target, files, cancellationToken); + + // The crop may have changed (or the slide may be gone) while the set was cut: then it stays pending at the newest crop. + var current = await repository.GetSlideFreshAsync(slide.Id, cancellationToken); + if (current is null || !current.HasOwnCrop || HeroMapping.TargetSetHash(HeroMapping.Effective(current)!) != target) + { + logger.LogInformation("Hero slide {SlideId}: recut superseded, its crop changed while its variants were being cut.", slide.Id); + return slide.Id; + } + + slide.SetHash = target; + slide.Variants = variants; + if (oldSet.Length > 0 && oldSet != target) + { + repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = oldSet, RetiredAt = _time.GetUtcNow() }); + } + + await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); + indexCache?.Invalidate(); + logger.LogInformation("Hero slide {SlideId}: {Variants} variants cut in {Seconds:0.0} s", slide.Id, variants.Count, watch.Elapsed.TotalSeconds); + return slide.Id; + } + private async Task RenderAsync(HeroImage image, CancellationToken cancellationToken) { @@ -353,6 +529,7 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); indexCache?.Invalidate(); logger.LogInformation("Hero image {ImageId}: {Variants} variants cut in {Seconds:0.0} s", image.Id, variants.Count, watch.Elapsed.TotalSeconds); return image.Id; @@ -371,6 +548,7 @@ public class HeroImageService( repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = now }); repository.RemoveImage(image); await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); logger.LogInformation("Hero image {ImageId} removed by user {UserId}", imageId, callerId); } @@ -394,6 +572,7 @@ public class HeroImageService( image.DeletedAt = now; await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); indexCache?.Invalidate(); logger.LogInformation("Hero image {ImageId} moved to the trash by user {UserId} ({Slides} slides removed)", imageId, callerId, slides.Count); @@ -418,6 +597,7 @@ public class HeroImageService( var (setHash, originalKey) = (image.SetHash, image.OriginalKey); repository.RemoveImage(image); await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); if (setHash.Length > 0 && !setShared) { @@ -444,6 +624,7 @@ public class HeroImageService( image.DeletedAt = null; await repository.SaveChangesAsync(cancellationToken); + await SyncLibraryAsync(); signal?.Notify(); logger.LogInformation("Hero image {ImageId} restored from the trash by user {UserId}", imageId, callerId); 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/HeroMapping.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMapping.cs index f32ed56..159c96e 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMapping.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMapping.cs @@ -47,6 +47,49 @@ public static class HeroMapping _ => format.ToString().ToUpperInvariant(), }; + /// + /// The photo as a slide shows it: the photo itself when the slide has no crop of its own, else a copy of the photo with the + /// slide's crop and, once complete, the slide's own set (until then the photo's set stays live). Never saved. + /// + public static HeroImage? Effective(HeroSlide slide) + { + var photo = slide.Image; + if (photo is null || !slide.HasOwnCrop) + { + return photo; + } + + var ready = slide.Variants is { Count: > 0 } && !string.IsNullOrEmpty(slide.SetHash); + return new HeroImage + { + Id = photo.Id, + OriginalSha256 = photo.OriginalSha256, + OriginalKey = photo.OriginalKey, + SourceFormat = photo.SourceFormat, + Width = photo.Width, + Height = photo.Height, + Bytes = photo.Bytes, + CreatedAt = photo.CreatedAt, + CreatedByUserId = photo.CreatedByUserId, + FocusX = slide.FocusX!.Value, + FocusY = slide.FocusY!.Value, + Zoom = slide.Zoom ?? 1.0, + WideFocusX = slide.WideFocusX, + WideFocusY = slide.WideFocusY, + WideZoom = slide.WideZoom, + TallFocusX = slide.TallFocusX, + TallFocusY = slide.TallFocusY, + TallZoom = slide.TallZoom, + SetHash = ready ? slide.SetHash! : photo.SetHash, + Variants = ready ? slide.Variants! : photo.Variants, + }; + } + + /// True when the slide has its own crop and its own set is missing or was cut for another crop (the work of the renderer). + public static bool SlideNeedsRender(HeroSlide slide) + => slide.HasOwnCrop && slide.Image is not null && Effective(slide) is { } effective + && (slide.Variants is not { Count: > 0 } || slide.SetHash != TargetSetHash(effective)); + /// The variants websites and the admin lists use: everything except the full-frame preview. public static IEnumerable Published(HeroImage image) => image.Variants.Where(v => v.Ratio != VariantPlanner.PreviewRatioName); @@ -121,10 +164,11 @@ public static class HeroMapping } var slides = hero.Slides - .Where(s => s.IsActive && s.Image is { Variants.Count: > 0 } && (s.Decorative || !string.IsNullOrWhiteSpace(s.Alt))) - .OrderBy(s => s.SortOrder) + .Select(s => (Slide: s, Image: Effective(s))) + .Where(x => x.Slide.IsActive && x.Image is { Variants.Count: > 0 } && (x.Slide.Decorative || !string.IsNullOrWhiteSpace(x.Slide.Alt))) + .OrderBy(x => x.Slide.SortOrder) .Take(Math.Max(1, maxPublishedSlides)) - .Select(s => new PublicSlideDto(s.Id.ToString(), s.Decorative ? string.Empty : s.Alt, s.Decorative, ToPublicImage(s.Image!))) + .Select(x => new PublicSlideDto(x.Slide.Id.ToString(), x.Slide.Decorative ? string.Empty : x.Slide.Alt, x.Slide.Decorative, ToPublicImage(x.Image!))) .ToList(); return new PublicHeroDto(hero.Display, hero.Title, hero.Tagline, hero.Lead, Buttons(hero), slides); @@ -152,6 +196,10 @@ public static class HeroMapping byRatio); } + /// One slide for the admin, with the photo as the slide shows it. + public static HeroSlideAdminDto ToSlideAdmin(HeroSlide s) + => new(s.Id.ToString(), s.ImageId.ToString(), s.Alt, s.Decorative, s.IsActive, s.SortOrder, ToImageDto(Effective(s)!), s.HasOwnCrop); + public static HeroAdminDto ToAdmin(HeroContent? hero, DateTimeOffset now, int maxPublishedSlides) { if (hero is null) @@ -162,7 +210,7 @@ public static class HeroMapping var slides = hero.Slides .Where(s => s.Image is not null) .OrderBy(s => s.SortOrder) - .Select(s => new HeroSlideAdminDto(s.Id.ToString(), s.ImageId.ToString(), s.Alt, s.Decorative, s.IsActive, s.SortOrder, ToImageDto(s.Image!))) + .Select(s => new HeroSlideAdminDto(s.Id.ToString(), s.ImageId.ToString(), s.Alt, s.Decorative, s.IsActive, s.SortOrder, ToImageDto(Effective(s)!), s.HasOwnCrop)) .ToList(); return new HeroAdminDto( diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs index 024c6f6..0ddde60 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs @@ -18,7 +18,9 @@ public class HeroMediaCleanup( IMediaStore store, IOptions options, ILogger logger, - TimeProvider? time = null) + TimeProvider? time = null, + SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, + IHeroLegacyImageMirror? legacyMirror = null) { private readonly TimeProvider _time = time ?? TimeProvider.System; @@ -42,14 +44,25 @@ public class HeroMediaCleanup( // 1b. Photos that have been in the trash for the trash period: their files go now (they are retired, immediately due). var trashCutoff = now.AddDays(-Math.Max(0, options.Value.TrashDays)); + var trashedGone = false; foreach (var image in await repository.GetTrashedBeforeAsync(trashCutoff, cancellationToken)) { + trashedGone = true; repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) }); repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) }); repository.RemoveImage(image); } 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); + } // 2. Retired files whose retention has passed, unless an image uses them again. var referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs index 141b4ec..fd2adcc 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs @@ -26,8 +26,22 @@ public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrat for (var index = 0; index < ordered.Count; index++) { var slide = ordered[index]; - var setHash = images.TryGetValue(slide.ImageId, out var image) ? image.SetHash : string.Empty; - uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash)); + var setHash = string.Empty; + IReadOnlyList? crops = null; + if (images.TryGetValue(slide.ImageId, out var image)) + { + // The slide as it shows the photo: its own crop and set when it has them (crop per use), else the photo's. + slide.Image = image; + var shown = HeroMapping.Effective(slide) ?? image; + setHash = shown.SetHash; + if (slide.HasOwnCrop) + { + crops = [Crop("wide", shown), Crop("tall", shown)]; + } + } + + var variantsJson = slide.HasOwnCrop && slide.Variants is { Count: > 0 } ? Json(slide.Variants) : null; + uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash, crops, variantsJson)); } } @@ -53,7 +67,17 @@ public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrat i.CreatedByUserId, i.OriginalFileName, i.LastUsedAt, - i.DeletedAt); + i.DeletedAt, + Json(i.Variants), + i.WideFocusX, + i.WideFocusY, + i.WideZoom, + i.TallFocusX, + i.TallFocusY, + i.TallZoom); + + /// The variants as the hero stores them (the same serializer as its database column), so they can go back unchanged. + public static string Json(IEnumerable variants) => System.Text.Json.JsonSerializer.Serialize(variants.ToList(), (System.Text.Json.JsonSerializerOptions?)null); private static MediaCropSnapshot Crop(string ratio, HeroImage image) { diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs index 86f96de..e1137f7 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Logging; using SlpModularCms.Core.Media; using SlpModularCms.Core.Media.Library; using SlpModularCms.Modules.Hero.Data.Entities; @@ -9,21 +10,59 @@ namespace SlpModularCms.Modules.Hero.Services; /// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the /// hero), and moves them to the trash and back for it. The files are removed by after the trash period. /// -public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider +public class HeroMediaUsageProvider( + IHeroRepository repository, + IHeroImageService images, + IMediaLibraryReader? reader = null, + ILogger? logger = null) : IMediaUsageProvider, IMediaReadVerifier { public const string SlideKind = "slide"; public string Module => HeroMapping.Area; + private MediaReadMode Mode => reader?.Mode ?? MediaReadMode.Off; + public async Task> ListPhotosAsync(CancellationToken cancellationToken) - => (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); + { + if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: false, cancellationToken) is { } fromLibrary) + { + return fromLibrary; + } + + await CompareInTheLogAsync(cancellationToken); + return (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); + } public async Task> ListTrashAsync(CancellationToken cancellationToken) - => (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); + { + if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: true, cancellationToken) is { } fromLibrary) + { + return fromLibrary; + } + + return (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); + } public async Task> ListUsesAsync(IReadOnlyCollection photoIds, CancellationToken cancellationToken) { var wanted = photoIds.ToHashSet(); + if (Mode == MediaReadMode.On) + { + try + { + return (await reader!.ListUsesAsync(Module, cancellationToken)) + .Where(u => wanted.Contains(u.AssetId)) + .GroupBy(u => u.AssetId) + .SelectMany(g => g.OrderBy(u => u.Position)) + .Select(u => new MediaUse(Module, u.AssetId, u.PlaceKind, u.Position, u.Active)) + .ToList(); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger?.LogError(exception, "Reading the hero uses from the media library failed; the hero's own tables are used instead."); + } + } + var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken); // The position of a slide is its place in its hero, in display order (1-based), also for slides that are off. @@ -44,6 +83,64 @@ public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageServic return uses; } + /// + /// The photos of the library tables (identity, size, name, dates), with the thumbnail from the hero (the variants are not in + /// the library tables until phase 3). Null when the library cannot be read: the hero's own tables are used then. + /// + private async Task?> TryReadPhotosAsync(bool trash, CancellationToken cancellationToken) + { + try + { + var assets = (await reader!.ListAssetsAsync(Module, cancellationToken)).Where(a => (a.DeletedAt is not null) == trash).ToList(); + var own = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id); + var photos = assets.Select(a => new MediaPhoto( + Module, a.Id, a.Bytes, a.Width, a.Height, a.CreatedAt, own.TryGetValue(a.Id, out var image) ? Thumbnail(image) : null, a.OriginalFileName, a.LastUsedAt, a.DeletedAt)); + return (trash ? photos.OrderByDescending(p => p.DeletedAt) : photos.OrderByDescending(p => p.CreatedAt)).ToList(); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger?.LogError(exception, "Reading the hero photos from the media library failed; the hero's own tables are used instead."); + return null; + } + } + + /// In compare mode: every difference between the hero's tables and the library tables is a warning in the log. + private async Task CompareInTheLogAsync(CancellationToken cancellationToken) + { + if (Mode != MediaReadMode.Compare) + { + return; + } + + try + { + var check = await CheckAsync(cancellationToken); + foreach (var difference in check.Differences.Take(20)) + { + logger?.LogWarning("Media library read check (hero): {Difference}", difference); + } + + if (check.Differences.Count > 20) + { + logger?.LogWarning("Media library read check (hero): {More} more differences not listed.", check.Differences.Count - 20); + } + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger?.LogError(exception, "The media library read check of the hero failed."); + } + } + + public async Task CheckAsync(CancellationToken cancellationToken) + { + var source = new HeroMediaMigrationSource(repository); + var ownAssets = await source.ListAssetsAsync(cancellationToken); + var ownUses = await source.ListUsesAsync(cancellationToken); + var libraryAssets = await reader!.ListAssetsAsync(Module, cancellationToken); + var libraryUses = await reader.ListUsesAsync(Module, cancellationToken); + return new MediaReadCheck(Module, Mode, ownAssets.Count, ownUses.Count, MediaReadComparer.Compare(ownAssets, ownUses, libraryAssets, libraryUses)); + } + public async Task TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken) { try diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs index 162b691..5245edf 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs @@ -30,7 +30,9 @@ public class HeroService( ILogger logger, TimeProvider? time = null, IIndexHtmlCache? indexCache = null, - ITextCatalog? texts = null) : IHeroService + ITextCatalog? texts = null, + 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; @@ -44,7 +46,8 @@ public class HeroService( { Slides = dto.Slides.Select(s => { - var id = Guid.Parse(s.ImageId); + // A slide with its own crop is cut under its own id (its progress and failures are the slide's, not the photo's). + var id = s.OwnCrop ? Guid.Parse(s.Id) : Guid.Parse(s.ImageId); var progress = imageService.RenderProgress(id); var failed = imageService.IsRenderFailed(id); return failed || progress is not null @@ -113,6 +116,12 @@ public class HeroService( } keep.Add(slide.Id); + if (slide.ImageId != r.ImageId && slide.ImageId != Guid.Empty) + { + // Another photo on the slide: a crop made for the old photo means nothing for the new one. + slide.ClearOwnCrop(); + } + slide.ImageId = r.ImageId; slide.Image = images[r.ImageId]; slide.Decorative = r.Decorative; @@ -148,6 +157,16 @@ 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); + } + logger.LogInformation("Hero {HeroId} {Action} by user {LastModifiedByUserId} ({Slides} slides)", hero.Id, created ? "Created" : "Updated", callerId, keep.Count); return WithFailures(HeroMapping.ToAdmin(hero, now, options.Value.MaxPublishedSlides)); diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs index 700656a..234415f 100644 --- a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs +++ b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs @@ -112,4 +112,19 @@ public class MediaLibraryCopierTests : IDisposable report.Assets.Should().Be(2); report.Uses.Should().Be(1); } + [Fact] + public async Task Copies_and_verifies_the_variants_the_set_hash_and_the_crop_overrides_of_a_photo_and_of_a_use() + { + var variants = "[{\"Ratio\":\"wide\",\"Width\":480,\"Height\":270,\"Type\":\"image/webp\",\"FileName\":\"wide-480.webp\",\"Bytes\":1234}]"; + var a = Asset() with { VariantsJson = variants, WideFocusX = 0.1, WideFocusY = 0.2, WideZoom = 1.3, TallFocusX = 0.7 }; + var use = Use(a) with { VariantsJson = variants, SetHash = "fedcba9876543210" }; + + await Copier(new Source("hero", [a], [use])).CopyAsync(CancellationToken.None); + + var photo = await _db.MediaAssets.SingleAsync(); + photo.Variants.Should().Be(variants); + photo.SetHash.Should().Be(a.SetHash); + (photo.WideFocusX, photo.WideFocusY, photo.WideZoom, photo.TallFocusX, photo.TallFocusY).Should().Be((0.1, 0.2, 1.3, 0.7, null)); + (await _db.MediaUses.SingleAsync()).Variants.Should().Be(variants); + } } diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs new file mode 100644 index 0000000..8a71e44 --- /dev/null +++ b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs @@ -0,0 +1,85 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Media.Library; +using SlpModularCms.Modules.Media.Data; +using SlpModularCms.Modules.Media.Services; + +namespace SlpModularCms.Modules.Media.Tests; + +/// Phase 2, step C: reading the library tables back, per module, and the mode from the setting. +public class MediaLibraryReaderTests : IDisposable +{ + private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); + private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-read-" + Guid.NewGuid()).Options); + + public void Dispose() => _db.Dispose(); + + private sealed class Source(string module, List assets, List uses) : IMediaMigrationSource + { + public string Module => module; + + public Task> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult>(assets); + + public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses); + } + + private static MediaAssetSnapshot Asset(string module) + => new(Guid.NewGuid(), module, Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.5, 0.5, 1, "0123456789abcdef", + [new MediaCropSnapshot("wide", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), null); + + [Fact] + public async Task Reads_back_what_was_mirrored_for_one_module_and_agrees_with_the_comparer() + { + var hero = Asset("hero"); + var other = Asset("gallery"); + var use = new MediaUseSnapshot(Guid.NewGuid(), hero.Id, "hero", "slide", 2, false, "fedcba9876543210"); + await new MediaLibraryCopier(_db, [new Source("hero", [hero], [use]), new Source("gallery", [other], [])], NullLogger.Instance).CopyAsync(CancellationToken.None); + var reader = new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions { ReadFromLibrary = MediaReadMode.Compare })); + + var assets = await reader.ListAssetsAsync("hero", CancellationToken.None); + var uses = await reader.ListUsesAsync("hero", CancellationToken.None); + + reader.Mode.Should().Be(MediaReadMode.Compare); + assets.Should().ContainSingle().Which.Should().Match(a => a.Id == hero.Id && a.OriginalFileName == "zomer.jpg" && a.Bytes == 2_000_000); + uses.Should().ContainSingle().Which.Should().Match(u => u.Id == use.Id && u.Position == 2 && !u.Active && u.SetHash == "fedcba9876543210"); + MediaReadComparer.Compare([hero], [use], assets, uses).Should().BeEmpty(); + } + + [Fact] + public void The_mode_is_off_by_default() + => new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions())).Mode.Should().Be(MediaReadMode.Off); + + [Fact] + public void The_comparer_reports_missing_extra_and_changed_rows() + { + var a = Asset("hero"); + var b = Asset("hero"); + var useA = new MediaUseSnapshot(Guid.NewGuid(), a.Id, "hero", "slide", 1, true, "aaaa"); + var ghost = new MediaUseSnapshot(Guid.NewGuid(), a.Id, "hero", "slide", 2, true, "aaaa"); + + var differences = MediaReadComparer.Compare([a, b], [useA], [a with { Bytes = 5 }, Asset("hero")], [useA with { Active = false }, ghost]); + + differences.Should().Contain(d => d.Contains(b.Id.ToString()) && d.Contains("missing in the library")); + differences.Should().Contain(d => d.Contains(a.Id.ToString()) && d.Contains("hash, key or size")); + differences.Should().Contain(d => d.Contains("in the library but not in the module")); + differences.Should().Contain(d => d.StartsWith("use ") && d.Contains("differs")); + differences.Should().Contain(d => d.Contains(ghost.Id.ToString())); + } + [Fact] + public async Task Reads_back_the_variants_and_set_hash_and_the_comparer_notices_a_difference_in_them() + { + var hero = Asset("hero") with { VariantsJson = "[{\"Ratio\":\"wide\"}]", WideFocusX = 0.3 }; + await new MediaLibraryCopier(_db, [new Source("hero", [hero], [])], NullLogger.Instance).CopyAsync(CancellationToken.None); + var reader = new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions())); + + var back = (await reader.ListAssetsAsync("hero", CancellationToken.None)).Single(); + + back.VariantsJson.Should().Be(hero.VariantsJson); + back.SetHash.Should().Be(hero.SetHash); + back.WideFocusX.Should().Be(0.3); + MediaReadComparer.Compare([hero], [], [back], []).Should().BeEmpty(); + MediaReadComparer.Compare([hero with { VariantsJson = "[]" }], [], [back], []).Should().Contain(d => d.Contains("set hash or variants differ")); + } +} diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs new file mode 100644 index 0000000..a2e2b6a --- /dev/null +++ b/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs @@ -0,0 +1,85 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Media.Library; +using SlpModularCms.Modules.Media.Data; +using SlpModularCms.Modules.Media.Services; + +namespace SlpModularCms.Modules.Media.Tests; + +/// Phase 2, step A: the write-through mirrors one module into the library tables, only when it is switched on, and never throws. +public class MediaLibrarySyncTests : IDisposable +{ + private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); + private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-sync-" + Guid.NewGuid()).Options); + + public void Dispose() => _db.Dispose(); + + private sealed class Source(string module, Func> assets, Func> uses, bool fails = false) : IMediaMigrationSource + { + public string Module => module; + + public Task> ListAssetsAsync(CancellationToken cancellationToken) + => fails ? throw new InvalidOperationException("source failed") : Task.FromResult>(assets()); + + public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses()); + } + + private static MediaAssetSnapshot Asset(string module) + => new(Guid.NewGuid(), module, Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.5, 0.5, 1, "0123456789abcdef", + [new MediaCropSnapshot("wide", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", null, null); + + private MediaLibrarySync Sync(bool on, params IMediaMigrationSource[] sources) + => new(new MediaLibraryCopier(_db, sources, NullLogger.Instance), Options.Create(new MediaLibraryOptions { DualWrite = on }), NullLogger.Instance); + + [Fact] + public async Task Does_nothing_while_the_switch_is_off() + { + var a = Asset("hero"); + + await Sync(false, new Source("hero", () => [a], () => [])).SyncAsync("hero", CancellationToken.None); + + (await _db.MediaAssets.CountAsync()).Should().Be(0); + } + + [Fact] + public async Task Mirrors_only_the_given_module_and_follows_the_changes_of_the_source() + { + var hero = Asset("hero"); + var other = Asset("gallery"); + var heroAssets = new List { hero }; + var sync = Sync(true, new Source("hero", () => heroAssets.ToList(), () => []), new Source("gallery", () => [other], () => [])); + + await sync.SyncAsync("hero", CancellationToken.None); + (await _db.MediaAssets.Select(a => a.Module).ToListAsync()).Should().Equal("hero"); + + var second = Asset("hero"); + heroAssets.Add(second); + heroAssets.Remove(hero); + await sync.SyncAsync("hero", CancellationToken.None); + + (await _db.MediaAssets.Select(a => a.Id).ToListAsync()).Should().Equal(second.Id); + } + + [Fact] + public async Task Stores_the_crops_of_a_use_when_the_source_gives_them() + { + var hero = Asset("hero"); + var use = new MediaUseSnapshot(Guid.NewGuid(), hero.Id, "hero", "slide", 1, true, "fedcba9876543210", [new MediaCropSnapshot("wide", 0.1, 0.9, 1.4)]); + + await Sync(true, new Source("hero", () => [hero], () => [use])).SyncAsync("hero", CancellationToken.None); + + var row = await _db.MediaUses.SingleAsync(); + row.SetHash.Should().Be("fedcba9876543210"); + row.Crops.Should().Contain("0.9").And.Contain("1.4"); + } + + [Fact] + public async Task Never_throws_when_the_source_fails() + { + var act = () => Sync(true, new Source("hero", () => [], () => [], fails: true)).SyncAsync("hero", CancellationToken.None); + + await act.Should().NotThrowAsync(); + } +} diff --git a/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs b/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs index b99fd79..00df410 100644 --- a/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs +++ b/src/SlpModularCms.Modules.Media/Data/Entities/MediaEntities.cs @@ -24,6 +24,17 @@ public class MediaAsset public double DefaultFocusY { get; set; } = 0.5; public double DefaultZoom { get; set; } = 1.0; + // Phase 3: what the owning module needs to publish the photo from here. The overrides of the photo's crop per ratio + // (null = the default crop), the hash of the current set and its variants as the module's own JSON. + 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; } + public string? Variants { get; set; } + public DateTimeOffset CreatedAt { get; set; } public Guid CreatedByUserId { get; set; } public string? OriginalFileName { get; set; } @@ -56,4 +67,7 @@ public class MediaAssetUse /// The crops of this use as JSON: [{ratio, x, y, zoom}]. Empty for a natural-ratio use. public string Crops { get; set; } = "[]"; + + /// Phase 3: the variants of this use's own set as the module's own JSON (null = the photo's). + public string? Variants { get; set; } } diff --git a/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs b/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs index 3f85b1d..7ac4c60 100644 --- a/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs +++ b/src/SlpModularCms.Modules.Media/Data/MediaDbContext.cs @@ -22,6 +22,8 @@ public class MediaDbContext(DbContextOptions options) : DbContex entity.Property(e => e.OriginalKey).IsRequired().HasMaxLength(200); entity.Property(e => e.SourceFormat).IsRequired().HasMaxLength(10); entity.Property(e => e.OriginalFileName).HasMaxLength(200); + entity.Property(e => e.SetHash).HasMaxLength(16); + entity.Property(e => e.Variants).HasColumnType("longtext"); entity.HasIndex(e => e.CreatedAt); entity.HasMany(e => e.Uses).WithOne(u => u.Asset).HasForeignKey(u => u.AssetId).OnDelete(DeleteBehavior.Cascade); }); @@ -34,6 +36,7 @@ public class MediaDbContext(DbContextOptions options) : DbContex 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.Property(e => e.Variants).HasColumnType("longtext"); entity.HasIndex(e => e.AssetId); }); } diff --git a/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs b/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs index 8e2a65a..aaa7826 100644 --- a/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs +++ b/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs @@ -10,4 +10,17 @@ public class MediaLibraryOptions /// (Media__Library__CopyOnStartup=true). Safe to repeat: the copy mirrors the source. Never touches the modules' own data. /// public bool CopyOnStartup { get; set; } + + /// + /// Write-through (phase 2, step A, Media__Library__DualWrite=true): after every change a module saved to its own tables, + /// the library tables are mirrored too. Off by default; the modules' own tables stay the truth. + /// + public bool DualWrite { get; set; } + + /// + /// Where the library screen reads the photos from (phase 2, step C, Media__Library__ReadFromLibrary): Off (the + /// module's own tables, the default), Compare (still those, but differences with the library tables are logged and + /// shown by GET media-library/read-check) or On (the library tables; needs DualWrite). + /// + public SlpModularCms.Core.Media.Library.MediaReadMode ReadFromLibrary { get; set; } = SlpModularCms.Core.Media.Library.MediaReadMode.Off; } diff --git a/src/SlpModularCms.Modules.Media/MediaModule.cs b/src/SlpModularCms.Modules.Media/MediaModule.cs index 76625e6..3797c46 100644 --- a/src/SlpModularCms.Modules.Media/MediaModule.cs +++ b/src/SlpModularCms.Modules.Media/MediaModule.cs @@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using SlpModularCms.Core.Media.Library; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -36,6 +38,9 @@ public class MediaModule : IModule services.AddOptions().Configure((options, configuration) => configuration.GetSection(MediaLibraryOptions.SectionName).Bind(options)); services.AddScoped(); + services.Replace(ServiceDescriptor.Scoped()); + services.Replace(ServiceDescriptor.Scoped()); + services.Replace(ServiceDescriptor.Singleton()); services.AddHostedService(); } diff --git a/src/SlpModularCms.Modules.Media/Migrations/20261117225718_AddHeroPhotoColumns.Designer.cs b/src/SlpModularCms.Modules.Media/Migrations/20261117225718_AddHeroPhotoColumns.Designer.cs new file mode 100644 index 0000000..2e16b41 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Migrations/20261117225718_AddHeroPhotoColumns.Designer.cs @@ -0,0 +1,182 @@ +// +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("20261117225718_AddHeroPhotoColumns")] + partial class AddHeroPhotoColumns + { + /// + 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("SetHash") + .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") + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + + 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.Property("Variants") + .HasColumnType("longtext"); + + 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/20261117225718_AddHeroPhotoColumns.cs b/src/SlpModularCms.Modules.Media/Migrations/20261117225718_AddHeroPhotoColumns.cs new file mode 100644 index 0000000..2d20a9e --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Migrations/20261117225718_AddHeroPhotoColumns.cs @@ -0,0 +1,109 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SlpModularCms.Modules.Media.Migrations +{ + /// + public partial class AddHeroPhotoColumns : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Variants", + table: "MediaUses", + type: "longtext", + nullable: true); + + migrationBuilder.AddColumn( + name: "SetHash", + table: "MediaAssets", + type: "varchar(16)", + maxLength: 16, + nullable: true); + + migrationBuilder.AddColumn( + name: "TallFocusX", + table: "MediaAssets", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "TallFocusY", + table: "MediaAssets", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "TallZoom", + table: "MediaAssets", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "Variants", + table: "MediaAssets", + type: "longtext", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideFocusX", + table: "MediaAssets", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideFocusY", + table: "MediaAssets", + type: "double", + nullable: true); + + migrationBuilder.AddColumn( + name: "WideZoom", + table: "MediaAssets", + type: "double", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Variants", + table: "MediaUses"); + + migrationBuilder.DropColumn( + name: "SetHash", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "TallFocusX", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "TallFocusY", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "TallZoom", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "Variants", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "WideFocusX", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "WideFocusY", + table: "MediaAssets"); + + migrationBuilder.DropColumn( + name: "WideZoom", + table: "MediaAssets"); + } + } +} diff --git a/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs b/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs index 1d7f5b3..a4f6571 100644 --- a/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs +++ b/src/SlpModularCms.Modules.Media/Migrations/MediaDbContextModelSnapshot.cs @@ -71,11 +71,36 @@ namespace SlpModularCms.Modules.Media.Migrations .HasMaxLength(64) .HasColumnType("varchar(64)"); + b.Property("SetHash") + .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") + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + b.Property("Width") .HasColumnType("int"); @@ -123,6 +148,9 @@ namespace SlpModularCms.Modules.Media.Migrations .HasMaxLength(16) .HasColumnType("varchar(16)"); + b.Property("Variants") + .HasColumnType("longtext"); + b.HasKey("Id"); b.HasIndex("AssetId"); diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs index 0856f74..418dfca 100644 --- a/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs +++ b/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs @@ -29,12 +29,13 @@ public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerable CopyAsync(CancellationToken cancellationToken) + /// Mirror only this module (the write-through); null = every module (the verified copy). + public async Task CopyAsync(CancellationToken cancellationToken, string? onlyModule = null) { var assets = new List(); var uses = new List(); var modules = new List(); - foreach (var source in sources) + foreach (var source in sources.Where(s => onlyModule is null || s.Module == onlyModule)) { modules.Add(source.Module); assets.AddRange(await source.ListAssetsAsync(cancellationToken)); @@ -121,6 +122,10 @@ public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerableReads a module's photos and uses back from the library's own tables (phase 2, step C). +public sealed class MediaLibraryReader(MediaDbContext db, IOptions options) : IMediaLibraryReader +{ + public MediaReadMode Mode => options.Value.ReadFromLibrary; + + public async Task> ListAssetsAsync(string module, CancellationToken cancellationToken) + => (await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken)) + .Select(a => new MediaAssetSnapshot( + a.Id, a.Module, a.OriginalSha256, a.OriginalKey, a.SourceFormat, a.Width, a.Height, a.Bytes, + a.DefaultFocusX, a.DefaultFocusY, a.DefaultZoom, a.SetHash ?? string.Empty, [], a.CreatedAt, a.CreatedByUserId, a.OriginalFileName, a.LastUsedAt, a.DeletedAt, + a.Variants, a.WideFocusX, a.WideFocusY, a.WideZoom, a.TallFocusX, a.TallFocusY, a.TallZoom)) + .ToList(); + + public async Task> ListUsesAsync(string module, CancellationToken cancellationToken) + => (await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken)) + .Select(u => new MediaUseSnapshot(u.Id, u.AssetId, u.Module, u.PlaceKind, u.Position, u.Active, u.SetHash, null, u.Variants)) + .ToList(); +} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs new file mode 100644 index 0000000..3d9b975 --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs @@ -0,0 +1,10 @@ +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Media.Library; + +namespace SlpModularCms.Modules.Media.Services; + +/// The migration switches as configured right now (read from the options on every call). +public sealed class MediaLibrarySwitchState(IOptionsMonitor options) : IMediaLibrarySwitchState +{ + public MediaLibrarySwitches Current => new(options.CurrentValue.CopyOnStartup, options.CurrentValue.DualWrite, options.CurrentValue.ReadFromLibrary); +} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs new file mode 100644 index 0000000..f0925ad --- /dev/null +++ b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Media.Library; + +namespace SlpModularCms.Modules.Media.Services; + +/// +/// The write-through (): mirrors one module into the library tables with the same verified +/// copy the start-up copy uses, one at a time. Off unless Media:Library:DualWrite is on. Never throws: the module's +/// own data is the truth and a failed mirror is logged and repaired by the next sync. +/// +public sealed class MediaLibrarySync(MediaLibraryCopier copier, IOptions options, ILogger logger) : IMediaLibrarySync +{ + // Two requests (or the background renderer) must not mirror at the same moment: one transaction at a time. + private static readonly SemaphoreSlim Gate = new(1, 1); + + public async Task SyncAsync(string module, CancellationToken cancellationToken) + { + if (!options.Value.DualWrite) + { + return; + } + + await Gate.WaitAsync(CancellationToken.None); + try + { + await copier.CopyAsync(CancellationToken.None, module); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + logger.LogError(exception, "The write-through of {Module} to the media library failed; the module's own data is untouched and the next change repairs it.", module); + } + finally + { + Gate.Release(); + } + } +}