Mediabibliotheek fase A: read-only gebruikscontract, Hero-provider, opruimselectie en selectiecomponenten
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 26s
Continuous Integration / compile-sites (pull_request) Successful in 1m50s
Continuous Integration / backend-build (pull_request) Successful in 1m53s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m53s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m18s
Continuous Integration / frontend-build (pull_request) Successful in 56s
Continuous Integration / frontend-lint (pull_request) Successful in 2m20s
Continuous Integration / frontend-test (pull_request) Successful in 2m31s
Continuous Integration / backend-test (pull_request) Successful in 3m7s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 26s
Continuous Integration / compile-sites (pull_request) Successful in 1m50s
Continuous Integration / backend-build (pull_request) Successful in 1m53s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m53s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m18s
Continuous Integration / frontend-build (pull_request) Successful in 56s
Continuous Integration / frontend-lint (pull_request) Successful in 2m20s
Continuous Integration / frontend-test (pull_request) Successful in 2m31s
Continuous Integration / backend-test (pull_request) Successful in 3m7s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using FluentAssertions;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Media;
|
||||
|
||||
public class MediaLibraryTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private static MediaLibraryItem Item(int daysOld, bool used = false, long bytes = 1000, string module = "hero")
|
||||
{
|
||||
var photo = new MediaPhoto(module, Guid.NewGuid(), bytes, 1600, 1067, Now.AddDays(-daysOld), null);
|
||||
IReadOnlyList<MediaUse> uses = used ? [new MediaUse(module, photo.Id, "slide", 1, true)] : [];
|
||||
return new MediaLibraryItem(photo, uses);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Cleanup_selects_photos_older_than_the_period_newest_first()
|
||||
{
|
||||
var items = new[] { Item(3), Item(10), Item(30), Item(8) };
|
||||
|
||||
var selected = CleanupSelection.Select(items, Now, 7, onlyUnused: false);
|
||||
|
||||
selected.Select(i => (Now - i.Photo.CreatedAt).Days).Should().Equal(8, 10, 30);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Cleanup_only_unused_leaves_out_photos_in_use_but_all_photos_does_not()
|
||||
{
|
||||
var used = Item(20, used: true);
|
||||
var unused = Item(20);
|
||||
|
||||
CleanupSelection.Select([used, unused], Now, 7, onlyUnused: true).Should().ContainSingle().Which.Should().Be(unused);
|
||||
CleanupSelection.Select([used, unused], Now, 7, onlyUnused: false).Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_photo_used_only_by_an_inactive_slide_still_counts_as_used()
|
||||
{
|
||||
var photo = new MediaPhoto("hero", Guid.NewGuid(), 10, 100, 100, Now.AddDays(-40), null);
|
||||
var item = new MediaLibraryItem(photo, [new MediaUse("hero", photo.Id, "slide", 2, Active: false)]);
|
||||
|
||||
CleanupSelection.Select([item], Now, 7, onlyUnused: true).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-5, 1)]
|
||||
[InlineData(0, 1)]
|
||||
[InlineData(7, 7)]
|
||||
[InlineData(365, 365)]
|
||||
[InlineData(4000, 365)]
|
||||
public void The_period_stays_inside_the_slider_range(int days, int expected) => CleanupSelection.ClampDays(days).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public void A_photo_exactly_at_the_boundary_is_not_selected_yet()
|
||||
{
|
||||
var item = Item(7);
|
||||
|
||||
CleanupSelection.Select([item], Now, 7, onlyUnused: false).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Cleanup_bytes_adds_up_the_originals() => CleanupSelection.Bytes([Item(9, bytes: 100), Item(9, bytes: 250)]).Should().Be(350);
|
||||
|
||||
[Fact]
|
||||
public async Task The_library_merges_all_modules_newest_first_and_attaches_the_uses()
|
||||
{
|
||||
var oldHero = new MediaPhoto("hero", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-5), null);
|
||||
var newGallery = new MediaPhoto("gallery", Guid.NewGuid(), 1, 1, 1, Now.AddDays(-1), null);
|
||||
var heroUse = new MediaUse("hero", oldHero.Id, "slide", 3, true);
|
||||
var library = new MediaLibraryService([new FakeProvider("hero", [oldHero], [heroUse]), new FakeProvider("gallery", [newGallery], [])]);
|
||||
|
||||
var items = await library.ListAsync(CancellationToken.None);
|
||||
|
||||
items.Select(i => i.Photo.Module).Should().Equal("gallery", "hero");
|
||||
items[0].InUse.Should().BeFalse();
|
||||
items[1].Uses.Should().ContainSingle().Which.Should().Be(heroUse);
|
||||
library.Modules.Should().Equal("gallery", "hero");
|
||||
}
|
||||
|
||||
private sealed class FakeProvider(string module, MediaPhoto[] photos, MediaUse[] uses) : IMediaUsageProvider
|
||||
{
|
||||
public string Module => module;
|
||||
|
||||
public Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<MediaPhoto>>(photos);
|
||||
|
||||
public Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken)
|
||||
=> Task.FromResult<IReadOnlyList<MediaUse>>(uses.Where(u => photoIds.Contains(u.PhotoId)).ToList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>
|
||||
/// The photos a clean-up would preselect: uploaded before the period and, by default, not used anywhere. Pure, so it is
|
||||
/// easy to test. The period is measured from the upload date (assumption 8 of the media-library list).
|
||||
/// </summary>
|
||||
public static class CleanupSelection
|
||||
{
|
||||
public const int DefaultDays = 7;
|
||||
public const int MinDays = 1;
|
||||
public const int MaxDays = 365;
|
||||
|
||||
/// <summary>The days kept inside the slider range.</summary>
|
||||
public static int ClampDays(int days) => Math.Clamp(days, MinDays, MaxDays);
|
||||
|
||||
/// <summary>Items uploaded more than <paramref name="days"/> days before <paramref name="now"/>, newest first; with <paramref name="onlyUnused"/> only those no module uses.</summary>
|
||||
public static IReadOnlyList<MediaLibraryItem> Select(IEnumerable<MediaLibraryItem> items, DateTimeOffset now, int days, bool onlyUnused)
|
||||
{
|
||||
var cutoff = now.AddDays(-ClampDays(days));
|
||||
return items
|
||||
.Where(item => item.Photo.CreatedAt < cutoff)
|
||||
.Where(item => !onlyUnused || !item.InUse)
|
||||
.OrderByDescending(item => item.Photo.CreatedAt)
|
||||
.ThenBy(item => item.Photo.Id)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>The bytes the given items take (originals; the variants are counted by the storage service).</summary>
|
||||
public static long Bytes(IEnumerable<MediaLibraryItem> items) => items.Sum(item => item.Photo.Bytes);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>The library over all modules that report photos: one list, newest first, each photo with its uses. Read-only.</summary>
|
||||
public interface IMediaLibraryService
|
||||
{
|
||||
/// <summary>All photos of all modules with their uses, newest first.</summary>
|
||||
Task<IReadOnlyList<MediaLibraryItem>> ListAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>The modules that report photos (the screen warns about a module that is not in this list).</summary>
|
||||
IReadOnlyList<string> Modules { get; }
|
||||
}
|
||||
|
||||
public sealed class MediaLibraryService(IEnumerable<IMediaUsageProvider> providers) : IMediaLibraryService
|
||||
{
|
||||
private readonly IMediaUsageProvider[] _providers = providers.ToArray();
|
||||
|
||||
public IReadOnlyList<string> Modules => _providers.Select(p => p.Module).OrderBy(m => m, StringComparer.Ordinal).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<MediaLibraryItem>> ListAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = new List<MediaLibraryItem>();
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
var photos = await provider.ListPhotosAsync(cancellationToken);
|
||||
var uses = await provider.ListUsesAsync(photos.Select(p => p.Id).ToList(), cancellationToken);
|
||||
var byPhoto = uses.ToLookup(u => u.PhotoId);
|
||||
items.AddRange(photos.Select(photo => new MediaLibraryItem(photo, byPhoto[photo.Id].ToList())));
|
||||
}
|
||||
|
||||
return items.OrderByDescending(i => i.Photo.CreatedAt).ThenBy(i => i.Photo.Id).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>A photo as the library shows it. The module that owns it fills it; the library only reads.</summary>
|
||||
/// <param name="Module">The module key (for example <c>hero</c>); with <paramref name="Id"/> it names the photo.</param>
|
||||
/// <param name="ThumbnailUrl">A public address of a small variant, or null while no variant exists yet.</param>
|
||||
public sealed record MediaPhoto(string Module, Guid Id, long Bytes, int Width, int Height, DateTimeOffset CreatedAt, string? ThumbnailUrl);
|
||||
|
||||
/// <summary>One place where a photo is used. The library shows it as "module, place kind, position" (for example "Hero, slide 3").</summary>
|
||||
/// <param name="PlaceKind">What kind of place (for example <c>slide</c>); the screen translates it.</param>
|
||||
/// <param name="Position">The 1-based position inside the place, or null when it has none.</param>
|
||||
/// <param name="Active">False when the place is switched off (still a use: the photo is kept).</param>
|
||||
public sealed record MediaUse(string Module, Guid PhotoId, string PlaceKind, int? Position, bool Active);
|
||||
|
||||
/// <summary>A photo with everything that uses it.</summary>
|
||||
public sealed record MediaLibraryItem(MediaPhoto Photo, IReadOnlyList<MediaUse> Uses)
|
||||
{
|
||||
public bool InUse => Uses.Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What a module tells the media library. Read-only: the library never changes a module's data through this contract
|
||||
/// (deleting and detaching come in a later step). The answers are computed from the module's own data, never copied.
|
||||
/// </summary>
|
||||
public interface IMediaUsageProvider
|
||||
{
|
||||
/// <summary>The module key, lower case, equal to its media area when it has one.</summary>
|
||||
string Module { get; }
|
||||
|
||||
/// <summary>The photos of this module, newest first.</summary>
|
||||
Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Where the given photos are used (only photos of this module).</summary>
|
||||
Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public static class MediaServiceCollectionExtensions
|
||||
services.TryAddSingleton<MediaAreaRegistry>();
|
||||
services.TryAddSingleton<IMediaStore, FileSystemMediaStore>();
|
||||
services.TryAddSingleton<IMediaUploadGuard, MediaUploadGuard>();
|
||||
services.TryAddScoped<Library.IMediaLibraryService, Library.MediaLibraryService>();
|
||||
if (!services.Any(d => d.ImplementationType == typeof(ImageProcessorSelfTest)))
|
||||
{
|
||||
services.AddHostedService<ImageProcessorSelfTest>();
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using FluentAssertions;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Hero.Models;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
public class HeroMediaUsageProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Lists_the_images_newest_first_with_a_thumbnail_and_reports_one_use_per_slide_with_its_position()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
|
||||
rig.Time.Now = rig.Time.Now.AddMinutes(5);
|
||||
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
|
||||
rig.Time.Now = rig.Time.Now.AddMinutes(5);
|
||||
var unused = await rig.Upload(HeroTestRig.Photo(seed: 3));
|
||||
await rig.Service.SaveAsync(
|
||||
new HeroSaveRequest("Titel", null, null, "single", [],
|
||||
[new HeroSlideRequest(null, Guid.Parse(b.Id), "tweede"), new HeroSlideRequest(null, Guid.Parse(a.Id), "eerste", IsActive: false)]),
|
||||
HeroTestRig.Admin, CancellationToken.None);
|
||||
var provider = new HeroMediaUsageProvider(rig.Repository);
|
||||
|
||||
var photos = await provider.ListPhotosAsync(CancellationToken.None);
|
||||
var uses = await provider.ListUsesAsync(photos.Select(p => p.Id).ToList(), CancellationToken.None);
|
||||
|
||||
provider.Module.Should().Be("hero");
|
||||
photos.Select(p => p.Id).Should().Equal(Guid.Parse(unused.Id), Guid.Parse(b.Id), Guid.Parse(a.Id));
|
||||
photos.Should().OnlyContain(p => p.ThumbnailUrl != null && p.ThumbnailUrl.StartsWith("/media/hero/"));
|
||||
uses.Should().HaveCount(2);
|
||||
uses.Single(u => u.PhotoId == Guid.Parse(b.Id)).Should().Be(new MediaUse("hero", Guid.Parse(b.Id), "slide", 1, true));
|
||||
uses.Single(u => u.PhotoId == Guid.Parse(a.Id)).Should().Be(new MediaUse("hero", Guid.Parse(a.Id), "slide", 2, false));
|
||||
uses.Should().NotContain(u => u.PhotoId == Guid.Parse(unused.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reading_changes_nothing()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var image = await rig.Upload(HeroTestRig.Photo());
|
||||
var provider = new HeroMediaUsageProvider(rig.Repository);
|
||||
|
||||
await provider.ListPhotosAsync(CancellationToken.None);
|
||||
await provider.ListUsesAsync([Guid.Parse(image.Id)], CancellationToken.None);
|
||||
|
||||
(await rig.Repository.CountImagesAsync(CancellationToken.None)).Should().Be(1);
|
||||
rig.Db.ChangeTracker.HasChanges().Should().BeFalse();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Core.Media;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Core.Media.Magick;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Core.Texts;
|
||||
@@ -44,6 +45,7 @@ public class HeroModule : IModule
|
||||
services.AddScoped<IHeroService, HeroService>();
|
||||
services.AddScoped<IPublicHeroService, PublicHeroService>();
|
||||
services.AddScoped<IHeroImageService, HeroImageService>();
|
||||
services.AddScoped<IMediaUsageProvider, HeroMediaUsageProvider>();
|
||||
services.AddScoped<IHeroPageModel, HeroPageModel>();
|
||||
services.AddScoped<IHtmlPageTransform, HeroHtmlTransform>();
|
||||
services.AddScoped<ICmsDataBlockContributor, HeroDataContributor>();
|
||||
|
||||
@@ -22,6 +22,12 @@ 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>All images, newest first, not tracked (the media library lists them).</summary>
|
||||
Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>All slides, not tracked (where each image is used).</summary>
|
||||
Task<IReadOnlyList<HeroSlide>> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Images with a complete set made before the full-frame preview existed (tracked).</summary>
|
||||
@@ -76,6 +82,12 @@ public class HeroRepository(HeroDbContext context) : IHeroRepository
|
||||
|
||||
public Task<int> CountImagesAsync(CancellationToken cancellationToken) => context.HeroImages.CountAsync(cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetAllImagesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).OrderByDescending(i => i.CreatedAt).ToList();
|
||||
|
||||
public async Task<IReadOnlyList<HeroSlide>> GetAllSlidesReadOnlyAsync(CancellationToken cancellationToken)
|
||||
=> await context.HeroSlides.AsNoTracking().ToListAsync(cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
|
||||
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using SlpModularCms.Core.Media;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Hero.Data.Entities;
|
||||
using SlpModularCms.Modules.Hero.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the
|
||||
/// hero). Read-only: nothing of the hero changes through this class.
|
||||
/// </summary>
|
||||
public class HeroMediaUsageProvider(IHeroRepository repository) : IMediaUsageProvider
|
||||
{
|
||||
public const string SlideKind = "slide";
|
||||
|
||||
public string Module => HeroMapping.Area;
|
||||
|
||||
public async Task<IReadOnlyList<MediaPhoto>> ListPhotosAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var images = await repository.GetAllImagesReadOnlyAsync(cancellationToken);
|
||||
return images
|
||||
.Select(i => new MediaPhoto(Module, i.Id, i.Bytes, i.Width, i.Height, i.CreatedAt, Thumbnail(i)))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaUse>> ListUsesAsync(IReadOnlyCollection<Guid> photoIds, CancellationToken cancellationToken)
|
||||
{
|
||||
var wanted = photoIds.ToHashSet();
|
||||
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.
|
||||
var uses = new List<MediaUse>();
|
||||
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];
|
||||
if (wanted.Contains(slide.ImageId))
|
||||
{
|
||||
uses.Add(new MediaUse(Module, slide.ImageId, SlideKind, index + 1, slide.IsActive));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return uses;
|
||||
}
|
||||
|
||||
/// <summary>The smallest published WebP of the set; null while the set is not cut yet.</summary>
|
||||
private static string? Thumbnail(HeroImage image)
|
||||
{
|
||||
var pick = HeroMapping.Published(image).Where(v => v.Type == ImageFormat.WebP.MimeType()).OrderBy(v => v.Width).FirstOrDefault();
|
||||
return pick is null || string.IsNullOrEmpty(image.SetHash) ? null : MediaPaths.Url(HeroMapping.Area, image.SetHash, pick.FileName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user