Merge pull request 'Mediabibliotheek fase 3c: HeroImages-tabel verwijderen (met controle)' (#28) from feature/media-library-phase3c into master
Continuous Integration / config-sls (push) Successful in 9s
Continuous Integration / config-asf (push) Successful in 8s
Continuous Integration / changes (push) Successful in 14s
Continuous Integration / frontend-prepare (push) Successful in 45s
Continuous Integration / backend-build (push) Successful in 1m34s
Continuous Integration / frontend-build (push) Successful in 54s
Continuous Integration / vulnerability-scan (push) Successful in 1m43s
Continuous Integration / frontend-lint (push) Successful in 36s
Continuous Integration / frontend-test (push) Successful in 2m43s
Continuous Integration / backend-test (push) Successful in 3m23s
Continuous Integration / publish-sls-production (push) Skipped
Continuous Integration / deploy-sls-production (push) Skipped
Continuous Integration / publish-asf-test (push) Successful in 2m17s
Continuous Integration / publish-sls-test (push) Successful in 2m19s
Continuous Integration / deploy-sls-test (push) Successful in 1m50s
Continuous Integration / deploy-asf-test (push) Successful in 1m54s
Continuous Integration / smoke-sls-test (push) Successful in 18s
Continuous Integration / smoke-asf-test (push) Successful in 19s
Continuous Integration / compile-sites (push) Successful in 12m58s
Deploy (SCP) / deploy (push) Successful in 1m46s
Continuous Integration / config-sls (push) Successful in 9s
Continuous Integration / config-asf (push) Successful in 8s
Continuous Integration / changes (push) Successful in 14s
Continuous Integration / frontend-prepare (push) Successful in 45s
Continuous Integration / backend-build (push) Successful in 1m34s
Continuous Integration / frontend-build (push) Successful in 54s
Continuous Integration / vulnerability-scan (push) Successful in 1m43s
Continuous Integration / frontend-lint (push) Successful in 36s
Continuous Integration / frontend-test (push) Successful in 2m43s
Continuous Integration / backend-test (push) Successful in 3m23s
Continuous Integration / publish-sls-production (push) Skipped
Continuous Integration / deploy-sls-production (push) Skipped
Continuous Integration / publish-asf-test (push) Successful in 2m17s
Continuous Integration / publish-sls-test (push) Successful in 2m19s
Continuous Integration / deploy-sls-test (push) Successful in 1m50s
Continuous Integration / deploy-asf-test (push) Successful in 1m54s
Continuous Integration / smoke-sls-test (push) Successful in 18s
Continuous Integration / smoke-asf-test (push) Successful in 19s
Continuous Integration / compile-sites (push) Successful in 12m58s
Deploy (SCP) / deploy (push) Successful in 1m46s
This commit was merged in pull request #28.
This commit is contained in:
@@ -15,26 +15,6 @@ on:
|
||||
description: 'Deploy ASF test (Andrea Slop Fotografie)'
|
||||
type: boolean
|
||||
default: false
|
||||
media_library_copy:
|
||||
description: 'Media library copy on the TEST deploys (Media__Library__CopyOnStartup in shared/env): unchanged, on or off'
|
||||
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:
|
||||
@@ -575,10 +555,6 @@ jobs:
|
||||
service_name: ${{ vars.SERVICE_NAME_TEST }}
|
||||
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
|
||||
@@ -706,10 +682,6 @@ jobs:
|
||||
service_name: ${{ vars.SERVICE_NAME_ASF_TEST }}
|
||||
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
|
||||
|
||||
@@ -37,26 +37,6 @@ on:
|
||||
required: false
|
||||
type: string
|
||||
default: scp
|
||||
media_library_copy:
|
||||
description: 'unchanged | on | off: sets Media__Library__CopyOnStartup in shared/env before the restart. TEST only, never production.'
|
||||
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:
|
||||
@@ -231,119 +211,6 @@ jobs:
|
||||
ln -sf \"../../shared/modules/\$name\" \"$RELEASE_DIR/\$name\"; \
|
||||
done"
|
||||
|
||||
# The media library copy switch (migration plan of the media library, decision 13): the application reads
|
||||
# Media__Library__CopyOnStartup from the host environment file shared/env (D-16). The deploy account can edit that file,
|
||||
# nobody else needs a shell on the host. Only this ONE key is touched (nothing else of the file is read or printed, the
|
||||
# file holds secrets), only on a test environment, and only with a value that is exactly on, off or unchanged.
|
||||
# `on` makes the next start copy the photos into the library's tables and verify the copy (log line
|
||||
# "Media library copy verified: N photos and M uses"); `off` removes the key again (the default is off).
|
||||
- name: Set the media library copy switch (test only)
|
||||
if: inputs.media_library_copy != '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_copy }}"
|
||||
if [ "${{ inputs.environment }}" != "test" ]; then
|
||||
echo "The media library copy switch is for the test environment only (environment: ${{ inputs.environment }})." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then
|
||||
echo "media_library_copy 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__CopyOnStartup=/d' $ENV_FILE && echo 'Media__Library__CopyOnStartup=true' >> $ENV_FILE"
|
||||
else
|
||||
rssh "touch $ENV_FILE && sed -i '/^Media__Library__CopyOnStartup=/d' $ENV_FILE"
|
||||
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
|
||||
@@ -360,20 +227,6 @@ jobs:
|
||||
"ln -sfn $RELEASE_DIR ${{ inputs.deploy_path }}/current && \
|
||||
systemctl --user restart ${{ inputs.service_name }}"
|
||||
|
||||
# After a restart with the copy on, show the verification line of the copy in this run's log (the journal of the user
|
||||
# service), so nobody needs a shell on the host to see "Media library copy verified: N photos and M uses".
|
||||
- name: Report the media library copy (test only)
|
||||
if: inputs.media_library_copy == 'on'
|
||||
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: |
|
||||
sleep 20
|
||||
rssh "journalctl --user -u ${{ inputs.service_name }} --since '-10 min' --no-pager 2>/dev/null | grep -E 'Media library copy|media library copy' | tail -5 || true"
|
||||
echo "Look for 'Media library copy verified: N photos and M uses'. An error line means the copy was rolled back and nothing was copied."
|
||||
|
||||
# Verifies the restart actually produced a healthy process before this run is allowed to
|
||||
# report success. Retries absorb process-startup time, which is about 10 seconds normally but
|
||||
# 30 seconds or more when the host is busy (a migration step, two services restarting at once
|
||||
|
||||
@@ -82,3 +82,4 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
| 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` |
|
||||
| 54 | **3c is built on its own branch and drops `HeroImages` behind a guard** (2026-11-18). | `feature/media-library-phase3c`: the photos always live in `MediaAssets`; the switches, copier, write-through, reader, mirror, read-check endpoint and the "Controle bibliotheek" panel are gone (Bryan: keep the panel until 3b is verified, then remove it in 3c); migration `DropHeroImagesTable` is destructive and only runs when the library holds every photo (guard before the migrations), `Down` gives the empty table. Not deployed before 3b is verified on test, a fresh restore point exists and SLS-Main has been told the exact migration. `MediaUses` stays unused in the database. | Keep the migration aids for another release: more code to carry for no use. | `HeroImagesDropGuard`, `DropHeroImagesTable`, `HeroDbContext` |
|
||||
|
||||
@@ -60,3 +60,23 @@ Public addresses (`/media/hero/<setHash>/...`), ids and hashes stay as they are
|
||||
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.
|
||||
|
||||
## Step 3c as built (2026-11-18) - on its own branch, NOT deployed
|
||||
|
||||
Branch `feature/media-library-phase3c` (from `feature/media-library-phase2`). It is a separate branch on purpose: dispatching `phase2` can never carry the destructive migration by accident. It is code, docs and tests only; nothing has been run on a test host.
|
||||
|
||||
**What it removes**
|
||||
- The three migration switches and everything behind them: `Media__Library__CopyOnStartup` and the copier, `DualWrite` and the write-through (`IMediaLibrarySync`), `ReadFromLibrary` and the reader/compare mode (`IMediaLibraryReader`), `HeroPhotosInLibrary` and the legacy mirror, the start-up guard and the storage switch (`HeroStorage`, model cache key).
|
||||
- The "Controle bibliotheek" panel, the `read-check` endpoint and the migration source (`IMediaMigrationSource`, `HeroMediaMigrationSource`). Bryan: keep the panel until 3b is verified on test, then remove it together with the other migration aids (so: in this step).
|
||||
- The four workflow inputs (`media_library_copy`, `_dual_write`, `_read`, `_hero_photos`) and their deploy steps.
|
||||
|
||||
**What it makes permanent:** the hero's photos are rows of the library's `MediaAssets` (module `hero`, filter and `Module` column in the hero's model; the table and its migrations belong to the Media module). `MediaUses` is no longer written or read (it stays in the database, empty of meaning; dropping it is a later clean-up).
|
||||
|
||||
**The destructive migration:** Hero `DropHeroImagesTable` (`DROP TABLE HeroImages`). Protected twice:
|
||||
1. `HeroImagesDropGuard` runs before the migrations at start-up: when that migration is pending and `HeroImages` holds rows, it throws (the application does not start, nothing is dropped) unless every row is in `MediaAssets` (module hero) with the same hash, set and variants, or when `MediaAssets` does not exist. A new database (nothing in `HeroImages`) passes.
|
||||
2. The restore point of the test sites, taken right before the deploy.
|
||||
`Down` brings back the EMPTY table (data comes from the restore point). Tested on MariaDB: refused with a missing photo, refused with a different set, dropped when the library holds everything, Down recreates the empty table.
|
||||
|
||||
**Order on test (needs Bryan's OK on 3b and a notice to SLS-Main with this exact migration):** (1) 3b verified: copy done, library check matches, `HeroPhotosInLibrary=on` verified by Bryan and live for a quiet period; (2) fresh restore point (Pi-Main script); (3) deploy `feature/media-library-phase3c` (the switches no longer exist; the variable `Media__Library__HeroPhotosInLibrary` in `shared/env` can stay, it is ignored); (4) check the hero admin, the public site and the library screen. Rollback: restore the restore point and deploy the previous release; `Down` only gives the empty table.
|
||||
|
||||
Not tested here: the deploy itself, the media directory on the Pi, the production database (never).
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# Media library: order of rollout to production
|
||||
|
||||
Status (2026-11-18): NOT executed. Production only on an explicit go from Bryan. No `release/*` branches. Written on `feature/media-library-phase3c`.
|
||||
|
||||
## Why the order matters
|
||||
|
||||
The test sites went phase 2 -> copy -> 3b switch -> 3c. Production must take the same road, because the 3c release has **no copier and no switches** any more: `HeroImagesDropGuard` refuses to start the application (nothing is dropped) unless every photo of `HeroImages` is in `MediaAssets` (module `hero`) with the same hash, set and variants. A direct jump from today's production to 3c therefore fails at start-up, by design, unless `HeroImages` is empty.
|
||||
|
||||
## Steps (each a separate release; every step: notice to SLS-Main, then Bryan's go)
|
||||
|
||||
| # | Release | Settings | Check before the next step | Rollback |
|
||||
|---|---|---|---|---|
|
||||
| 0 | Fresh restore point of production (database dump + media directory listing, the Pi-Main script) and row counts: `HeroImages`, `HeroSlides`, `MediaAssets`, 10 sample public addresses | none | Restore point exists and its path is written down | n/a |
|
||||
| 1 | Phase 2 (master after PR #27: write-through, crop per slide, read switch, 3a columns, `DropSlideImageForeignKey`) | `Media__Library__CopyOnStartup=true`, `Media__Library__DualWrite=true`; the other switches off | `GET media-library/read-check` says `matches: true` (hash, set and variants); public addresses unchanged | Switches off; `Down` of the additive migrations; last resort the restore point |
|
||||
| 2 | Same build, setting only | `Media__Library__HeroPhotosInLibrary=true` (the guard refuses to start if the copy is incomplete; log says why) | Hero admin: upload, crop per slide, save, trash, restore; public site; read-check still matches. Leave it running for a quiet period (test had days) | Switch off: `HeroImages` is still a complete copy |
|
||||
| 3 | Phase 3c (PR after #27): `DropHeroImagesTable` + guard; switches and copier removed | none needed (settings of step 1 and 2 are ignored; remove them from the environment file afterwards) | Journal: no guard error, `Applying migration ..._DropHeroImagesTable`, no failure; `HeroImages` gone; hero admin and site work | Only from the restore point of step 0 (take a NEW one right before this step): restore the dump, deploy the previous release. `Down` only recreates an empty table |
|
||||
|
||||
Step 3 needs its own fresh restore point and Bryan's go on the exact migration (`DROP TABLE HeroImages`), as on test.
|
||||
|
||||
## Things that differ from test
|
||||
|
||||
- Production has real photos on both sites; test SlpSoftware had none (guard passes on an empty `HeroImages`). Count the photos per site in step 0 and compare them with `MediaAssets` after step 1.
|
||||
- The workflow inputs for the switches exist only on the phase 2 build. After 3c they are gone, so steps 1 and 2 must be run while phase 2 is the latest release (do not merge PR 3c to master before step 2 is done on production).
|
||||
- The new default `Media__AutoCleanupUnattached=false` applies from step 1: unused uploads are kept until someone deletes them in the library.
|
||||
- The media directory is never changed destructively by these steps (retired sets stay for the retention time), so a database restore does not need a media restore.
|
||||
|
||||
## Order of merging versus deploying to test
|
||||
|
||||
Master deploys to test. After PR #27 (phase 2) is merged, test runs phase 2 code on databases where `HeroImages` is already dropped by 3c. With `Media__Library__HeroPhotosInLibrary=true` still in the environment file of test the photos are read from the library, but the legacy mirror (`HeroLegacyImageMirror`) then writes to `HeroImages`, which does not exist, so saving in the hero admin on test can fail until 3c is merged too. Merge #27 and the 3c PR directly after each other (or accept a short window on test where saving the hero fails).
|
||||
@@ -1,70 +0,0 @@
|
||||
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 (
|
||||
<section className="space-y-2 rounded-md border p-3" aria-labelledby="library-check-title" data-testid="library-check">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<h2 id="library-check-title" className="text-sm font-medium">
|
||||
{t('media.check.title')}
|
||||
</h2>
|
||||
<Button type="button" variant="outline" size="sm" disabled={check.isFetching} onClick={() => (asked ? void check.refetch() : setAsked(true))} data-testid="library-check-run">
|
||||
<ListChecks className="mr-1.5 size-4" aria-hidden="true" />
|
||||
{check.isFetching ? t('media.check.running') : t('media.check.run')}
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">{t('media.check.hint')}</p>
|
||||
{check.isError && (
|
||||
<p role="alert" className="text-sm text-destructive" data-testid="library-check-error">
|
||||
{t('media.check.error')}
|
||||
</p>
|
||||
)}
|
||||
{check.data && (
|
||||
<div className="space-y-2" role="status" data-testid="library-check-result">
|
||||
<p className="text-sm" data-testid="library-check-switches">
|
||||
{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}`),
|
||||
})}
|
||||
</p>
|
||||
{check.data.modules.length === 0 && <p className="text-sm text-muted-foreground">{t('media.check.noModules')}</p>}
|
||||
{check.data.modules.map((module) => (
|
||||
<div key={module.module} data-testid={`library-check-${module.module}`}>
|
||||
{module.matches ? (
|
||||
<p className="flex items-center gap-2 text-sm text-green-700 dark:text-green-400">
|
||||
<CheckCircle2 className="size-4 shrink-0" aria-hidden="true" />
|
||||
{t('media.check.matches', { module: t(`media.module.${module.module}`, { defaultValue: module.module }), photos: module.photos, uses: module.uses })}
|
||||
</p>
|
||||
) : (
|
||||
<div className="space-y-1">
|
||||
<p className="flex items-center gap-2 text-sm font-medium text-destructive">
|
||||
<TriangleAlert className="size-4 shrink-0" aria-hidden="true" />
|
||||
{t('media.check.differs', { module: t(`media.module.${module.module}`, { defaultValue: module.module }), count: module.differences.length })}
|
||||
</p>
|
||||
<ul className="list-disc space-y-0.5 pl-6 text-xs" data-testid={`library-check-${module.module}-differences`}>
|
||||
{module.differences.map((difference) => (
|
||||
<li key={difference}>{difference}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types';
|
||||
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types';
|
||||
|
||||
const daysAgo = (days: number) => new Date(Date.now() - days * 86_400_000).toISOString();
|
||||
|
||||
@@ -81,9 +81,6 @@ 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);
|
||||
|
||||
@@ -339,52 +339,6 @@ 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();
|
||||
|
||||
@@ -7,7 +7,6 @@ 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';
|
||||
@@ -91,7 +90,6 @@ export function MediaLibraryPage() {
|
||||
</div>
|
||||
|
||||
{storage.data && <StorageMeter storage={storage.data} />}
|
||||
<LibraryCheck />
|
||||
|
||||
<div className="flex gap-2" role="group" aria-label={t('media.view.label')}>
|
||||
{(['library', 'trash'] as const).map((value) => (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types';
|
||||
import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types';
|
||||
|
||||
export const MEDIA_KEY = ['media-library'];
|
||||
|
||||
@@ -22,17 +22,6 @@ 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<MediaReadCheckResponse, Error>({
|
||||
queryKey: [...MEDIA_KEY, 'read-check'],
|
||||
queryFn: () => api.get<MediaReadCheckResponse>('/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<MediaCleanupPreviewDto, Error>({
|
||||
|
||||
@@ -29,23 +29,6 @@ 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. */
|
||||
|
||||
@@ -902,25 +902,6 @@
|
||||
"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."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -902,25 +902,6 @@
|
||||
"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."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1260,76 +1260,6 @@
|
||||
},
|
||||
"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": {
|
||||
@@ -4134,35 +4064,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"/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": {
|
||||
|
||||
@@ -103,37 +103,4 @@ 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<IMediaLibraryService>();
|
||||
library.Modules.Returns(["hero"]);
|
||||
var verifier = Substitute.For<IMediaReadVerifier>();
|
||||
verifier.CheckAsync(Arg.Any<CancellationToken>()).Returns(new MediaReadCheck("hero", MediaReadMode.Compare, 3, 2, ["photo x is missing in the library"]));
|
||||
var reader = Substitute.For<IMediaLibraryReader>();
|
||||
reader.Mode.Returns(MediaReadMode.Compare);
|
||||
var switches = Substitute.For<IMediaLibrarySwitchState>();
|
||||
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<OkObjectResult>().Subject.Value.Should().BeOfType<MediaReadCheckResponse>().Subject;
|
||||
response.Mode.Should().Be("compare");
|
||||
response.CopyOnStartup.Should().BeTrue();
|
||||
response.DualWrite.Should().BeTrue();
|
||||
response.Modules.Should().ContainSingle().Which.Should().Match<MediaReadCheckDto>(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<IMediaLibraryService>();
|
||||
|
||||
var result = await new MediaLibraryController(library, new Clock()).ReadCheck(CancellationToken.None);
|
||||
|
||||
var response = result.Should().BeOfType<OkObjectResult>().Subject.Value.Should().BeOfType<MediaReadCheckResponse>().Subject;
|
||||
response.Mode.Should().Be("off");
|
||||
response.Modules.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +87,6 @@ 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<Media.Library.IMediaLibraryService, Media.Library.MediaLibraryService>();
|
||||
services.TryAddScoped<Media.Library.IMediaLibrarySync, Media.Library.NoMediaLibrarySync>();
|
||||
services.TryAddScoped<Media.Library.IMediaLibraryReader, Media.Library.NoMediaLibraryReader>();
|
||||
services.TryAddSingleton<Media.Library.IMediaLibrarySwitchState, Media.Library.NoMediaLibrarySwitchState>();
|
||||
services.TryAddSingleton(sp =>
|
||||
new ResetCooldown(sp.GetRequiredService<TimeProvider>(), TimeSpan.FromMinutes(sp.GetRequiredService<IOptions<PasswordResetSettings>>().Value.CooldownMinutes)));
|
||||
services.AddScoped<IPasswordResetService, PasswordResetService>();
|
||||
|
||||
@@ -20,14 +20,6 @@ public sealed record MediaPhotoDto(
|
||||
DateTimeOffset? UnusedSince = null,
|
||||
DateTimeOffset? DeletedAt = null);
|
||||
|
||||
/// <summary>The result of comparing a module's own tables with the library tables (read switch, phase 2 step C).</summary>
|
||||
public sealed record MediaReadCheckDto(string Module, string Mode, int Photos, int Uses, bool Matches, IReadOnlyList<string> Differences);
|
||||
|
||||
/// <param name="Mode">The read switch: off, compare or on.</param>
|
||||
/// <param name="CopyOnStartup">The copy switch (start-up copy into the library tables).</param>
|
||||
/// <param name="DualWrite">The write-through switch.</param>
|
||||
public sealed record MediaReadCheckResponse(string Mode, IReadOnlyList<MediaReadCheckDto> Modules, bool CopyOnStartup = false, bool DualWrite = false);
|
||||
|
||||
public sealed record MediaLibraryDto(IReadOnlyList<MediaPhotoDto> Photos, IReadOnlyList<string> Modules);
|
||||
|
||||
public sealed record MediaStorageDto(long UsedBytes, long LimitBytes, string Level, int WarningPercent);
|
||||
@@ -57,27 +49,8 @@ public sealed record MediaTrashResponse(IReadOnlyList<MediaUseResultDto> Results
|
||||
[ApiController]
|
||||
[Route("media-library")]
|
||||
[Authorize(Policy = "AdminOnly")]
|
||||
public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null, IEnumerable<IMediaReadVerifier>? verifiers = null, IMediaLibraryReader? reader = null, IMediaLibrarySwitchState? switches = null) : ControllerBase
|
||||
public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
[HttpGet("read-check")]
|
||||
[ProducesResponseType<MediaReadCheckResponse>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> ReadCheck(CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new List<MediaReadCheckDto>();
|
||||
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<MediaLibraryDto>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Photos(CancellationToken cancellationToken)
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>Where the media library screen reads the photos of a module from (migration phase 2, step C).</summary>
|
||||
public enum MediaReadMode
|
||||
{
|
||||
/// <summary>The module's own tables (the default, and the truth until phase 3).</summary>
|
||||
Off,
|
||||
|
||||
/// <summary>Still the module's own tables, but the library tables are read too and every difference is logged and reported.</summary>
|
||||
Compare,
|
||||
|
||||
/// <summary>The library tables. Needs the write-through (<c>Media:Library:DualWrite</c>) to be current.</summary>
|
||||
On,
|
||||
}
|
||||
|
||||
/// <summary>Reads a module's photos and uses back from the library's own tables.</summary>
|
||||
public interface IMediaLibraryReader
|
||||
{
|
||||
MediaReadMode Mode { get; }
|
||||
|
||||
Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(string module, CancellationToken cancellationToken);
|
||||
|
||||
Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(string module, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>The default: no library tables to read, so everything stays with the module.</summary>
|
||||
public sealed class NoMediaLibraryReader : IMediaLibraryReader
|
||||
{
|
||||
public MediaReadMode Mode => MediaReadMode.Off;
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(string module, CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>([]);
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(string module, CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaUseSnapshot>>([]);
|
||||
}
|
||||
|
||||
/// <summary>The current state of the migration switches (for the check in the library screen).</summary>
|
||||
public sealed record MediaLibrarySwitches(bool CopyOnStartup, bool DualWrite, MediaReadMode ReadFromLibrary);
|
||||
|
||||
/// <summary>Tells the current state of the migration switches.</summary>
|
||||
public interface IMediaLibrarySwitchState
|
||||
{
|
||||
MediaLibrarySwitches Current { get; }
|
||||
}
|
||||
|
||||
/// <summary>The default: no library tables, so every switch is off.</summary>
|
||||
public sealed class NoMediaLibrarySwitchState : IMediaLibrarySwitchState
|
||||
{
|
||||
public MediaLibrarySwitches Current => new(false, false, MediaReadMode.Off);
|
||||
}
|
||||
|
||||
/// <summary>What a comparison of a module's own tables with the library tables found. <c>Differences</c> is empty when they agree.</summary>
|
||||
public sealed record MediaReadCheck(string Module, MediaReadMode Mode, int Photos, int Uses, IReadOnlyList<string> Differences)
|
||||
{
|
||||
public bool Matches => Differences.Count == 0;
|
||||
}
|
||||
|
||||
/// <summary>A module that can compare its own data with the library tables (used by <c>GET media-library/read-check</c>).</summary>
|
||||
public interface IMediaReadVerifier
|
||||
{
|
||||
string Module { get; }
|
||||
|
||||
Task<MediaReadCheck> CheckAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Compares what a module holds with what the library tables hold, by id, and lists every difference in words.</summary>
|
||||
public static class MediaReadComparer
|
||||
{
|
||||
public static IReadOnlyList<string> Compare(
|
||||
IReadOnlyList<MediaAssetSnapshot> ownAssets,
|
||||
IReadOnlyList<MediaUseSnapshot> ownUses,
|
||||
IReadOnlyList<MediaAssetSnapshot> libraryAssets,
|
||||
IReadOnlyList<MediaUseSnapshot> libraryUses)
|
||||
{
|
||||
var differences = new List<string>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>
|
||||
/// 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 <c>Media:Library:DualWrite</c> off (the default) it does nothing.
|
||||
/// </summary>
|
||||
public interface IMediaLibrarySync
|
||||
{
|
||||
/// <summary>Mirrors everything the module offers through its <see cref="IMediaMigrationSource"/> into the library tables.</summary>
|
||||
Task SyncAsync(string module, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>The default: the Media module is not there or the switch is off, so nothing is written.</summary>
|
||||
public sealed class NoMediaLibrarySync : IMediaLibrarySync
|
||||
{
|
||||
public Task SyncAsync(string module, CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>One crop of a photo for a ratio (focus point and zoom).</summary>
|
||||
public sealed record MediaCropSnapshot(string Ratio, double X, double Y, double Zoom);
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
// 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,
|
||||
string OriginalSha256,
|
||||
string OriginalKey,
|
||||
string SourceFormat,
|
||||
int Width,
|
||||
int Height,
|
||||
long Bytes,
|
||||
double FocusX,
|
||||
double FocusY,
|
||||
double Zoom,
|
||||
string SetHash,
|
||||
IReadOnlyList<MediaCropSnapshot> Crops,
|
||||
DateTimeOffset CreatedAt,
|
||||
Guid CreatedByUserId,
|
||||
string? OriginalFileName,
|
||||
DateTimeOffset? LastUsedAt,
|
||||
DateTimeOffset? DeletedAt,
|
||||
string? VariantsJson = null,
|
||||
double? WideFocusX = null,
|
||||
double? WideFocusY = null,
|
||||
double? WideZoom = null,
|
||||
double? TallFocusX = null,
|
||||
double? TallFocusY = null,
|
||||
double? TallZoom = null);
|
||||
|
||||
/// <summary>One place a photo is used, as a module holds it today. <c>Crops</c> are the crops of THIS use (crop per use); null = the crops of its photo.</summary>
|
||||
public sealed record MediaUseSnapshot(Guid Id, Guid AssetId, string Module, string PlaceKind, int Position, bool Active, string SetHash, IReadOnlyList<MediaCropSnapshot>? Crops = null, string? VariantsJson = null);
|
||||
|
||||
/// <summary>
|
||||
/// What a module offers so the library can copy its photos and uses into its own tables. Read-only: the module's data is
|
||||
/// never changed by the copy.
|
||||
/// </summary>
|
||||
public interface IMediaMigrationSource
|
||||
{
|
||||
string Module { get; }
|
||||
|
||||
/// <summary>Every photo of the module, also those in the trash.</summary>
|
||||
Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>Phase 2, step A: the hero tells the media library after every change it saved (the write-through).</summary>
|
||||
public class HeroLibrarySyncTests
|
||||
{
|
||||
private static (HeroTestRig Rig, IMediaLibrarySync Sync) Rig()
|
||||
{
|
||||
var sync = Substitute.For<IMediaLibrarySync>();
|
||||
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<CancellationToken>());
|
||||
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<List<HeroVariant>>(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));
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using SlpModularCms.Modules.Hero.Models;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
public class HeroMediaMigrationSourceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Offers_every_photo_with_the_ids_hashes_and_set_hashes_the_hero_uses_and_one_use_per_slide()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
|
||||
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
|
||||
var c = await rig.Upload(HeroTestRig.Photo(seed: 3));
|
||||
await rig.Service.SaveAsync(
|
||||
new HeroSaveRequest("Titel", null, null, "single", [], [new HeroSlideRequest(null, Guid.Parse(b.Id), "twee"), new HeroSlideRequest(null, Guid.Parse(a.Id), "een", IsActive: false)]),
|
||||
HeroTestRig.Admin, CancellationToken.None);
|
||||
await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(c.Id), HeroTestRig.Admin, CancellationToken.None);
|
||||
var source = new HeroMediaMigrationSource(rig.Repository);
|
||||
|
||||
var assets = await source.ListAssetsAsync(CancellationToken.None);
|
||||
var uses = await source.ListUsesAsync(CancellationToken.None);
|
||||
|
||||
source.Module.Should().Be("hero");
|
||||
assets.Select(x => x.Id).Should().BeEquivalentTo([Guid.Parse(a.Id), Guid.Parse(b.Id), Guid.Parse(c.Id)]);
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
var row = rig.Db.HeroImages.Single(i => i.Id == asset.Id);
|
||||
asset.OriginalSha256.Should().Be(row.OriginalSha256);
|
||||
asset.OriginalKey.Should().Be(row.OriginalKey);
|
||||
asset.SetHash.Should().Be(row.SetHash).And.NotBeEmpty();
|
||||
asset.Crops.Select(x => x.Ratio).Should().Equal("wide", "tall");
|
||||
}
|
||||
|
||||
assets.Single(x => x.Id == Guid.Parse(c.Id)).DeletedAt.Should().NotBeNull();
|
||||
uses.Should().HaveCount(2);
|
||||
uses.Single(u => u.AssetId == Guid.Parse(b.Id)).Should().Match<SlpModularCms.Core.Media.Library.MediaUseSnapshot>(u => u.Position == 1 && u.Active && u.PlaceKind == "slide");
|
||||
uses.Single(u => u.AssetId == Guid.Parse(a.Id)).Should().Match<SlpModularCms.Core.Media.Library.MediaUseSnapshot>(u => u.Position == 2 && !u.Active);
|
||||
uses.Select(u => u.SetHash).Should().OnlyContain(h => h.Length == 16);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reading_changes_nothing()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
await rig.Upload(HeroTestRig.Photo());
|
||||
var source = new HeroMediaMigrationSource(rig.Repository);
|
||||
|
||||
await source.ListAssetsAsync(CancellationToken.None);
|
||||
await source.ListUsesAsync(CancellationToken.None);
|
||||
|
||||
rig.Db.ChangeTracker.HasChanges().Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// <c>ConnectionStrings__DefaultConnection</c> is set (CI sets it); otherwise it does nothing.
|
||||
/// </summary>
|
||||
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<HeroDbContext>()
|
||||
.UseMySQL(connectionString)
|
||||
.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>()
|
||||
.ReplaceService<IModelCacheKeyFactory, HeroModelCacheKeyFactory>()
|
||||
.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning))
|
||||
.Options,
|
||||
new HeroStorage(inLibrary));
|
||||
|
||||
private static MediaDbContext OpenMedia(string connectionString)
|
||||
=> new(new DbContextOptionsBuilder<MediaDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().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<HeroLegacyImageMirror>.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<IMigrator>();
|
||||
await migrator.MigrateAsync("20261117071410_RemoveHeroScheduleAndSwitch");
|
||||
(await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().NotBe(0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DropAsync(connection);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<int> 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using MySql.Data.MySqlClient;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Phase 3c against a real MariaDB in a database of its own: the hero's photos are rows of the library's MediaAssets table, the
|
||||
/// migration that drops HeroImages only runs when the library holds every photo, and its Down brings back the (empty) table.
|
||||
/// Runs when <c>ConnectionStrings__DefaultConnection</c> is set (CI sets it); otherwise it does nothing.
|
||||
/// </summary>
|
||||
public class HeroPhotosInLibraryTableTests
|
||||
{
|
||||
private static string? ConnectionString()
|
||||
{
|
||||
var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
|
||||
return string.IsNullOrWhiteSpace(baseString)
|
||||
? null
|
||||
: new MySqlConnectionStringBuilder(baseString) { Database = "hero_phase3c_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString;
|
||||
}
|
||||
|
||||
private static HeroDbContext OpenHero(string connectionString)
|
||||
=> new(new DbContextOptionsBuilder<HeroDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().Options);
|
||||
|
||||
private static MediaDbContext OpenMedia(string connectionString)
|
||||
=> new(new DbContextOptionsBuilder<MediaDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().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",
|
||||
};
|
||||
|
||||
private static async Task<long> ScalarAsync(HeroDbContext db, string sql)
|
||||
{
|
||||
var connection = db.Database.GetDbConnection();
|
||||
if (connection.State != System.Data.ConnectionState.Open)
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
}
|
||||
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
return Convert.ToInt64(await command.ExecuteScalarAsync());
|
||||
}
|
||||
|
||||
private static Task<long> TableExists(HeroDbContext db, string table)
|
||||
=> ScalarAsync(db, $"SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{table}'");
|
||||
|
||||
[Fact]
|
||||
public async Task On_a_new_database_the_hero_writes_and_reads_rows_of_MediaAssets_and_HeroImages_is_gone()
|
||||
{
|
||||
var connection = ConnectionString();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var db = OpenHero(connection);
|
||||
HeroImagesDropGuard.Verify(db);
|
||||
await db.Database.MigrateAsync();
|
||||
await using (var media = OpenMedia(connection))
|
||||
{
|
||||
await media.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
(await TableExists(db, "HeroImages")).Should().Be(0);
|
||||
|
||||
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();
|
||||
|
||||
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");
|
||||
|
||||
var read = await new Repositories.HeroRepository(OpenHero(connection)).GetReadOnlyAsync(HeroLanguages.Default, CancellationToken.None);
|
||||
read!.Slides.Single().Image!.Id.Should().Be(photo.Id);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DropAsync(connection);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HeroImages_is_only_dropped_when_the_library_holds_every_photo_and_the_down_brings_back_the_empty_table()
|
||||
{
|
||||
var connection = ConnectionString();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var db = OpenHero(connection);
|
||||
var migrations = db.Database.GetMigrations().ToList();
|
||||
var dropIndex = migrations.FindIndex(m => m.EndsWith("_DropHeroImagesTable", StringComparison.Ordinal));
|
||||
dropIndex.Should().BeGreaterThan(0);
|
||||
var migrator = db.GetService<IMigrator>();
|
||||
await migrator.MigrateAsync(migrations[dropIndex - 1]);
|
||||
await using (var media = OpenMedia(connection))
|
||||
{
|
||||
await media.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
// A photo the library does not have: refused, nothing is dropped.
|
||||
var sha = new string('c', 64);
|
||||
var id = Guid.NewGuid();
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"INSERT INTO `HeroImages` (Id, OriginalSha256, OriginalKey, SourceFormat, Width, Height, Bytes, FocusX, FocusY, Zoom, SetHash, Variants, CreatedAt, CreatedByUserId) "
|
||||
+ $"VALUES ('{id}', '{sha}', 'originals/c.jpg', 'jpg', 10, 10, 10, 0.5, 0.5, 1, '0123456789abcdef', '[]', NOW(), '{Guid.NewGuid()}')");
|
||||
var refused = () => HeroImagesDropGuard.Verify(db);
|
||||
refused.Should().Throw<InvalidOperationException>().Which.Message.Should().Contain("not in the library");
|
||||
(await TableExists(db, "HeroImages")).Should().Be(1);
|
||||
|
||||
// A photo that differs in its set: refused as well.
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"INSERT INTO `MediaAssets` (Id, Module, OriginalSha256, OriginalKey, SourceFormat, Width, Height, Bytes, DefaultFocusX, DefaultFocusY, DefaultZoom, CreatedAt, CreatedByUserId, SetHash, Variants) "
|
||||
+ $"VALUES ('{id}', 'hero', '{sha}', 'originals/c.jpg', 'jpg', 10, 10, 10, 0.5, 0.5, 1, NOW(), '{Guid.NewGuid()}', 'ffffffffffffffff', '[]')");
|
||||
var differs = () => HeroImagesDropGuard.Verify(db);
|
||||
differs.Should().Throw<InvalidOperationException>();
|
||||
|
||||
// The library holds it as it is: the guard passes and the migration drops the table.
|
||||
await db.Database.ExecuteSqlRawAsync($"UPDATE `MediaAssets` SET SetHash = '0123456789abcdef' WHERE Id = '{id}'");
|
||||
HeroImagesDropGuard.Verify(db);
|
||||
await db.Database.MigrateAsync();
|
||||
(await TableExists(db, "HeroImages")).Should().Be(0);
|
||||
(await ScalarAsync(db, "SELECT COUNT(*) FROM `MediaAssets`")).Should().Be(1, "the library rows are untouched");
|
||||
|
||||
// Down: the table is back, empty (the data comes from the restore point).
|
||||
await migrator.MigrateAsync(migrations[dropIndex - 1]);
|
||||
(await TableExists(db, "HeroImages")).Should().Be(1);
|
||||
(await ScalarAsync(db, "SELECT COUNT(*) FROM `HeroImages`")).Should().Be(0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DropAsync(connection);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>Phase 3b: the hero's photos live in the library's table (rows of module "hero"), and HeroImages stays a complete copy.</summary>
|
||||
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<InvalidOperationException>()).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<InvalidOperationException>()).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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
/// <summary>Phase 3c: the hero's photos are rows of the library's table, not of a table of the hero.</summary>
|
||||
public class HeroPhotosModelTests
|
||||
{
|
||||
[Fact]
|
||||
public void The_model_maps_the_photos_to_the_library_table_with_a_module_column_and_a_filter_on_it()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var photo = rig.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();
|
||||
photo.GetQueryFilter().Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_new_photo_belongs_to_the_hero_module()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var image = await rig.Upload(HeroTestRig.Photo());
|
||||
|
||||
rig.Db.Entry(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(image.Id))).Property(HeroDbContext.ModuleColumn).CurrentValue.Should().Be("hero");
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>Phase 2, step C: the library screen reads the hero's photos from the library tables (on), or compares (compare).</summary>
|
||||
public class HeroReadFromLibraryTests
|
||||
{
|
||||
private sealed class FakeReader(MediaReadMode mode, List<MediaAssetSnapshot> assets, List<MediaUseSnapshot> uses, bool fails = false) : IMediaLibraryReader
|
||||
{
|
||||
public MediaReadMode Mode => mode;
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(string module, CancellationToken cancellationToken)
|
||||
=> fails ? throw new InvalidOperationException("library down") : Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>(assets.ToList());
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(string module, CancellationToken cancellationToken)
|
||||
=> fails ? throw new InvalidOperationException("library down") : Task.FromResult<IReadOnlyList<MediaUseSnapshot>>(uses.ToList());
|
||||
}
|
||||
|
||||
private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides);
|
||||
|
||||
/// <summary>A hero with two photos, one in a slide, one not; the library tables made equal to it.</summary>
|
||||
private static async Task<(HeroTestRig Rig, List<MediaAssetSnapshot> Assets, List<MediaUseSnapshot> 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<HeroMediaUsageProvider>.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<MediaPhoto>(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<MediaUse>(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();
|
||||
}
|
||||
}
|
||||
@@ -39,15 +39,12 @@ public sealed class HeroTestRig : IDisposable
|
||||
public HeroMediaCleanup Cleanup { get; }
|
||||
public HeroOptions HeroOptions { get; } = new();
|
||||
|
||||
public IHeroLegacyImageMirror LegacyMirror { get; }
|
||||
|
||||
public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, bool photosInLibrary = false)
|
||||
public HeroTestRig(int maxPublishedSlides = 50)
|
||||
{
|
||||
HeroOptions.MaxPublishedSlides = maxPublishedSlides;
|
||||
Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 };
|
||||
var mediaOptions = Options.Create(Media);
|
||||
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).ReplaceService<Microsoft.EntityFrameworkCore.Infrastructure.IModelCacheKeyFactory, HeroModelCacheKeyFactory>().Options, new HeroStorage(photosInLibrary));
|
||||
LegacyMirror = new HeroLegacyImageMirror(Db, NullLogger<HeroLegacyImageMirror>.Instance);
|
||||
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options);
|
||||
Repository = new HeroRepository(Db);
|
||||
Store = new FileSystemMediaStore(mediaOptions);
|
||||
Processor = new MagickImageProcessor(mediaOptions, NullLogger<MagickImageProcessor>.Instance);
|
||||
@@ -55,10 +52,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<HeroImageService>.Instance, Time, IndexCache, Signal, librarySync, LegacyMirror);
|
||||
Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache, null, librarySync, LegacyMirror);
|
||||
Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger<HeroImageService>.Instance, Time, IndexCache, Signal);
|
||||
Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache);
|
||||
Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time);
|
||||
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time, librarySync, LegacyMirror);
|
||||
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time);
|
||||
}
|
||||
|
||||
/// <summary>A photo-like fixture: the left half red, the right half blue, with a camera tag.</summary>
|
||||
|
||||
@@ -112,37 +112,6 @@ public class HeroImage
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The same photo as <see cref="HeroImage"/>, always stored in <c>HeroImages</c>. Only used while the photos live in the
|
||||
/// library (phase 3b): then <c>HeroImages</c> is kept up to date from the library rows so the switch can be turned back.
|
||||
/// </summary>
|
||||
public class LegacyHeroImage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string OriginalSha256 { get; set; } = string.Empty;
|
||||
public string OriginalKey { get; set; } = string.Empty;
|
||||
public string SourceFormat { get; set; } = string.Empty;
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public long Bytes { get; set; }
|
||||
public double FocusX { get; set; } = 0.5;
|
||||
public double FocusY { get; set; } = 0.5;
|
||||
public double Zoom { get; set; } = 1.0;
|
||||
public double? WideFocusX { get; set; }
|
||||
public double? WideFocusY { get; set; }
|
||||
public double? WideZoom { get; set; }
|
||||
public double? TallFocusX { get; set; }
|
||||
public double? TallFocusY { get; set; }
|
||||
public double? TallZoom { get; set; }
|
||||
public string SetHash { get; set; } = string.Empty;
|
||||
public List<HeroVariant> Variants { get; set; } = [];
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public Guid CreatedByUserId { get; set; }
|
||||
public string? OriginalFileName { get; set; }
|
||||
public DateTimeOffset? LastUsedAt { get; set; }
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>One file of the current set: the ratio, its pixel size, the type and the file name inside the set.</summary>
|
||||
public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes);
|
||||
|
||||
|
||||
@@ -6,14 +6,8 @@ using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage? storage = null) : DbContext(options)
|
||||
public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(options)
|
||||
{
|
||||
/// <summary>True while the photos live in the library's table (phase 3b); see <see cref="HeroStorage"/>.</summary>
|
||||
public bool PhotosInLibrary { get; } = storage?.PhotosInLibrary ?? false;
|
||||
|
||||
/// <summary>The photos as they are always kept in <c>HeroImages</c> (only mapped while <see cref="PhotosInLibrary"/> is on).</summary>
|
||||
public DbSet<LegacyHeroImage> LegacyHeroImages => Set<LegacyHeroImage>();
|
||||
|
||||
public DbSet<HeroContent> Heroes => Set<HeroContent>();
|
||||
public DbSet<HeroSlide> HeroSlides => Set<HeroSlide>();
|
||||
public DbSet<HeroImage> HeroImages => Set<HeroImage>();
|
||||
@@ -69,7 +63,14 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage?
|
||||
|
||||
modelBuilder.Entity<HeroImage>(entity =>
|
||||
{
|
||||
entity.ToTable("HeroImages");
|
||||
// The hero's photos are rows of the library's table (module "hero"). The library (Media module) owns that table and its
|
||||
// migrations, the hero's context only excludes it from its own.
|
||||
entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations());
|
||||
entity.Property<string>(ModuleColumn).HasMaxLength(20).IsRequired().HasDefaultValue(HeroMapping.Area);
|
||||
entity.Property(e => e.FocusX).HasColumnName("DefaultFocusX");
|
||||
entity.Property(e => e.FocusY).HasColumnName("DefaultFocusY");
|
||||
entity.Property(e => e.Zoom).HasColumnName("DefaultZoom");
|
||||
entity.HasQueryFilter(e => EF.Property<string>(e, ModuleColumn) == HeroMapping.Area);
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.OriginalSha256).IsRequired().HasMaxLength(64);
|
||||
entity.HasIndex(e => e.OriginalSha256).IsUnique();
|
||||
@@ -93,8 +94,6 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage?
|
||||
v => v.ToList()));
|
||||
});
|
||||
|
||||
ConfigureStorage(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<HeroRetiredFile>(entity =>
|
||||
{
|
||||
entity.ToTable("HeroRetiredFiles");
|
||||
@@ -105,39 +104,6 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage?
|
||||
});
|
||||
}
|
||||
|
||||
private void ConfigureStorage(ModelBuilder modelBuilder)
|
||||
{
|
||||
if (!PhotosInLibrary)
|
||||
{
|
||||
modelBuilder.Ignore<LegacyHeroImage>();
|
||||
return;
|
||||
}
|
||||
|
||||
// The hero's photos are rows of the library's table, module "hero". The library owns that table and its migrations.
|
||||
modelBuilder.Entity<HeroImage>(entity =>
|
||||
{
|
||||
entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations());
|
||||
entity.Property<string>(ModuleColumn).HasMaxLength(20).IsRequired().HasDefaultValue(HeroMapping.Area);
|
||||
entity.Property(e => e.FocusX).HasColumnName("DefaultFocusX");
|
||||
entity.Property(e => e.FocusY).HasColumnName("DefaultFocusY");
|
||||
entity.Property(e => e.Zoom).HasColumnName("DefaultZoom");
|
||||
entity.HasQueryFilter(e => EF.Property<string>(e, ModuleColumn) == HeroMapping.Area);
|
||||
});
|
||||
|
||||
// And HeroImages stays a complete copy, so switching back loses nothing.
|
||||
modelBuilder.Entity<LegacyHeroImage>(entity =>
|
||||
{
|
||||
entity.ToTable("HeroImages");
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
|
||||
entity.Property(e => e.Variants)
|
||||
.HasConversion(
|
||||
variants => JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null),
|
||||
json => JsonSerializer.Deserialize<List<HeroVariant>>(json, (JsonSerializerOptions?)null) ?? new List<HeroVariant>())
|
||||
.HasColumnType("longtext");
|
||||
});
|
||||
}
|
||||
|
||||
public const string ModuleColumn = "Module";
|
||||
|
||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||
@@ -155,11 +121,6 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage?
|
||||
/// <summary>A photo the hero adds to the library's table belongs to the hero module.</summary>
|
||||
private void MarkNewPhotosAsHero()
|
||||
{
|
||||
if (!PhotosInLibrary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entry in ChangeTracker.Entries<HeroImage>().Where(e => e.State == EntityState.Added))
|
||||
{
|
||||
entry.Property(ModuleColumn).CurrentValue = HeroMapping.Area;
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Runs before the migrations at start-up. The migration <c>DropHeroImagesTable</c> destroys <c>HeroImages</c>; this refuses to let
|
||||
/// it run (it throws, the application does not start, nothing is dropped) unless every photo of <c>HeroImages</c> is in the
|
||||
/// library's <c>MediaAssets</c> (module hero) with the same hash, set and variants. A database where <c>HeroImages</c> is empty
|
||||
/// (a new one) passes without the library table having to exist yet.
|
||||
/// </summary>
|
||||
public static class HeroImagesDropGuard
|
||||
{
|
||||
public static void Verify(HeroDbContext db)
|
||||
{
|
||||
// A database that does not exist yet (a new environment) has nothing to protect; Migrate creates it.
|
||||
if (!db.Database.CanConnect())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!db.Database.GetPendingMigrations().Any(id => id.EndsWith("_DropHeroImagesTable", StringComparison.Ordinal)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Exists(db, "HeroImages") || Scalar(db, "SELECT COUNT(*) FROM `HeroImages`") == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Exists(db, "MediaAssets"))
|
||||
{
|
||||
throw new InvalidOperationException("HeroImages holds photos but the library table MediaAssets does not exist: the hero's photos are not in the library, so HeroImages is not dropped. Run the media library copy first.");
|
||||
}
|
||||
|
||||
var missing = Scalar(db, @"SELECT COUNT(*) FROM `HeroImages` h
|
||||
LEFT JOIN `MediaAssets` m ON m.`Id` = h.`Id` AND m.`Module` = 'hero'
|
||||
WHERE m.`Id` IS NULL
|
||||
OR NOT (m.`OriginalSha256` <=> h.`OriginalSha256`)
|
||||
OR NOT (m.`SetHash` <=> h.`SetHash`)
|
||||
OR NOT (m.`Variants` <=> h.`Variants`)");
|
||||
if (missing > 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{missing} photo(s) of HeroImages are not in the library table MediaAssets (or differ in hash, set or variants), so HeroImages is not dropped. Run the media library copy first and check it.");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Exists(HeroDbContext db, string table)
|
||||
=> Scalar(db, $"SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{table}'") > 0;
|
||||
|
||||
private static long Scalar(HeroDbContext db, string sql)
|
||||
{
|
||||
var connection = db.Database.GetDbConnection();
|
||||
var wasOpen = connection.State == System.Data.ConnectionState.Open;
|
||||
if (!wasOpen)
|
||||
{
|
||||
connection.Open();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
return Convert.ToInt64(command.ExecuteScalar());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!wasOpen)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Where the hero's photos are stored (media library phase 3b). Off (default): the hero's own table <c>HeroImages</c>. On
|
||||
/// (<c>Media__Library__HeroPhotosInLibrary=true</c>): the library's <c>MediaAssets</c> table (rows of module <c>hero</c>), with
|
||||
/// <c>HeroImages</c> kept up to date as a safety net so the switch can go back. The choice is made when the application starts.
|
||||
/// </summary>
|
||||
public sealed record HeroStorage(bool PhotosInLibrary);
|
||||
|
||||
/// <summary>EF builds the model once per key: the two storage modes have different models.</summary>
|
||||
public sealed class HeroModelCacheKeyFactory : IModelCacheKeyFactory
|
||||
{
|
||||
public object Create(DbContext context, bool designTime)
|
||||
=> (context.GetType(), context is HeroDbContext { PhotosInLibrary: true }, designTime);
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
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;
|
||||
@@ -28,21 +26,12 @@ public class HeroModule : IModule
|
||||
|
||||
public void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
// Where the photos are stored (media library phase 3b): off = HeroImages, on = the library's MediaAssets table.
|
||||
services.AddSingleton(serviceProvider => new HeroStorage(serviceProvider.GetRequiredService<IConfiguration>().GetValue<bool>("Media:Library:HeroPhotosInLibrary")));
|
||||
services.AddDbContext<HeroDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
||||
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
options.ReplaceService<IModelCacheKeyFactory, HeroModelCacheKeyFactory>();
|
||||
if (serviceProvider.GetRequiredService<HeroStorage>().PhotosInLibrary)
|
||||
{
|
||||
// The model differs from the migrations snapshot on purpose (the photos' table belongs to the library).
|
||||
options.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
|
||||
}
|
||||
});
|
||||
services.AddScoped<IHeroLegacyImageMirror, HeroLegacyImageMirror>();
|
||||
|
||||
services.AddCmsTextsFrom(typeof(HeroModule).Assembly);
|
||||
services.AddOptions<HeroOptions>().Configure<IConfiguration>((options, configuration) =>
|
||||
@@ -58,8 +47,6 @@ public class HeroModule : IModule
|
||||
services.AddScoped<IHeroImageService, HeroImageService>();
|
||||
services.AddScoped<HeroMediaUsageProvider>();
|
||||
services.AddScoped<IMediaUsageProvider>(provider => provider.GetRequiredService<HeroMediaUsageProvider>());
|
||||
services.AddScoped<IMediaReadVerifier>(provider => provider.GetRequiredService<HeroMediaUsageProvider>());
|
||||
services.AddScoped<IMediaMigrationSource, HeroMediaMigrationSource>();
|
||||
services.AddScoped<IHeroPageModel, HeroPageModel>();
|
||||
services.AddScoped<IHtmlPageTransform, HeroHtmlTransform>();
|
||||
services.AddScoped<ICmsDataBlockContributor, HeroDataContributor>();
|
||||
@@ -73,7 +60,7 @@ public class HeroModule : IModule
|
||||
{
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
|
||||
HeroImagesDropGuard.Verify(db);
|
||||
db.Database.Migrate();
|
||||
HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None).GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
+304
@@ -0,0 +1,304 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
[DbContext(typeof(HeroDbContext))]
|
||||
[Migration("20261117231338_DropHeroImagesTable")]
|
||||
partial class DropHeroImagesTable
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.9")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Button1Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button1Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Button2Label")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("varchar(100)");
|
||||
|
||||
b.Property<string>("Button2Target")
|
||||
.HasMaxLength(500)
|
||||
.HasColumnType("varchar(500)");
|
||||
|
||||
b.Property<string>("Display")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<string>("Language")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<Guid>("LastModifiedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Lead")
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<string>("Tagline")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.IsRequired()
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("varchar(1000)");
|
||||
|
||||
b.Property<DateTimeOffset>("UpdatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Language")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Heroes", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<long>("Bytes")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<Guid>("CreatedByUserId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<double>("FocusX")
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultFocusX");
|
||||
|
||||
b.Property<double>("FocusY")
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultFocusY");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)")
|
||||
.HasDefaultValue("hero");
|
||||
|
||||
b.Property<string>("OriginalFileName")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalKey")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("OriginalSha256")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<string>("SourceFormat")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<double?>("TallFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<string>("Variants")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<double?>("WideFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<int>("Width")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Zoom")
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultZoom");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MediaAssets", null, t =>
|
||||
{
|
||||
t.ExcludeFromMigrations();
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.IsRequired()
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
|
||||
b.Property<string>("Kind")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<DateTimeOffset>("RetiredAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("HeroRetiredFiles", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Alt")
|
||||
.IsRequired()
|
||||
.HasMaxLength(600)
|
||||
.HasColumnType("varchar(600)");
|
||||
|
||||
b.Property<bool>("Decorative")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<double?>("FocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("FocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<Guid>("HeroId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("ImageId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<int>("SortOrder")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double?>("TallFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("TallZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<string>("Variants")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<double?>("WideFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("WideZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double?>("Zoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ImageId");
|
||||
|
||||
b.HasIndex("HeroId", "SortOrder");
|
||||
|
||||
b.ToTable("HeroSlides", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
|
||||
{
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
|
||||
.WithMany("Slides")
|
||||
.HasForeignKey("HeroId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
|
||||
.WithMany()
|
||||
.HasForeignKey("ImageId")
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Image");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
|
||||
{
|
||||
b.Navigation("Slides");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Migrations
|
||||
{
|
||||
/// <summary>
|
||||
/// Phase 3c of the media library: the hero's photos live in the library's <c>MediaAssets</c> table, so <c>HeroImages</c> goes.
|
||||
/// DESTRUCTIVE: the table and its rows are dropped. <see cref="Data.HeroImagesDropGuard"/> runs before this migration and
|
||||
/// refuses to continue (the application does not start, nothing is dropped) unless every photo of <c>HeroImages</c> is in
|
||||
/// <c>MediaAssets</c> with the same hash, set and variants. <c>Down</c> only brings back the EMPTY table; the data comes
|
||||
/// back from the restore point (database dump) that is taken before this migration runs.
|
||||
/// The model snapshot is the generated one (a slide's photo is a relation to the library table); the foreign key itself is
|
||||
/// deliberately not created in the database (see <c>DropSlideImageForeignKey</c>): the photo lives in another module's table.
|
||||
/// </summary>
|
||||
public partial class DropHeroImagesTable : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(name: "HeroImages");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "HeroImages",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
OriginalSha256 = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false),
|
||||
OriginalKey = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false),
|
||||
SourceFormat = table.Column<string>(type: "varchar(10)", maxLength: 10, nullable: false),
|
||||
Width = table.Column<int>(type: "int", nullable: false),
|
||||
Height = table.Column<int>(type: "int", nullable: false),
|
||||
Bytes = table.Column<long>(type: "bigint", nullable: false),
|
||||
FocusX = table.Column<double>(type: "double", nullable: false),
|
||||
FocusY = table.Column<double>(type: "double", nullable: false),
|
||||
Zoom = table.Column<double>(type: "double", nullable: false),
|
||||
WideFocusX = table.Column<double>(type: "double", nullable: true),
|
||||
WideFocusY = table.Column<double>(type: "double", nullable: true),
|
||||
WideZoom = table.Column<double>(type: "double", nullable: true),
|
||||
TallFocusX = table.Column<double>(type: "double", nullable: true),
|
||||
TallFocusY = table.Column<double>(type: "double", nullable: true),
|
||||
TallZoom = table.Column<double>(type: "double", nullable: true),
|
||||
SetHash = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: false),
|
||||
Variants = table.Column<string>(type: "longtext", nullable: false),
|
||||
CreatedAt = table.Column<DateTimeOffset>(type: "datetime", nullable: false),
|
||||
CreatedByUserId = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
OriginalFileName = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true),
|
||||
LastUsedAt = table.Column<DateTimeOffset>(type: "datetime", nullable: true),
|
||||
DeletedAt = table.Column<DateTimeOffset>(type: "datetime", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_HeroImages", x => x.Id);
|
||||
})
|
||||
.Annotation("MySQL:Charset", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_HeroImages_OriginalSha256",
|
||||
table: "HeroImages",
|
||||
column: "OriginalSha256",
|
||||
unique: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,10 +97,12 @@ namespace SlpModularCms.Modules.Hero.Migrations
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<double>("FocusX")
|
||||
.HasColumnType("double");
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultFocusX");
|
||||
|
||||
b.Property<double>("FocusY")
|
||||
.HasColumnType("double");
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultFocusY");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
@@ -108,6 +110,13 @@ namespace SlpModularCms.Modules.Hero.Migrations
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)")
|
||||
.HasDefaultValue("hero");
|
||||
|
||||
b.Property<string>("OriginalFileName")
|
||||
.HasMaxLength(200)
|
||||
.HasColumnType("varchar(200)");
|
||||
@@ -158,14 +167,18 @@ namespace SlpModularCms.Modules.Hero.Migrations
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Zoom")
|
||||
.HasColumnType("double");
|
||||
.HasColumnType("double")
|
||||
.HasColumnName("DefaultZoom");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("HeroImages", (string)null);
|
||||
b.ToTable("MediaAssets", null, t =>
|
||||
{
|
||||
t.ExcludeFromMigrations();
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
|
||||
|
||||
@@ -113,24 +113,8 @@ public class HeroImageService(
|
||||
ILogger<HeroImageService> logger,
|
||||
TimeProvider? time = null,
|
||||
IIndexHtmlCache? indexCache = null,
|
||||
HeroRenderSignal? signal = null,
|
||||
IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = null) : IHeroImageService
|
||||
HeroRenderSignal? signal = null) : IHeroImageService
|
||||
{
|
||||
/// <summary>Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws.</summary>
|
||||
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<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null)
|
||||
@@ -153,7 +137,6 @@ 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();
|
||||
@@ -193,7 +176,6 @@ 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);
|
||||
@@ -240,7 +222,6 @@ public class HeroImageService(
|
||||
|
||||
ApplyFocus(image, request);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await SyncLibraryAsync();
|
||||
signal?.Reset(imageId);
|
||||
signal?.Notify();
|
||||
|
||||
@@ -261,7 +242,6 @@ public class HeroImageService(
|
||||
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();
|
||||
|
||||
@@ -285,7 +265,6 @@ public class HeroImageService(
|
||||
}
|
||||
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await SyncLibraryAsync();
|
||||
signal?.Reset(slideId);
|
||||
indexCache?.Invalidate();
|
||||
|
||||
@@ -475,7 +454,6 @@ public class HeroImageService(
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -529,7 +507,6 @@ 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;
|
||||
@@ -548,7 +525,6 @@ 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);
|
||||
}
|
||||
@@ -572,7 +548,6 @@ 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);
|
||||
@@ -597,7 +572,6 @@ public class HeroImageService(
|
||||
var (setHash, originalKey) = (image.SetHash, image.OriginalKey);
|
||||
repository.RemoveImage(image);
|
||||
await repository.SaveChangesAsync(cancellationToken);
|
||||
await SyncLibraryAsync();
|
||||
|
||||
if (setHash.Length > 0 && !setShared)
|
||||
{
|
||||
@@ -624,7 +598,6 @@ 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);
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
/// <summary>Keeps <c>HeroImages</c> a complete copy of the hero's photos while they live in the library's table (phase 3b).</summary>
|
||||
public interface IHeroLegacyImageMirror
|
||||
{
|
||||
/// <summary>Does nothing unless the photos live in the library. Never throws: the library rows are the truth then.</summary>
|
||||
Task SyncAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public sealed class HeroLegacyImageMirror(HeroDbContext db, ILogger<HeroLegacyImageMirror> logger) : IHeroLegacyImageMirror
|
||||
{
|
||||
public async Task SyncAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!db.PhotosInLibrary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var library = await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken);
|
||||
var legacy = (await db.LegacyHeroImages.ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
|
||||
foreach (var photo in library)
|
||||
{
|
||||
if (!legacy.Remove(photo.Id, out var row))
|
||||
{
|
||||
row = new LegacyHeroImage { Id = photo.Id };
|
||||
db.LegacyHeroImages.Add(row);
|
||||
}
|
||||
|
||||
Copy(photo, row);
|
||||
}
|
||||
|
||||
db.LegacyHeroImages.RemoveRange(legacy.Values);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(exception, "Keeping HeroImages up to date with the library photos failed; the library rows are the truth, the next change repairs it.");
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Copy(HeroImage from, LegacyHeroImage to)
|
||||
{
|
||||
to.OriginalSha256 = from.OriginalSha256;
|
||||
to.OriginalKey = from.OriginalKey;
|
||||
to.SourceFormat = from.SourceFormat;
|
||||
to.Width = from.Width;
|
||||
to.Height = from.Height;
|
||||
to.Bytes = from.Bytes;
|
||||
(to.FocusX, to.FocusY, to.Zoom) = (from.FocusX, from.FocusY, from.Zoom);
|
||||
(to.WideFocusX, to.WideFocusY, to.WideZoom) = (from.WideFocusX, from.WideFocusY, from.WideZoom);
|
||||
(to.TallFocusX, to.TallFocusY, to.TallZoom) = (from.TallFocusX, from.TallFocusY, from.TallZoom);
|
||||
to.SetHash = from.SetHash;
|
||||
to.Variants = [.. from.Variants];
|
||||
to.CreatedAt = from.CreatedAt;
|
||||
to.CreatedByUserId = from.CreatedByUserId;
|
||||
to.OriginalFileName = from.OriginalFileName;
|
||||
to.LastUsedAt = from.LastUsedAt;
|
||||
to.DeletedAt = from.DeletedAt;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run at start-up when the photos are to live in the library: the library rows must already be a complete copy of
|
||||
/// <c>HeroImages</c> (same ids, hashes, sets and variants), otherwise the application refuses to start with a clear message
|
||||
/// instead of showing a hero with missing photos. Fix: run the verified copy first (Media__Library__CopyOnStartup) and check
|
||||
/// the library check, then switch the storage.
|
||||
/// </summary>
|
||||
public static class HeroPhotoStorageGuard
|
||||
{
|
||||
public static async Task VerifyAsync(HeroDbContext db, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!db.PhotosInLibrary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var library = (await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
|
||||
var problems = new List<string>();
|
||||
foreach (var legacy in await db.LegacyHeroImages.AsNoTracking().ToListAsync(cancellationToken))
|
||||
{
|
||||
if (!library.TryGetValue(legacy.Id, out var photo))
|
||||
{
|
||||
problems.Add($"photo {legacy.Id} is not in the library");
|
||||
}
|
||||
else if (photo.OriginalSha256 != legacy.OriginalSha256 || photo.SetHash != legacy.SetHash || HeroMediaMigrationSource.Json(photo.Variants) != HeroMediaMigrationSource.Json(legacy.Variants))
|
||||
{
|
||||
problems.Add($"photo {legacy.Id} differs from the library (hash, set or variants)");
|
||||
}
|
||||
}
|
||||
|
||||
if (problems.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Media__Library__HeroPhotosInLibrary is on, but the library does not hold every hero photo as HeroImages does: "
|
||||
+ string.Join("; ", problems.Take(5))
|
||||
+ (problems.Count > 5 ? $" (and {problems.Count - 5} more)" : string.Empty)
|
||||
+ ". Run the media library copy first (Media__Library__CopyOnStartup=true), check it with the library check, then switch the storage.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,7 @@ public class HeroMediaCleanup(
|
||||
IMediaStore store,
|
||||
IOptions<MediaOptions> options,
|
||||
ILogger<HeroMediaCleanup> logger,
|
||||
TimeProvider? time = null,
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = null)
|
||||
TimeProvider? time = null)
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
|
||||
@@ -44,25 +42,14 @@ 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);
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
using SlpModularCms.Modules.Hero.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Offers the hero's photos (also those in the trash) and slides to the media library's copy, with the same ids, hashes and
|
||||
/// set hashes the hero uses. Read-only: the hero's tables are never changed by the copy.
|
||||
/// </summary>
|
||||
public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrationSource
|
||||
{
|
||||
public string Module => HeroMapping.Area;
|
||||
|
||||
public async Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken)
|
||||
=> (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).Select(ToSnapshot).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var images = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id);
|
||||
var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken);
|
||||
var uses = new List<MediaUseSnapshot>();
|
||||
foreach (var hero in slides.GroupBy(s => s.HeroId))
|
||||
{
|
||||
var ordered = hero.OrderBy(s => s.SortOrder).ThenBy(s => s.Id).ToList();
|
||||
for (var index = 0; index < ordered.Count; index++)
|
||||
{
|
||||
var slide = ordered[index];
|
||||
var setHash = string.Empty;
|
||||
IReadOnlyList<MediaCropSnapshot>? 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));
|
||||
}
|
||||
}
|
||||
|
||||
return uses;
|
||||
}
|
||||
|
||||
private MediaAssetSnapshot ToSnapshot(HeroImage i)
|
||||
=> new(
|
||||
i.Id,
|
||||
Module,
|
||||
i.OriginalSha256,
|
||||
i.OriginalKey,
|
||||
i.SourceFormat,
|
||||
i.Width,
|
||||
i.Height,
|
||||
i.Bytes,
|
||||
i.FocusX,
|
||||
i.FocusY,
|
||||
i.Zoom,
|
||||
i.SetHash,
|
||||
[Crop("wide", i), Crop("tall", i)],
|
||||
i.CreatedAt,
|
||||
i.CreatedByUserId,
|
||||
i.OriginalFileName,
|
||||
i.LastUsedAt,
|
||||
i.DeletedAt,
|
||||
Json(i.Variants),
|
||||
i.WideFocusX,
|
||||
i.WideFocusY,
|
||||
i.WideZoom,
|
||||
i.TallFocusX,
|
||||
i.TallFocusY,
|
||||
i.TallZoom);
|
||||
|
||||
/// <summary>The variants as the hero stores them (the same serializer as its database column), so they can go back unchanged.</summary>
|
||||
public static string Json(IEnumerable<HeroVariant> variants) => System.Text.Json.JsonSerializer.Serialize(variants.ToList(), (System.Text.Json.JsonSerializerOptions?)null);
|
||||
|
||||
private static MediaCropSnapshot Crop(string ratio, HeroImage image)
|
||||
{
|
||||
var focus = image.FocusFor(ratio);
|
||||
return new MediaCropSnapshot(ratio, focus.X, focus.Y, focus.Zoom);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Media;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
@@ -10,59 +9,21 @@ 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 <see cref="HeroMediaCleanup"/> after the trash period.
|
||||
/// </summary>
|
||||
public class HeroMediaUsageProvider(
|
||||
IHeroRepository repository,
|
||||
IHeroImageService images,
|
||||
IMediaLibraryReader? reader = null,
|
||||
ILogger<HeroMediaUsageProvider>? logger = null) : IMediaUsageProvider, IMediaReadVerifier
|
||||
public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider
|
||||
{
|
||||
public const string SlideKind = "slide";
|
||||
|
||||
public string Module => HeroMapping.Area;
|
||||
|
||||
private MediaReadMode Mode => reader?.Mode ?? MediaReadMode.Off;
|
||||
|
||||
public async Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: false, cancellationToken) is { } fromLibrary)
|
||||
{
|
||||
return fromLibrary;
|
||||
}
|
||||
|
||||
await CompareInTheLogAsync(cancellationToken);
|
||||
return (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
}
|
||||
=> (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: true, cancellationToken) is { } fromLibrary)
|
||||
{
|
||||
return fromLibrary;
|
||||
}
|
||||
|
||||
return (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
}
|
||||
=> (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> 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.
|
||||
@@ -83,64 +44,6 @@ public class HeroMediaUsageProvider(
|
||||
return uses;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private async Task<IReadOnlyList<MediaPhoto>?> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>In compare mode: every difference between the hero's tables and the library tables is a warning in the log.</summary>
|
||||
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<MediaReadCheck> 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<int?> TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -30,9 +30,7 @@ public class HeroService(
|
||||
ILogger<HeroService> logger,
|
||||
TimeProvider? time = null,
|
||||
IIndexHtmlCache? indexCache = null,
|
||||
ITextCatalog? texts = null,
|
||||
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
|
||||
IHeroLegacyImageMirror? legacyMirror = null) : IHeroService
|
||||
ITextCatalog? texts = null) : IHeroService
|
||||
{
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
|
||||
@@ -157,15 +155,6 @@ 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);
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using MySql.Data.MySqlClient;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Tests;
|
||||
|
||||
/// <summary>The library's photo table against a real MariaDB (runs when <c>ConnectionStrings__DefaultConnection</c> is set; CI sets it).</summary>
|
||||
public class MediaAssetsTableTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task The_migrations_create_the_photo_table_with_everything_the_hero_needs_to_publish()
|
||||
{
|
||||
var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
|
||||
if (string.IsNullOrWhiteSpace(baseString))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var connectionString = new MySqlConnectionStringBuilder(baseString) { Database = "media_table_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString;
|
||||
try
|
||||
{
|
||||
await using var db = new MediaDbContext(new DbContextOptionsBuilder<MediaDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().Options);
|
||||
await db.Database.MigrateAsync();
|
||||
var id = Guid.NewGuid();
|
||||
db.MediaAssets.Add(new Data.Entities.MediaAsset
|
||||
{
|
||||
Id = id,
|
||||
Module = "hero",
|
||||
OriginalSha256 = new string('a', 64),
|
||||
OriginalKey = "originals/a.jpg",
|
||||
SourceFormat = "jpg",
|
||||
Width = 10,
|
||||
Height = 10,
|
||||
Bytes = 10,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
CreatedByUserId = Guid.NewGuid(),
|
||||
SetHash = "0123456789abcdef",
|
||||
Variants = "[]",
|
||||
WideFocusX = 0.2,
|
||||
TallZoom = 1.3,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
var row = await db.MediaAssets.AsNoTracking().SingleAsync(a => a.Id == id);
|
||||
(row.Module, row.SetHash, row.Variants, row.WideFocusX, row.TallZoom).Should().Be(("hero", "0123456789abcdef", "[]", 0.2, 1.3));
|
||||
}
|
||||
finally
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Tests;
|
||||
|
||||
public class MediaLibraryCopierTests : IDisposable
|
||||
{
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero);
|
||||
private readonly MediaDbContext _db = new(new DbContextOptionsBuilder<MediaDbContext>().UseInMemoryDatabase("media-" + Guid.NewGuid()).Options);
|
||||
|
||||
public void Dispose() => _db.Dispose();
|
||||
|
||||
private sealed class Source(string module, List<MediaAssetSnapshot> assets, List<MediaUseSnapshot> uses) : IMediaMigrationSource
|
||||
{
|
||||
public string Module => module;
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>(assets.ToList());
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaUseSnapshot>>(uses.ToList());
|
||||
}
|
||||
|
||||
private static MediaAssetSnapshot Asset(string module = "hero", string? sha = null, string setHash = "0123456789abcdef", DateTimeOffset? deleted = null)
|
||||
=> new(Guid.NewGuid(), module, sha ?? Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.4, 0.6, 1.5, setHash,
|
||||
[new MediaCropSnapshot("wide", 0.4, 0.6, 1.5), new MediaCropSnapshot("tall", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), deleted);
|
||||
|
||||
private static MediaUseSnapshot Use(MediaAssetSnapshot asset, int position = 1, bool active = true)
|
||||
=> new(Guid.NewGuid(), asset.Id, asset.Module, "slide", position, active, asset.SetHash);
|
||||
|
||||
private MediaLibraryCopier Copier(params IMediaMigrationSource[] sources) => new(_db, sources, NullLogger<MediaLibraryCopier>.Instance);
|
||||
|
||||
[Fact]
|
||||
public async Task Copies_the_photos_and_uses_with_the_same_ids_hashes_and_set_hashes()
|
||||
{
|
||||
var a = Asset();
|
||||
var b = Asset(deleted: Now.AddDays(-1));
|
||||
var use = Use(a, 3, active: false);
|
||||
|
||||
var report = await Copier(new Source("hero", [a, b], [use])).CopyAsync(CancellationToken.None);
|
||||
|
||||
report.Should().Match<MediaCopyReport>(r => r.Assets == 2 && r.Uses == 1 && r.Verified);
|
||||
var copy = await _db.MediaAssets.Include(x => x.Uses).SingleAsync(x => x.Id == a.Id);
|
||||
copy.OriginalSha256.Should().Be(a.OriginalSha256);
|
||||
copy.OriginalKey.Should().Be(a.OriginalKey);
|
||||
copy.OriginalFileName.Should().Be("zomer.jpg");
|
||||
copy.LastUsedAt.Should().Be(a.LastUsedAt);
|
||||
copy.DefaultFocusX.Should().Be(0.4);
|
||||
copy.Uses.Should().ContainSingle().Which.Should().Match<Data.Entities.MediaAssetUse>(u => u.Id == use.Id && u.SetHash == a.SetHash && u.Position == 3 && !u.Active && u.PlaceKind == "slide");
|
||||
copy.Uses[0].Crops.Should().Contain("wide").And.Contain("0.4");
|
||||
(await _db.MediaAssets.SingleAsync(x => x.Id == b.Id)).DeletedAt.Should().Be(b.DeletedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Copying_twice_gives_the_same_result()
|
||||
{
|
||||
var a = Asset();
|
||||
var copier = Copier(new Source("hero", [a], [Use(a)]));
|
||||
|
||||
await copier.CopyAsync(CancellationToken.None);
|
||||
var second = await copier.CopyAsync(CancellationToken.None);
|
||||
|
||||
second.Should().Match<MediaCopyReport>(r => r.Assets == 1 && r.Uses == 1 && r.Verified);
|
||||
(await _db.MediaAssets.CountAsync()).Should().Be(1);
|
||||
(await _db.MediaUses.CountAsync()).Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_copy_mirrors_the_source_a_changed_photo_is_updated_and_a_removed_one_leaves()
|
||||
{
|
||||
var a = Asset();
|
||||
var gone = Asset();
|
||||
var assets = new List<MediaAssetSnapshot> { a, gone };
|
||||
var uses = new List<MediaUseSnapshot> { Use(a), Use(gone) };
|
||||
var copier = Copier(new Source("hero", assets, uses));
|
||||
await copier.CopyAsync(CancellationToken.None);
|
||||
|
||||
assets.Remove(gone);
|
||||
uses.RemoveAll(u => u.AssetId == gone.Id);
|
||||
assets[0] = a with { SetHash = "fedcba9876543210", OriginalFileName = "nieuw.jpg" };
|
||||
uses[0] = uses[0] with { SetHash = "fedcba9876543210" };
|
||||
await copier.CopyAsync(CancellationToken.None);
|
||||
|
||||
(await _db.MediaAssets.ToListAsync()).Should().ContainSingle().Which.OriginalFileName.Should().Be("nieuw.jpg");
|
||||
(await _db.MediaUses.ToListAsync()).Should().ContainSingle().Which.SetHash.Should().Be("fedcba9876543210");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Photos_of_a_module_without_a_source_are_left_alone()
|
||||
{
|
||||
var other = Asset(module: "gallery");
|
||||
_db.MediaAssets.Add(new Data.Entities.MediaAsset { Id = other.Id, Module = "gallery", OriginalSha256 = other.OriginalSha256, OriginalKey = "k", SourceFormat = "jpg" });
|
||||
await _db.SaveChangesAsync();
|
||||
var a = Asset();
|
||||
|
||||
await Copier(new Source("hero", [a], [])).CopyAsync(CancellationToken.None);
|
||||
|
||||
(await _db.MediaAssets.CountAsync()).Should().Be(2);
|
||||
(await _db.MediaAssets.AnyAsync(x => x.Id == other.Id)).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Two_sources_are_copied_together()
|
||||
{
|
||||
var hero = Asset();
|
||||
var gallery = Asset(module: "gallery");
|
||||
|
||||
var report = await Copier(new Source("hero", [hero], [Use(hero)]), new Source("gallery", [gallery], [])).CopyAsync(CancellationToken.None);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using MySql.Data.MySqlClient;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The copy against a real MariaDB in a database of its own (created and dropped here): the migration, the foreign key and the
|
||||
/// rollback of a failed copy. Runs when <c>ConnectionStrings__DefaultConnection</c> is set (CI sets it); otherwise it does nothing.
|
||||
/// </summary>
|
||||
public class MediaLibraryMariaDbTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private static string? ConnectionString()
|
||||
{
|
||||
var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
|
||||
if (string.IsNullOrWhiteSpace(baseString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new MySqlConnectionStringBuilder(baseString) { Database = "media_library_test_" + Guid.NewGuid().ToString("N")[..12] };
|
||||
return builder.ConnectionString;
|
||||
}
|
||||
|
||||
private static MediaDbContext Open(string connectionString)
|
||||
=> new(new DbContextOptionsBuilder<MediaDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().Options);
|
||||
|
||||
private sealed class Source(List<MediaAssetSnapshot> assets, List<MediaUseSnapshot> uses) : IMediaMigrationSource
|
||||
{
|
||||
public string Module => "hero";
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>(assets);
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaUseSnapshot>>(uses);
|
||||
}
|
||||
|
||||
private static MediaAssetSnapshot Asset(string sha)
|
||||
=> new(Guid.NewGuid(), "hero", sha, "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.4, 0.6, 1.5, "0123456789abcdef",
|
||||
[new MediaCropSnapshot("wide", 0.4, 0.6, 1.5)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), null);
|
||||
|
||||
[Fact]
|
||||
public async Task The_migration_creates_the_tables_and_a_verified_copy_survives_a_second_run()
|
||||
{
|
||||
var connection = ConnectionString();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using var db = Open(connection);
|
||||
try
|
||||
{
|
||||
await db.Database.MigrateAsync();
|
||||
var a = Asset(new string('a', 64));
|
||||
var b = Asset(new string('b', 64));
|
||||
var uses = new List<MediaUseSnapshot> { new(Guid.NewGuid(), a.Id, "hero", "slide", 1, true, a.SetHash) };
|
||||
var copier = new MediaLibraryCopier(db, [new Source([a, b], uses)], NullLogger<MediaLibraryCopier>.Instance);
|
||||
|
||||
var first = await copier.CopyAsync(CancellationToken.None);
|
||||
var second = await copier.CopyAsync(CancellationToken.None);
|
||||
|
||||
first.Should().Match<MediaCopyReport>(r => r.Assets == 2 && r.Uses == 1 && r.Verified);
|
||||
second.Verified.Should().BeTrue();
|
||||
(await db.MediaAssets.CountAsync()).Should().Be(2);
|
||||
(await db.MediaUses.CountAsync()).Should().Be(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await db.Database.EnsureDeletedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_failed_copy_is_rolled_back_completely()
|
||||
{
|
||||
var connection = ConnectionString();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using var db = Open(connection);
|
||||
try
|
||||
{
|
||||
await db.Database.MigrateAsync();
|
||||
var good = Asset(new string('c', 64));
|
||||
await new MediaLibraryCopier(db, [new Source([good], [])], NullLogger<MediaLibraryCopier>.Instance).CopyAsync(CancellationToken.None);
|
||||
|
||||
// Two photos with the same hash break the unique index: the whole copy must be undone, the earlier copy stays.
|
||||
var one = Asset(new string('d', 64));
|
||||
var two = Asset(new string('d', 64));
|
||||
var failing = new MediaLibraryCopier(db, [new Source([good, one, two], [])], NullLogger<MediaLibraryCopier>.Instance);
|
||||
|
||||
var act = () => failing.CopyAsync(CancellationToken.None);
|
||||
|
||||
await act.Should().ThrowAsync<Exception>();
|
||||
await using var check = Open(connection);
|
||||
(await check.MediaAssets.Select(x => x.Id).ToListAsync()).Should().Equal(good.Id);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await db.Database.EnsureDeletedAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>Phase 2, step C: reading the library tables back, per module, and the mode from the setting.</summary>
|
||||
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<MediaDbContext>().UseInMemoryDatabase("media-read-" + Guid.NewGuid()).Options);
|
||||
|
||||
public void Dispose() => _db.Dispose();
|
||||
|
||||
private sealed class Source(string module, List<MediaAssetSnapshot> assets, List<MediaUseSnapshot> uses) : IMediaMigrationSource
|
||||
{
|
||||
public string Module => module;
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>(assets);
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaUseSnapshot>>(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<MediaLibraryCopier>.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<MediaAssetSnapshot>(a => a.Id == hero.Id && a.OriginalFileName == "zomer.jpg" && a.Bytes == 2_000_000);
|
||||
uses.Should().ContainSingle().Which.Should().Match<MediaUseSnapshot>(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<MediaLibraryCopier>.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"));
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>Phase 2, step A: the write-through mirrors one module into the library tables, only when it is switched on, and never throws.</summary>
|
||||
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<MediaDbContext>().UseInMemoryDatabase("media-sync-" + Guid.NewGuid()).Options);
|
||||
|
||||
public void Dispose() => _db.Dispose();
|
||||
|
||||
private sealed class Source(string module, Func<List<MediaAssetSnapshot>> assets, Func<List<MediaUseSnapshot>> uses, bool fails = false) : IMediaMigrationSource
|
||||
{
|
||||
public string Module => module;
|
||||
|
||||
public Task<IReadOnlyList<MediaAssetSnapshot>> ListAssetsAsync(CancellationToken cancellationToken)
|
||||
=> fails ? throw new InvalidOperationException("source failed") : Task.FromResult<IReadOnlyList<MediaAssetSnapshot>>(assets());
|
||||
|
||||
public Task<IReadOnlyList<MediaUseSnapshot>> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaUseSnapshot>>(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<MediaLibraryCopier>.Instance), Options.Create(new MediaLibraryOptions { DualWrite = on }), NullLogger<MediaLibrarySync>.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<MediaAssetSnapshot> { 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();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
namespace SlpModularCms.Modules.Media;
|
||||
|
||||
/// <summary>Settings of the library's own tables (<c>Media:Library:*</c>). Everything is off by default: a deploy of the code alone only adds the empty tables.</summary>
|
||||
public class MediaLibraryOptions
|
||||
{
|
||||
public const string SectionName = "Media:Library";
|
||||
|
||||
/// <summary>
|
||||
/// Copy the photos and uses of the modules into the library's tables once at start-up and verify the copy
|
||||
/// (<c>Media__Library__CopyOnStartup=true</c>). Safe to repeat: the copy mirrors the source. Never touches the modules' own data.
|
||||
/// </summary>
|
||||
public bool CopyOnStartup { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Write-through (phase 2, step A, <c>Media__Library__DualWrite=true</c>): 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.
|
||||
/// </summary>
|
||||
public bool DualWrite { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Where the library screen reads the photos from (phase 2, step C, <c>Media__Library__ReadFromLibrary</c>): <c>Off</c> (the
|
||||
/// module's own tables, the default), <c>Compare</c> (still those, but differences with the library tables are logged and
|
||||
/// shown by <c>GET media-library/read-check</c>) or <c>On</c> (the library tables; needs <c>DualWrite</c>).
|
||||
/// </summary>
|
||||
public SlpModularCms.Core.Media.Library.MediaReadMode ReadFromLibrary { get; set; } = SlpModularCms.Core.Media.Library.MediaReadMode.Off;
|
||||
}
|
||||
@@ -4,21 +4,16 @@ 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;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Media;
|
||||
|
||||
/// <summary>
|
||||
/// The library's own tables (photos and where they are used). Phase 1 of the migration plan: only the tables, and a copy
|
||||
/// of the modules' photos behind <c>Media:Library:CopyOnStartup</c> (off by default). Nothing reads from these tables yet.
|
||||
/// The library's own table of photos (<c>MediaAssets</c>, with the module that owns each photo). Since phase 3 of the media
|
||||
/// library it is where the hero keeps its photos; the screen, the trash and the clean-up work through the modules' providers.
|
||||
/// The module owns the table and its migrations.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaModule : IModule
|
||||
@@ -34,14 +29,6 @@ public class MediaModule : IModule
|
||||
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
||||
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
});
|
||||
|
||||
services.AddOptions<MediaLibraryOptions>().Configure<IConfiguration>((options, configuration) =>
|
||||
configuration.GetSection(MediaLibraryOptions.SectionName).Bind(options));
|
||||
services.AddScoped<MediaLibraryCopier>();
|
||||
services.Replace(ServiceDescriptor.Scoped<IMediaLibrarySync, MediaLibrarySync>());
|
||||
services.Replace(ServiceDescriptor.Scoped<IMediaLibraryReader, MediaLibraryReader>());
|
||||
services.Replace(ServiceDescriptor.Singleton<IMediaLibrarySwitchState, MediaLibrarySwitchState>());
|
||||
services.AddHostedService<MediaLibraryCopyService>();
|
||||
}
|
||||
|
||||
public void UseModule(IApplicationBuilder app)
|
||||
@@ -51,27 +38,3 @@ public class MediaModule : IModule
|
||||
db.Database.Migrate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Runs the verified copy once at start-up when <c>Media:Library:CopyOnStartup</c> is on; a failure is logged and never stops the host.</summary>
|
||||
public sealed class MediaLibraryCopyService(IServiceScopeFactory scopes, IOptions<MediaLibraryOptions> options, ILogger<MediaLibraryCopyService> logger) : IHostedService
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!options.Value.CopyOnStartup)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var scope = scopes.CreateScope();
|
||||
await scope.ServiceProvider.GetRequiredService<MediaLibraryCopier>().CopyAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(exception, "The media library copy failed and was rolled back; the modules' own data is untouched.");
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <summary>What a copy did and what the verification found.</summary>
|
||||
public sealed record MediaCopyReport(int Assets, int Uses, IReadOnlyList<string> Differences)
|
||||
{
|
||||
public bool Verified => Differences.Count == 0;
|
||||
}
|
||||
|
||||
public sealed class MediaCopyVerificationException(MediaCopyReport report)
|
||||
: Exception($"The copy of the media library does not match its source: {string.Join("; ", report.Differences.Take(5))}")
|
||||
{
|
||||
public MediaCopyReport Report { get; } = report;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the photos and uses of every module (<see cref="IMediaMigrationSource"/>) into the library's own tables and
|
||||
/// verifies the copy against the source (migration plan, decision 13). The copy MIRRORS the source: re-running it gives
|
||||
/// the same result, rows of a module that no longer exist at the source are removed from the copy. The modules' own data
|
||||
/// is never changed. A difference after copying rolls the whole copy back and raises <see cref="MediaCopyVerificationException"/>.
|
||||
/// </summary>
|
||||
public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerable<IMediaMigrationSource> sources, ILogger<MediaLibraryCopier> logger)
|
||||
{
|
||||
private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
|
||||
|
||||
/// <param name="onlyModule">Mirror only this module (the write-through); null = every module (the verified copy).</param>
|
||||
public async Task<MediaCopyReport> CopyAsync(CancellationToken cancellationToken, string? onlyModule = null)
|
||||
{
|
||||
var assets = new List<MediaAssetSnapshot>();
|
||||
var uses = new List<MediaUseSnapshot>();
|
||||
var modules = new List<string>();
|
||||
foreach (var source in sources.Where(s => onlyModule is null || s.Module == onlyModule))
|
||||
{
|
||||
modules.Add(source.Module);
|
||||
assets.AddRange(await source.ListAssetsAsync(cancellationToken));
|
||||
uses.AddRange(await source.ListUsesAsync(cancellationToken));
|
||||
}
|
||||
|
||||
var transaction = db.Database.IsRelational() ? await db.Database.BeginTransactionAsync(cancellationToken) : null;
|
||||
try
|
||||
{
|
||||
await MirrorAsync(assets, uses, modules, cancellationToken);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var report = await VerifyAsync(assets, uses, modules, cancellationToken);
|
||||
if (!report.Verified)
|
||||
{
|
||||
throw new MediaCopyVerificationException(report);
|
||||
}
|
||||
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
logger.LogInformation("Media library copy verified: {Assets} photos and {Uses} uses from {Modules}.", report.Assets, report.Uses, string.Join(", ", modules));
|
||||
return report;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.RollbackAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
db.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MirrorAsync(IReadOnlyList<MediaAssetSnapshot> assets, IReadOnlyList<MediaUseSnapshot> uses, IReadOnlyList<string> modules, CancellationToken cancellationToken)
|
||||
{
|
||||
var existingAssets = new Dictionary<Guid, MediaAsset>();
|
||||
var existingUses = new Dictionary<Guid, MediaAssetUse>();
|
||||
foreach (var module in modules)
|
||||
{
|
||||
// One equality query per module: the MySQL provider cannot translate a list parameter in an IN clause.
|
||||
foreach (var asset in await db.MediaAssets.Where(a => a.Module == module).ToListAsync(cancellationToken))
|
||||
{
|
||||
existingAssets[asset.Id] = asset;
|
||||
}
|
||||
|
||||
foreach (var use in await db.MediaUses.Where(u => u.Module == module).ToListAsync(cancellationToken))
|
||||
{
|
||||
existingUses[use.Id] = use;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var snapshot in assets)
|
||||
{
|
||||
if (!existingAssets.Remove(snapshot.Id, out var row))
|
||||
{
|
||||
row = new MediaAsset { Id = snapshot.Id };
|
||||
db.MediaAssets.Add(row);
|
||||
}
|
||||
|
||||
row.Module = snapshot.Module;
|
||||
row.OriginalSha256 = snapshot.OriginalSha256;
|
||||
row.OriginalKey = snapshot.OriginalKey;
|
||||
row.SourceFormat = snapshot.SourceFormat;
|
||||
row.Width = snapshot.Width;
|
||||
row.Height = snapshot.Height;
|
||||
row.Bytes = snapshot.Bytes;
|
||||
row.DefaultFocusX = snapshot.FocusX;
|
||||
row.DefaultFocusY = snapshot.FocusY;
|
||||
row.DefaultZoom = snapshot.Zoom;
|
||||
row.CreatedAt = snapshot.CreatedAt;
|
||||
row.CreatedByUserId = snapshot.CreatedByUserId;
|
||||
row.OriginalFileName = snapshot.OriginalFileName;
|
||||
row.LastUsedAt = snapshot.LastUsedAt;
|
||||
row.DeletedAt = snapshot.DeletedAt;
|
||||
row.SetHash = snapshot.SetHash;
|
||||
row.Variants = snapshot.VariantsJson;
|
||||
(row.WideFocusX, row.WideFocusY, row.WideZoom) = (snapshot.WideFocusX, snapshot.WideFocusY, snapshot.WideZoom);
|
||||
(row.TallFocusX, row.TallFocusY, row.TallZoom) = (snapshot.TallFocusX, snapshot.TallFocusY, snapshot.TallZoom);
|
||||
}
|
||||
|
||||
// Uses reference their asset; the crops of a use are the crops of its photo (the hero's wide and tall).
|
||||
var cropsByAsset = assets.ToDictionary(a => a.Id, a => JsonSerializer.Serialize(a.Crops, Json));
|
||||
foreach (var snapshot in uses)
|
||||
{
|
||||
if (!existingUses.Remove(snapshot.Id, out var row))
|
||||
{
|
||||
row = new MediaAssetUse { Id = snapshot.Id };
|
||||
db.MediaUses.Add(row);
|
||||
}
|
||||
|
||||
row.AssetId = snapshot.AssetId;
|
||||
row.Module = snapshot.Module;
|
||||
row.PlaceKind = snapshot.PlaceKind;
|
||||
row.Position = snapshot.Position;
|
||||
row.Active = snapshot.Active;
|
||||
row.SetHash = snapshot.SetHash;
|
||||
row.Variants = snapshot.VariantsJson;
|
||||
row.Crops = snapshot.Crops is not null ? JsonSerializer.Serialize(snapshot.Crops, Json) : cropsByAsset.TryGetValue(snapshot.AssetId, out var crops) ? crops : "[]";
|
||||
}
|
||||
|
||||
// What is left no longer exists at the source.
|
||||
db.MediaUses.RemoveRange(existingUses.Values);
|
||||
db.MediaAssets.RemoveRange(existingAssets.Values);
|
||||
}
|
||||
|
||||
private async Task<MediaCopyReport> VerifyAsync(IReadOnlyList<MediaAssetSnapshot> assets, IReadOnlyList<MediaUseSnapshot> uses, IReadOnlyList<string> modules, CancellationToken cancellationToken)
|
||||
{
|
||||
var differences = new List<string>();
|
||||
var copiedAssets = new Dictionary<Guid, MediaAsset>();
|
||||
var copiedUses = new Dictionary<Guid, MediaAssetUse>();
|
||||
foreach (var module in modules)
|
||||
{
|
||||
foreach (var asset in await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken))
|
||||
{
|
||||
copiedAssets[asset.Id] = asset;
|
||||
}
|
||||
|
||||
foreach (var use in await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken))
|
||||
{
|
||||
copiedUses[use.Id] = use;
|
||||
}
|
||||
}
|
||||
|
||||
if (copiedAssets.Count != assets.Count)
|
||||
{
|
||||
differences.Add($"photos: source {assets.Count}, copy {copiedAssets.Count}");
|
||||
}
|
||||
|
||||
if (copiedUses.Count != uses.Count)
|
||||
{
|
||||
differences.Add($"uses: source {uses.Count}, copy {copiedUses.Count}");
|
||||
}
|
||||
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
if (!copiedAssets.TryGetValue(asset.Id, out var copy))
|
||||
{
|
||||
differences.Add($"photo {asset.Id} is missing in the copy");
|
||||
}
|
||||
else if (copy.OriginalSha256 != asset.OriginalSha256 || copy.OriginalKey != asset.OriginalKey || copy.Bytes != asset.Bytes)
|
||||
{
|
||||
differences.Add($"photo {asset.Id}: hash, key or size differs");
|
||||
}
|
||||
else if (copy.SetHash != asset.SetHash || copy.Variants != asset.VariantsJson)
|
||||
{
|
||||
differences.Add($"photo {asset.Id}: set hash or variants differ");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var use in uses)
|
||||
{
|
||||
if (!copiedUses.TryGetValue(use.Id, out var copy))
|
||||
{
|
||||
differences.Add($"use {use.Id} is missing in the copy");
|
||||
}
|
||||
else if (copy.SetHash != use.SetHash || copy.AssetId != use.AssetId || copy.Variants != use.VariantsJson)
|
||||
{
|
||||
differences.Add($"use {use.Id}: set hash, variants or photo differs");
|
||||
}
|
||||
}
|
||||
|
||||
return new MediaCopyReport(copiedAssets.Count, copiedUses.Count, differences);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <summary>Reads a module's photos and uses back from the library's own tables (phase 2, step C).</summary>
|
||||
public sealed class MediaLibraryReader(MediaDbContext db, IOptions<MediaLibraryOptions> options) : IMediaLibraryReader
|
||||
{
|
||||
public MediaReadMode Mode => options.Value.ReadFromLibrary;
|
||||
|
||||
public async Task<IReadOnlyList<MediaAssetSnapshot>> 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<IReadOnlyList<MediaUseSnapshot>> 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();
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <summary>The migration switches as configured right now (read from the options on every call).</summary>
|
||||
public sealed class MediaLibrarySwitchState(IOptionsMonitor<MediaLibraryOptions> options) : IMediaLibrarySwitchState
|
||||
{
|
||||
public MediaLibrarySwitches Current => new(options.CurrentValue.CopyOnStartup, options.CurrentValue.DualWrite, options.CurrentValue.ReadFromLibrary);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The write-through (<see cref="IMediaLibrarySync"/>): mirrors one module into the library tables with the same verified
|
||||
/// copy the start-up copy uses, one at a time. Off unless <c>Media:Library:DualWrite</c> is on. Never throws: the module's
|
||||
/// own data is the truth and a failed mirror is logged and repaired by the next sync.
|
||||
/// </summary>
|
||||
public sealed class MediaLibrarySync(MediaLibraryCopier copier, IOptions<MediaLibraryOptions> options, ILogger<MediaLibrarySync> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user