Mediabibliotheek: module Media met eigen tabellen en geverifieerde kopie van de Hero-foto's (fase 1 van de migratie, standaard uit)
Continuous Integration / config-sls (pull_request) Successful in 10s
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 20s
Continuous Integration / compile-sites (pull_request) Canceled after 0s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / vulnerability-scan (pull_request) Canceled after 0s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / frontend-prepare (pull_request) Canceled after 27s
Continuous Integration / frontend-lint (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-build (pull_request) Canceled after 54s
Continuous Integration / config-sls (pull_request) Successful in 10s
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 20s
Continuous Integration / compile-sites (pull_request) Canceled after 0s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / vulnerability-scan (pull_request) Canceled after 0s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / frontend-prepare (pull_request) Canceled after 27s
Continuous Integration / frontend-lint (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-build (pull_request) Canceled after 54s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
"src\\SlpModularCms.Modules.Offerings.Tests\\SlpModularCms.Modules.Offerings.Tests.csproj",
|
||||
"src\\SlpModularCms.Modules.Hero\\SlpModularCms.Modules.Hero.csproj",
|
||||
"src\\SlpModularCms.Modules.Hero.Tests\\SlpModularCms.Modules.Hero.Tests.csproj",
|
||||
"src\\SlpModularCms.Modules.Media\\SlpModularCms.Modules.Media.csproj",
|
||||
"src\\SlpModularCms.Modules.Media.Tests\\SlpModularCms.Modules.Media.Tests.csproj",
|
||||
"src\\SlpModularCms.Modules.Contact\\SlpModularCms.Modules.Contact.csproj",
|
||||
"src\\SlpModularCms.Modules.Contact.Tests\\SlpModularCms.Modules.Contact.Tests.csproj"
|
||||
]
|
||||
|
||||
@@ -50,6 +50,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Hero"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Hero.Tests", "src\SlpModularCms.Modules.Hero.Tests\SlpModularCms.Modules.Hero.Tests.csproj", "{838B2DA6-8B01-4C83-A696-D910AB2BC5D9}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Media", "src\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj", "{729ABD96-ADEB-4352-8030-2F6074B977F0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlpModularCms.Modules.Media.Tests", "src\SlpModularCms.Modules.Media.Tests\SlpModularCms.Modules.Media.Tests.csproj", "{E697D4E1-FA8D-468C-BC31-81520715E246}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -300,6 +306,30 @@ Global
|
||||
{838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x64.Build.0 = Release|Any CPU
|
||||
{838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{838B2DA6-8B01-4C83-A696-D910AB2BC5D9}.Release|x86.Build.0 = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x64.Build.0 = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0}.Release|x86.Build.0 = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -327,5 +357,7 @@ Global
|
||||
{9EAC8A27-8DB0-4E3B-B144-280882828DE8} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
|
||||
{4A7E3360-C7D0-4ADF-BC98-79AA912A8867} = {30D8F44D-4B6E-4980-8D1F-29D1A64F438C}
|
||||
{838B2DA6-8B01-4C83-A696-D910AB2BC5D9} = {77DF6642-7863-4D67-BAEE-217EC2D99894}
|
||||
{729ABD96-ADEB-4352-8030-2F6074B977F0} = {30D8F44D-4B6E-4980-8D1F-29D1A64F438C}
|
||||
{E697D4E1-FA8D-468C-BC31-81520715E246} = {77DF6642-7863-4D67-BAEE-217EC2D99894}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -61,3 +61,5 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
- **Confirmed by Bryan "as designed"**: 1, 2, 5, 6, 9, 11, 14, 15, 16, 19, 20, 26, 27, 28.
|
||||
- **Still assumptions** (not put to him separately): 17 (language), 18 (what phase A contained, now overtaken), 22 (selectability), 23 (performance), 24 (ASF data), 25 (use definitions); and the older rows 18 to 25 above. Assumptions 29 to 32 follow from his decisions.
|
||||
- Assumption 12 now reads: there IS a trash (decision 12), the earlier "no trash in phase A" no longer applies. Assumption 10: deleting a used photo is possible after a second confirmation (built). Assumption 21: the original file name is stored from now on (built).
|
||||
| 33 | **Where the copy runs and what it copies** (built 2026-11-17). | A new module `Modules.Media` owns the tables (`MediaAssets`, `MediaUses`); modules offer their data through `IMediaMigrationSource` so the library module does not depend on the Hero. The copy is a verified mirror in one transaction behind `Media:Library:CopyOnStartup` (default false). The use of a hero slide carries both crops (`wide`, `tall`) of its photo as JSON; its set hash is the photo's current set hash. | A copy job triggered from the admin instead of start-up: easier to time, needs an endpoint and a button. | `Modules.Media/Services/MediaLibraryCopier.cs`, `HeroMediaMigrationSource` |
|
||||
| 34 | **Naming**: entity `MediaAssetUse` (table `MediaUses`), because `MediaUse` already names the read model in Core. | As built. | Rename the table or the record: a small migration. | `Modules.Media/Data` |
|
||||
|
||||
@@ -64,10 +64,26 @@ The migration job compares and **aborts** (rolls its transaction back) on any di
|
||||
3. Back-up first (above), on the test host too.
|
||||
4. Deploy with the setting **off**, check the verification result in the log, then switch it on, then the smoke test (hero markers, `/theme-init.js`, a sample of media addresses), then Bryan checks the admin.
|
||||
|
||||
## What is built first (next step, when this plan is accepted)
|
||||
## Status (2026-11-17): phase 1 (expand) is built, behind a setting that is OFF
|
||||
|
||||
1. A new module `SlpModularCms.Modules.Media` with its own `DbContext`, the tables `MediaAssets` and `MediaUses` and an additive EF migration (no existing table touched), with tests.
|
||||
2. The copy-and-verify job (idempotent, safe to run twice) behind a setting; unit and in-memory tests, and a test against MariaDB.
|
||||
3. The library reading from the new tables behind `Media:Library:ReadFromLibrary` (default off).
|
||||
Built on `feature/media-library-screen`, not yet deployed anywhere:
|
||||
|
||||
Nothing in steps 1 to 3 runs without the setting, so a deploy of that code alone changes no data except the new, empty tables.
|
||||
- New module `SlpModularCms.Modules.Media` (own `MediaDbContext`, tables `MediaAssets` and `MediaUses`, additive migration `InitialCreate`; no existing table is touched). It is referenced by the three hosts, so a deploy of this code creates the two empty tables at start-up and nothing else.
|
||||
- `IMediaMigrationSource` (Core) and `HeroMediaMigrationSource` (Hero, read-only): the hero's photos (also those in the trash) and slides with their own ids, hashes and set hashes.
|
||||
- `MediaLibraryCopier`: copies and **verifies** (counts, ids, hash, key, size, set hash) in one transaction; any difference or error rolls the whole copy back and nothing is left. It MIRRORS the source, so running it again gives the same result; rows of other modules are not touched.
|
||||
- The copy runs only when `Media__Library__CopyOnStartup=true` (default **false**) and a failure is logged, never fatal.
|
||||
- Tested in memory and against a real MariaDB (migration, copy twice, rollback on a failing copy).
|
||||
|
||||
Nothing reads from the new tables yet (phase 2, the switch, is not built). The library screen still reads the hero through the usage provider.
|
||||
|
||||
### Roll-out on TEST (needs SLS-Main to be told first)
|
||||
|
||||
1. Back-up (database dump and media listing) as above.
|
||||
2. Deploy the branch with `Media__Library__CopyOnStartup` unset: the two empty tables appear; check the log for the Media migration.
|
||||
3. Set `Media__Library__CopyOnStartup=true` and restart: the log says `Media library copy verified: N photos and M uses from hero` (or the error and the rollback).
|
||||
4. Rollback at any point: set it back to false (the tables stay, unused) or drop `MediaUses` and `MediaAssets`.
|
||||
5. Never on production.
|
||||
|
||||
### Still to build (phases 2 and 3)
|
||||
|
||||
The switch (`Media:Library:ReadFromLibrary`, dual write of new uploads, library reading from the new tables) and the contract phase. They need Bryan's acceptance of the copy on test first.
|
||||
|
||||
+1
@@ -31,6 +31,7 @@
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Availability\SlpModularCms.Modules.Availability.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Offerings\SlpModularCms.Modules.Offerings.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Contact\SlpModularCms.Modules.Contact.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Identity\SlpModularCms.Modules.Identity.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Master\SlpModularCms.Modules.Master.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Offerings\SlpModularCms.Modules.Offerings.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
|
||||
@@ -21,7 +21,7 @@ public class SiteContractTests
|
||||
public void TheMasterCms_ReferencesEveryModule()
|
||||
{
|
||||
SiteFiles.Modules("SlpModularCms.Api").Should().BeEquivalentTo(
|
||||
["Availability", "Contact", "Hero", "Identity", "Master", "Offerings"],
|
||||
["Availability", "Contact", "Hero", "Identity", "Master", "Media", "Offerings"],
|
||||
"the master Api is the all-modules composition the CI builds and tests");
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class SiteContractTests
|
||||
public void SlpSoftware_IsTheFullComposition()
|
||||
{
|
||||
SiteFiles.Modules(SiteFiles.SlpSoftware).Should().BeEquivalentTo(
|
||||
["Availability", "Contact", "Hero", "Identity", "Master", "Offerings"]);
|
||||
["Availability", "Contact", "Hero", "Identity", "Master", "Media", "Offerings"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -37,7 +37,7 @@ public class SiteContractTests
|
||||
{
|
||||
// Not a master/slave setup: no Master module, so /System/capabilities does not list it.
|
||||
SiteFiles.Modules(SiteFiles.AndreaSlopFotografie).Should().BeEquivalentTo(
|
||||
["Availability", "Contact", "Hero", "Identity", "Offerings"]);
|
||||
["Availability", "Contact", "Hero", "Identity", "Media", "Offerings"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Master\SlpModularCms.Modules.Master.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Offerings\SlpModularCms.Modules.Offerings.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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>
|
||||
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);
|
||||
|
||||
/// <summary>One place a photo is used, as a module holds it today.</summary>
|
||||
public sealed record MediaUseSnapshot(Guid Id, Guid AssetId, string Module, string PlaceKind, int Position, bool Active, string SetHash);
|
||||
|
||||
/// <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);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public class HeroModule : IModule
|
||||
services.AddScoped<IPublicHeroService, PublicHeroService>();
|
||||
services.AddScoped<IHeroImageService, HeroImageService>();
|
||||
services.AddScoped<IMediaUsageProvider, HeroMediaUsageProvider>();
|
||||
services.AddScoped<IMediaMigrationSource, HeroMediaMigrationSource>();
|
||||
services.AddScoped<IHeroPageModel, HeroPageModel>();
|
||||
services.AddScoped<IHtmlPageTransform, HeroHtmlTransform>();
|
||||
services.AddScoped<ICmsDataBlockContributor, HeroDataContributor>();
|
||||
|
||||
@@ -22,6 +22,9 @@ public interface IHeroRepository
|
||||
/// <summary>Images whose set is missing or does not match their focus, oldest first (tracked).</summary>
|
||||
Task<int> CountImagesAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Every image including those in the trash, not tracked (the media library copy).</summary>
|
||||
Task<IReadOnlyList<HeroImage>> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>An image whether or not it is in the trash, tracked (restore, trash).</summary>
|
||||
Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken);
|
||||
|
||||
@@ -87,6 +90,9 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository
|
||||
public Task<HeroImage?> GetImageIncludingTrashedAsync(Guid id, CancellationToken cancellationToken)
|
||||
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetAllImagesIncludingTrashedReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).OrderBy(i => i.CreatedAt).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetTrashedImagesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.AsNoTracking().Where(i => i.DeletedAt != null).ToListAsync(cancellationToken)).OrderByDescending(i => i.DeletedAt).ToList();
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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 = images.TryGetValue(slide.ImageId, out var image) ? image.SetHash : string.Empty;
|
||||
uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
private static MediaCropSnapshot Crop(string ratio, HeroImage image)
|
||||
{
|
||||
var focus = image.FocusFor(ratio);
|
||||
return new MediaCropSnapshot(ratio, focus.X, focus.Y, focus.Zoom);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.10.0" />
|
||||
<PackageReference Include="FsCheck.Xunit" Version="3.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="10.0.9" />
|
||||
<!-- Pinned above the version DataProtection pulls transitively (FR-22, OPEN-03) - see Core.csproj -->
|
||||
<PackageReference Include="System.Security.Cryptography.Xml" Version="10.0.14" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.Extensions.Options" />
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,59 @@
|
||||
namespace SlpModularCms.Modules.Media.Data.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// A photo in the library: the original (never served), its default focus and when it was last used or trashed. The ids,
|
||||
/// hashes and keys are the ones the owning module already uses, so public addresses stay as they are.
|
||||
/// </summary>
|
||||
public class MediaAsset
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>The module that uploaded it (for example <c>hero</c>); the library is shared, this only says where it came from.</summary>
|
||||
public string Module { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>SHA-256 of the file as uploaded (hex); the same photo uploaded twice is one record.</summary>
|
||||
public string OriginalSha256 { get; set; } = string.Empty;
|
||||
|
||||
public string OriginalKey { get; set; } = string.Empty;
|
||||
public string SourceFormat { get; set; } = string.Empty;
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public long Bytes { get; set; }
|
||||
|
||||
public double DefaultFocusX { get; set; } = 0.5;
|
||||
public double DefaultFocusY { get; set; } = 0.5;
|
||||
public double DefaultZoom { get; set; } = 1.0;
|
||||
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public Guid CreatedByUserId { get; set; }
|
||||
public string? OriginalFileName { get; set; }
|
||||
public DateTimeOffset? LastUsedAt { get; set; }
|
||||
public DateTimeOffset? DeletedAt { get; set; }
|
||||
|
||||
public List<MediaAssetUse> Uses { get; set; } = [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One place where a photo is used, with its own crops. The set hash is computed by the module with the existing formula
|
||||
/// over the photo's hash and these crops.
|
||||
/// </summary>
|
||||
public class MediaAssetUse
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid AssetId { get; set; }
|
||||
public MediaAsset? Asset { get; set; }
|
||||
|
||||
public string Module { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>For example <c>slide</c>.</summary>
|
||||
public string PlaceKind { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The 1-based position in the place (display order).</summary>
|
||||
public int Position { get; set; }
|
||||
|
||||
public bool Active { get; set; } = true;
|
||||
public string SetHash { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The crops of this use as JSON: <c>[{ratio, x, y, zoom}]</c>. Empty for a natural-ratio use.</summary>
|
||||
public string Crops { get; set; } = "[]";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Media.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Data;
|
||||
|
||||
public class MediaDbContext(DbContextOptions<MediaDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<MediaAsset> MediaAssets => Set<MediaAsset>();
|
||||
public DbSet<MediaAssetUse> MediaUses => Set<MediaAssetUse>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<MediaAsset>(entity =>
|
||||
{
|
||||
entity.ToTable("MediaAssets");
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.Module).IsRequired().HasMaxLength(20);
|
||||
entity.Property(e => e.OriginalSha256).IsRequired().HasMaxLength(64);
|
||||
entity.HasIndex(e => e.OriginalSha256).IsUnique();
|
||||
entity.Property(e => e.OriginalKey).IsRequired().HasMaxLength(200);
|
||||
entity.Property(e => e.SourceFormat).IsRequired().HasMaxLength(10);
|
||||
entity.Property(e => e.OriginalFileName).HasMaxLength(200);
|
||||
entity.HasIndex(e => e.CreatedAt);
|
||||
entity.HasMany(e => e.Uses).WithOne(u => u.Asset).HasForeignKey(u => u.AssetId).OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<MediaAssetUse>(entity =>
|
||||
{
|
||||
entity.ToTable("MediaUses");
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.Module).IsRequired().HasMaxLength(20);
|
||||
entity.Property(e => e.PlaceKind).IsRequired().HasMaxLength(20);
|
||||
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
|
||||
entity.Property(e => e.Crops).IsRequired().HasColumnType("longtext");
|
||||
entity.HasIndex(e => e.AssetId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Media;
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaModule : IModule
|
||||
{
|
||||
public string Name => "Media";
|
||||
public string Version => "1.0.0";
|
||||
|
||||
public void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDbContext<MediaDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
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.AddHostedService<MediaLibraryCopyService>();
|
||||
}
|
||||
|
||||
public void UseModule(IApplicationBuilder app)
|
||||
{
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<MediaDbContext>();
|
||||
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;
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Migrations
|
||||
{
|
||||
[DbContext(typeof(MediaDbContext))]
|
||||
[Migration("20261117143138_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <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.Media.Data.Entities.MediaAsset", 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<double>("DefaultFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("DefaultFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("DefaultZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
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>("SourceFormat")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<int>("Width")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MediaAssets", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("Active")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<Guid>("AssetId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Crops")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<string>("PlaceKind")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<int>("Position")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("AssetId");
|
||||
|
||||
b.ToTable("MediaUses", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b =>
|
||||
{
|
||||
b.HasOne("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", "Asset")
|
||||
.WithMany("Uses")
|
||||
.HasForeignKey("AssetId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Asset");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b =>
|
||||
{
|
||||
b.Navigation("Uses");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AlterDatabase()
|
||||
.Annotation("MySQL:Charset", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "MediaAssets",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
Module = table.Column<string>(type: "varchar(20)", maxLength: 20, 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),
|
||||
DefaultFocusX = table.Column<double>(type: "double", nullable: false),
|
||||
DefaultFocusY = table.Column<double>(type: "double", nullable: false),
|
||||
DefaultZoom = table.Column<double>(type: "double", 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_MediaAssets", x => x.Id);
|
||||
})
|
||||
.Annotation("MySQL:Charset", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "MediaUses",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
AssetId = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
Module = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false),
|
||||
PlaceKind = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false),
|
||||
Position = table.Column<int>(type: "int", nullable: false),
|
||||
Active = table.Column<bool>(type: "tinyint(1)", nullable: false),
|
||||
SetHash = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: false),
|
||||
Crops = table.Column<string>(type: "longtext", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_MediaUses", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_MediaUses_MediaAssets_AssetId",
|
||||
column: x => x.AssetId,
|
||||
principalTable: "MediaAssets",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
})
|
||||
.Annotation("MySQL:Charset", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_MediaAssets_CreatedAt",
|
||||
table: "MediaAssets",
|
||||
column: "CreatedAt");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_MediaAssets_OriginalSha256",
|
||||
table: "MediaAssets",
|
||||
column: "OriginalSha256",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_MediaUses_AssetId",
|
||||
table: "MediaUses",
|
||||
column: "AssetId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "MediaUses");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "MediaAssets");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Migrations
|
||||
{
|
||||
[DbContext(typeof(MediaDbContext))]
|
||||
partial class MediaDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.9")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b =>
|
||||
{
|
||||
b.Property<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<double>("DefaultFocusX")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("DefaultFocusY")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<double>("DefaultZoom")
|
||||
.HasColumnType("double");
|
||||
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<int>("Height")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTimeOffset?>("LastUsedAt")
|
||||
.HasColumnType("datetime");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
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>("SourceFormat")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("varchar(10)");
|
||||
|
||||
b.Property<int>("Width")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedAt");
|
||||
|
||||
b.HasIndex("OriginalSha256")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("MediaAssets", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<bool>("Active")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<Guid>("AssetId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Crops")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Module")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<string>("PlaceKind")
|
||||
.IsRequired()
|
||||
.HasMaxLength(20)
|
||||
.HasColumnType("varchar(20)");
|
||||
|
||||
b.Property<int>("Position")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("SetHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("AssetId");
|
||||
|
||||
b.ToTable("MediaUses", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAssetUse", b =>
|
||||
{
|
||||
b.HasOne("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", "Asset")
|
||||
.WithMany("Uses")
|
||||
.HasForeignKey("AssetId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Asset");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SlpModularCms.Modules.Media.Data.Entities.MediaAsset", b =>
|
||||
{
|
||||
b.Navigation("Uses");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Data.Entities;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <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);
|
||||
|
||||
public async Task<MediaCopyReport> CopyAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var assets = new List<MediaAssetSnapshot>();
|
||||
var uses = new List<MediaUseSnapshot>();
|
||||
var modules = new List<string>();
|
||||
foreach (var source in sources)
|
||||
{
|
||||
modules.Add(source.Module);
|
||||
assets.AddRange(await source.ListAssetsAsync(cancellationToken));
|
||||
uses.AddRange(await source.ListUsesAsync(cancellationToken));
|
||||
}
|
||||
|
||||
var transaction = db.Database.IsRelational() ? await db.Database.BeginTransactionAsync(cancellationToken) : null;
|
||||
try
|
||||
{
|
||||
await MirrorAsync(assets, uses, modules, cancellationToken);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var report = await VerifyAsync(assets, uses, modules, cancellationToken);
|
||||
if (!report.Verified)
|
||||
{
|
||||
throw new MediaCopyVerificationException(report);
|
||||
}
|
||||
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
logger.LogInformation("Media library copy verified: {Assets} photos and {Uses} uses from {Modules}.", report.Assets, report.Uses, string.Join(", ", modules));
|
||||
return report;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.RollbackAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
db.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (transaction is not null)
|
||||
{
|
||||
await transaction.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MirrorAsync(IReadOnlyList<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;
|
||||
}
|
||||
|
||||
// Uses reference their asset; the crops of a use are the crops of its photo (the hero's wide and tall).
|
||||
var cropsByAsset = assets.ToDictionary(a => a.Id, a => JsonSerializer.Serialize(a.Crops, Json));
|
||||
foreach (var snapshot in uses)
|
||||
{
|
||||
if (!existingUses.Remove(snapshot.Id, out var row))
|
||||
{
|
||||
row = new MediaAssetUse { Id = snapshot.Id };
|
||||
db.MediaUses.Add(row);
|
||||
}
|
||||
|
||||
row.AssetId = snapshot.AssetId;
|
||||
row.Module = snapshot.Module;
|
||||
row.PlaceKind = snapshot.PlaceKind;
|
||||
row.Position = snapshot.Position;
|
||||
row.Active = snapshot.Active;
|
||||
row.SetHash = snapshot.SetHash;
|
||||
row.Crops = cropsByAsset.TryGetValue(snapshot.AssetId, out var crops) ? crops : "[]";
|
||||
}
|
||||
|
||||
// What is left no longer exists at the source.
|
||||
db.MediaUses.RemoveRange(existingUses.Values);
|
||||
db.MediaAssets.RemoveRange(existingAssets.Values);
|
||||
}
|
||||
|
||||
private async Task<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");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var use in uses)
|
||||
{
|
||||
if (!copiedUses.TryGetValue(use.Id, out var copy))
|
||||
{
|
||||
differences.Add($"use {use.Id} is missing in the copy");
|
||||
}
|
||||
else if (copy.SetHash != use.SetHash || copy.AssetId != use.AssetId)
|
||||
{
|
||||
differences.Add($"use {use.Id}: set hash or photo differs");
|
||||
}
|
||||
}
|
||||
|
||||
return new MediaCopyReport(copiedAssets.Count, copiedUses.Count, differences);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||
<_Parameter1>SlpModularCms.Modules.Media.Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user