Fase 2 stap C: leesschakelaar (off/compare/on) voor het bibliotheekscherm, read-check endpoint, workflow-input

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 23:25:14 +01:00
co-authored by Claude Sonnet 5.5
parent 3558e91fde
commit e1010007fa
16 changed files with 633 additions and 6 deletions
+3 -1
View File
@@ -45,7 +45,9 @@ public class HeroModule : IModule
services.AddScoped<IHeroService, HeroService>();
services.AddScoped<IPublicHeroService, PublicHeroService>();
services.AddScoped<IHeroImageService, HeroImageService>();
services.AddScoped<IMediaUsageProvider, HeroMediaUsageProvider>();
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>();
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Library;
using SlpModularCms.Modules.Hero.Data.Entities;
@@ -9,21 +10,59 @@ namespace SlpModularCms.Modules.Hero.Services;
/// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the
/// hero), and moves them to the trash and back for it. The files are removed by <see cref="HeroMediaCleanup"/> after the trash period.
/// </summary>
public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider
public class HeroMediaUsageProvider(
IHeroRepository repository,
IHeroImageService images,
IMediaLibraryReader? reader = null,
ILogger<HeroMediaUsageProvider>? logger = null) : IMediaUsageProvider, IMediaReadVerifier
{
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)
=> (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
{
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: false, cancellationToken) is { } fromLibrary)
{
return fromLibrary;
}
await CompareInTheLogAsync(cancellationToken);
return (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
}
public async Task<IReadOnlyList<MediaPhoto>> ListTrashAsync(CancellationToken cancellationToken)
=> (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
{
if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: true, cancellationToken) is { } fromLibrary)
{
return fromLibrary;
}
return (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList();
}
public async Task<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.
@@ -44,6 +83,64 @@ public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageServic
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