Merge origin/master into feature/messages-badge
Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 11s
Continuous Integration / backend-build (pull_request) Successful in 1m46s
Continuous Integration / compile-sites (pull_request) Successful in 1m51s
Continuous Integration / frontend-prepare (pull_request) Successful in 1m57s
Continuous Integration / vulnerability-scan (pull_request) Successful in 2m2s
Continuous Integration / frontend-build (pull_request) Successful in 43s
Continuous Integration / frontend-test (pull_request) Successful in 2m30s
Continuous Integration / backend-test (pull_request) Successful in 2m43s
Continuous Integration / frontend-lint (pull_request) Successful in 2m28s
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:
2026-11-17 12:58:12 +01:00
co-authored by Claude Sonnet 5.5
180 changed files with 17199 additions and 193 deletions
@@ -30,6 +30,7 @@
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
<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.Contact\SlpModularCms.Modules.Contact.csproj" />
<ProjectReference Include="..\SlpModularCms.Modules.Identity\SlpModularCms.Modules.Identity.csproj" />
</ItemGroup>
@@ -24,6 +24,7 @@
<ProjectReference Include="..\SlpModularCms.Modules.Identity\SlpModularCms.Modules.Identity.csproj" />
<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" />
</ItemGroup>
<!--
@@ -9,6 +9,9 @@
}
},
"AllowedHosts": "*",
// The SLP hero has no images: the host writes its title, lead and buttons as text into the first HTML.
// TitlePrefix: visually hidden brand text before the title lines in the h1 (for crawlers and screen readers).
"Hero": { "TextOnlyRegion": true, "TitlePrefix": "SLP Software: " },
"Website": {
// Secure default: every response carries noindex and /robots.txt disallows all. A production
// instance must set Website__NoIndex=false and Website__PublicBaseUrl=https://slpsoftware.nl explicitly;
@@ -0,0 +1,168 @@
using System.Net;
using System.Text.Json;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Api.Tests;
/// <summary>
/// Runs the real master host with ASF settings over the REAL build of the ASF website (its <c>dist/client</c>), with the
/// hero of ASF's seed made through the module's services. Optional: it only runs when <c>ASF_WEB_DIR</c> points at the
/// <c>web</c> folder of the ASF website repository (with a build in <c>dist/client</c>); otherwise it does nothing.
/// With <c>HERO_E2E_OUT</c> set it also writes the served page and the API answer there, so they can be compared
/// with the website's own renderer.
/// </summary>
public sealed class AsfSiteFactory : AsfVariantFactory, IDisposable
{
public string WebRoot { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-site-" + Guid.NewGuid().ToString("N"));
public string MediaDirectory { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-media-" + Guid.NewGuid().ToString("N"));
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
var web = Environment.GetEnvironmentVariable("ASF_WEB_DIR");
if (!string.IsNullOrEmpty(web))
{
CopyDirectory(Path.Combine(web, "dist", "client"), Path.Combine(WebRoot, "web"));
}
builder.UseWebRoot(WebRoot);
builder.ConfigureAppConfiguration((_, config) =>
config.AddInMemoryCollection(new Dictionary<string, string?> { ["Media:Directory"] = MediaDirectory, ["Website:PublicBaseUrl"] = "https://test.andreaslop.nl" }));
}
private static void CopyDirectory(string from, string to)
{
Directory.CreateDirectory(to);
foreach (var file in Directory.GetFiles(from)) File.Copy(file, Path.Combine(to, Path.GetFileName(file)), overwrite: true);
foreach (var dir in Directory.GetDirectories(from)) CopyDirectory(dir, Path.Combine(to, Path.GetFileName(dir)));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
foreach (var directory in new[] { WebRoot, MediaDirectory })
{
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
}
}
}
}
[Collection("HeroDatabase")]
public class HeroAsfSiteE2ETests : IClassFixture<AsfSiteFactory>
{
private readonly AsfSiteFactory _factory;
public HeroAsfSiteE2ETests(AsfSiteFactory factory) => _factory = factory;
private async Task<T> InScope<T>(Func<IServiceProvider, Task<T>> action)
{
await using var scope = _factory.Services.CreateAsyncScope();
return await action(scope.ServiceProvider);
}
[Fact]
public async Task TheRealAsfBuild_GetsTheSeededHero_InItsFirstHtml()
{
var web = Environment.GetEnvironmentVariable("ASF_WEB_DIR");
if (string.IsNullOrEmpty(web) || !File.Exists(Path.Combine(web, "dist", "client", "index.html")))
{
return; // optional check: needs the ASF website repository and its build
}
var client = _factory.CreateClient();
var caller = Guid.NewGuid();
// The image library self-test and the media directory check run in the background after start-up.
var status = _factory.Services.GetRequiredService<SlpModularCms.Core.Media.MediaStatus>();
for (var i = 0; i < 300 && !status.UploadsAvailable; i++) await Task.Delay(100);
await InScope(async sp =>
{
var db = sp.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
return 0;
});
try
{
var seed = JsonDocument.Parse(File.ReadAllText(Path.Combine(web, "contract", "host-injection", "hero", "seed.json"))).RootElement;
var slides = new List<HeroSlideRequest>();
var focusEdits = new List<HeroImageFocusRequest>();
foreach (var slide in seed.GetProperty("slides").EnumerateArray())
{
var photo = File.ReadAllBytes(Path.Combine(web, "src", "assets", slide.GetProperty("file").GetString()!));
var image = await InScope(sp => sp.GetRequiredService<IHeroImageService>().UploadAsync(new MemoryStream(photo), photo.Length, caller, default));
var focus = slide.GetProperty("focus");
// The focus travels with the save of the hero, like the admin does it.
focusEdits.Add(new HeroImageFocusRequest(Guid.Parse(image.Id), new FocusDto(focus.GetProperty("x").GetDouble(), focus.GetProperty("y").GetDouble(), 1)));
slides.Add(new HeroSlideRequest(null, Guid.Parse(image.Id), slide.GetProperty("alt").GetString()));
}
await InScope(sp => sp.GetRequiredService<IHeroService>().SaveAsync(
new HeroSaveRequest(seed.GetProperty("title").GetString(), seed.GetProperty("tagline").GetString(), null, seed.GetProperty("display").GetString(),
seed.GetProperty("buttons").EnumerateArray().Select(b => new HeroButtonRequest(b.GetProperty("label").GetString(), b.GetProperty("target").GetString())).ToList(),
slides, focusEdits), caller, default));
await InScope(async sp =>
{
// The variants are cut by the background renderer in production; here it is run to the end at once.
// The shared test database may hold rows of earlier runs whose files are gone: those fail and are skipped,
// the way the background renderer gives up on them.
var skip = new HashSet<Guid>();
while (true)
{
try
{
if (await sp.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, default) is null)
{
break;
}
}
catch (HeroImageRenderException failure)
{
skip.Add(failure.ImageId);
}
}
return true;
});
var response = await client.GetAsync("/");
var page = await response.Content.ReadAsStringAsync();
var api = await client.GetStringAsync("/api/v1/hero");
response.StatusCode.Should().Be(HttpStatusCode.OK);
page.Should().Contain("<picture class=\"cms-hero-picture\">").And.Contain("<link rel=\"preload\" as=\"image\"");
page.Should().Contain("<!--CMS:HERO-HTML--><picture").And.Contain("</p><!--/CMS:HERO-HTML-->");
page.Should().NotContain("<!--CMS:DATA-->").And.Contain("\"hero\":{");
JsonDocument.Parse(api).RootElement.GetProperty("slides").GetArrayLength().Should().Be(8);
var outDir = Environment.GetEnvironmentVariable("HERO_E2E_OUT");
if (!string.IsNullOrEmpty(outDir))
{
Directory.CreateDirectory(outDir);
File.WriteAllText(Path.Combine(outDir, "page.html"), page);
File.WriteAllText(Path.Combine(outDir, "api.json"), api);
}
}
finally
{
await InScope(async sp =>
{
var db = sp.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
return 0;
});
}
}
}
@@ -0,0 +1,207 @@
using System.Net;
using System.Text.Json;
using FluentAssertions;
using ImageMagick;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Api.Tests;
/// <summary>The hero on the master host with Andrea Slop Fotografie's settings: a prerendered page with the hero markers, a media directory and a real database.</summary>
public sealed class HeroInjectionFactory : AsfVariantFactory, IDisposable
{
public string WebRoot { get; } = Path.Combine(Path.GetTempPath(), "slp-hero-web-" + Guid.NewGuid().ToString("N"));
public string MediaDirectory { get; } = Path.Combine(Path.GetTempPath(), "slp-hero-media-" + Guid.NewGuid().ToString("N"));
public HeroInjectionFactory() => Directory.CreateDirectory(Path.Combine(WebRoot, "web"));
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseWebRoot(WebRoot);
builder.ConfigureAppConfiguration((_, config) =>
config.AddInMemoryCollection(new Dictionary<string, string?> { ["Media:Directory"] = MediaDirectory, ["Website:PublicBaseUrl"] = "https://test.andreaslop.nl" }));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
foreach (var directory in new[] { WebRoot, MediaDirectory })
{
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
}
}
}
}
[Collection("HeroDatabase")]
public class HeroInjectionPipelineTests : IClassFixture<HeroInjectionFactory>
{
private const string Page =
"<!doctype html><html><head><!--CMS:HERO-PRELOAD-->baked preload<!--/CMS:HERO-PRELOAD-->" +
"<!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>" +
"<body><!--CMS:HERO-HTML-->baked hero<!--/CMS:HERO-HTML--></body></html>";
private readonly HeroInjectionFactory _factory;
public HeroInjectionPipelineTests(HeroInjectionFactory factory)
{
_factory = factory;
File.WriteAllText(Path.Combine(factory.WebRoot, "web", "hero.html"), Page);
}
private async Task<T> InScope<T>(Func<IServiceProvider, Task<T>> action)
{
_ = _factory.CreateClient(); // starts the host, migrates and starts the start-up checks
// The image library self-test and the media directory check run in the background after start-up.
var status = _factory.Services.GetRequiredService<SlpModularCms.Core.Media.MediaStatus>();
for (var i = 0; i < 300 && !status.UploadsAvailable; i++) await Task.Delay(100);
await using var scope = _factory.Services.CreateAsyncScope();
return await action(scope.ServiceProvider);
}
private static byte[] Photo()
{
using var image = new MagickImage(MagickColors.Red, 1600, 1067);
using var blue = new MagickImage(MagickColors.Blue, 800, 1067);
image.Composite(blue, 800, 0, CompositeOperator.Over);
using var stream = new MemoryStream();
image.Write(stream, MagickFormat.Jpeg);
return stream.ToArray();
}
private static string DataBlock(string body)
{
const string open = "<script type=\"application/json\" id=\"cms-data\">";
var start = body.IndexOf(open, StringComparison.Ordinal) + open.Length;
return body[start..body.IndexOf("</script>", start, StringComparison.Ordinal)];
}
[Fact]
public async Task AHeroMadeThroughTheModule_ShowsInTheFirstHtml_AndItsImagesAreServed()
{
try
{
await RunAsync();
}
finally
{
await ClearHeroes();
}
}
private async Task ClearHeroes()
=> await InScope(async sp =>
{
var db = sp.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
return 0;
});
private async Task RunAsync()
{
var client = _factory.CreateClient();
var caller = Guid.NewGuid();
// Clean slate: the database is shared with other tests.
await InScope(async sp =>
{
var db = sp.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
return 0;
});
// Without a hero the page stays as it was built, and the data block says so.
var before = await client.GetAsync("/hero");
before.StatusCode.Should().Be(HttpStatusCode.OK);
var beforeBody = await before.Content.ReadAsStringAsync();
beforeBody.Should().Contain("baked hero").And.Contain("baked preload");
JsonDocument.Parse(DataBlock(beforeBody)).RootElement.GetProperty("hero").ValueKind.Should().Be(JsonValueKind.Null);
var etagBefore = before.Headers.ETag!.Tag;
// Upload a photo and publish a hero through the module's services.
var image = await InScope(sp => sp.GetRequiredService<IHeroImageService>().UploadAsync(new MemoryStream(Photo()), Photo().Length, caller, default));
await InScope(async sp => await sp.GetRequiredService<IHeroService>().SaveAsync(
new HeroSaveRequest("Andrea Slop", "Lifestyle fotograaf · Huizen", null, "slideshow", [new HeroButtonRequest("Lees meer", "/over-mij")],
[new HeroSlideRequest(null, Guid.Parse(image.Id), "Zwangerschapsfoto in de duinen")]), caller, default));
await InScope(async sp =>
{
// The variants are cut by the background renderer in production; here it is run to the end at once.
// The shared test database may hold rows of earlier runs whose files are gone: those fail and are skipped,
// the way the background renderer gives up on them.
var skip = new HashSet<Guid>();
while (true)
{
try
{
if (await sp.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, default) is null)
{
break;
}
}
catch (HeroImageRenderException failure)
{
skip.Add(failure.ImageId);
}
}
return true;
});
var live = await client.GetAsync("/hero");
var body = await live.Content.ReadAsStringAsync();
live.Headers.ETag!.Tag.Should().NotBe(etagBefore);
body.Should().NotContain("baked hero").And.NotContain("baked preload");
body.Should().Contain("<picture class=\"cms-hero-picture\"><source media=\"(orientation: portrait)\"");
body.Should().Contain("alt=\"Zwangerschapsfoto in de duinen\"").And.Contain("<span class=\"cms-hero-name\">Andrea Slop</span>");
body.Should().Contain("<a class=\"cms-hero-button\" href=\"/over-mij\">Lees meer</a>");
body.Should().Contain("<link rel=\"preload\" as=\"image\"").And.Contain("media=\"(orientation: landscape)\"");
// The data block carries the same hero as the public endpoint, after the site address.
var data = JsonDocument.Parse(DataBlock(body)).RootElement;
data.EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl", "offerings", "banners", "hero");
var api = JsonDocument.Parse(await client.GetStringAsync("/api/v1/hero")).RootElement;
System.Text.Json.Nodes.JsonNode.DeepEquals(System.Text.Json.Nodes.JsonNode.Parse(data.GetProperty("hero").GetRawText()), System.Text.Json.Nodes.JsonNode.Parse(api.GetRawText()))
.Should().BeTrue("the data block carries the same JSON as GET /api/v1/hero");
// Every address in the page is served by the host: immutable, same origin.
var src = api.GetProperty("slides")[0].GetProperty("image").GetProperty("src").GetString()!;
body.Should().Contain($"src=\"{src}\"");
foreach (var url in api.GetProperty("slides")[0].GetProperty("image").GetProperty("variants").EnumerateArray().Select(v => v.GetProperty("url").GetString()!))
{
var media = await client.GetAsync(url);
media.StatusCode.Should().Be(HttpStatusCode.OK, url);
media.Headers.CacheControl!.ToString().Should().Be("public, max-age=31536000, immutable");
(await media.Content.ReadAsByteArrayAsync()).Length.Should().BeGreaterThan(100);
}
// A matching ETag is a 304.
var conditional = new HttpRequestMessage(HttpMethod.Get, "/hero");
conditional.Headers.IfNoneMatch.Add(live.Headers.ETag);
(await client.SendAsync(conditional)).StatusCode.Should().Be(HttpStatusCode.NotModified);
// A media directory that lost its files leaves the baked hero too (no broken first image).
Directory.Delete(Path.Combine(_factory.MediaDirectory, "hero"), recursive: true);
await InScope(sp => { sp.GetRequiredService<SlpModularCms.Core.Hosting.Website.IIndexHtmlCache>().Invalidate(); return Task.FromResult(0); });
var missing = await client.GetStringAsync("/hero");
missing.Should().Contain("baked hero").And.NotContain("cms-hero-picture");
// Clean up for the other tests.
await InScope(async sp =>
{
var db = sp.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
return 0;
});
}
}
@@ -0,0 +1,82 @@
using System.Net;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Modules.Hero.Data;
namespace SlpModularCms.Api.Tests;
/// <summary>The Hero module on the real master host and the real, migrated database.</summary>
[Collection("HeroDatabase")]
public class HeroOnTheMasterHostTests : IClassFixture<ApiWebApplicationFactory>
{
private readonly ApiWebApplicationFactory _factory;
public HeroOnTheMasterHostTests(ApiWebApplicationFactory factory) => _factory = factory;
[Fact]
public async Task Capabilities_ListTheHeroModule()
{
var response = await _factory.CreateClient().GetAsync("/api/v1/System/capabilities");
using var doc = System.Text.Json.JsonDocument.Parse(await response.Content.ReadAsStringAsync());
doc.RootElement.GetProperty("modules").EnumerateArray().Select(e => e.GetString()).Should().Contain("Hero");
}
[Fact]
public async Task WithoutAHero_ThePublicEndpointAnswers204_WithTheShortCache()
{
// The database is shared with other tests (and earlier runs): start without a hero.
_ = _factory.CreateClient();
using (var scope = _factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
await db.SaveChangesAsync();
}
var response = await _factory.CreateClient().GetAsync("/api/v1/hero");
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
response.Headers.CacheControl!.ToString().Should().Be("public, max-age=60");
}
[Theory]
[InlineData("GET", "/api/v1/hero/all")]
[InlineData("PUT", "/api/v1/hero")]
[InlineData("POST", "/api/v1/hero/images")]
[InlineData("DELETE", "/api/v1/hero/images/11111111-2222-3333-4444-555555555555")]
public async Task Management_RequiresAnAdministrator(string method, string url)
{
var request = new HttpRequestMessage(new HttpMethod(method), url);
if (method == "POST")
{
request.Content = new MultipartFormDataContent { { new ByteArrayContent([1, 2, 3]), "file", "x.jpg" } };
}
var response = await _factory.CreateClient().SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
}
[Theory]
[InlineData("/media/hero/0123456789abcdef/wide-960.webp")]
[InlineData("/media/hero/0123456789abcdef/../../secret")]
[InlineData("/media/originals/anything.jpg")]
public async Task TheMediaEndpoint_AnswersAPlain404_ForAnythingNotStored(string url)
{
var response = await _factory.CreateClient().GetAsync(url);
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public void HeroMigrations_MatchTheModel()
{
_ = _factory.CreateClient(); // starts the host and migrates
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
db.Database.HasPendingModelChanges().Should().BeFalse("every model change needs its migration and snapshot");
}
}
@@ -21,7 +21,7 @@ public class SiteContractTests
public void TheMasterCms_ReferencesEveryModule()
{
SiteFiles.Modules("SlpModularCms.Api").Should().BeEquivalentTo(
["Availability", "Contact", "Identity", "Master", "Offerings"],
["Availability", "Contact", "Hero", "Identity", "Master", "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", "Identity", "Master", "Offerings"]);
["Availability", "Contact", "Hero", "Identity", "Master", "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", "Identity", "Offerings"]);
["Availability", "Contact", "Hero", "Identity", "Offerings"]);
}
[Theory]
@@ -59,6 +59,23 @@ public class SiteContractTests
(configured ?? "SpaFallback").Should().Be(mode);
}
[Theory]
[InlineData(SiteFiles.SlpSoftware, true)]
[InlineData(SiteFiles.AndreaSlopFotografie, false)]
public void TheHeroIsWrittenAsTextOnlyForTheSiteWithoutHeroImages(string site, bool textOnly)
{
// SLP Software's hero has no images, so the host writes it as text; ASF's baked hero is a picture and stays when the CMS hero has no slide.
SiteFiles.Committed(site).GetValue<bool>("Hero:TextOnlyRegion").Should().Be(textOnly);
}
[Theory]
[InlineData(SiteFiles.SlpSoftware, "SLP Software: ")]
[InlineData(SiteFiles.AndreaSlopFotografie, "")]
public void TheHeroTitlePrefix_IsSetForSlpSoftware_AndEmptyForAsf(string site, string prefix)
{
(SiteFiles.Committed(site)["Hero:TitlePrefix"] ?? string.Empty).Should().Be(prefix);
}
[Theory]
[MemberData(nameof(Sites))]
public void WebsiteIsNoIndex_ByDefault(string site)
@@ -24,6 +24,7 @@
<ProjectReference Include="..\SlpModularCms.Modules.Identity\SlpModularCms.Modules.Identity.csproj" />
<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" />
</ItemGroup>
<!--
@@ -0,0 +1,398 @@
using ImageMagick;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Media.Magick;
/// <summary>
/// <see cref="IImageProcessor"/> on Magick.NET (Q8). The format is always passed explicitly (it was decided from the
/// signature by the caller), only the first frame is read, the orientation is applied before cropping, and every output
/// is stripped of metadata after the colours are converted to sRGB. The resource limits of ImageMagick are process
/// wide and are set once from <see cref="MediaOptions"/>.
/// </summary>
public sealed class MagickImageProcessor : IImageProcessor
{
private static readonly object LimitsLock = new();
private static bool _limitsSet;
private readonly MediaOptions _options;
private readonly ILogger<MagickImageProcessor> _logger;
public MagickImageProcessor(IOptions<MediaOptions> options, ILogger<MagickImageProcessor> logger)
{
_options = options.Value;
_logger = logger;
SetLimits(_options);
SupportedInput = DetectInput();
SupportedOutput = DetectOutput();
}
public IReadOnlySet<ImageFormat> SupportedInput { get; }
public IReadOnlySet<ImageFormat> SupportedOutput { get; }
public Task<ImageInfo> InspectAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken)
=> RunAsync(() => Inspect(data, declared), cancellationToken);
public Task<RenderedImage> RenderAsync(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken)
=> RunAsync(() => Render(data, declared, variant), cancellationToken);
public Task<IReadOnlyList<RenderedImage>> RenderSetAsync(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
=> RunAsync(() => RenderSet(data, declared, variants, progress, cancellationToken), cancellationToken, _options.RenderSetTimeoutSeconds);
public Task<byte[]?> StripMetadataAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken)
=> RunAsync(() => StripMetadata(data, declared), cancellationToken, _options.StripTimeoutSeconds);
public async Task SelfTestAsync(CancellationToken cancellationToken)
{
await RunAsync(() =>
{
foreach (var format in SupportedOutput)
{
using var image = new MagickImage(MagickColors.CornflowerBlue, 32, 32);
using var stream = new MemoryStream();
image.Quality = 80;
image.Write(stream, ToMagick(format));
var bytes = stream.ToArray();
var detected = ImageSignature.Detect(bytes);
if (detected != format && !(format == ImageFormat.Avif && detected == ImageFormat.Heic))
{
throw new InvalidOperationException($"The self-test wrote {format} but the signature says {detected}.");
}
using var back = new MagickImage(bytes, new MagickReadSettings { Format = ToMagick(format) });
if (back.Width != 32 || back.Height != 32)
{
throw new InvalidOperationException($"The self-test could not read back {format}.");
}
}
return true;
}, cancellationToken);
_logger.LogInformation(
"Image library Magick.NET self-test passed. Input: {Input}. Output: {Output}.",
string.Join(",", SupportedInput.Order()),
string.Join(",", SupportedOutput.Order()));
}
// ----- work done on a pool thread with a time limit (the native limit below stops a runaway decode) ------------
private async Task<T> RunAsync<T>(Func<T> work, CancellationToken cancellationToken, int? timeoutSeconds = null)
{
try
{
return await Task.Run(work, cancellationToken)
.WaitAsync(TimeSpan.FromSeconds(Math.Max(1, timeoutSeconds ?? _options.DecodeTimeoutSeconds)), cancellationToken);
}
catch (TimeoutException)
{
throw new ImageRejectedException(ImageRejection.Timeout, "Processing the image took too long.");
}
catch (ImageRejectedException)
{
throw;
}
catch (MagickResourceLimitErrorException exception)
{
_logger.LogWarning("Image refused by a resource limit: {Message}", Shorten(exception.Message));
throw new ImageRejectedException(ImageRejection.TooManyPixels, "The image exceeds a processing limit.");
}
catch (MagickException exception)
{
_logger.LogInformation("Image could not be decoded: {Message}", Shorten(exception.Message));
throw new ImageRejectedException(ImageRejection.Corrupt, "The image could not be decoded.");
}
}
private static string Shorten(string message) => message.Length <= 160 ? message : message[..160];
private ImageInfo Inspect(ReadOnlyMemory<byte> data, ImageFormat declared)
{
var settings = new MagickReadSettings { Format = ToMagick(declared) };
using var image = new MagickImage();
image.Ping(data.Span, settings);
var width = (int)image.Width;
var height = (int)image.Height;
if (image.Orientation is Orientation.LeftTop or Orientation.RightTop or Orientation.RightBottom or Orientation.LeftBottom)
{
(width, height) = (height, width);
}
var frames = 1;
try
{
frames = Math.Min(MagickImageInfo.ReadCollection(data.ToArray(), settings).Count(), 10_000);
}
catch (MagickException)
{
// A single readable frame is enough; the count is informational.
}
var hasMetadata = image.GetExifProfile() is not null || image.GetXmpProfile() is not null || image.GetIptcProfile() is not null;
return new ImageInfo(declared, width, height, Math.Max(1, frames), hasMetadata);
}
private RenderedImage Render(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant)
{
using var image = DecodeOriented(data, declared);
return Cut(image, variant);
}
/// <summary>
/// One decode for the whole set, and as little repeated work as possible: per crop the widths are cut from large to
/// small, each from the previous (smaller input than the full picture), and a width is resized once for all of its
/// formats (WebP and AVIF are encoded from the same pixels).
/// </summary>
private IReadOnlyList<RenderedImage> RenderSet(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken)
{
using var image = DecodeOriented(data, declared);
var results = new RenderedImage?[variants.Count];
var done = 0;
foreach (var group in variants.Select((spec, index) => (spec, index)).GroupBy(x => (x.spec.Ratio.Name, x.spec.Crop)))
{
var crop = group.Key.Crop;
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|| crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height)
{
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
}
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
IMagickImage<byte> current = image.Clone();
try
{
current.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
current.ResetPage();
ToSrgb(current);
current.Strip();
foreach (var width in group.GroupBy(x => x.spec.Width).OrderByDescending(g => g.Key))
{
// A cancelled or timed-out job stops at the next width; the native library itself cannot be interrupted.
cancellationToken.ThrowIfCancellationRequested();
var first = width.First().spec;
if (current.Width != (uint)first.Width || current.Height != (uint)first.Height)
{
current.Resize(new MagickGeometry((uint)first.Width, (uint)first.Height) { IgnoreAspectRatio = true });
}
foreach (var (spec, index) in width)
{
results[index] = Encode(current, spec);
progress?.Invoke(++done, variants.Count);
}
}
}
finally
{
current.Dispose();
}
}
return results.Select(r => r!).ToList();
}
/// <summary>Writes already cut and resized pixels in the format and quality of the variant.</summary>
private static RenderedImage Encode(IMagickImage<byte> pixels, VariantSpec variant)
{
pixels.Quality = (uint)variant.Quality;
if (variant.Format == ImageFormat.Avif)
{
pixels.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
}
using var stream = new MemoryStream();
pixels.Write(stream, ToMagick(variant.Format));
return new RenderedImage(stream.ToArray(), (int)pixels.Width, (int)pixels.Height, variant.Format);
}
private static MagickImage DecodeOriented(ReadOnlyMemory<byte> data, ImageFormat declared)
{
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
var image = ReadStrict(data, settings);
image.AutoOrient();
return image;
}
/// <summary>Crops a copy of the decoded picture, resizes and encodes it.</summary>
private static RenderedImage Cut(IMagickImage<byte> source, VariantSpec variant)
{
var crop = variant.Crop;
if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1
|| crop.X + crop.Width > source.Width || crop.Y + crop.Height > source.Height)
{
throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image.");
}
// A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding.
using var part = source.Clone();
part.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height));
part.ResetPage();
part.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true });
ToSrgb(part);
part.Strip();
part.Quality = (uint)variant.Quality;
var format = ToMagick(variant.Format);
if (variant.Format == ImageFormat.Avif)
{
part.Settings.SetDefine(MagickFormat.Avif, "speed", "6");
}
using var stream = new MemoryStream();
part.Write(stream, format);
return new RenderedImage(stream.ToArray(), (int)part.Width, (int)part.Height, variant.Format);
}
private static byte[]? StripMetadata(ReadOnlyMemory<byte> data, ImageFormat declared)
{
var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 };
using var probe = new MagickImage();
probe.Ping(data.Span, settings);
if (probe.GetExifProfile() is null && probe.GetXmpProfile() is null && probe.GetIptcProfile() is null)
{
return null;
}
using var image = ReadStrict(data, settings);
var colour = image.GetColorProfile();
var quality = image.Quality;
image.Strip();
if (colour is not null)
{
image.SetProfile(colour);
}
if (declared is ImageFormat.Jpeg or ImageFormat.WebP or ImageFormat.Avif or ImageFormat.Heic)
{
image.Quality = Math.Max(quality, 92u);
}
using var stream = new MemoryStream();
image.Write(stream, declared == ImageFormat.Heic ? MagickFormat.Jpeg : ToMagick(declared));
return stream.ToArray();
}
// Warnings that mean "the file is damaged": the library would hand back a partly decoded picture, but a truncated
// upload must not become a published image.
private static readonly System.Text.RegularExpressions.Regex DamageWarning = new(
"premature end|truncat|unexpected end|insufficient image data|corrupt|not enough|bad (?:huffman|data)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.CultureInvariant);
private static MagickImage ReadStrict(ReadOnlyMemory<byte> data, MagickReadSettings settings)
{
var image = new MagickImage();
string? damage = null;
image.Warning += (_, e) =>
{
if (damage is null && DamageWarning.IsMatch(e.Message))
{
damage = e.Message;
}
};
try
{
image.Read(data.Span, settings);
}
catch
{
image.Dispose();
throw;
}
if (damage is not null)
{
image.Dispose();
throw new ImageRejectedException(ImageRejection.Corrupt, "The image is damaged or incomplete.");
}
return image;
}
private static void ToSrgb(IMagickImage<byte> image)
{
try
{
if (image.ColorSpace == ColorSpace.CMYK || image.GetColorProfile() is not null)
{
image.TransformColorSpace(ColorProfiles.SRGB);
}
}
catch (MagickException)
{
// An unusual profile: keep the pixels, the metadata is stripped afterwards anyway.
}
}
// ----- capabilities and limits ---------------------------------------------------------------------------------
private static void SetLimits(MediaOptions options)
{
lock (LimitsLock)
{
if (_limitsSet)
{
return;
}
ResourceLimits.Area = (ulong)Math.Max(1, options.MaxPixels);
ResourceLimits.Width = (ulong)Math.Max(1, options.MaxSide);
ResourceLimits.Height = (ulong)Math.Max(1, options.MaxSide);
ResourceLimits.Memory = 1UL * 1024 * 1024 * 1024;
// The library's own clock runs per picture from its creation: it must not end a set that the job timeout allows.
ResourceLimits.Time = (ulong)Math.Max(1, Math.Max(options.DecodeTimeoutSeconds, Math.Max(options.StripTimeoutSeconds, options.RenderSetTimeoutSeconds)) + 30);
_limitsSet = true;
}
}
private static HashSet<ImageFormat> DetectInput()
{
var wanted = new Dictionary<MagickFormat, ImageFormat>
{
[MagickFormat.Jpeg] = ImageFormat.Jpeg,
[MagickFormat.Png] = ImageFormat.Png,
[MagickFormat.Gif] = ImageFormat.Gif,
[MagickFormat.Bmp] = ImageFormat.Bmp,
[MagickFormat.Tiff] = ImageFormat.Tiff,
[MagickFormat.WebP] = ImageFormat.WebP,
[MagickFormat.Avif] = ImageFormat.Avif,
[MagickFormat.Heic] = ImageFormat.Heic,
};
var supported = MagickNET.SupportedFormats.Where(f => f.SupportsReading).Select(f => f.Format).ToHashSet();
return wanted.Where(w => supported.Contains(w.Key) || (w.Key == MagickFormat.Jpeg && supported.Contains(MagickFormat.Jpg)))
.Select(w => w.Value).ToHashSet();
}
private static HashSet<ImageFormat> DetectOutput()
{
var writable = MagickNET.SupportedFormats.Where(f => f.SupportsWriting).Select(f => f.Format).ToHashSet();
var result = new HashSet<ImageFormat> { ImageFormat.Jpeg };
if (writable.Contains(MagickFormat.WebP))
{
result.Add(ImageFormat.WebP);
}
if (writable.Contains(MagickFormat.Avif))
{
result.Add(ImageFormat.Avif);
}
return result;
}
private static MagickFormat ToMagick(ImageFormat format) => format switch
{
ImageFormat.Jpeg => MagickFormat.Jpeg,
ImageFormat.Png => MagickFormat.Png,
ImageFormat.Gif => MagickFormat.Gif,
ImageFormat.Bmp => MagickFormat.Bmp,
ImageFormat.Tiff => MagickFormat.Tiff,
ImageFormat.WebP => MagickFormat.WebP,
ImageFormat.Avif => MagickFormat.Avif,
ImageFormat.Heic => MagickFormat.Heic,
_ => throw new ArgumentOutOfRangeException(nameof(format), format, "Not an image format the processor knows."),
};
}
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Media.Magick;
public static class MagickServiceCollectionExtensions
{
/// <summary>Registers the Magick.NET implementation of <see cref="IImageProcessor"/> (stateless, thread-safe).</summary>
public static IServiceCollection AddMagickImageProcessor(this IServiceCollection services)
{
services.TryAddSingleton<IImageProcessor, MagickImageProcessor>();
return services;
}
}
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- The image library behind IImageProcessor (Apache-2.0). Chosen in the library proof of the hero-module feature
(aidlc-docs/features/hero-module/construction/h1-image-pipeline/nfr-requirements/library-proof.md). Kept in its own
project so hosts that never process images (the slave) do not carry the 35 MB native library. -->
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="15.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,97 @@
using System.Text.Json;
using FluentAssertions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using SlpModularCms.Core.Hosting.Website;
namespace SlpModularCms.Core.Tests.Hosting.Website;
public class CmsDataBlockTransformTests
{
private static readonly HtmlTransformContext Context = new("https://test.example.nl");
private const string Open = "<script type=\"application/json\" id=\"cms-data\">";
private static ICmsDataBlockContributor Contributor(string key, int order, object? value)
{
var c = Substitute.For<ICmsDataBlockContributor>();
c.Key.Returns(key);
c.Order.Returns(order);
c.GetAsync(default!, default).ReturnsForAnyArgs(value);
return c;
}
private static JsonElement Data(string html)
{
var start = html.IndexOf(Open, StringComparison.Ordinal) + Open.Length;
return JsonDocument.Parse(html[start..html.IndexOf("</script>", start, StringComparison.Ordinal)]).RootElement;
}
[Theory]
[InlineData("<!--CMS:DATA-->", true)]
[InlineData("<p>nothing</p>", false)]
[InlineData("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->", false)]
public void Wants_OnlyPagesWithTheDataMarker(string html, bool expected)
=> new CmsDataBlockTransform([]).Wants(html).Should().Be(expected);
[Fact]
public async Task TheKeys_FollowTheOrderOfTheContributors_AfterTheSiteUrl()
{
var transform = new CmsDataBlockTransform([Contributor("hero", 200, new { a = 1 }), Contributor("offerings", 100, new[] { 1, 2 }), Contributor("banners", 110, new { top = (string?)null })]);
var html = await transform.ApplyAsync("<head><!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>", Context, default);
html.Should().StartWith("<head>" + Open).And.EndWith("</script></head>").And.NotContain("baked");
var data = Data(html);
data.EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl", "offerings", "banners", "hero");
data.GetProperty("siteUrl").GetString().Should().Be("https://test.example.nl");
data.GetProperty("offerings").GetRawText().Should().Be("[1,2]");
data.GetProperty("banners").GetProperty("top").ValueKind.Should().Be(JsonValueKind.Null);
}
[Fact]
public async Task ANullValue_IsWrittenAsNull_SoTheWebsiteKeepsItsBakedVersion()
{
var html = await new CmsDataBlockTransform([Contributor("hero", 200, null)]).ApplyAsync("<!--CMS:DATA-->", Context, default);
Data(html).GetProperty("hero").ValueKind.Should().Be(JsonValueKind.Null);
}
[Fact]
public async Task TheValues_UseTheCamelCaseShapeOfThePublicApi()
{
var html = await new CmsDataBlockTransform([Contributor("x", 1, new { SomeName = "a", Nested = new { InnerValue = (int?)null } })]).ApplyAsync("<!--CMS:DATA-->", Context, default);
html.Should().Contain("\"someName\":\"a\"").And.Contain("\"innerValue\":null");
}
[Fact]
public async Task TheBlock_CannotCloseTheScriptElement()
{
var html = await new CmsDataBlockTransform([Contributor("x", 1, new { text = "</script><img src=x onerror=alert(1)> & \u2028" })]).ApplyAsync("<!--CMS:DATA-->", Context, default);
var inner = html[Open.Length..^"</script>".Length];
inner.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028");
JsonDocument.Parse(inner).RootElement.GetProperty("x").GetProperty("text").GetString().Should().Be("</script><img src=x onerror=alert(1)> & \u2028");
}
[Fact]
public async Task AFailingContributor_FailsTheWholeBlock_SoThePageKeepsItsBakedBlock()
{
var failing = Substitute.For<ICmsDataBlockContributor>();
failing.Key.Returns("hero");
failing.Order.Returns(200);
failing.GetAsync(default!, default).ThrowsAsyncForAnyArgs(new InvalidOperationException("boom"));
var act = async () => await new CmsDataBlockTransform([Contributor("offerings", 100, new[] { 1 }), failing]).ApplyAsync("<!--CMS:DATA-->", Context, default);
await act.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public async Task WithoutContributors_TheBlockHoldsTheSiteUrlOnly()
{
var html = await new CmsDataBlockTransform([]).ApplyAsync("<!--CMS:DATA-->", Context, default);
Data(html).EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl");
}
}
@@ -172,6 +172,37 @@ public class AuthServiceTests
allTokens.Should().AllSatisfy(t => t.IsRevoked.Should().BeTrue());
}
[Fact]
public async Task RefreshTokenAsync_ShouldAnswerAConcurrentSecondRefresh_WithoutRevokingTheSessions()
{
var user = new ApplicationUser { Id = Guid.NewGuid(), Email = "user@example.com", IsActive = true };
_context.Users.Add(user);
_context.RefreshTokens.Add(new RefreshToken { Token = "old", UserId = user.Id, ExpiryDate = DateTimeOffset.UtcNow.AddDays(1), IsUsed = true, CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-10) });
_context.RefreshTokens.Add(new RefreshToken { Token = "new", UserId = user.Id, ExpiryDate = DateTimeOffset.UtcNow.AddDays(1), CreatedAt = DateTimeOffset.UtcNow.AddSeconds(-2) });
await _context.SaveChangesAsync();
_userManager.GetRolesAsync(Arg.Any<ApplicationUser>()).Returns(new List<string> { "User" });
var result = await _service.RefreshTokenAsync("old");
result.AccessToken.Should().NotBeNullOrEmpty();
(await _context.RefreshTokens.Where(t => t.UserId == user.Id && t.Token != "old").ToListAsync()).Should().OnlyContain(t => !t.IsRevoked);
}
[Fact]
public async Task RefreshTokenAsync_ShouldStillRevokeEverything_WhenAUsedTokenComesBackAfterTheGracePeriod()
{
var user = new ApplicationUser { Id = Guid.NewGuid(), Email = "user@example.com", IsActive = true };
_context.Users.Add(user);
_context.RefreshTokens.Add(new RefreshToken { Token = "old", UserId = user.Id, ExpiryDate = DateTimeOffset.UtcNow.AddDays(1), IsUsed = true, CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-10) });
_context.RefreshTokens.Add(new RefreshToken { Token = "new", UserId = user.Id, ExpiryDate = DateTimeOffset.UtcNow.AddDays(1), CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-5) });
await _context.SaveChangesAsync();
var act = () => _service.RefreshTokenAsync("old");
await act.Should().ThrowAsync<UnauthorizedException>();
(await _context.RefreshTokens.ToListAsync()).Should().AllSatisfy(t => t.IsRevoked.Should().BeTrue());
}
[Fact]
public async Task RefreshTokenAsync_ShouldReturnNewTokens_WhenTokenValid()
{
@@ -0,0 +1,235 @@
using FluentAssertions;
using ImageMagick;
using ImageMagick.Formats;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Magick;
namespace SlpModularCms.Core.Tests.Media;
/// <summary>The real image library on small generated fixtures (no files from disk, so they run the same everywhere).</summary>
public class MagickImageProcessorTests
{
private static readonly MagickImageProcessor Processor = new(Options.Create(new MediaOptions()), NullLogger<MagickImageProcessor>.Instance);
private static byte[] Encode(MagickImage image, MagickFormat format)
{
using var stream = new MemoryStream();
image.Write(stream, format);
return stream.ToArray();
}
/// <summary>Left half red, right half blue: lets a test see which part of the picture a crop kept.</summary>
private static MagickImage TwoColours(int width, int height)
{
var image = new MagickImage(MagickColors.Red, (uint)width, (uint)height);
using var blue = new MagickImage(MagickColors.Blue, (uint)(width / 2), (uint)height);
image.Composite(blue, width - width / 2, 0, CompositeOperator.Over);
return image;
}
private static VariantSpec Spec(int w, int h, int x, int y, int cw, int ch, ImageFormat format = ImageFormat.WebP, Ratio? ratio = null)
=> new(ratio ?? Ratio.Wide, w, h, format, 80, new CropRect(x, y, cw, ch));
[Fact]
public void TheCapabilities_CoverTheCommonFormats()
{
Processor.SupportedInput.Should().Contain([ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif, ImageFormat.Bmp, ImageFormat.Tiff, ImageFormat.WebP, ImageFormat.Avif]);
Processor.SupportedOutput.Should().Contain([ImageFormat.WebP, ImageFormat.Jpeg]);
}
[Fact]
public async Task TheSelfTest_Passes() => await Processor.Invoking(p => p.SelfTestAsync(CancellationToken.None)).Should().NotThrowAsync();
[Theory]
[InlineData(MagickFormat.Jpeg, ImageFormat.Jpeg)]
[InlineData(MagickFormat.Png, ImageFormat.Png)]
[InlineData(MagickFormat.Gif, ImageFormat.Gif)]
[InlineData(MagickFormat.Bmp, ImageFormat.Bmp)]
[InlineData(MagickFormat.Tiff, ImageFormat.Tiff)]
[InlineData(MagickFormat.WebP, ImageFormat.WebP)]
[InlineData(MagickFormat.Avif, ImageFormat.Avif)]
public async Task EveryCommonFormat_IsRecognisedAndInspected(MagickFormat magick, ImageFormat expected)
{
using var image = TwoColours(120, 80);
var bytes = Encode(image, magick);
var detected = ImageSignature.Detect(bytes);
detected.Should().Be(expected == ImageFormat.Avif ? ImageFormat.Avif : expected);
var info = await Processor.InspectAsync(bytes, detected, CancellationToken.None);
info.Width.Should().Be(120);
info.Height.Should().Be(80);
info.FrameCount.Should().Be(1);
}
[Fact]
public async Task ARotatedPhoto_IsReportedInItsOrientedSize()
{
using var image = new MagickImage(MagickColors.Green, 400, 200);
var exif = new ExifProfile();
image.Orientation = Orientation.RightTop; // rotate 90 degrees clockwise on display
exif.SetValue(ExifTag.Orientation, (ushort)6);
image.SetProfile(exif);
var bytes = Encode(image, MagickFormat.Jpeg);
var info = await Processor.InspectAsync(bytes, ImageFormat.Jpeg, CancellationToken.None);
(info.Width, info.Height).Should().Be((200, 400));
info.HasMetadata.Should().BeTrue();
}
[Fact]
public async Task AnAnimatedGif_ReportsItsFrames_AndRendersTheFirst()
{
using var frames = new MagickImageCollection();
frames.Add(new MagickImage(MagickColors.Red, 60, 40) { AnimationDelay = 10 });
frames.Add(new MagickImage(MagickColors.Blue, 60, 40) { AnimationDelay = 10 });
using var stream = new MemoryStream();
frames.Write(stream, MagickFormat.Gif);
var bytes = stream.ToArray();
var info = await Processor.InspectAsync(bytes, ImageFormat.Gif, CancellationToken.None);
var rendered = await Processor.RenderAsync(bytes, ImageFormat.Gif, Spec(60, 40, 0, 0, 60, 40, ImageFormat.Png), CancellationToken.None);
info.FrameCount.Should().Be(2);
using var back = new MagickImage(rendered.Bytes);
back.GetPixels().GetPixel(5, 5).ToColor()!.ToString().Should().StartWith("#FF0000");
}
[Fact]
public async Task ARender_HasTheRequestedSize_AndNoMetadata()
{
using var image = TwoColours(1600, 1067);
var exif = new ExifProfile();
exif.SetValue(ExifTag.Software, "camera firmware");
exif.SetValue(ExifTag.GPSLatitudeRef, "N");
image.SetProfile(exif);
var bytes = Encode(image, MagickFormat.Jpeg);
var rendered = await Processor.RenderAsync(bytes, ImageFormat.Jpeg, Spec(960, 640, 0, 0, 1600, 1067), CancellationToken.None);
rendered.Format.Should().Be(ImageFormat.WebP);
(rendered.Width, rendered.Height).Should().Be((960, 640));
ImageSignature.Detect(rendered.Bytes).Should().Be(ImageFormat.WebP);
using var back = new MagickImage();
back.Ping(rendered.Bytes);
back.GetExifProfile().Should().BeNull();
back.GetXmpProfile().Should().BeNull();
}
[Fact]
public async Task TheCrop_KeepsTheChosenPartOfThePicture()
{
using var image = TwoColours(1000, 500);
var bytes = Encode(image, MagickFormat.Png);
var left = FocusMath.CropFor(1000, 500, Ratio.Tall, new FocusSetting(0.0, 0.5, 1));
var right = FocusMath.CropFor(1000, 500, Ratio.Tall, new FocusSetting(1.0, 0.5, 1));
var leftImage = await Processor.RenderAsync(bytes, ImageFormat.Png, Spec(200, 250, left.X, left.Y, left.Width, left.Height, ImageFormat.Png, Ratio.Tall), CancellationToken.None);
var rightImage = await Processor.RenderAsync(bytes, ImageFormat.Png, Spec(200, 250, right.X, right.Y, right.Width, right.Height, ImageFormat.Png, Ratio.Tall), CancellationToken.None);
using var l = new MagickImage(leftImage.Bytes);
using var r = new MagickImage(rightImage.Bytes);
l.GetPixels().GetPixel(100, 125).ToColor()!.ToString().Should().StartWith("#FF0000"); // red
r.GetPixels().GetPixel(100, 125).ToColor()!.ToString().Should().StartWith("#0000FF"); // blue
}
[Fact]
public async Task ARotatedPhoto_IsCroppedInItsOrientedSize()
{
using var image = TwoColours(400, 200);
var exif = new ExifProfile();
image.Orientation = Orientation.RightTop;
exif.SetValue(ExifTag.Orientation, (ushort)6);
image.SetProfile(exif);
var bytes = Encode(image, MagickFormat.Jpeg);
var rendered = await Processor.RenderAsync(bytes, ImageFormat.Jpeg, Spec(100, 200, 0, 0, 200, 400, ImageFormat.Jpeg, Ratio.Tall), CancellationToken.None);
(rendered.Width, rendered.Height).Should().Be((100, 200));
}
[Fact]
public async Task AvifOutput_IsWrittenWhenSupported()
{
Processor.SupportedOutput.Should().Contain(ImageFormat.Avif);
using var image = TwoColours(640, 427);
var bytes = Encode(image, MagickFormat.Png);
var rendered = await Processor.RenderAsync(bytes, ImageFormat.Png, Spec(320, 213, 0, 0, 640, 427, ImageFormat.Avif), CancellationToken.None);
ImageSignature.Detect(rendered.Bytes).Should().Be(ImageFormat.Avif);
}
[Fact]
public async Task ATruncatedFile_IsRejectedAsCorrupt()
{
using var image = TwoColours(800, 600);
var bytes = Encode(image, MagickFormat.Jpeg);
var truncated = bytes.AsMemory(0, bytes.Length / 3);
var act = async () => await Processor.RenderAsync(truncated, ImageFormat.Jpeg, Spec(100, 67, 0, 0, 800, 534), CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Reason.Should().Be(ImageRejection.Corrupt);
}
[Fact]
public async Task AFileWithAJpegHeaderAndGarbage_IsRejectedAsCorrupt()
{
var garbage = new byte[2000];
new Random(7).NextBytes(garbage);
garbage[0] = 0xFF;
garbage[1] = 0xD8;
garbage[2] = 0xFF;
var act = async () => await Processor.InspectAsync(garbage, ImageFormat.Jpeg, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Reason.Should().Be(ImageRejection.Corrupt);
}
[Fact]
public async Task AFileThatClaimsAnotherFormat_IsRejected()
{
using var image = TwoColours(50, 50);
var png = Encode(image, MagickFormat.Png);
var act = async () => await Processor.InspectAsync(png, ImageFormat.Tiff, CancellationToken.None);
await act.Should().ThrowAsync<ImageRejectedException>();
}
[Fact]
public async Task StrippingMetadata_RemovesTheProfiles_AndKeepsTheSize()
{
using var image = TwoColours(300, 200);
var exif = new ExifProfile();
exif.SetValue(ExifTag.Software, "camera");
image.SetProfile(exif);
var withExif = Encode(image, MagickFormat.Jpeg);
using var plain = TwoColours(300, 200);
var withoutExif = Encode(plain, MagickFormat.Jpeg);
var stripped = await Processor.StripMetadataAsync(withExif, ImageFormat.Jpeg, CancellationToken.None);
var untouched = await Processor.StripMetadataAsync(withoutExif, ImageFormat.Jpeg, CancellationToken.None);
untouched.Should().BeNull();
stripped.Should().NotBeNull();
using var back = new MagickImage();
back.Ping(stripped!);
back.GetExifProfile().Should().BeNull();
(back.Width, back.Height).Should().Be((300u, 200u));
}
[Fact]
public async Task APlannedCrop_ThatDoesNotFit_IsRefused()
{
using var image = TwoColours(100, 100);
var bytes = Encode(image, MagickFormat.Png);
var act = async () => await Processor.RenderAsync(bytes, ImageFormat.Png, Spec(50, 33, 50, 50, 100, 100), CancellationToken.None);
await act.Should().ThrowAsync<ImageRejectedException>();
}
}
@@ -0,0 +1,290 @@
using FluentAssertions;
using FsCheck;
using FsCheck.Fluent;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Tests.Media;
public class FocusMathTests
{
private static Arbitrary<(int W, int H, FocusSetting Focus, Ratio Ratio)> Cases() =>
(from w in Gen.Choose(1, 9000)
from h in Gen.Choose(1, 9000)
from fx in Gen.Choose(0, 1000)
from fy in Gen.Choose(0, 1000)
from zoom in Gen.Choose(1000, 4000)
from tall in Gen.Elements(true, false)
select (w, h, new FocusSetting(fx / 1000.0, fy / 1000.0, zoom / 1000.0), tall ? Ratio.Tall : Ratio.Wide)).ToArbitrary();
private static void ForAllCases(Action<(int W, int H, FocusSetting Focus, Ratio Ratio)> check) =>
Prop.ForAll(Cases(), c => { check(c); return true; }).Check(Config.QuickThrowOnFailure.WithMaxTest(2000));
[Fact]
public void TheCrop_IsAlwaysInsideTheImage() => ForAllCases(c =>
{
var crop = FocusMath.CropFor(c.W, c.H, c.Ratio, c.Focus);
crop.Width.Should().BeInRange(1, c.W);
crop.Height.Should().BeInRange(1, c.H);
crop.X.Should().BeGreaterThanOrEqualTo(0);
crop.Y.Should().BeGreaterThanOrEqualTo(0);
(crop.X + crop.Width).Should().BeLessThanOrEqualTo(c.W);
(crop.Y + crop.Height).Should().BeLessThanOrEqualTo(c.H);
});
[Fact]
public void TheFocusPoint_IsInsideTheCrop() => ForAllCases(c =>
{
var crop = FocusMath.CropFor(c.W, c.H, c.Ratio, c.Focus);
var px = c.Focus.X * c.W;
var py = c.Focus.Y * c.H;
px.Should().BeInRange(crop.X - 1, crop.X + crop.Width + 1);
py.Should().BeInRange(crop.Y - 1, crop.Y + crop.Height + 1);
});
[Fact]
public void TheCrop_HasTheRatio_WithinOnePixel() => ForAllCases(c =>
{
// Only meaningful when the crop is big enough for one pixel not to matter.
var crop = FocusMath.CropFor(c.W, c.H, c.Ratio, c.Focus);
if (crop.Width >= 50 && crop.Height >= 50)
{
var expectedHeight = crop.Width * (double)c.Ratio.Denominator / c.Ratio.Numerator;
Math.Abs(crop.Height - expectedHeight).Should().BeLessThanOrEqualTo(1.0);
}
});
[Fact]
public void MoreZoom_NeverGivesABiggerCrop() => ForAllCases(c =>
{
var plain = FocusMath.CropFor(c.W, c.H, c.Ratio, c.Focus with { Zoom = 1 });
var zoomed = FocusMath.CropFor(c.W, c.H, c.Ratio, c.Focus);
zoomed.Width.Should().BeLessThanOrEqualTo(plain.Width);
});
[Fact]
public void InvalidFocusValues_AreClamped_NotFatal()
{
var crop = FocusMath.CropFor(1600, 1067, Ratio.Tall, new FocusSetting(double.NaN, double.PositiveInfinity, double.NegativeInfinity));
crop.X.Should().BeGreaterThanOrEqualTo(0);
(crop.X + crop.Width).Should().BeLessThanOrEqualTo(1600);
}
// Shared vectors: the admin's TypeScript twin has the same cases (frontend/src/features/hero/focusMath.test.ts).
[Theory]
[InlineData(1600, 1067, "wide", 0.5, 0.5, 1.0, 0, 0, 1600, 1067)]
[InlineData(1600, 1067, "tall", 0.5, 0.5, 1.0, 374, 1, 853, 1066)]
[InlineData(1600, 1067, "tall", 0.0, 0.5, 1.0, 0, 1, 853, 1066)]
[InlineData(1600, 1067, "tall", 1.0, 0.5, 1.0, 747, 1, 853, 1066)]
[InlineData(1600, 1067, "wide", 0.45, 0.35, 2.0, 320, 107, 800, 533)]
[InlineData(2560, 1702, "tall", 0.55, 0.6, 1.0, 728, 1, 1361, 1701)]
[InlineData(1000, 2000, "wide", 0.5, 0.5, 1.0, 0, 667, 1000, 667)]
public void KnownVectors(int w, int h, string ratio, double fx, double fy, double zoom, int x, int y, int cw, int ch)
{
var crop = FocusMath.CropFor(w, h, ratio == "tall" ? Ratio.Tall : Ratio.Wide, new FocusSetting(fx, fy, zoom));
crop.Should().Be(new CropRect(x, y, cw, ch));
}
}
public class VariantPlannerTests
{
private static readonly IReadOnlySet<ImageFormat> WebPAndAvif = new HashSet<ImageFormat> { ImageFormat.WebP, ImageFormat.Avif };
private static readonly IReadOnlySet<ImageFormat> WebPOnly = new HashSet<ImageFormat> { ImageFormat.WebP };
private static RatioPlan[] Plans() =>
[
new(Ratio.Wide, FocusSetting.Centre, VariantPlanner.DefaultWidths(Ratio.Wide)),
new(Ratio.Tall, FocusSetting.Centre, VariantPlanner.DefaultWidths(Ratio.Tall)),
];
[Fact]
public void ASmallSource_IsNeverScaledUp()
{
var plan = VariantPlanner.Plan(1600, 1067, Plans(), WebPAndAvif);
plan.Should().OnlyContain(v => v.Width <= v.Crop.Width);
plan.Where(v => v.Ratio.Name == "wide").Select(v => v.Width).Distinct().Should().Equal(640, 960, 1280, 1600);
plan.Where(v => v.Ratio.Name == "tall").Select(v => v.Width).Distinct().Should().Equal(480, 640); // the tall crop of a 1600x1067 image is 853 wide
}
[Fact]
public void ALargeSource_GetsTheWholeLadder()
{
var plan = VariantPlanner.Plan(6000, 4000, Plans(), WebPAndAvif);
plan.Where(v => v.Ratio.Name == "wide").Select(v => v.Width).Distinct().Should().Equal(640, 960, 1280, 1600, 2560);
plan.Where(v => v.Ratio.Name == "tall").Select(v => v.Width).Distinct().Should().Equal(480, 640, 960, 1280);
}
[Fact]
public void ATinySource_StillGetsOneVariantPerRatio()
{
var plan = VariantPlanner.Plan(300, 200, Plans(), WebPOnly);
plan.Where(v => v.Ratio.Name == "wide").Should().ContainSingle().Which.Width.Should().Be(300);
plan.Where(v => v.Ratio.Name == "tall").Should().ContainSingle().Which.Width.Should().Be(160);
}
[Fact]
public void WebPIsAlwaysPlanned_AvifOnlyWhenTheProcessorCan()
{
VariantPlanner.Plan(2000, 1500, Plans(), WebPOnly).Select(v => v.Format).Distinct().Should().Equal(ImageFormat.WebP);
VariantPlanner.Plan(2000, 1500, Plans(), WebPAndAvif).Select(v => v.Format).Distinct().Should().BeEquivalentTo([ImageFormat.WebP, ImageFormat.Avif]);
}
[Fact]
public void WithoutAWebPEncoder_TheFallbackIsJpeg()
{
var onlyAvif = new HashSet<ImageFormat> { ImageFormat.Avif };
VariantPlanner.Plan(2000, 1500, Plans(), onlyAvif).Select(v => v.Format).Distinct().Should().BeEquivalentTo([ImageFormat.Jpeg, ImageFormat.Avif]);
}
[Fact]
public void Widths_AreAscendingPerRatio_AndHeightsKeepTheRatio()
{
Prop.ForAll(Gen.Choose(50, 9000).SelectMany(w => Gen.Choose(50, 9000).Select(h => (w, h))).ToArbitrary(), size =>
{
var plan = VariantPlanner.Plan(size.w, size.h, Plans(), WebPAndAvif);
foreach (var group in plan.GroupBy(v => v.Ratio.Name))
{
var widths = group.Where(v => v.Format == ImageFormat.WebP).Select(v => v.Width).ToList();
widths.Should().BeInAscendingOrder().And.OnlyHaveUniqueItems();
widths.Should().NotBeEmpty();
foreach (var v in group)
{
Math.Abs(v.Height - v.Width * (double)v.Ratio.Denominator / v.Ratio.Numerator).Should().BeLessThanOrEqualTo(0.5);
}
}
return true;
}).Check(Config.QuickThrowOnFailure.WithMaxTest(500));
}
}
public class MediaPathsTests
{
private const string Sha = "0f8c6d3e2a1b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5";
[Fact]
public void TheSetHash_IsStable_AndChangesWhenTheFocusChanges()
{
var a = MediaPaths.SetHash(Sha, [("wide", FocusSetting.Centre), ("tall", FocusSetting.Centre)]);
var again = MediaPaths.SetHash(Sha, [("tall", FocusSetting.Centre), ("wide", FocusSetting.Centre)]);
var moved = MediaPaths.SetHash(Sha, [("wide", FocusSetting.Centre), ("tall", new FocusSetting(0.4, 0.5, 1))]);
var other = MediaPaths.SetHash(Sha.Replace('0', '1'), [("wide", FocusSetting.Centre), ("tall", FocusSetting.Centre)]);
a.Should().MatchRegex("^[0-9a-f]{16}$");
again.Should().Be(a);
moved.Should().NotBe(a);
other.Should().NotBe(a);
}
[Fact]
public void SmallDifferences_BelowTheCanonicalPrecision_GiveTheSameSet()
=> MediaPaths.SetHash(Sha, [("wide", new FocusSetting(0.50001, 0.5, 1))])
.Should().Be(MediaPaths.SetHash(Sha, [("wide", new FocusSetting(0.5, 0.5, 1))]));
[Fact]
public void AnAddress_RoundTrips()
{
var hash = MediaPaths.SetHash(Sha, [("wide", FocusSetting.Centre)]);
var name = MediaPaths.FileName("wide", 1600, ImageFormat.WebP);
var url = MediaPaths.Url("hero", hash, name);
url.Should().Be($"/media/hero/{hash}/wide-1600.webp");
MediaPaths.TryParse(url[MediaPaths.UrlPrefix.Length..], out var address).Should().BeTrue();
address.Should().Be(new MediaAddress("hero", hash, "wide-1600.webp"));
MediaPaths.TryParseName(name, out var ratio, out var width, out var format).Should().BeTrue();
(ratio, width, format).Should().Be(("wide", 1600, ImageFormat.WebP));
}
[Theory]
[InlineData("")]
[InlineData("hero/0123456789abcdef/wide-1600.png")]
[InlineData("hero/0123456789abcde/wide-1600.webp")]
[InlineData("hero/0123456789ABCDEF/wide-1600.webp")]
[InlineData("hero/0123456789abcdef/../wide-1600.webp")]
[InlineData("../hero/0123456789abcdef/wide-1600.webp")]
[InlineData("hero//0123456789abcdef/wide-1600.webp")]
[InlineData("hero/0123456789abcdef/wide-1600.webp/")]
[InlineData("hero\\0123456789abcdef\\wide-1600.webp")]
[InlineData("hero/0123456789abcdef/wide-16.webp ")]
[InlineData("hero/0123456789abcdef/WIDE-1600.webp")]
[InlineData("hero/0123456789abcdef/original.jpg")]
[InlineData("originals/0123456789abcdef/wide-1600.webp.exe")]
public void AnythingElse_IsNotAnAddress(string path) => MediaPaths.TryParse(path, out _).Should().BeFalse();
}
public class ImageSignatureTests
{
private static byte[] Padded(params byte[] head) => head.Concat(new byte[32 - head.Length]).ToArray();
[Fact]
public void KnownSignatures_AreRecognised()
{
ImageSignature.Detect(Padded(0xFF, 0xD8, 0xFF, 0xE0)).Should().Be(ImageFormat.Jpeg);
ImageSignature.Detect(Padded(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)).Should().Be(ImageFormat.Png);
ImageSignature.Detect("GIF89a"u8.ToArray().Concat(new byte[26]).ToArray()).Should().Be(ImageFormat.Gif);
ImageSignature.Detect(Padded(0x49, 0x49, 0x2A, 0x00)).Should().Be(ImageFormat.Tiff);
ImageSignature.Detect(Padded(0x4D, 0x4D, 0x00, 0x2A)).Should().Be(ImageFormat.Tiff);
ImageSignature.Detect(Riff("WEBP")).Should().Be(ImageFormat.WebP);
ImageSignature.Detect(Ftyp("avif")).Should().Be(ImageFormat.Avif);
ImageSignature.Detect(Ftyp("heic")).Should().Be(ImageFormat.Heic);
ImageSignature.Detect(Bmp(40)).Should().Be(ImageFormat.Bmp);
}
[Theory]
[InlineData("<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>")]
[InlineData("<?xml version=\"1.0\"?><svg/>")]
[InlineData("%PDF-1.7 and more text here to fill")]
[InlineData("MZ\u0090\u0000\u0003 an executable header")]
[InlineData("<html><script>alert(1)</script></html>")]
[InlineData("PK\u0003\u0004 a zip archive of something")]
[InlineData("")]
public void NotAnImage_IsUnknown(string text)
=> ImageSignature.Detect(System.Text.Encoding.Latin1.GetBytes(text)).Should().Be(ImageFormat.Unknown);
[Fact]
public void ARiffThatIsNotWebP_IsUnknown() => ImageSignature.Detect(Riff("WAVE")).Should().Be(ImageFormat.Unknown);
[Fact]
public void AnIsoFileWithAnotherBrand_IsUnknown() => ImageSignature.Detect(Ftyp("mp42")).Should().Be(ImageFormat.Unknown);
[Fact]
public void ABmThatIsNoBitmap_IsUnknown() => ImageSignature.Detect(Bmp(7)).Should().Be(ImageFormat.Unknown);
[Fact]
public void ARandomHead_NeverThrows()
=> Prop.ForAll(Gen.Choose(0, 255).Select(i => (byte)i).ArrayOf().ToArbitrary(), bytes =>
{
var act = () => ImageSignature.Detect(bytes);
act.Should().NotThrow();
return true;
}).Check(Config.QuickThrowOnFailure.WithMaxTest(1000));
private static byte[] Riff(string kind)
{
var b = new byte[32];
"RIFF"u8.CopyTo(b);
System.Text.Encoding.ASCII.GetBytes(kind).CopyTo(b, 8);
return b;
}
private static byte[] Ftyp(string brand)
{
var b = new byte[32];
b[3] = 0x1C;
"ftyp"u8.CopyTo(b.AsSpan(4));
System.Text.Encoding.ASCII.GetBytes(brand).CopyTo(b, 8);
return b;
}
private static byte[] Bmp(int headerSize)
{
var b = new byte[32];
b[0] = (byte)'B';
b[1] = (byte)'M';
b[14] = (byte)headerSize;
return b;
}
}
@@ -0,0 +1,323 @@
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Tests.Media;
public sealed class TempMedia : IDisposable
{
public string Directory { get; } = Path.Combine(Path.GetTempPath(), "slp-media-tests-" + Guid.NewGuid().ToString("N"));
public FileSystemMediaStore Store { get; }
public TempMedia() => Store = new FileSystemMediaStore(Options.Create(new MediaOptions { Directory = Directory }));
public void Dispose()
{
if (System.IO.Directory.Exists(Directory))
{
System.IO.Directory.Delete(Directory, recursive: true);
}
}
}
public class FileSystemMediaStoreTests
{
private const string Hash = "0123456789abcdef";
[Fact]
public async Task ASet_IsWrittenAtOnce_AndCanBeReadBack()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1, 2, 3], ["tall-640.webp"] = [4, 5] }, CancellationToken.None);
await using var file = (await media.Store.OpenReadAsync($"hero/{Hash}/wide-960.webp", CancellationToken.None))!.Content;
var bytes = new MemoryStream();
await file.CopyToAsync(bytes);
bytes.ToArray().Should().Equal(1, 2, 3);
(await media.Store.ExistsAsync($"hero/{Hash}/tall-640.webp", CancellationToken.None)).Should().BeTrue();
System.IO.Directory.Exists(Path.Combine(media.Directory, ".tmp")).Should().BeTrue();
System.IO.Directory.GetDirectories(Path.Combine(media.Directory, ".tmp")).Should().BeEmpty("no temporary set is left behind");
}
[Fact]
public async Task WritingTheSameSetTwice_KeepsTheFirst()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1] }, CancellationToken.None);
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [9, 9] }, CancellationToken.None);
await using var file = (await media.Store.OpenReadAsync($"hero/{Hash}/wide-960.webp", CancellationToken.None))!.Content;
file.Length.Should().Be(1);
}
[Fact]
public async Task AFailedWrite_LeavesNothingVisible()
{
using var media = new TempMedia();
var act = async () => await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["ok-960.webp"] = [1], ["../evil.webp"] = [2] }, CancellationToken.None);
await act.Should().ThrowAsync<ArgumentException>();
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
[Theory]
[InlineData("../secret")]
[InlineData("hero/../../secret")]
[InlineData("/etc/passwd")]
[InlineData("hero\\0123456789abcdef\\wide-960.webp")]
[InlineData("HERO/0123456789abcdef/wide-960.webp")]
[InlineData("hero/0123456789abcdef/wide 960.webp")]
[InlineData("hero/0123456789abcdef/.hidden")]
[InlineData("a/b/c/d/e")]
[InlineData("")]
public async Task AKeyOutsideTheAllowList_IsRefused(string key)
{
using var media = new TempMedia();
var act = async () => await media.Store.OpenReadAsync(key, CancellationToken.None);
await act.Should().ThrowAsync<ArgumentException>();
}
[Fact]
public async Task APlainFile_CanBeReplaced_AndDeleted()
{
using var media = new TempMedia();
await media.Store.PutAsync("originals/abc.jpg", new byte[] { 1 }, CancellationToken.None);
await media.Store.PutAsync("originals/abc.jpg", new byte[] { 1, 2 }, CancellationToken.None);
(await media.Store.OpenReadAsync("originals/abc.jpg", CancellationToken.None))!.Length.Should().Be(2);
await media.Store.DeleteAsync("originals/abc.jpg", CancellationToken.None);
(await media.Store.ExistsAsync("originals/abc.jpg", CancellationToken.None)).Should().BeFalse();
}
[Fact]
public async Task SetsAreListed_AndRemoved_AndCounted()
{
using var media = new TempMedia();
await media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = new byte[10] }, CancellationToken.None);
await media.Store.PutSetAsync("hero", "fedcba9876543210", new Dictionary<string, byte[]> { ["wide-960.webp"] = new byte[5] }, CancellationToken.None);
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Select(s => s.SetHash).Should().BeEquivalentTo(Hash, "fedcba9876543210");
(await media.Store.GetTotalBytesAsync(CancellationToken.None)).Should().Be(15);
await media.Store.DeleteSetAsync("hero", Hash, CancellationToken.None);
(await media.Store.ListSetsAsync("hero", CancellationToken.None)).Select(s => s.SetHash).Should().Equal("fedcba9876543210");
}
[Fact]
public async Task TheProbe_CreatesTheDirectory_WhenItIsMissing()
{
using var media = new TempMedia();
(await media.Store.ProbeAsync(CancellationToken.None)).Should().BeTrue();
System.IO.Directory.Exists(media.Directory).Should().BeTrue();
}
[Fact]
public async Task WithoutADirectory_NothingIsAvailable()
{
var store = new FileSystemMediaStore(Options.Create(new MediaOptions()));
(await store.ProbeAsync(CancellationToken.None)).Should().BeFalse();
(await store.OpenReadAsync("hero/0123456789abcdef/wide-960.webp", CancellationToken.None)).Should().BeNull();
store.GetFreeBytes().Should().BeNull();
}
[Fact]
public async Task TheFreeSpace_IsReportedForAnExistingVolume()
{
using var media = new TempMedia();
await media.Store.ProbeAsync(CancellationToken.None);
media.Store.GetFreeBytes().Should().BeGreaterThan(0);
}
}
public class MediaEndpointTests : IAsyncLifetime
{
private const string Hash = "0123456789abcdef";
private readonly TempMedia _media = new();
private IHost _host = null!;
private HttpClient _client = null!;
public async Task InitializeAsync()
{
await _media.Store.PutSetAsync("hero", Hash, new Dictionary<string, byte[]> { ["wide-960.webp"] = [1, 2, 3, 4], ["tall-640.avif"] = [9] }, CancellationToken.None);
await _media.Store.PutAsync("originals/secret.jpg", new byte[] { 7 }, CancellationToken.None);
_host = await new HostBuilder().ConfigureWebHost(web => web
.UseTestServer()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSingleton<IMediaStore>(_media.Store);
services.AddSingleton(new MediaAreaRegistry());
services.AddSingleton(new MediaAreaRegistration("hero"));
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapCmsMedia());
})).StartAsync();
_client = _host.GetTestClient();
}
public async Task DisposeAsync()
{
_client.Dispose();
await _host.StopAsync();
_host.Dispose();
_media.Dispose();
}
[Fact]
public async Task AStoredFile_IsServed_WithTheImmutableHeaders()
{
var response = await _client.GetAsync($"/media/hero/{Hash}/wide-960.webp");
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.Content.Headers.ContentType!.MediaType.Should().Be("image/webp");
response.Headers.CacheControl!.ToString().Should().Be("public, max-age=31536000, immutable");
response.Headers.ETag!.Tag.Should().Be($"\"{Hash}-wide-960.webp\"");
response.Headers.GetValues("X-Content-Type-Options").Should().Equal("nosniff");
(await response.Content.ReadAsByteArrayAsync()).Should().Equal(1, 2, 3, 4);
}
[Fact]
public async Task AnAvifFile_HasItsOwnContentType()
{
var response = await _client.GetAsync($"/media/hero/{Hash}/tall-640.avif");
response.Content.Headers.ContentType!.MediaType.Should().Be("image/avif");
}
[Fact]
public async Task AMatchingEtag_GivesNotModified()
{
var request = new HttpRequestMessage(HttpMethod.Get, $"/media/hero/{Hash}/wide-960.webp");
request.Headers.IfNoneMatch.ParseAdd($"\"{Hash}-wide-960.webp\"");
var response = await _client.SendAsync(request);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotModified);
}
[Fact]
public async Task Head_HasTheHeadersAndNoBody()
{
var response = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"/media/hero/{Hash}/wide-960.webp"));
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.Content.Headers.ContentLength.Should().Be(4);
(await response.Content.ReadAsByteArrayAsync()).Should().BeEmpty();
}
[Theory]
[InlineData("/media/hero/0123456789abcdef/missing-960.webp")]
[InlineData("/media/hero/0123456789abcdef/wide-960.png")]
[InlineData("/media/other/0123456789abcdef/wide-960.webp")]
[InlineData("/media/originals/secret.jpg")]
[InlineData("/media/hero/0123456789abcdef/")]
[InlineData("/media/hero/")]
[InlineData("/media/")]
[InlineData("/media/hero/0123456789abcdef/..%2f..%2fsecret.jpg")]
[InlineData("/media/hero/..%5c..%5cwide-960.webp")]
[InlineData("/media/%2e%2e/%2e%2e/etc/passwd")]
public async Task EverythingElse_IsAPlain404(string url)
{
var response = await _client.GetAsync(url);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
(await response.Content.ReadAsStringAsync()).Should().BeEmpty();
}
[Fact]
public async Task OtherMethods_AreNotAllowed()
{
var response = await _client.PostAsync($"/media/hero/{Hash}/wide-960.webp", new ByteArrayContent([1]));
response.StatusCode.Should().Be(System.Net.HttpStatusCode.MethodNotAllowed);
}
[Fact]
public async Task WithoutARegisteredArea_NoEndpointExists()
{
using var host = await new HostBuilder().ConfigureWebHost(web => web
.UseTestServer()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSingleton<IMediaStore>(_media.Store);
services.AddSingleton(new MediaAreaRegistry());
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapCmsMedia());
})).StartAsync();
var response = await host.GetTestClient().GetAsync($"/media/hero/{Hash}/wide-960.webp");
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound);
}
}
public class MediaDirectoryCheckTests
{
private static MediaDirectoryCheck Check(IMediaStore store, MediaStatus status, MediaAreaRegistry areas, string? directory, params string[] registered)
=> new(store, status, areas, registered.Select(a => new MediaAreaRegistration(a)), Options.Create(new MediaOptions { Directory = directory }), NullLogger<MediaDirectoryCheck>.Instance);
[Fact]
public async Task AWritableDirectory_TurnsUploadsOn_AndRegistersTheAreas()
{
using var media = new TempMedia();
var status = new MediaStatus();
var areas = new MediaAreaRegistry();
var check = Check(media.Store, status, areas, media.Directory, "hero");
await check.StartAsync(CancellationToken.None);
await check.Completion;
status.DirectoryWritable.Should().BeTrue();
areas.Contains("hero").Should().BeTrue();
}
[Fact]
public async Task AMissingConfiguration_TurnsUploadsOff()
{
var status = new MediaStatus();
status.SetDirectory(true);
var check = Check(Substitute.For<IMediaStore>(), status, new MediaAreaRegistry(), null);
await check.StartAsync(CancellationToken.None);
await check.Completion;
status.DirectoryWritable.Should().BeFalse();
}
[Fact]
public async Task AnUnwritableDirectory_TurnsUploadsOff()
{
var store = Substitute.For<IMediaStore>();
store.ProbeAsync(default).ReturnsForAnyArgs(false);
var status = new MediaStatus();
var check = Check(store, status, new MediaAreaRegistry(), "/somewhere");
await check.StartAsync(CancellationToken.None);
await check.Completion;
status.DirectoryWritable.Should().BeFalse();
}
}
@@ -0,0 +1,157 @@
using FluentAssertions;
using Microsoft.Extensions.Options;
using NSubstitute;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Core.Tests.Media;
public class MediaUploadGuardTests
{
private static readonly byte[] Png = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 1, 2, 3, 4, 5, 6, 7, 8];
private sealed class Rig
{
public MediaOptions Options { get; } = new() { MaxUploadBytes = 1000, MaxPixels = 1_000_000, MaxSide = 5000, MaxParallelJobs = 1, BusyWaitSeconds = 0 };
public IImageProcessor Processor { get; } = Substitute.For<IImageProcessor>();
public MediaStatus Status { get; } = new();
public MediaProcessingGate Gate { get; }
public MediaUploadGuard Guard { get; }
public Rig(bool available = true)
{
Gate = new MediaProcessingGate(Microsoft.Extensions.Options.Options.Create(Options));
Processor.SupportedInput.Returns(new HashSet<ImageFormat> { ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.WebP });
Processor.InspectAsync(default, default, default).ReturnsForAnyArgs(new ImageInfo(ImageFormat.Png, 800, 600, 1, false));
Status.SetProcessor(available, Processor.SupportedInput, new HashSet<ImageFormat> { ImageFormat.WebP });
Status.SetDirectory(true);
Guard = new MediaUploadGuard(Processor, Gate, Status, Microsoft.Extensions.Options.Options.Create(Options));
}
}
private static MemoryStream Stream(byte[] bytes) => new(bytes);
[Fact]
public async Task AValidImage_IsAdmitted_WithItsInfo()
{
var rig = new Rig();
using var upload = await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
upload.Bytes.Should().Equal(Png);
upload.Info.Width.Should().Be(800);
}
[Fact]
public async Task ADeclaredLength_AboveTheLimit_IsRefusedBeforeReading()
{
var rig = new Rig();
var act = async () => await rig.Guard.AdmitAsync(Stream(Png), 5000, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("too_large");
}
[Fact]
public async Task AnUpload_LongerThanItsDeclaredLength_IsStillStopped()
{
var rig = new Rig();
var big = new byte[2000];
Png.CopyTo(big, 0);
var act = async () => await rig.Guard.AdmitAsync(Stream(big), 10, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("too_large");
}
[Theory]
[InlineData("<svg xmlns=\"http://www.w3.org/2000/svg\"><script>alert(1)</script></svg>")]
[InlineData("%PDF-1.4 not an image")]
[InlineData("GIF89a is not in the accepted list of this rig but the processor lacks it")]
public async Task NotAnAcceptedImage_IsRefused(string text)
{
var rig = new Rig();
var bytes = System.Text.Encoding.ASCII.GetBytes(text);
var act = async () => await rig.Guard.AdmitAsync(Stream(bytes), bytes.Length, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("unsupported_format");
}
[Fact]
public async Task AnEmptyUpload_IsCorrupt()
{
var rig = new Rig();
var act = async () => await rig.Guard.AdmitAsync(Stream([]), 0, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("corrupt");
}
[Theory]
[InlineData(2000, 2000)] // 4 MP, above the 1 MP ceiling
[InlineData(6000, 10)] // a side above the limit
public async Task ATooBigPicture_IsRefused(int width, int height)
{
var rig = new Rig();
rig.Processor.InspectAsync(default, default, default).ReturnsForAnyArgs(new ImageInfo(ImageFormat.Png, width, height, 1, false));
var act = async () => await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("too_many_pixels");
}
[Fact]
public async Task WhenTheLibraryIsNotAvailable_NothingIsAccepted()
{
var rig = new Rig(available: false);
var act = async () => await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("unavailable");
}
[Fact]
public async Task WhenTheDirectoryIsNotWritable_NothingIsAccepted()
{
var rig = new Rig();
rig.Status.SetDirectory(false);
var act = async () => await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("unavailable");
}
[Fact]
public async Task WhenAllSlotsAreTaken_TheAnswerIsBusy_AndAFreedSlotWorksAgain()
{
var rig = new Rig();
var first = await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
var act = async () => await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be("busy");
first.Dispose();
using var second = await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
second.Info.Should().NotBeNull();
}
[Fact]
public async Task ARefusal_ReleasesTheSlot()
{
var rig = new Rig();
var bad = System.Text.Encoding.ASCII.GetBytes("not an image at all");
for (var i = 0; i < 3; i++)
{
var act = async () => await rig.Guard.AdmitAsync(Stream(bad), bad.Length, CancellationToken.None);
await act.Should().ThrowAsync<ImageRejectedException>();
}
using var ok = await rig.Guard.AdmitAsync(Stream(Png), Png.Length, CancellationToken.None);
ok.Should().NotBeNull();
}
}
@@ -36,6 +36,7 @@
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
<ProjectReference Include="..\SlpModularCms.Core.Media.Magick\SlpModularCms.Core.Media.Magick.csproj" />
</ItemGroup>
<!--
@@ -27,6 +27,14 @@ public class GlobalExceptionHandler : IExceptionHandler
Exception exception,
CancellationToken cancellationToken)
{
// The client went away (closed tab, navigation, a poll that was replaced): nobody is waiting for an answer, so
// this is neither an error nor worth a warning, and writing a response to a closed connection would only fail again.
if (httpContext.RequestAborted.IsCancellationRequested && exception is OperationCanceledException)
{
_logger.LogDebug("Request cancelled by the client: {Path}", httpContext.Request.Path);
return true;
}
var traceId = Activity.Current?.Id ?? httpContext.TraceIdentifier;
var (statusCode, title) = MapException(exception);
@@ -67,7 +75,14 @@ public class GlobalExceptionHandler : IExceptionHandler
httpContext.Response.StatusCode = statusCode;
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
try
{
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
}
catch (OperationCanceledException) when (httpContext.RequestAborted.IsCancellationRequested)
{
// The client left while the answer was being written; there is nobody to tell.
}
return true;
}
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Hosting.Health;
using SlpModularCms.Core.Hosting.Observability;
using SlpModularCms.Core.Hosting.Proxy;
@@ -165,6 +166,9 @@ public static class CmsHost
app.MapControllers();
// Stored media (hero images and other modules with a media area): anonymous, immutable, only registered areas.
app.MapCmsMedia();
// Infrastructure liveness. Deliberately outside /api/v1 and on the availability gate's bypass
// list: this reports whether the process is alive, which is a different question from whether
// the CMS is switched on (/api/v1/Availability/status) or which modules it carries
@@ -279,6 +279,42 @@ public static class ServiceCollectionExtensions
QueueLimit = 0
});
});
// The public hero read, the media files the pages load, and image uploads (per signed-in user, a few per hour).
options.AddPolicy("hero-public", context =>
{
var settings = configuration.GetSection("RateLimiting:HeroPublic");
return RateLimitPartition.GetFixedWindowLimiter(ClientPartitionKey.For(context), _ => new FixedWindowRateLimiterOptions
{
PermitLimit = settings.GetValue<int>("PermitLimit", 120),
Window = TimeSpan.FromSeconds(settings.GetValue<int>("WindowSeconds", 60)),
QueueLimit = 0
});
});
options.AddPolicy("media-public", context =>
{
var settings = configuration.GetSection("RateLimiting:MediaPublic");
return RateLimitPartition.GetFixedWindowLimiter(ClientPartitionKey.For(context), _ => new FixedWindowRateLimiterOptions
{
PermitLimit = settings.GetValue<int>("PermitLimit", 600),
Window = TimeSpan.FromSeconds(settings.GetValue<int>("WindowSeconds", 60)),
QueueLimit = 0
});
});
options.AddPolicy("hero-upload", context =>
{
var settings = configuration.GetSection("RateLimiting:HeroUpload");
var user = context.User.Identity?.Name;
var key = string.IsNullOrEmpty(user) ? ClientPartitionKey.For(context) : "user:" + user;
return RateLimitPartition.GetFixedWindowLimiter(key, _ => new FixedWindowRateLimiterOptions
{
PermitLimit = settings.GetValue<int>("PermitLimit", 100),
Window = TimeSpan.FromSeconds(settings.GetValue<int>("WindowSeconds", 3600)),
QueueLimit = 0
});
});
});
return services;
@@ -0,0 +1,68 @@
using System.Buffers;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Logging;
namespace SlpModularCms.Core.Hosting.Website;
/// <summary>
/// A module's key in the data block that the host writes into a website page (<c>&lt;script type="application/json"
/// id="cms-data"&gt;</c> after the <c>&lt;!--CMS:DATA--&gt;</c> marker). The website reads this block instead of
/// fetching, so the same data that the public API answers is in the first HTML. Contributors are scoped.
/// </summary>
public interface ICmsDataBlockContributor
{
/// <summary>The property name in the block (<c>offerings</c>, <c>banners</c>, <c>hero</c>).</summary>
string Key { get; }
/// <summary>Fixes the order of the keys in the block: lower first (100 offerings, 110 banners, 200 hero).</summary>
int Order { get; }
/// <summary>The value to write for <see cref="Key"/>; null is written as JSON null (the website then keeps what it was built with).</summary>
Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken);
}
/// <summary>
/// Owns the <c>&lt;!--CMS:DATA--&gt;</c> marker: composes one block from <c>siteUrl</c> and every registered
/// <see cref="ICmsDataBlockContributor"/>, so modules never edit each other's output. If one contributor fails the whole
/// block stays as it was built: a half-filled block would mislead the website (fail-open for the block as a whole).
/// </summary>
public sealed class CmsDataBlockTransform(IEnumerable<ICmsDataBlockContributor> contributors) : IHtmlPageTransform
{
public const string Marker = "CMS:DATA";
public const string BlockId = "cms-data";
// The shape of the public API answers: camelCase, nulls written.
private static readonly JsonSerializerOptions ApiJson = new(JsonSerializerDefaults.Web);
public bool Wants(string html) => html.Contains("<!--" + Marker + "-->", StringComparison.Ordinal);
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
{
var buffer = new ArrayBufferWriter<byte>();
await using (var writer = new Utf8JsonWriter(buffer))
{
writer.WriteStartObject();
writer.WriteString("siteUrl", context.SiteUrl);
foreach (var contributor in contributors.OrderBy(c => c.Order).ThenBy(c => c.Key, StringComparer.Ordinal))
{
var value = await contributor.GetAsync(context, cancellationToken);
writer.WritePropertyName(contributor.Key);
if (value is null)
{
writer.WriteNullValue();
}
else
{
JsonSerializer.Serialize(writer, value, value.GetType(), ApiJson);
}
}
writer.WriteEndObject();
}
var json = Encoding.UTF8.GetString(buffer.WrittenSpan);
var block = "<script type=\"application/json\" id=\"" + BlockId + "\">" + HtmlMarkerEditor.EscapeForScript(json) + "</script>";
return HtmlMarkerEditor.ReplaceDataBlock(html, Marker, BlockId, block);
}
}
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
namespace SlpModularCms.Core.Hosting.Website;
@@ -40,6 +41,9 @@ public static class PrerenderedSiteExtensions
services.AddSingleton<HtmlMarkerInjector>();
services.AddSingleton<IIndexHtmlCache>(provider => provider.GetRequiredService<HtmlMarkerInjector>());
// The shared CMS data block of the pages (siteUrl plus what each module contributes).
services.TryAddEnumerable(ServiceDescriptor.Scoped<IHtmlPageTransform, CmsDataBlockTransform>());
// Presence of the marker switches on the full security-header set for website responses.
services.AddSingleton<WebsiteHostingMarker>();
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
@@ -24,6 +25,9 @@ public static class SpaSeoExtensions
services.AddOptions<WebsiteOptions>().Bind(configuration.GetSection(WebsiteOptions.SectionName));
services.AddSingleton<HtmlMarkerInjector>();
services.AddSingleton<IIndexHtmlCache>(provider => provider.GetRequiredService<HtmlMarkerInjector>());
// The shared CMS data block of the pages (siteUrl plus what each module contributes).
services.TryAddEnumerable(ServiceDescriptor.Scoped<IHtmlPageTransform, CmsDataBlockTransform>());
return services;
}
@@ -55,6 +55,9 @@ public class AuthService : IAuthService
return await GenerateTokenResponseAsync(user);
}
/// <summary>How long after a refresh token was replaced the same token is still answered with a fresh pair (concurrent refreshes).</summary>
private static readonly TimeSpan ConcurrentRefreshGrace = TimeSpan.FromSeconds(20);
public async Task<TokenResponse> RefreshTokenAsync(string refreshToken)
{
var savedRefreshToken = await _context.RefreshTokens
@@ -68,6 +71,21 @@ public class AuthService : IAuthService
if (savedRefreshToken.IsUsed)
{
// Two requests (two tabs, or a page that wakes up after a restart) can send the same refresh token at the
// same moment; the second one arrives after the first rotated it. That is not theft: when this token was
// replaced a moment ago (a newer token of the same user was created within the grace period), give this
// caller a fresh pair as well and leave the sessions alone. A real replay later still revokes everything.
var graceStart = DateTimeOffset.UtcNow - ConcurrentRefreshGrace;
var replacedJustNow = await _context.RefreshTokens.AnyAsync(t =>
t.UserId == savedRefreshToken.UserId
&& !t.IsRevoked
&& t.CreatedAt > savedRefreshToken.CreatedAt
&& t.CreatedAt >= graceStart);
if (replacedJustNow && savedRefreshToken.User.IsActive)
{
return await GenerateTokenResponseAsync(savedRefreshToken.User);
}
// Re-use detection: trek alle tokens van de gebruiker in
var userId = savedRefreshToken.UserId;
var allTokens = await _context.RefreshTokens.Where(t => t.UserId == userId).ToListAsync();
+55
View File
@@ -0,0 +1,55 @@
namespace SlpModularCms.Core.Media;
/// <summary>
/// The crop for a ratio around a focus point. Pure and deterministic; the admin's TypeScript twin uses the same
/// steps and the same test vectors so the preview matches what the host cuts.
/// </summary>
public static class FocusMath
{
/// <summary>
/// The largest rectangle of the ratio that fits the image, shrunk by the zoom, centred on the focus point and
/// moved the smallest amount needed to stay inside the image. The focus point is therefore always inside the crop.
/// </summary>
public static CropRect CropFor(int imageWidth, int imageHeight, Ratio ratio, FocusSetting focus)
{
if (imageWidth <= 0 || imageHeight <= 0)
{
throw new ArgumentOutOfRangeException(nameof(imageWidth), "The image must have a size.");
}
var f = focus.Normalize();
// Largest crop of the ratio inside the image (width:height = numerator:denominator).
double cw0, ch0;
if ((long)imageWidth * ratio.Denominator > (long)imageHeight * ratio.Numerator)
{
ch0 = imageHeight;
cw0 = ch0 * ratio.Numerator / ratio.Denominator;
}
else
{
cw0 = imageWidth;
ch0 = cw0 * ratio.Denominator / ratio.Numerator;
}
var cw = Math.Clamp((int)Math.Floor(cw0 / f.Zoom), 1, imageWidth);
var ch = Math.Max(1, RoundHalfUp(cw * (double)ratio.Denominator / ratio.Numerator));
// The height follows from the width; when rounding pushed it past the image, derive the width from the height.
if (ch > imageHeight)
{
ch = imageHeight;
cw = Math.Clamp((int)Math.Floor(ch * (double)ratio.Numerator / ratio.Denominator), 1, imageWidth);
}
var x = RoundHalfUp(f.X * imageWidth - cw / 2.0);
var y = RoundHalfUp(f.Y * imageHeight - ch / 2.0);
x = Math.Clamp(x, 0, imageWidth - cw);
y = Math.Clamp(y, 0, imageHeight - ch);
return new CropRect(x, y, cw, ch);
}
/// <summary>Rounds .5 up (like JavaScript's Math.round), so the admin's TypeScript twin gives the same numbers.</summary>
public static int RoundHalfUp(double value) => (int)Math.Floor(value + 0.5);
}
@@ -0,0 +1,164 @@
using Microsoft.Extensions.Options;
namespace SlpModularCms.Core.Media;
/// <summary>Why an upload was refused. The names are the stable codes the API returns (see <see cref="ImageRejectedException.Code"/>).</summary>
public enum ImageRejection
{
TooLarge,
UnsupportedFormat,
Corrupt,
TooManyPixels,
Timeout,
Busy,
Unavailable,
}
/// <summary>A refused image. The message is technical English for logs; users see a text from the language files.</summary>
public sealed class ImageRejectedException(ImageRejection reason, string message) : Exception(message)
{
public ImageRejection Reason { get; } = reason;
/// <summary>A stable code for the API: <c>too_large</c>, <c>unsupported_format</c>, <c>corrupt</c>, <c>too_many_pixels</c>, <c>timeout</c>, <c>busy</c>, <c>unavailable</c>.</summary>
public string Code => Reason switch
{
ImageRejection.TooLarge => "too_large",
ImageRejection.UnsupportedFormat => "unsupported_format",
ImageRejection.Corrupt => "corrupt",
ImageRejection.TooManyPixels => "too_many_pixels",
ImageRejection.Timeout => "timeout",
ImageRejection.Busy => "busy",
_ => "unavailable",
};
}
/// <summary>Settings of the media service (<c>Media:*</c>).</summary>
public sealed class MediaOptions
{
public const string SectionName = "Media";
/// <summary>The directory outside the release where files are stored. Empty = uploads are unavailable.</summary>
public string? Directory { get; set; }
public long MaxUploadBytes { get; set; } = 10 * 1024 * 1024;
public long MaxPixels { get; set; } = 64_000_000;
public int MaxSide { get; set; } = 16_384;
public int MaxParallelJobs { get; set; } = 2;
public int DecodeTimeoutSeconds { get; set; } = 20;
/// <summary>The time allowed to remove metadata from an upload (it decodes and writes the whole picture again).</summary>
public int StripTimeoutSeconds { get; set; } = 60;
/// <summary>
/// The time allowed to cut ALL variants of one photo in the background (one decode, then every variant). Generous on
/// purpose: a large photo on a Raspberry Pi needs minutes; a runaway job is stopped after this.
/// </summary>
public int RenderSetTimeoutSeconds { get; set; } = 600;
/// <summary>How long a free processing slot is awaited before the answer is "busy".</summary>
public int BusyWaitSeconds { get; set; } = 3;
public long MaxTotalBytes { get; set; } = 2L * 1024 * 1024 * 1024;
public int RetentionDays { get; set; } = 7;
}
/// <summary>
/// The image library behind one small interface. Implementations must apply the resource limits of
/// <see cref="MediaOptions"/>, decode only the format the caller names (decided from the signature), never use a
/// client file name and never write metadata into an output.
/// </summary>
public interface IImageProcessor
{
/// <summary>The formats it can decode.</summary>
IReadOnlySet<ImageFormat> SupportedInput { get; }
/// <summary>The formats it can write (WebP always; AVIF when the platform can).</summary>
IReadOnlySet<ImageFormat> SupportedOutput { get; }
/// <summary>Reads size (after orientation), frame count and whether metadata is present, without decoding the pixels.</summary>
/// <exception cref="ImageRejectedException">The data is not a decodable image of that format.</exception>
Task<ImageInfo> InspectAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken);
/// <summary>
/// Decodes the first frame, applies the orientation, cuts the crop, resizes to the variant size (never above the
/// crop), strips metadata and encodes in the variant's format and quality.
/// </summary>
Task<RenderedImage> RenderAsync(ReadOnlyMemory<byte> data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken);
/// <summary>
/// Cuts all variants of a photo from ONE decode (the picture is decoded and oriented once, then every variant is
/// cropped, resized and encoded from it). <paramref name="progress"/> is called with (variants done, variants in total)
/// after every variant. Stops between variants when cancelled. The result has the order of <paramref name="variants"/>.
/// </summary>
Task<IReadOnlyList<RenderedImage>> RenderSetAsync(ReadOnlyMemory<byte> data, ImageFormat declared, IReadOnlyList<VariantSpec> variants, Action<int, int>? progress, CancellationToken cancellationToken);
/// <summary>
/// Removes metadata from an original that carries any, keeping the pixels as they are as far as the format
/// allows. Returns null when there is nothing to remove (the original is stored as received).
/// </summary>
Task<byte[]?> StripMetadataAsync(ReadOnlyMemory<byte> data, ImageFormat declared, CancellationToken cancellationToken);
/// <summary>Encodes and decodes a tiny image in every supported format. Throws when the library does not work here.</summary>
Task SelfTestAsync(CancellationToken cancellationToken);
}
/// <summary>What the start-up self-test found; uploads are refused while the processor is not available.</summary>
public sealed class MediaStatus
{
private volatile bool _processorAvailable;
private volatile bool _directoryWritable;
public bool ProcessorAvailable => _processorAvailable;
public bool DirectoryWritable => _directoryWritable;
public bool UploadsAvailable => _processorAvailable && _directoryWritable;
public IReadOnlySet<ImageFormat> AcceptedInput { get; private set; } = new HashSet<ImageFormat>();
public IReadOnlySet<ImageFormat> Output { get; private set; } = new HashSet<ImageFormat>();
public void SetProcessor(bool available, IReadOnlySet<ImageFormat> input, IReadOnlySet<ImageFormat> output)
{
AcceptedInput = input;
Output = output;
_processorAvailable = available;
}
public void SetDirectory(bool writable) => _directoryWritable = writable;
}
/// <summary>Limits how many images are processed at once; a full gate answers "busy" after a short wait.</summary>
public sealed class MediaProcessingGate(IOptions<MediaOptions> options)
{
private readonly SemaphoreSlim _slots = new(Math.Max(1, options.Value.MaxParallelJobs));
private readonly TimeSpan _wait = TimeSpan.FromSeconds(Math.Max(0, options.Value.BusyWaitSeconds));
public async Task<IDisposable> EnterAsync(CancellationToken cancellationToken)
{
if (!await _slots.WaitAsync(_wait, cancellationToken))
{
throw new ImageRejectedException(ImageRejection.Busy, "All image processing slots are in use.");
}
return new Releaser(_slots);
}
private sealed class Releaser(SemaphoreSlim slots) : IDisposable
{
private int _released;
public void Dispose()
{
if (Interlocked.Exchange(ref _released, 1) == 0)
{
slots.Release();
}
}
}
}
@@ -0,0 +1,89 @@
namespace SlpModularCms.Core.Media;
/// <summary>
/// Decides an image format from the first bytes of a file, never from a name or a declared content type. The check is
/// deliberately strict and small: a file that is not one of these is rejected before any library touches it. SVG,
/// PDF, scripts and archives are never recognised.
/// </summary>
public static class ImageSignature
{
/// <summary>How many bytes <see cref="Detect"/> needs at most.</summary>
public const int HeaderLength = 32;
private static readonly string[] AvifBrands = ["avif", "avis"];
private static readonly string[] HeicBrands = ["heic", "heix", "hevc", "hevx", "heim", "heis", "mif1", "msf1"];
public static ImageFormat Detect(ReadOnlySpan<byte> head)
{
if (head.Length >= 3 && head[0] == 0xFF && head[1] == 0xD8 && head[2] == 0xFF)
{
return ImageFormat.Jpeg;
}
if (head.Length >= 8 && head[..8].SequenceEqual((ReadOnlySpan<byte>)[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]))
{
return ImageFormat.Png;
}
if (head.Length >= 6 && (Ascii(head, 0, "GIF87a") || Ascii(head, 0, "GIF89a")))
{
return ImageFormat.Gif;
}
if (head.Length >= 4 && ((head[0] == 0x49 && head[1] == 0x49 && head[2] == 0x2A && head[3] == 0x00)
|| (head[0] == 0x4D && head[1] == 0x4D && head[2] == 0x00 && head[3] == 0x2A)))
{
return ImageFormat.Tiff;
}
if (head.Length >= 12 && Ascii(head, 0, "RIFF") && Ascii(head, 8, "WEBP"))
{
return ImageFormat.WebP;
}
if (head.Length >= 12 && Ascii(head, 4, "ftyp"))
{
var brand = System.Text.Encoding.ASCII.GetString(head.Slice(8, 4));
if (AvifBrands.Contains(brand))
{
return ImageFormat.Avif;
}
if (HeicBrands.Contains(brand))
{
return ImageFormat.Heic;
}
}
if (head.Length >= 18 && head[0] == (byte)'B' && head[1] == (byte)'M'
&& head[6] == 0 && head[7] == 0 && head[8] == 0 && head[9] == 0)
{
// BITMAPCOREHEADER 12, BITMAPINFOHEADER 40, and the 52, 56, 64, 108, 124 byte variants.
var headerSize = head[14] | (head[15] << 8) | (head[16] << 16) | (head[17] << 24);
if (headerSize is 12 or 40 or 52 or 56 or 64 or 108 or 124)
{
return ImageFormat.Bmp;
}
}
return ImageFormat.Unknown;
}
private static bool Ascii(ReadOnlySpan<byte> data, int offset, string text)
{
if (data.Length < offset + text.Length)
{
return false;
}
for (var i = 0; i < text.Length; i++)
{
if (data[offset + i] != (byte)text[i])
{
return false;
}
}
return true;
}
}
@@ -0,0 +1,78 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Core.Availability;
namespace SlpModularCms.Core.Media;
public static class MediaEndpoint
{
public const string RateLimiterName = "media-public";
/// <summary>
/// <c>GET|HEAD /media/{area}/{setHash}/{name}</c>: anonymous, same origin, immutable. Only addresses of the exact
/// scheme in a registered area are served; everything else (a bad path, an unknown area, a missing file) is the same
/// plain 404. No directory is ever listed and no processing happens on this path.
/// </summary>
public static IEndpointRouteBuilder MapCmsMedia(this IEndpointRouteBuilder endpoints)
{
// The areas are known from the modules' registrations at the time the routes are mapped (before the hosted
// services run), so they are copied into the registry here.
var registrations = endpoints.ServiceProvider.GetServices<MediaAreaRegistration>().ToList();
if (registrations.Count == 0 || endpoints.ServiceProvider.GetService<MediaAreaRegistry>() is not { } registry)
{
return endpoints;
}
foreach (var registration in registrations)
{
registry.Add(registration.Area);
}
endpoints.MapMethods("/media/{**path}", ["GET", "HEAD"], ServeAsync)
.WithMetadata(new PublicSiteEndpointAttribute())
.RequireRateLimiting(RateLimiterName)
.ExcludeFromDescription();
return endpoints;
}
private static async Task<IResult> ServeAsync(string? path, HttpContext context, IMediaStore store, MediaAreaRegistry areas)
{
if (!MediaPaths.TryParse(path, out var address)
|| !areas.Contains(address.Area)
|| !MediaPaths.TryParseName(address.Name, out _, out _, out var format))
{
return Results.NotFound();
}
var file = await store.OpenReadAsync(address.Key, context.RequestAborted);
if (file is null)
{
return Results.NotFound();
}
var response = context.Response;
var etag = $"\"{address.SetHash}-{address.Name}\"";
response.Headers.CacheControl = "public, max-age=31536000, immutable";
response.Headers.ETag = etag;
response.Headers["X-Content-Type-Options"] = "nosniff";
var ifNoneMatch = context.Request.Headers.IfNoneMatch.ToString();
if (ifNoneMatch.Split(',').Select(t => t.Trim()).Contains(etag))
{
await file.Content.DisposeAsync();
return Results.StatusCode(StatusCodes.Status304NotModified);
}
response.ContentType = format.MimeType();
response.ContentLength = file.Length;
if (HttpMethods.IsHead(context.Request.Method))
{
await file.Content.DisposeAsync();
return Results.Empty;
}
return Results.Stream(file.Content, format.MimeType(), enableRangeProcessing: false);
}
}
@@ -0,0 +1,78 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace SlpModularCms.Core.Media;
/// <summary>
/// The address scheme of stored media: <c>/media/{area}/{setHash}/{ratio}-{width}.{ext}</c>. The set hash contains the
/// content hash of the original and the crop parameters, so a changed focus gives a new address and the files can be
/// cached for a long time. Parsing is a strict allow-list: anything else is not an address.
/// </summary>
public static partial class MediaPaths
{
public const string UrlPrefix = "/media/";
[GeneratedRegex(@"^(?<area>[a-z]{2,20})/(?<hash>[0-9a-f]{16})/(?<name>[a-z]{2,10}-[0-9]{2,5}\.(?:webp|avif|jpg))$", RegexOptions.CultureInvariant)]
private static partial Regex AddressPattern();
/// <summary>
/// 16 hex characters of SHA-256 over the original's hash and the crops (ratio name and canonical focus, ordered by
/// ratio name), so the same photo with the same focus always gives the same set.
/// </summary>
public static string SetHash(string originalSha256, IEnumerable<(string Ratio, FocusSetting Focus)> crops)
{
var text = new StringBuilder(originalSha256.ToLowerInvariant());
foreach (var (ratio, focus) in crops.OrderBy(c => c.Ratio, StringComparer.Ordinal))
{
text.Append('|').Append(ratio).Append(':').Append(focus.Normalize().Canonical());
}
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString()));
return Convert.ToHexStringLower(hash)[..16];
}
/// <summary>The file name of a variant, for example <c>wide-1600.webp</c>.</summary>
public static string FileName(string ratioName, int width, ImageFormat format)
=> $"{ratioName}-{width}.{format.Extension()}";
/// <summary>The public address, for example <c>/media/hero/0123456789abcdef/wide-1600.webp</c>.</summary>
public static string Url(string area, string setHash, string name) => $"{UrlPrefix}{area}/{setHash}/{name}";
/// <summary>Parses the part after <c>/media/</c>; false for anything that is not exactly an address of this scheme.</summary>
public static bool TryParse(string? path, out MediaAddress address)
{
address = null!;
if (string.IsNullOrEmpty(path) || path.Length > 100)
{
return false;
}
var match = AddressPattern().Match(path);
if (!match.Success)
{
return false;
}
address = new MediaAddress(match.Groups["area"].Value, match.Groups["hash"].Value, match.Groups["name"].Value);
return true;
}
/// <summary>The ratio name, width and format of a variant file name; false when it is not one.</summary>
public static bool TryParseName(string name, out string ratio, out int width, out ImageFormat format)
{
ratio = string.Empty;
width = 0;
format = ImageFormat.Unknown;
var dash = name.IndexOf('-');
var dot = name.LastIndexOf('.');
if (dash <= 0 || dot <= dash + 1 || !int.TryParse(name.AsSpan(dash + 1, dot - dash - 1), out width))
{
return false;
}
ratio = name[..dash];
format = ImageFormatExtensions.FromExtension(name[(dot + 1)..]);
return format != ImageFormat.Unknown;
}
}
@@ -0,0 +1,177 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SlpModularCms.Core.Media;
public static class MediaServiceCollectionExtensions
{
/// <summary>
/// The general media service: options (<c>Media:*</c>), the processing gate, the status the admin reads, the upload
/// guard and the start-up self-test. Safe to call from every module that needs it. An <see cref="IImageProcessor"/>
/// has to be registered separately (the Magick project does that); without one the status stays "unavailable".
/// </summary>
public static IServiceCollection AddCmsMedia(this IServiceCollection services)
{
services.AddOptions<MediaOptions>().Configure<IConfiguration>((options, configuration) =>
configuration.GetSection(MediaOptions.SectionName).Bind(options));
services.TryAddSingleton<MediaProcessingGate>();
services.TryAddSingleton<MediaStatus>();
services.TryAddSingleton<MediaAreaRegistry>();
services.TryAddSingleton<IMediaStore, FileSystemMediaStore>();
services.TryAddSingleton<IMediaUploadGuard, MediaUploadGuard>();
if (!services.Any(d => d.ImplementationType == typeof(ImageProcessorSelfTest)))
{
services.AddHostedService<ImageProcessorSelfTest>();
services.AddHostedService<MediaDirectoryCheck>();
}
return services;
}
/// <summary>Lets the <c>/media</c> endpoint serve the given area (for example <c>hero</c>).</summary>
public static IServiceCollection AddCmsMediaArea(this IServiceCollection services, string area)
{
services.AddCmsMedia();
services.AddSingleton(new MediaAreaRegistration(area));
return services;
}
}
/// <summary>A module declares the media area it owns; the host adds all of them to the <see cref="MediaAreaRegistry"/> at start-up.</summary>
public sealed record MediaAreaRegistration(string Area);
/// <summary>
/// At start-up, proves that the media directory exists (creating it when needed) and is writable, fills the area
/// registry and warns when the directory fills up. A failure only turns image uploads off; files that are already
/// stored keep being served.
/// </summary>
public sealed class MediaDirectoryCheck(
IMediaStore store,
MediaStatus status,
MediaAreaRegistry areas,
IEnumerable<MediaAreaRegistration> registrations,
Microsoft.Extensions.Options.IOptions<MediaOptions> options,
ILogger<MediaDirectoryCheck> logger) : IHostedService
{
/// <summary>The check in the background; completes when the status is known (tests await it).</summary>
public Task Completion { get; private set; } = Task.CompletedTask;
private readonly CancellationTokenSource _stopping = new();
public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var registration in registrations)
{
areas.Add(registration.Area);
}
// Not awaited: a hosted service delays the moment the host starts listening, and a disk that is slow to answer must
// never keep the website from coming up. Until it is done, uploads are simply not available yet.
Completion = Task.Run(() => CheckAsync(_stopping.Token), CancellationToken.None);
return Task.CompletedTask;
}
private async Task CheckAsync(CancellationToken cancellationToken)
{
try
{
if (string.IsNullOrWhiteSpace(options.Value.Directory))
{
status.SetDirectory(false);
logger.LogWarning("Media:Directory is not configured: image uploads are unavailable on this host.");
return;
}
var writable = await store.ProbeAsync(cancellationToken);
status.SetDirectory(writable);
if (!writable)
{
logger.LogError("The media directory is missing or not writable: image uploads are unavailable until it is fixed.");
return;
}
var total = await store.GetTotalBytesAsync(cancellationToken);
if (options.Value.MaxTotalBytes > 0 && total > options.Value.MaxTotalBytes * 8 / 10)
{
logger.LogError("The media directory is over 80 percent of its safety limit ({Bytes} bytes).", total);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
status.SetDirectory(false);
logger.LogError(exception, "The media directory check failed: image uploads are unavailable on this host.");
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _stopping.CancelAsync();
try
{
await Completion.WaitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}
/// <summary>
/// At start-up, proves the image library works on this host (encodes and decodes a tiny image in every supported
/// format) and publishes what it found. A failure only turns image uploads off: nothing else depends on it, and
/// reading pages and media keeps working.
/// </summary>
public sealed class ImageProcessorSelfTest(IServiceProvider services, MediaStatus status, ILogger<ImageProcessorSelfTest> logger) : IHostedService
{
/// <summary>The self-test in the background; completes when the status is known (tests await it).</summary>
public Task Completion { get; private set; } = Task.CompletedTask;
private readonly CancellationTokenSource _stopping = new();
public Task StartAsync(CancellationToken cancellationToken)
{
// Not awaited: loading the native library and encoding a test image can take seconds on a small machine, and a
// hosted service delays the moment the host starts listening (the health check of a deploy waits for it).
Completion = Task.Run(() => TestAsync(_stopping.Token), CancellationToken.None);
return Task.CompletedTask;
}
private async Task TestAsync(CancellationToken cancellationToken)
{
try
{
var processor = services.GetService<IImageProcessor>();
if (processor is null)
{
status.SetProcessor(false, new HashSet<ImageFormat>(), new HashSet<ImageFormat>());
logger.LogWarning("No image processor is registered: image uploads are unavailable.");
return;
}
await processor.SelfTestAsync(cancellationToken);
status.SetProcessor(true, processor.SupportedInput, processor.SupportedOutput);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
status.SetProcessor(false, new HashSet<ImageFormat>(), new HashSet<ImageFormat>());
logger.LogError(exception, "The image library self-test failed: image uploads are unavailable on this host.");
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _stopping.CancelAsync();
try
{
await Completion.WaitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}
+309
View File
@@ -0,0 +1,309 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.Options;
namespace SlpModularCms.Core.Media;
/// <summary>An opened stored file. The caller disposes <see cref="Content"/>.</summary>
public sealed record MediaFile(Stream Content, long Length, DateTimeOffset LastModified);
/// <summary>A stored set of files (one directory under an area) and when it was written.</summary>
public sealed record MediaSetInfo(string Area, string SetHash, DateTimeOffset CreatedUtc);
/// <summary>
/// Where media files live. Keys are relative, lower-case, slash-separated paths made by the code (never from a client
/// file name). The file-system implementation keeps them in a directory outside the release; a database
/// implementation can replace it without touching the modules.
/// </summary>
public interface IMediaStore
{
/// <summary>Writes a whole set (all the files of one set hash) so that it appears at once or not at all. Writing a set that already exists is a no-op: sets are content addressed.</summary>
Task PutSetAsync(string area, string setHash, IReadOnlyDictionary<string, byte[]> files, CancellationToken cancellationToken);
/// <summary>Writes one file (used for originals). An existing file with the same key is replaced.</summary>
Task PutAsync(string key, ReadOnlyMemory<byte> content, CancellationToken cancellationToken);
Task<MediaFile?> OpenReadAsync(string key, CancellationToken cancellationToken);
Task<bool> ExistsAsync(string key, CancellationToken cancellationToken);
Task DeleteAsync(string key, CancellationToken cancellationToken);
/// <summary>Removes a whole set directory.</summary>
Task DeleteSetAsync(string area, string setHash, CancellationToken cancellationToken);
Task<IReadOnlyList<MediaSetInfo>> ListSetsAsync(string area, CancellationToken cancellationToken);
Task<long> GetTotalBytesAsync(CancellationToken cancellationToken);
/// <summary>Free space on the volume, or null when it cannot be determined.</summary>
long? GetFreeBytes();
/// <summary>Creates the directory when needed and proves it is writable. False when it is not configured or not writable.</summary>
Task<bool> ProbeAsync(CancellationToken cancellationToken);
}
/// <summary>The areas the <c>/media</c> endpoint serves (for example <c>hero</c>); any other area is a 404.</summary>
public sealed class MediaAreaRegistry
{
private readonly HashSet<string> _areas = new(StringComparer.Ordinal);
public void Add(string area)
{
lock (_areas)
{
_areas.Add(area);
}
}
public bool Contains(string area)
{
lock (_areas)
{
return _areas.Contains(area);
}
}
public bool Any
{
get
{
lock (_areas)
{
return _areas.Count > 0;
}
}
}
}
public sealed partial class FileSystemMediaStore(IOptions<MediaOptions> options) : IMediaStore
{
[GeneratedRegex("^[a-z0-9][a-z0-9._-]{0,79}$", RegexOptions.CultureInvariant)]
private static partial Regex SegmentPattern();
private string? Root
{
get
{
var directory = options.Value.Directory;
return string.IsNullOrWhiteSpace(directory) ? null : Path.GetFullPath(directory);
}
}
public async Task PutSetAsync(string area, string setHash, IReadOnlyDictionary<string, byte[]> files, CancellationToken cancellationToken)
{
var root = RequireRoot();
var target = PathFor(root, area, setHash);
if (Directory.Exists(target))
{
return;
}
var temp = Path.Combine(root, ".tmp", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
try
{
foreach (var (name, bytes) in files)
{
await File.WriteAllBytesAsync(PathFor(temp, name), bytes, cancellationToken);
}
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
try
{
Directory.Move(temp, target);
}
catch (IOException) when (Directory.Exists(target))
{
// A parallel upload of the same photo and focus produced the same set first: identical content.
}
}
finally
{
if (Directory.Exists(temp))
{
Directory.Delete(temp, recursive: true);
}
}
}
public async Task PutAsync(string key, ReadOnlyMemory<byte> content, CancellationToken cancellationToken)
{
var root = RequireRoot();
var path = PathFor(root, key);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
var temp = Path.Combine(root, ".tmp", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.GetDirectoryName(temp)!);
try
{
await File.WriteAllBytesAsync(temp, content.ToArray(), cancellationToken);
File.Move(temp, path, overwrite: true);
}
finally
{
if (File.Exists(temp))
{
File.Delete(temp);
}
}
}
public Task<MediaFile?> OpenReadAsync(string key, CancellationToken cancellationToken)
{
var root = Root;
if (root is null)
{
return Task.FromResult<MediaFile?>(null);
}
var path = PathFor(root, key);
if (!File.Exists(path))
{
return Task.FromResult<MediaFile?>(null);
}
try
{
var info = new FileInfo(path);
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
return Task.FromResult<MediaFile?>(new MediaFile(stream, info.Length, info.LastWriteTimeUtc));
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
{
return Task.FromResult<MediaFile?>(null);
}
}
public Task<bool> ExistsAsync(string key, CancellationToken cancellationToken)
{
var root = Root;
return Task.FromResult(root is not null && File.Exists(PathFor(root, key)));
}
public Task DeleteAsync(string key, CancellationToken cancellationToken)
{
var root = Root;
if (root is not null)
{
var path = PathFor(root, key);
if (File.Exists(path))
{
File.Delete(path);
}
}
return Task.CompletedTask;
}
public Task DeleteSetAsync(string area, string setHash, CancellationToken cancellationToken)
{
var root = Root;
if (root is not null)
{
var path = PathFor(root, area, setHash);
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
return Task.CompletedTask;
}
public Task<IReadOnlyList<MediaSetInfo>> ListSetsAsync(string area, CancellationToken cancellationToken)
{
var root = Root;
var result = new List<MediaSetInfo>();
if (root is not null)
{
var areaPath = PathFor(root, area);
if (Directory.Exists(areaPath))
{
foreach (var directory in new DirectoryInfo(areaPath).EnumerateDirectories())
{
result.Add(new MediaSetInfo(area, directory.Name, directory.CreationTimeUtc));
}
}
}
return Task.FromResult<IReadOnlyList<MediaSetInfo>>(result);
}
public Task<long> GetTotalBytesAsync(CancellationToken cancellationToken)
{
var root = Root;
if (root is null || !Directory.Exists(root))
{
return Task.FromResult(0L);
}
return Task.FromResult(new DirectoryInfo(root).EnumerateFiles("*", SearchOption.AllDirectories).Sum(f => f.Length));
}
public long? GetFreeBytes()
{
var root = Root;
if (root is null)
{
return null;
}
try
{
var existing = root;
while (!Directory.Exists(existing) && Path.GetDirectoryName(existing) is { } parent)
{
existing = parent;
}
return new DriveInfo(Path.GetPathRoot(existing)!).AvailableFreeSpace;
}
catch (Exception exception) when (exception is IOException or ArgumentException or UnauthorizedAccessException)
{
return null;
}
}
public async Task<bool> ProbeAsync(CancellationToken cancellationToken)
{
var root = Root;
if (root is null)
{
return false;
}
try
{
var temp = Path.Combine(root, ".tmp");
Directory.CreateDirectory(temp);
var probe = Path.Combine(temp, "probe-" + Guid.NewGuid().ToString("N"));
await File.WriteAllBytesAsync(probe, [1, 2, 3], cancellationToken);
File.Delete(probe);
return true;
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
{
return false;
}
}
private string RequireRoot()
=> Root ?? throw new InvalidOperationException("Media:Directory is not configured.");
/// <summary>Builds a path below the root from key segments; every segment is checked against a strict allow-list and the result must stay inside the root.</summary>
private static string PathFor(string root, params string[] keys)
{
var segments = keys.SelectMany(k => k.Split('/')).ToArray();
if (segments.Length is 0 or > 4 || segments.Any(s => !SegmentPattern().IsMatch(s) || s.Contains("..", StringComparison.Ordinal)))
{
throw new ArgumentException("Not a valid media key.", nameof(keys));
}
var full = Path.GetFullPath(Path.Combine([root, .. segments]));
if (!full.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.Ordinal))
{
throw new ArgumentException("Not a valid media key.", nameof(keys));
}
return full;
}
}
+118
View File
@@ -0,0 +1,118 @@
using System.Globalization;
namespace SlpModularCms.Core.Media;
/// <summary>The image formats the media service knows. <see cref="Unknown"/> is never accepted.</summary>
public enum ImageFormat
{
Unknown = 0,
Jpeg,
Png,
Gif,
Bmp,
Tiff,
WebP,
Avif,
Heic,
}
public static class ImageFormatExtensions
{
public static string MimeType(this ImageFormat format) => format switch
{
ImageFormat.Jpeg => "image/jpeg",
ImageFormat.Png => "image/png",
ImageFormat.Gif => "image/gif",
ImageFormat.Bmp => "image/bmp",
ImageFormat.Tiff => "image/tiff",
ImageFormat.WebP => "image/webp",
ImageFormat.Avif => "image/avif",
ImageFormat.Heic => "image/heic",
_ => "application/octet-stream",
};
/// <summary>The file extension without a dot, as used in stored keys and media addresses.</summary>
public static string Extension(this ImageFormat format) => format switch
{
ImageFormat.Jpeg => "jpg",
ImageFormat.Png => "png",
ImageFormat.Gif => "gif",
ImageFormat.Bmp => "bmp",
ImageFormat.Tiff => "tif",
ImageFormat.WebP => "webp",
ImageFormat.Avif => "avif",
ImageFormat.Heic => "heic",
_ => "bin",
};
public static ImageFormat FromExtension(string extension) => extension.ToLowerInvariant() switch
{
"jpg" => ImageFormat.Jpeg,
"png" => ImageFormat.Png,
"gif" => ImageFormat.Gif,
"bmp" => ImageFormat.Bmp,
"tif" => ImageFormat.Tiff,
"webp" => ImageFormat.WebP,
"avif" => ImageFormat.Avif,
"heic" => ImageFormat.Heic,
_ => ImageFormat.Unknown,
};
}
/// <summary>What an image is, after orientation is applied. <paramref name="FrameCount"/> is 1 for a still image.</summary>
public sealed record ImageInfo(ImageFormat Format, int Width, int Height, int FrameCount, bool HasMetadata);
/// <summary>A named aspect ratio such as <c>wide</c> 3:2 (width:height).</summary>
public sealed record Ratio(string Name, int Numerator, int Denominator)
{
public static readonly Ratio Wide = new("wide", 3, 2);
public static readonly Ratio Tall = new("tall", 4, 5);
public double Value => (double)Numerator / Denominator;
}
/// <summary>
/// Where the subject is and how far to zoom: <see cref="X"/> and <see cref="Y"/> are fractions of the image width and
/// height (0 to 1, the centre is 0.5), <see cref="Zoom"/> is 1 to <see cref="MaxZoom"/>.
/// </summary>
public sealed record FocusSetting(double X, double Y, double Zoom)
{
public const double MaxZoom = 4.0;
public static readonly FocusSetting Centre = new(0.5, 0.5, 1.0);
public bool IsValid =>
X is >= 0 and <= 1 && Y is >= 0 and <= 1 && Zoom is >= 1 and <= MaxZoom
&& !double.IsNaN(X) && !double.IsNaN(Y) && !double.IsNaN(Zoom);
/// <summary>Clamps every value into its valid range (NaN becomes the default).</summary>
public FocusSetting Normalize() => new(
double.IsNaN(X) ? 0.5 : Math.Clamp(X, 0, 1),
double.IsNaN(Y) ? 0.5 : Math.Clamp(Y, 0, 1),
double.IsNaN(Zoom) ? 1.0 : Math.Clamp(Zoom, 1, MaxZoom));
/// <summary>The canonical text used inside the set hash: four decimals, invariant culture.</summary>
public string Canonical()
=> string.Create(CultureInfo.InvariantCulture, $"{Round(X)},{Round(Y)},{Round(Zoom)}");
private static string Round(double value) => Math.Round(value, 4).ToString("0.####", CultureInfo.InvariantCulture);
}
/// <summary>A rectangle in pixels of the oriented source image.</summary>
public readonly record struct CropRect(int X, int Y, int Width, int Height);
/// <summary>One output file to make: a ratio and width (and the height that follows), in one format, from one crop.</summary>
public sealed record VariantSpec(Ratio Ratio, int Width, int Height, ImageFormat Format, int Quality, CropRect Crop);
/// <summary>What to render for one ratio: the focus to cut around and the widths wanted when the source allows them.</summary>
public sealed record RatioPlan(Ratio Ratio, FocusSetting Focus, IReadOnlyList<int> Widths);
public sealed record RenderRequest(VariantSpec Variant);
public sealed record RenderedImage(byte[] Bytes, int Width, int Height, ImageFormat Format);
/// <summary>A parsed media address: <c>/media/{area}/{setHash}/{name}</c>.</summary>
public sealed record MediaAddress(string Area, string SetHash, string Name)
{
public string Key => $"{Area}/{SetHash}/{Name}";
}
@@ -0,0 +1,95 @@
using Microsoft.Extensions.Options;
namespace SlpModularCms.Core.Media;
/// <summary>An admitted upload: the bytes, what they are, and the processing slot, which is released on dispose.</summary>
public sealed class GuardedUpload(byte[] bytes, ImageInfo info, IDisposable slot) : IDisposable
{
public byte[] Bytes { get; } = bytes;
public ImageInfo Info { get; } = info;
public void Dispose() => slot.Dispose();
}
public interface IMediaUploadGuard
{
/// <summary>
/// The checks every image upload passes before anything is stored: availability, byte limit (counted while
/// reading, whatever the declared length says), format from the signature, library inspection with pixel and side
/// limits, and a free processing slot.
/// </summary>
/// <exception cref="ImageRejectedException">The upload is refused; see <see cref="ImageRejectedException.Code"/>.</exception>
Task<GuardedUpload> AdmitAsync(Stream upload, long declaredLength, CancellationToken cancellationToken);
}
public sealed class MediaUploadGuard(
IImageProcessor processor,
MediaProcessingGate gate,
MediaStatus status,
IOptions<MediaOptions> options) : IMediaUploadGuard
{
public async Task<GuardedUpload> AdmitAsync(Stream upload, long declaredLength, CancellationToken cancellationToken)
{
var limits = options.Value;
if (!status.UploadsAvailable)
{
throw new ImageRejectedException(ImageRejection.Unavailable, "Image uploads are not available on this host.");
}
if (declaredLength > limits.MaxUploadBytes)
{
throw new ImageRejectedException(ImageRejection.TooLarge, "The declared length is above the upload limit.");
}
var slot = await gate.EnterAsync(cancellationToken);
try
{
var bytes = await ReadLimitedAsync(upload, limits.MaxUploadBytes, cancellationToken);
var format = ImageSignature.Detect(bytes.AsSpan(0, Math.Min(bytes.Length, ImageSignature.HeaderLength)));
if (format == ImageFormat.Unknown || !processor.SupportedInput.Contains(format))
{
throw new ImageRejectedException(ImageRejection.UnsupportedFormat, "The file is not a supported image format.");
}
var info = await processor.InspectAsync(bytes, format, cancellationToken);
if (info.Width <= 0 || info.Height <= 0
|| info.Width > limits.MaxSide || info.Height > limits.MaxSide
|| (long)info.Width * info.Height > limits.MaxPixels)
{
throw new ImageRejectedException(ImageRejection.TooManyPixels, "The image is larger than the pixel limit.");
}
return new GuardedUpload(bytes, info, slot);
}
catch
{
slot.Dispose();
throw;
}
}
private static async Task<byte[]> ReadLimitedAsync(Stream upload, long limit, CancellationToken cancellationToken)
{
using var buffer = new MemoryStream();
var chunk = new byte[81920];
int read;
while ((read = await upload.ReadAsync(chunk, cancellationToken)) > 0)
{
if (buffer.Length + read > limit)
{
throw new ImageRejectedException(ImageRejection.TooLarge, "The upload is above the size limit.");
}
buffer.Write(chunk, 0, read);
}
if (buffer.Length == 0)
{
throw new ImageRejectedException(ImageRejection.Corrupt, "The upload is empty.");
}
return buffer.ToArray();
}
}
@@ -0,0 +1,85 @@
namespace SlpModularCms.Core.Media;
/// <summary>Decides which files to make from an image: per ratio the crop and the widths, per width the formats. Pure.</summary>
public static class VariantPlanner
{
public const int WebPQuality = 80;
public const int AvifQuality = 50;
public const int JpegQuality = 82;
/// <summary>The widths per ratio of the first version of the hero (never above the source: see <see cref="Plan"/>).</summary>
public static IReadOnlyList<int> DefaultWidths(Ratio ratio) => ratio.Name switch
{
"wide" => [640, 960, 1280, 1600, 2560],
"tall" => [480, 640, 960, 1280],
_ => [640, 960, 1280],
};
/// <summary>
/// For every ratio: the crop around the focus, then the widths that are not wider than the crop (an image is never
/// scaled up); when the crop is narrower than the smallest wanted width, one variant at the crop width. Each width is
/// planned in WebP, and in AVIF when <paramref name="outputs"/> contains it. WebP is required: without WebP the
/// plan falls back to JPEG so a platform without a WebP encoder still publishes something.
/// </summary>
/// <summary>The ratio name of the full-frame preview (the whole picture, not cut around a point).</summary>
public const string PreviewRatioName = "full";
/// <summary>The widest full-frame preview.</summary>
public const int PreviewMaxWidth = 1280;
/// <summary>
/// The full-frame preview an administrator needs to choose the focus point: the WHOLE oriented picture at its own
/// shape, scaled down to at most <see cref="PreviewMaxWidth"/> (never up). Not published to websites.
/// </summary>
public static VariantSpec Preview(int sourceWidth, int sourceHeight, IReadOnlySet<ImageFormat> outputs)
{
var width = Math.Min(PreviewMaxWidth, sourceWidth);
var height = Math.Max(1, (int)Math.Round(width * (double)sourceHeight / sourceWidth));
var (format, quality) = outputs.Contains(ImageFormat.WebP) ? (ImageFormat.WebP, WebPQuality) : (ImageFormat.Jpeg, JpegQuality);
return new VariantSpec(new Ratio(PreviewRatioName, sourceWidth, sourceHeight), width, height, format, quality, new CropRect(0, 0, sourceWidth, sourceHeight));
}
public static IReadOnlyList<VariantSpec> Plan(
int sourceWidth,
int sourceHeight,
IReadOnlyList<RatioPlan> ratios,
IReadOnlySet<ImageFormat> outputs)
{
var formats = new List<(ImageFormat Format, int Quality)>();
if (outputs.Contains(ImageFormat.WebP))
{
formats.Add((ImageFormat.WebP, WebPQuality));
}
else
{
formats.Add((ImageFormat.Jpeg, JpegQuality));
}
if (outputs.Contains(ImageFormat.Avif))
{
formats.Add((ImageFormat.Avif, AvifQuality));
}
var result = new List<VariantSpec>();
foreach (var plan in ratios)
{
var crop = FocusMath.CropFor(sourceWidth, sourceHeight, plan.Ratio, plan.Focus);
var widths = plan.Widths.Where(w => w > 0 && w <= crop.Width).Distinct().OrderBy(w => w).ToList();
if (widths.Count == 0)
{
widths.Add(crop.Width);
}
foreach (var width in widths)
{
var height = Math.Max(1, (int)Math.Round(width * (double)plan.Ratio.Denominator / plan.Ratio.Numerator));
foreach (var (format, quality) in formats)
{
result.Add(new VariantSpec(plan.Ratio, width, height, format, quality, crop));
}
}
}
return result;
}
}
@@ -0,0 +1,8 @@
# Golden files of the website build (ASF): hero
`host-injection/hero/` is a copy of `web/contract/host-injection/hero` of the Andrea Slop Fotografie website repository,
ASF commit `13f78c4` (branch `feature/hero-module`). It is the contract for the plain HTML the CMS host writes between the
CMS markers `CMS:HERO-HTML` (the hero) and `CMS:HERO-PRELOAD` (the preload links in the head); see
`host-injection/hero/README.md` for the rules. `HeroGoldenTests` runs `HeroPictureRenderer` against every input/expected
pair. When the website changes the contract it sends new files and the commit; replace the folder and note the new commit
here. `seed.json` is the seed data of the ASF test CMS (used by `scripts/Seed-Hero.ps1`).
@@ -0,0 +1,33 @@
# Hero contract (ASF site and CMS host)
Golden files for the hero regions that the CMS host fills, model B (decided 2026-11-16): the host cuts a `wide` (3:2) and a `tall` (4:5) variant per photo around the focus point, and the site uses `<picture>`. Same idea as the packages (`../README.md`): for every `NAME.input.json` the host's template must give exactly `NAME.expected.html` (between `<!--CMS:HERO-HTML-->` and `<!--/CMS:HERO-HTML-->`) and `NAME.expected-preload.html` (between `<!--CMS:HERO-PRELOAD-->` and `<!--/CMS:HERO-PRELOAD-->` in the head). The file ends with one newline that is not part of the HTML. The ASF repo tests its own implementation (`src/lib/hero.ts`, `src/lib/heroHtml.ts`) against the same files; to change the contract, change the code and run `UPDATE_GOLDEN=1 pnpm test src/lib/heroHtml.test.ts`, then send the new files to the CMS session. The ASF commit is in `git log -1 -- web/contract/host-injection/hero`.
## Input
The answer of `GET /api/v1/hero` (an object). A 204 (no hero) is `null`: the output is empty and the baked hero stays.
## Rules
1. Usable hero: an object with a non-empty `title` (trimmed) and at least one usable slide; otherwise both outputs are empty strings (fail open: the baked page stays).
2. A slide is kept when: `id` is non-empty; `image.src` is a path of our host (starts with one `/`, not `//`, no backslash); `image.width` and `image.height` are whole numbers above 0; and it has an `alt` (non-empty after trimming) or `decorative` is `true`. Other slides are skipped. Order is the given order. Only the FIRST kept slide is written; the client takes the rest from the data block.
3. A decorative slide writes `alt=""`.
4. Variants: kept when `ratio`, `type` and `url` are non-empty, `width` is a whole number above 0 and `url` is a path of our host. Only `image/webp` variants are used here (a preload cannot say "AVIF if supported", so a second format would be fetched twice). `wide` variants make the `img` `srcset`, `tall` variants the `source` `srcset`; sorted by width ascending, written `URL WIDTHw`, joined by `, `.
5. `focus` (`x` and `y` both between 0 and 1) is written as `style="object-position:X% Y%"` on the `img` (percentages without trailing zeros, at most two decimals); without a valid focus there is no `style`.
6. Buttons: at most 2 (the first two valid ones). A button is valid with a non-empty `label` and a `target` that is an anchor (`#name`), a path (one leading `/`, not `//`, no backslash) or an `https://` address that parses. `lead` and `display` are not written in the HTML.
7. Escape every text and attribute (`&` `&amp;`, `<` `&lt;`, `>` `&gt;`, `"` `&quot;`, `'` `&#39;`). A new line in the title becomes `<br>` (after escaping).
## Output, HERO-HTML
`<picture class="cms-hero-picture">` + (when a `tall` webp variant exists: `<source media="(orientation: portrait)" srcset="TALL" sizes="100vw" type="image/webp">`) + `<img class="cms-hero-image" src="SRC"` + (when a `wide` webp variant exists: ` srcset="WIDE" sizes="100vw"`) + ` width="W" height="H" alt="ALT"` + (focus: ` style="object-position:X% Y%"`) + ` fetchpriority="high">` + `</picture>` + `<h1 class="cms-hero-title">` + (tagline: `<span class="cms-hero-tagline">TAGLINE</span> `) + `<span class="cms-hero-name">TITLE</span></h1>` + (when there are buttons: `<p class="cms-hero-buttons">` + `<a class="cms-hero-button" href="TARGET">LABEL</a>`... + `</p>`).
`src` is `image.src`; `W` and `H` are `image.width` and `image.height`. Selection is by orientation, not width, because the hero is as high as the screen.
## Output, HERO-PRELOAD
With a `tall` variant: `<link rel="preload" as="image" imagesrcset="TALL" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high">` followed by the landscape link with `media="(orientation: landscape)"`. The landscape link has `imagesrcset="WIDE" imagesizes="100vw"`, or `href="SRC"` when there is no wide set. Without a tall variant there is one link without `media`. The `srcset`, `sizes` and `media` values are byte for byte the same as in the picture; the golden files check this.
## Files
`hero-standard` (two slides, wide and tall, AVIF ignored), `hero-minimal` (no variants, no tagline, no button), `hero-two-lines` (line break, anchor, https and a third button that is cut, decorative slide), `hero-hostile` (escaping, unsafe buttons, unusable slides skipped), `hero-no-alt` and `hero-none` (empty). `seed.json` holds the seed for the ASF test CMS with a first focus estimate per photo.
URL scheme suggestion for the variants (the CMS decides): `/media/hero/<hash>/wide-1600.webp`, `/media/hero/<hash>/tall-960.avif`.
@@ -0,0 +1 @@
<link rel="preload" as="image" imagesrcset="/media/hero/a&quot;b/w800.webp 800w" imagesizes="100vw" fetchpriority="high">
@@ -0,0 +1 @@
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/a&quot;b/w1600.webp" srcset="/media/hero/a&quot;b/w800.webp 800w" sizes="100vw" width="1600" height="1067" alt="Een &quot;foto&quot; &lt;script&gt;" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">&lt;i&gt;x&lt;/i&gt;</span> <span class="cms-hero-name">&lt;b&gt;&quot;Andrea&quot; &amp; &#39;Slop&#39;&lt;/b&gt;</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/a?b=1&amp;c=&quot;2&quot;">&lt;Goed&gt;</a></p>
@@ -0,0 +1,58 @@
{
"display": "slideshow",
"title": "<b>\"Andrea\" & 'Slop'</b>",
"tagline": "<i>x</i>",
"buttons": [
{ "label": "javascript", "target": "javascript:alert(1)" },
{ "label": "Protocolvrij", "target": "//evil.example/x" },
{ "label": "<Goed>", "target": "/a?b=1&c=\"2\"" }
],
"slides": [
{
"id": "skip1",
"alt": "",
"decorative": false,
"image": { "width": 10, "height": 10, "src": "/media/x.webp" }
},
{
"id": "skip2",
"alt": "extern",
"decorative": false,
"image": {
"width": 10,
"height": 10,
"src": "https://evil.example/x.webp"
}
},
{
"id": "skip3",
"alt": "geen afmeting",
"decorative": false,
"image": { "width": 0, "height": 10, "src": "/media/x.webp" }
},
{
"id": "a1",
"alt": "Een \"foto\" <script>",
"decorative": false,
"image": {
"width": 1600,
"height": 1067,
"src": "/media/hero/a\"b/w1600.webp",
"variants": [
{
"ratio": "wide",
"width": 800,
"type": "image/webp",
"url": "/media/hero/a\"b/w800.webp"
},
{
"ratio": "wide",
"width": 400,
"type": "image/webp",
"url": "https://evil.example/w400.webp"
}
]
}
}
]
}
@@ -0,0 +1 @@
<link rel="preload" as="image" href="/media/hero/abc123/w2560.webp" fetchpriority="high">
@@ -0,0 +1 @@
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w2560.webp" width="2560" height="1702" alt="Lifestyle fotografie" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-name">Andrea Slop</span></h1>
@@ -0,0 +1,17 @@
{
"display": "single",
"title": "Andrea Slop",
"buttons": [],
"slides": [
{
"id": "a1",
"alt": "Lifestyle fotografie",
"decorative": false,
"image": {
"width": 2560,
"height": 1702,
"src": "/media/hero/abc123/w2560.webp"
}
}
]
}
@@ -0,0 +1,13 @@
{
"display": "slideshow",
"title": "Titel",
"buttons": [],
"slides": [
{
"id": "x",
"alt": "",
"decorative": false,
"image": { "width": 1, "height": 1, "src": "/m.webp" }
}
]
}
@@ -0,0 +1 @@
<link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high"><link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" imagesizes="100vw" media="(orientation: landscape)" fetchpriority="high">
@@ -0,0 +1 @@
<picture class="cms-hero-picture"><source media="(orientation: portrait)" srcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" sizes="100vw" type="image/webp"><img class="cms-hero-image" src="/media/hero/3f9a1c/wide-1600.webp" srcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" sizes="100vw" width="1600" height="1067" alt="Zwangerschapsfoto in de duinen" style="object-position:50% 55%" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Lifestyle fotograaf · Huizen</span> <span class="cms-hero-name">Andrea Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/over-mij">Lees meer</a></p>
@@ -0,0 +1,76 @@
{
"display": "slideshow",
"title": "Andrea Slop",
"tagline": "Lifestyle fotograaf · Huizen",
"lead": null,
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
"slides": [
{
"id": "11111111-1111-1111-1111-111111111111",
"alt": "Zwangerschapsfoto in de duinen",
"decorative": false,
"image": {
"width": 1600,
"height": 1067,
"focus": { "x": 0.5, "y": 0.55 },
"src": "/media/hero/3f9a1c/wide-1600.webp",
"variants": [
{
"ratio": "wide",
"width": 1600,
"type": "image/webp",
"url": "/media/hero/3f9a1c/wide-1600.webp"
},
{
"ratio": "wide",
"width": 960,
"type": "image/webp",
"url": "/media/hero/3f9a1c/wide-960.webp"
},
{
"ratio": "wide",
"width": 1600,
"type": "image/avif",
"url": "/media/hero/3f9a1c/wide-1600.avif"
},
{
"ratio": "wide",
"width": 960,
"type": "image/avif",
"url": "/media/hero/3f9a1c/wide-960.avif"
},
{
"ratio": "tall",
"width": 960,
"type": "image/webp",
"url": "/media/hero/3f9a1c/tall-960.webp"
},
{
"ratio": "tall",
"width": 640,
"type": "image/webp",
"url": "/media/hero/3f9a1c/tall-640.webp"
},
{
"ratio": "tall",
"width": 960,
"type": "image/avif",
"url": "/media/hero/3f9a1c/tall-960.avif"
}
]
}
},
{
"id": "22222222-2222-2222-2222-222222222222",
"alt": "Knusse newbornfoto met de familie",
"decorative": false,
"image": {
"width": 1600,
"height": 1067,
"focus": { "x": 0.45, "y": 0.5 },
"src": "/media/hero/7b2e90/wide-1600.webp",
"variants": []
}
}
]
}
@@ -0,0 +1 @@
<link rel="preload" as="image" href="/media/hero/abc123/w1600.webp" fetchpriority="high">
@@ -0,0 +1 @@
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w1600.webp" width="1600" height="1067" alt="" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Fotografie</span> <span class="cms-hero-name">Andrea<br>Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="#contact">Boek een shoot</a><a class="cms-hero-button" href="https://andreaslop.nl/portfolio">Portfolio</a></p>
@@ -0,0 +1,23 @@
{
"display": "slideshow",
"title": "Andrea\nSlop",
"tagline": "Fotografie",
"buttons": [
{ "label": "Boek een shoot", "target": "#contact" },
{ "label": "Portfolio", "target": "https://andreaslop.nl/portfolio" },
{ "label": "Derde knop", "target": "/derde" }
],
"slides": [
{
"id": "a1",
"alt": "",
"decorative": true,
"image": {
"width": 1600,
"height": 1067,
"src": "/media/hero/abc123/w1600.webp",
"variants": []
}
}
]
}
@@ -0,0 +1,73 @@
{
"note": "Seed for the ASF hero on the test CMS. Focus points are a first estimate from looking at the photos (x and y between 0 and 1, from the top left); Bryan adjusts them in the admin. Sources are the existing exports in web/src/assets (not camera originals; never upscale).",
"display": "slideshow",
"title": "Andrea Slop",
"tagline": "Lifestyle fotograaf · Huizen",
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
"slides": [
{
"file": "hero-8.jpg",
"width": 1600,
"height": 1067,
"alt": "Zwangerschapsfoto in de duinen",
"focus": { "x": 0.5, "y": 0.55 },
"subject": "belly and hands"
},
{
"file": "hero-7.jpg",
"width": 1600,
"height": 1067,
"alt": "Knusse newbornfoto met de familie",
"focus": { "x": 0.45, "y": 0.5 },
"subject": "the kiss on the baby"
},
{
"file": "hero-1.jpg",
"width": 2560,
"height": 1702,
"alt": "Lifestyle fotografie",
"focus": { "x": 0.55, "y": 0.6 },
"subject": "tattoo and heather"
},
{
"file": "hero-2.jpg",
"width": 1600,
"height": 1067,
"alt": "Zwangerschapsshoot in de buitenlucht",
"focus": { "x": 0.45, "y": 0.45 },
"subject": "the couple"
},
{
"file": "hero-3.jpg",
"width": 1600,
"height": 1067,
"alt": "Newbornshoot van een pasgeboren baby",
"focus": { "x": 0.45, "y": 0.4 },
"subject": "the baby's face"
},
{
"file": "hero-4.jpg",
"width": 2048,
"height": 1362,
"alt": "Spontaan portret buiten",
"focus": { "x": 0.45, "y": 0.35 },
"subject": "the face"
},
{
"file": "hero-5.jpg",
"width": 1600,
"height": 1067,
"alt": "Natuurlijke lifestylefoto",
"focus": { "x": 0.5, "y": 0.55 },
"subject": "the hands with the ring"
},
{
"file": "hero-6.jpg",
"width": 2048,
"height": 1362,
"alt": "Portret in de natuur",
"focus": { "x": 0.5, "y": 0.35 },
"subject": "the face"
}
]
}
@@ -0,0 +1,84 @@
using System.Diagnostics;
using ImageMagick;
using SlpModularCms.Core.Media;
using Xunit.Abstractions;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// A measurement, not a check: where the time goes when a large photo (about 6000x4000, a few MB) is cut into its set.
/// Runs only when HERO_TIMING_BIG=1; otherwise it passes at once. Set MAGICK_THREAD_LIMIT=1 to look at a single core.
/// </summary>
public class HeroBigPhotoTimingTests(ITestOutputHelper output)
{
private static byte[] BigPhoto(int width, int height)
{
// Smooth gradients with a little noise: compresses like a photo (a few MB), unlike pure noise.
using var image = new MagickImage(MagickColors.SteelBlue, (uint)width, (uint)height);
image.AddNoise(NoiseType.Gaussian, 0.4);
using var clouds = new MagickImage("plasma:fractal", (uint)(width / 4), (uint)(height / 4));
clouds.Resize((uint)width, (uint)height);
image.Composite(clouds, CompositeOperator.Blend, "40");
image.Quality = 88;
using var stream = new MemoryStream();
image.Write(stream, MagickFormat.Jpeg);
return stream.ToArray();
}
[Fact]
public async Task MeasureASetOfABigPhoto()
{
if (Environment.GetEnvironmentVariable("HERO_TIMING_BIG") != "1")
{
return;
}
var width = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_W") ?? "6000");
var height = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_H") ?? "4000");
// Made before the processor exists: its resource limits (20 s) would stop the making of the fixture itself.
var bytes = BigPhoto(width, height);
using var rig = new HeroTestRig();
if (int.TryParse(Environment.GetEnvironmentVariable("HERO_TIMING_THREADS"), out var threads))
{
ResourceLimits.Thread = (ulong)threads;
output.WriteLine($"image library threads: {ResourceLimits.Thread}");
}
output.WriteLine($"photo {width}x{height}, {bytes.Length / 1024.0 / 1024.0:0.0} MB");
var watch = Stopwatch.StartNew();
var info = await rig.Processor.InspectAsync(bytes, ImageFormat.Jpeg, default);
output.WriteLine($"inspect: {watch.ElapsedMilliseconds} ms ({info.Width}x{info.Height})");
var ratios = new[] { Ratio.Wide, Ratio.Tall }
.Select(r => new RatioPlan(r, new FocusSetting(0.5, 0.5, 1).Normalize(), VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
var byFormat = new Dictionary<ImageFormat, List<long>>();
var total = Stopwatch.StartNew();
foreach (var spec in specs)
{
watch.Restart();
var rendered = await rig.Processor.RenderAsync(bytes, ImageFormat.Jpeg, spec, default);
var ms = watch.ElapsedMilliseconds;
if (!byFormat.TryGetValue(spec.Format, out var list))
{
byFormat[spec.Format] = list = [];
}
list.Add(ms);
output.WriteLine($" {spec.Ratio.Name,-4} {spec.Width,5} {spec.Format,-5}: {ms,6} ms, {rendered.Bytes.Length / 1024} KB");
}
output.WriteLine($"per variant, one decode each: {specs.Count} variants in {total.ElapsedMilliseconds} ms");
foreach (var (format, list) in byFormat)
{
output.WriteLine($" {format}: {list.Count} variants, mean {list.Average():0} ms, total {list.Sum()} ms");
}
// The whole set from one decode.
watch.Restart();
var steps = 0;
var set = await rig.Processor.RenderSetAsync(bytes, ImageFormat.Jpeg, specs, (_, _) => steps++, default);
output.WriteLine($"whole set from one decode: {specs.Count} variants in {watch.ElapsedMilliseconds} ms ({steps} progress steps, {set.Sum(r => r.Bytes.Length) / 1024} KB)");
}
}
@@ -0,0 +1,422 @@
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>A changed focus is part of saving the hero: nothing live changes before that save.</summary>
public class HeroFocusSaveTests
{
private static HeroSaveRequest Save(Guid imageId, IReadOnlyList<HeroImageFocusRequest>? focus)
=> new("Andrea Slop", null, null, "single", [], [new HeroSlideRequest(null, imageId, "foto")], focus);
[Fact]
public async Task TheLiveHero_KeepsItsOldSet_UntilTheNewSetIsCut_AndThenSwitchesToIt()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var before = (await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src;
// The save returns without cutting anything: the old set stays live and the admin sees the image as processing.
var saved = await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.5, 0.5, 1), null, new FocusDto(1.0, 0.5, 1))]), HeroTestRig.Admin, default);
(await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src.Should().Be(before);
saved.Slides[0].Image.Processing.Should().BeTrue();
(await rig.RenderPending()).Should().Be(1);
var after = (await rig.Public.GetActiveAsync(default))!.Slides[0].Image;
var beforeSet = before.Split('/')[3];
after.Variants.Should().OnlyContain(v => !v.Url.Contains(beforeSet), "a changed focus gives new addresses");
after.FocusByRatio.Should().ContainKey("tall");
(await rig.Service.GetAsync(default)).Slides[0].Image.Processing.Should().BeFalse();
}
[Fact]
public async Task TheOldFiles_StayOnDisk_AfterTheSwitch_UntilTheCleanUp()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var oldVariant = image.Variants.First();
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.1, 0.1, 1.5))]), HeroTestRig.Admin, default);
await rig.RenderPending();
(await rig.Store.ExistsAsync(oldVariant.Url["/media/".Length..], default)).Should().BeTrue("a site that still holds the old data keeps working for the retention time");
rig.Db.HeroRetiredFiles.Should().ContainSingle(f => f.Kind == "set");
}
[Fact]
public async Task AnUpload_ReturnsWithoutCuttingAnything_AndIsNotPublishedUntilItsSetExists()
{
using var rig = new HeroTestRig();
var dto = await rig.UploadWithoutRendering(HeroTestRig.Photo());
dto.Processing.Should().BeTrue();
dto.Variants.Should().BeEmpty();
await rig.Service.SaveAsync(Save(Guid.Parse(dto.Id), null), HeroTestRig.Admin, default);
(await rig.Public.GetActiveAsync(default))!.Slides.Should().BeEmpty("a slide whose image is not ready is not published");
await rig.RenderPending();
(await rig.Public.GetActiveAsync(default))!.Slides.Should().ContainSingle();
}
[Fact]
public async Task TwoFocusSaves_BeforeTheRenderRuns_AreCutOnce_AtTheLastFocus()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.2, 0.2, 1))]), HeroTestRig.Admin, default);
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.9, 0.9, 1))]), HeroTestRig.Admin, default);
(await rig.RenderPending()).Should().Be(1);
var done = rig.Db.HeroImages.Single();
done.SetHash.Should().Be(HeroMapping.TargetSetHash(done));
done.FocusX.Should().Be(0.9);
}
[Fact]
public async Task ACancelledRenderOrSave_LeavesTheStoredHeroAlone_AndARepeatWorks()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var live = rig.Db.HeroImages.Single().SetHash;
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.9, 0.1, 1))]), HeroTestRig.Admin, default);
using var cancelled = new CancellationTokenSource();
await cancelled.CancelAsync();
var act = async () => await rig.Images.RenderNextPendingAsync(new HashSet<Guid>(), cancelled.Token);
await act.Should().ThrowAsync<OperationCanceledException>();
rig.Db.HeroImages.Single().SetHash.Should().Be(live, "nothing was switched");
(await rig.RenderPending()).Should().Be(1);
HeroMapping.NeedsRender(rig.Db.HeroImages.Single()).Should().BeFalse();
}
[Fact]
public async Task APreparedFocusThatIsNeverSaved_LeavesOnlyUnreferencedFiles_ThatTheCleanUpRemoves()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var live = rig.Db.HeroImages.Single().SetHash;
// The save stops after the new files were written (here: the hero refers to a slide of another hero).
var failing = new HeroSaveRequest("Andrea Slop", null, null, "single", [], [new HeroSlideRequest(Guid.NewGuid(), id, "foto")],
[new HeroImageFocusRequest(id, new FocusDto(0.9, 0.9, 2))]);
var act = async () => await rig.Service.SaveAsync(failing, HeroTestRig.Admin, default);
await act.Should().ThrowAsync<HeroSaveRejectedException>();
rig.Db.HeroImages.Single().SetHash.Should().Be(live, "the failed save did not change the stored image");
(await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src.Should().Contain(live);
}
[Fact]
public async Task AFocusForAnUnknownImage_IsRejected()
{
using var rig = new HeroTestRig();
var act = async () => await rig.Service.SaveAsync(Save(Guid.NewGuid(), null) with { Slides = [], ImageFocus = [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(0.5, 0.5, 1))] }, HeroTestRig.Admin, default);
(await act.Should().ThrowAsync<HeroSaveRejectedException>()).Which.Field.Should().Be("ImageFocus[0].ImageId");
}
[Theory]
[InlineData(-0.1, 0.5, 1.0)]
[InlineData(0.5, 1.1, 1.0)]
[InlineData(0.5, 0.5, 0.5)]
[InlineData(0.5, 0.5, 4.5)]
public void AnInvalidFocusInTheRequest_IsRefusedByTheValidation(double x, double y, double zoom)
{
var request = Save(Guid.NewGuid(), [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(x, y, zoom))]);
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, true);
results.SelectMany(r => r.MemberNames).Should().Contain("ImageFocus[0]");
}
[Fact]
public void AValidFocusEdit_PassesTheValidation()
{
var request = Save(Guid.NewGuid(), [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(0.2, 0.9, 2), new FocusDto(0.1, 0.1, 1), null)]);
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, true);
results.Should().BeEmpty();
}
[Fact]
public async Task AnImageTheRendererGaveUpOn_IsShownAsFailed_AndARetryClearsIt()
{
using var rig = new HeroTestRig();
var dto = await rig.UploadWithoutRendering(HeroTestRig.Photo());
var id = Guid.Parse(dto.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
for (var i = 0; i < HeroRenderSignal.MaxAttempts; i++)
{
rig.Signal.RecordFailure(id);
}
var failed = (await rig.Service.GetAsync(default)).Slides[0].Image;
failed.Processing.Should().BeTrue();
failed.Failed.Should().BeTrue();
await rig.Images.RetryRenderAsync(id, default);
(await rig.Service.GetAsync(default)).Slides[0].Image.Failed.Should().BeFalse();
(await rig.RenderPending()).Should().Be(1);
}
[Fact]
public async Task ARetryForAnUnknownImage_IsNotFound()
{
using var rig = new HeroTestRig();
var act = async () => await rig.Images.RetryRenderAsync(Guid.NewGuid(), default);
await act.Should().ThrowAsync<HeroImageNotFoundException>();
}
[Fact]
public void TheUploadLimits_ListTheFormatsTheHostAccepts_InPlainWords()
{
using var rig = new HeroTestRig();
var limits = HeroMapping.UploadLimits(rig.Status, rig.Media);
limits.Formats.Should().Contain(["JPEG", "PNG", "WebP"]).And.OnlyHaveUniqueItems();
limits.Formats.Should().BeEquivalentTo(rig.Status.AcceptedInput.Select(f => f == SlpModularCms.Core.Media.ImageFormat.Jpeg ? "JPEG" : f == SlpModularCms.Core.Media.ImageFormat.WebP ? "WebP" : f.ToString().ToUpperInvariant()));
limits.MaxUploadBytes.Should().Be(rig.Media.MaxUploadBytes);
limits.MaxMegapixels.Should().Be((int)(rig.Media.MaxPixels / 1_000_000));
}
[Fact]
public async Task AnImageThatCannotBeCut_FailsWithItsId_AndTheOthersAreStillCut()
{
using var rig = new HeroTestRig();
var broken = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 1));
var fine = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 2));
// The original of the first photo is gone (a lost file): cutting it cannot work.
await rig.Store.DeleteAsync(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id)).OriginalKey, default);
var failure = await Assert.ThrowsAsync<HeroImageRenderException>(() => rig.Images.RenderNextPendingAsync(new HashSet<Guid>(), default));
failure.ImageId.Should().Be(Guid.Parse(broken.Id));
var handled = await rig.Images.RenderNextPendingAsync(new HashSet<Guid> { failure.ImageId }, default);
handled.Should().Be(Guid.Parse(fine.Id));
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(fine.Id))).Should().BeFalse();
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id))).Should().BeTrue();
}
[Fact]
public async Task ASetFromOneDecode_GivesTheSameSizesAndFormats_AsCuttingEveryVariantOnItsOwn_AndReportsEveryStep()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo(2400, 1600);
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
var ratios = new[] { SlpModularCms.Core.Media.Ratio.Wide, SlpModularCms.Core.Media.Ratio.Tall }
.Select(r => new SlpModularCms.Core.Media.RatioPlan(r, new SlpModularCms.Core.Media.FocusSetting(0.3, 0.6, 1.5).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
var steps = new List<(int Done, int Total)>();
var set = await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (done, total) => steps.Add((done, total)), default);
set.Should().HaveCount(specs.Count);
for (var i = 0; i < specs.Count; i++)
{
var single = await rig.Processor.RenderAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs[i], default);
(set[i].Width, set[i].Height, set[i].Format).Should().Be((single.Width, single.Height, single.Format), $"variant {i}");
}
steps.Should().HaveCount(specs.Count);
steps.Select(x => x.Done).Should().BeInAscendingOrder();
steps.Last().Should().Be((specs.Count, specs.Count));
}
[Fact]
public async Task ACancelledSet_StopsBetweenVariants()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo(2400, 1600);
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
var ratios = new[] { new SlpModularCms.Core.Media.RatioPlan(SlpModularCms.Core.Media.Ratio.Wide, new SlpModularCms.Core.Media.FocusSetting(0.5, 0.5, 1).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(SlpModularCms.Core.Media.Ratio.Wide)) };
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
using var cts = new CancellationTokenSource();
var done = 0;
var act = async () => await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (_, _) => { done++; cts.Cancel(); }, cts.Token);
await act.Should().ThrowAsync<OperationCanceledException>();
done.Should().BeLessThan(specs.Count);
}
[Fact]
public async Task EveryImageWithoutACompleteSet_IsProcessing_InTheAdminApi_AndTwoNewPhotosAreBothCut()
{
using var rig = new HeroTestRig();
// The helper Upload cuts everything that is pending, so the finished photo is made first.
var ready = await rig.Upload(HeroTestRig.Photo(seed: 3));
var first = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 1));
var second = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 2));
var request = new HeroSaveRequest("Titel", null, null, "slideshow", [],
[new HeroSlideRequest(null, Guid.Parse(first.Id), "een"), new HeroSlideRequest(null, Guid.Parse(second.Id), "twee"), new HeroSlideRequest(null, Guid.Parse(ready.Id), "drie")]);
var saved = await rig.Service.SaveAsync(request, HeroTestRig.Admin, default);
saved.Slides.Select(s => s.Image.Processing).Should().Equal(true, true, false);
(await rig.Service.GetAsync(default)).Slides.Select(s => s.Image.Processing).Should().Equal(true, true, false);
(await rig.RenderPending()).Should().Be(2, "both new photos are cut, one after the other");
(await rig.Service.GetAsync(default)).Slides.Should().OnlyContain(s => !s.Image.Processing && s.Image.Variants.Count > 0);
}
[Theory]
[InlineData(3000, 2000)]
[InlineData(2000, 3000)]
[InlineData(1600, 1600)]
[InlineData(3000, 1000)]
public async Task EverySet_HasAFullFramePreviewAtTheShapeOfThePicture_ThatIsNotPublishedToWebsites(int width, int height)
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo(width, height));
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var admin = (await rig.Service.GetAsync(default)).Slides[0].Image;
var stored = rig.Db.HeroImages.Single();
admin.PreviewSrc.Should().NotBeNull();
var preview = stored.Variants.Single(v => v.Ratio == "full");
(preview.Width / (double)preview.Height).Should().BeApproximately(width / (double)height, 0.01, "the preview is the whole picture, not a crop");
preview.Width.Should().Be(Math.Min(1280, width));
admin.Variants.Should().NotContain(v => v.Ratio == "full", "the admin lists only the variants of the website");
(await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Variants.Should().NotContain(v => v.Ratio == "full");
(await rig.Store.ExistsAsync($"hero/{stored.SetHash}/{preview.FileName}", default)).Should().BeTrue();
}
[Fact]
public async Task ASetMadeBeforeThePreviewExisted_GetsOnlyThePreviewAdded_WithoutBeingProcessingOrChangingItsAddresses()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo(2000, 3000));
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var stored = rig.Db.HeroImages.Single();
var setBefore = stored.SetHash;
var preview = stored.Variants.Single(v => v.Ratio == "full");
stored.Variants = stored.Variants.Where(v => v.Ratio != "full").ToList(); // as an older set looked
await rig.Db.SaveChangesAsync();
await rig.Store.DeleteAsync($"hero/{setBefore}/{preview.FileName}", default);
var oldLook = (await rig.Service.GetAsync(default)).Slides[0].Image;
oldLook.Processing.Should().BeFalse("adding the preview is not work the administrator waits for");
oldLook.PreviewSrc.Should().BeNull();
(await rig.RenderPending()).Should().Be(1);
var after = rig.Db.HeroImages.Single();
after.SetHash.Should().Be(setBefore, "nothing live changes");
(await rig.Service.GetAsync(default)).Slides[0].Image.PreviewSrc.Should().NotBeNull();
(await rig.Store.ExistsAsync($"hero/{setBefore}/{preview.FileName}", default)).Should().BeTrue();
}
[Fact]
public async Task APhotoThatIsAlreadyThere_ComesBackAsReused_WithItsVariants_AndNothingToWaitFor()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo(seed: 5);
var first = await rig.Upload(photo);
var again = await rig.UploadWithoutRendering(photo);
again.Id.Should().Be(first.Id);
again.Reused.Should().BeTrue();
again.Processing.Should().BeFalse();
again.Variants.Should().NotBeEmpty();
first.Reused.Should().BeFalse();
}
[Fact]
public async Task TheStateOfOneImage_IsAskedForByItsId_WithTheRunningRenderersFlags()
{
using var rig = new HeroTestRig();
var dto = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 6));
var id = Guid.Parse(dto.Id);
(await rig.Images.GetImageAsync(id, default))!.Processing.Should().BeTrue();
rig.Signal.SetProgress(id, 4, 19);
var busy = (await rig.Images.GetImageAsync(id, default))!;
(busy.RenderDone, busy.RenderTotal).Should().Be((4, 19));
rig.Signal.ClearProgress(id);
await rig.RenderPending();
var done = (await rig.Images.GetImageAsync(id, default))!;
done.Processing.Should().BeFalse();
done.Variants.Should().NotBeEmpty();
(await rig.Images.GetImageAsync(Guid.NewGuid(), default)).Should().BeNull();
}
[Fact]
public async Task APutWithAChangedFocus_ShowsTheImageAsBeingRecut_InTheNextAdminRead_AndAsReadyAfterTheRenderer()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo(2400, 1600));
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
(await rig.Service.GetAsync(default)).Slides[0].Image.Processing.Should().BeFalse();
// The body of the PUT as the admin sends it (camelCase JSON), bound the way the controller binds it.
var json = $$"""
{"title":"Andrea Slop","tagline":null,"lead":null,"display":"single","buttons":[],"isActive":true,"startsAt":null,"endsAt":null,
"slides":[{"id":null,"imageId":"{{id}}","alt":"foto","decorative":false,"isActive":true}],
"imageFocus":[{"imageId":"{{id}}","focus":{"x":0.2,"y":0.3,"zoom":1.5},"wide":null,"tall":null}]}
""";
var request = System.Text.Json.JsonSerializer.Deserialize<HeroSaveRequest>(json, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web))!;
request.ImageFocus.Should().ContainSingle().Which.Focus.Should().Be(new FocusDto(0.2, 0.3, 1.5));
var response = await rig.Service.SaveAsync(request, HeroTestRig.Admin, default);
response.Slides.Single(s => s.ImageId == image.Id).Image.Processing.Should().BeTrue("the answer of the PUT already says the new crop is being made");
var read = (await rig.Service.GetAsync(default)).Slides.Single(s => s.ImageId == image.Id).Image;
read.Processing.Should().BeTrue();
read.Variants.Should().NotBeEmpty("the old crop stays published meanwhile");
(await rig.RenderPending()).Should().Be(1);
var done = (await rig.Service.GetAsync(default)).Slides.Single(s => s.ImageId == image.Id).Image;
done.Processing.Should().BeFalse();
done.Focus.Should().Be(new FocusDto(0.2, 0.3, 1.5));
}
[Fact]
public async Task AHigherZoom_GivesASmallerSet_BecauseNoVariantIsMadeLargerThanItsCrop()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo(2400, 1600));
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var normal = rig.Db.HeroImages.Single().Variants.Count;
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.5, 0.5, 3))]), HeroTestRig.Admin, default);
await rig.RenderPending();
rig.Db.HeroImages.Single().Variants.Count.Should().BeLessThan(normal, "a zoomed crop is smaller, so the wide sizes above it are left out");
}
}
@@ -0,0 +1,287 @@
using System.Text.Json;
using FluentAssertions;
using FsCheck;
using FsCheck.Fluent;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// The HTML written between the hero markers must be exactly what the website expects: the golden files of the website
/// repository (Golden/SOURCE.md names the commit) are run through <see cref="HeroPictureRenderer"/>.
/// </summary>
public class HeroGoldenTests
{
private static string Dir => Path.Combine(AppContext.BaseDirectory, "Golden", "host-injection", "hero");
private static readonly JsonSerializerOptions Web = new(JsonSerializerDefaults.Web);
public static IEnumerable<object[]> Cases()
=> Directory.GetFiles(Dir, "*.input.json").Select(f => new object[] { Path.GetFileName(f)[..^".input.json".Length] }).OrderBy(o => (string)o[0]);
// The expected files end with one newline that is not part of the HTML.
private static string Expected(string file)
{
var text = File.ReadAllText(Path.Combine(Dir, file)).Replace("\r\n", "\n");
return text.EndsWith('\n') ? text[..^1] : text;
}
private static PublicHeroDto? Input(string name)
=> JsonSerializer.Deserialize<PublicHeroDto?>(File.ReadAllText(Path.Combine(Dir, name + ".input.json")), Web);
[Fact]
public void TheGoldenFiles_AreThere() => Cases().Count().Should().BeGreaterThanOrEqualTo(6);
[Theory]
[MemberData(nameof(Cases))]
public void TheRegion_MatchesTheGoldenFile(string name)
=> HeroPictureRenderer.Render(Input(name)).Region.Should().Be(Expected(name + ".expected.html"));
[Theory]
[MemberData(nameof(Cases))]
public void ThePreload_MatchesTheGoldenFile(string name)
=> HeroPictureRenderer.Render(Input(name)).Preload.Should().Be(Expected(name + ".expected-preload.html"));
[Fact]
public void ThePreloadAndThePicture_ShareTheirSrcsetSizesAndMedia()
{
var injection = HeroPictureRenderer.Render(Input("hero-standard"));
foreach (var set in new[] { "/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w", "/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" })
{
injection.Region.Should().Contain($"srcset=\"{set}\"");
injection.Preload.Should().Contain($"imagesrcset=\"{set}\"");
}
injection.Region.Should().Contain("media=\"(orientation: portrait)\"");
injection.Preload.Should().Contain("media=\"(orientation: portrait)\"").And.Contain("media=\"(orientation: landscape)\"");
}
}
public class HeroPictureRendererTests
{
private static PublicHeroDto Hero(PublicImageDto? image = null, string title = "Titel", string? tagline = null, params HeroButtonDto[] buttons)
=> new("single", title, tagline, null, buttons,
[new PublicSlideDto("s1", "Een foto", false, image ?? Image())]);
private static PublicImageDto Image(FocusDto? focus = null, params HeroVariantDto[] variants)
=> new(1600, 1067, focus ?? new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-1600.webp", variants);
[Theory]
[InlineData(0.5, 0.55, "50% 55%")]
[InlineData(0.455, 0.0, "45.5% 0%")]
[InlineData(1.0, 1.0, "100% 100%")]
[InlineData(0.123456, 0.987654, "12.35% 98.77%")]
public void TheFocus_BecomesAnObjectPosition(double x, double y, string expected)
=> HeroPictureRenderer.Render(Hero(Image(new FocusDto(x, y, 1)))).Region.Should().Contain($" style=\"object-position:{expected}\"");
[Theory]
[InlineData(-0.1, 0.5)]
[InlineData(0.5, 1.1)]
[InlineData(double.NaN, 0.5)]
public void AnInvalidFocus_GivesNoStyle(double x, double y)
=> HeroPictureRenderer.Render(Hero(Image(new FocusDto(x, y, 1)))).Region.Should().NotContain("style=");
[Fact]
public void WithoutATall_ThereIsOnePreload_WithoutMedia()
{
var wide = new HeroVariantDto("wide", 960, 640, "image/webp", "/media/hero/0123456789abcdef/wide-960.webp");
var injection = HeroPictureRenderer.Render(Hero(Image(null, wide)));
injection.Preload.Should().Be("<link rel=\"preload\" as=\"image\" imagesrcset=\"/media/hero/0123456789abcdef/wide-960.webp 960w\" imagesizes=\"100vw\" fetchpriority=\"high\">");
}
[Fact]
public void AvifVariants_NeverAppearInThePictureOrThePreload()
{
var variants = new[]
{
new HeroVariantDto("wide", 960, 640, "image/webp", "/media/hero/0123456789abcdef/wide-960.webp"),
new HeroVariantDto("wide", 960, 640, "image/avif", "/media/hero/0123456789abcdef/wide-960.avif"),
};
var injection = HeroPictureRenderer.Render(Hero(Image(null, variants)));
(injection.Region + injection.Preload).Should().NotContain("avif");
}
[Fact]
public void ASlideThatIsUnusable_OrNoTitle_RendersNothing()
{
HeroPictureRenderer.Render(new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))])).IsEmpty.Should().BeTrue();
HeroPictureRenderer.Render(Hero(title: " ")).IsEmpty.Should().BeTrue();
HeroPictureRenderer.Render(null).IsEmpty.Should().BeTrue();
}
[Fact]
public void ThePictureIsTheFirstUsableSlide_NotJustTheFirst()
{
var hero = new PublicHeroDto("slideshow", "Titel", null, null, [], [
new PublicSlideDto("a", "", false, Image()),
new PublicSlideDto("b", "Tweede", false, Image()),
]);
HeroPictureRenderer.Render(hero).Region.Should().Contain("alt=\"Tweede\"");
}
[Fact]
public void NoTextOfTheCmsCanBreakOutOfTheHtml()
{
var hero = Hero(Image(), "<script>alert(1)</script>\n\"x\" & 'y'", "<img src=x onerror=alert(1)>", new HeroButtonDto("<b>", "/ok?a=1&b=\"2\""));
var html = HeroPictureRenderer.Render(hero).Region;
html.Should().NotContain("<script").And.NotContain("<img src=x").And.NotContain("<b>");
html.Should().Contain("&lt;script&gt;alert(1)&lt;/script&gt;<br>&quot;x&quot; &amp; &#39;y&#39;");
}
[Fact]
public void ARendererNeverThrows_OnArbitraryText()
=> Prop.ForAll(
Gen.Elements("a", "<", ">", "&", "\"", "'", "\n", " ", "é", "\\", "/", "#").ArrayOf().Select(p => string.Concat(p)).ToArbitrary(),
text =>
{
var hero = new PublicHeroDto("single", "T" + text, text, text, [new HeroButtonDto(text, "/" + text)],
[new PublicSlideDto("id", "a" + text, false, new PublicImageDto(10, 10, new FocusDto(0.5, 0.5, 1), "/media/x/" + text.Replace("\\", string.Empty), []))]);
var html = HeroPictureRenderer.Render(hero).Region;
html.Should().NotContain("<script");
return true;
}).Check(Config.QuickThrowOnFailure.WithMaxTest(1000));
}
public class HeroHtmlTransformTests
{
private static readonly HtmlTransformContext Context = new("https://test.andreaslop.nl");
private const string Page =
"<head><!--CMS:HERO-PRELOAD-->baked preload<!--/CMS:HERO-PRELOAD--></head>" +
"<body><!--CMS:HERO-HTML-->baked hero<!--/CMS:HERO-HTML--><p>rest</p></body>";
private static PublicHeroDto Hero() => new("single", "Andrea Slop", null, null, [],
[new PublicSlideDto("s1", "Een foto", false, new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-1600.webp",
[new HeroVariantDto("wide", 1600, 1067, "image/webp", "/media/hero/0123456789abcdef/wide-1600.webp")]))]);
private static HeroHtmlTransform Sut(PublicHeroDto? hero)
{
var model = Substitute.For<IHeroPageModel>();
model.GetAsync(default).ReturnsForAnyArgs(hero);
return new HeroHtmlTransform(model);
}
[Theory]
[InlineData("<p>nothing</p>", false)]
[InlineData("<!--CMS:HERO-HTML--><!--/CMS:HERO-HTML-->", true)]
[InlineData("<!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD-->", true)]
[InlineData("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->", false)]
public void Wants_OnlyPagesWithHeroMarkers(string html, bool expected) => Sut(null).Wants(html).Should().Be(expected);
[Fact]
public async Task TheRegions_AreFilled_AndTheMarkersStay()
{
var html = await Sut(Hero()).ApplyAsync(Page, Context, default);
html.Should().Contain("<!--CMS:HERO-HTML--><picture class=\"cms-hero-picture\">").And.Contain("</h1><!--/CMS:HERO-HTML-->");
html.Should().Contain("<!--CMS:HERO-PRELOAD--><link rel=\"preload\" as=\"image\"").And.Contain("><!--/CMS:HERO-PRELOAD-->");
html.Should().NotContain("baked").And.Contain("<p>rest</p>");
}
[Fact]
public async Task WithoutAHero_ThePageStaysAsItWasBuilt()
{
(await Sut(null).ApplyAsync(Page, Context, default)).Should().Be(Page);
}
[Fact]
public async Task AHeroWithoutAUsableSlide_LeavesTheBakedHero()
{
var unusable = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]);
(await Sut(unusable).ApplyAsync(Page, Context, default)).Should().Be(Page);
}
[Fact]
public async Task APageWithOnlyOneMarker_OnlyGetsThatPart()
{
var html = await Sut(Hero()).ApplyAsync("<body><!--CMS:HERO-HTML-->x<!--/CMS:HERO-HTML--></body>", Context, default);
html.Should().Contain("cms-hero-picture").And.NotContain("preload");
}
}
public class HeroPageModelTests
{
private static PublicHeroDto Hero(string src = "/media/hero/0123456789abcdef/wide-1600.webp") => new("single", "Titel", null, null, [],
[new PublicSlideDto("s1", "Foto", false, new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), src,
[new HeroVariantDto("wide", 1600, 1067, "image/webp", src), new HeroVariantDto("tall", 640, 800, "image/webp", "/media/hero/0123456789abcdef/tall-640.webp")]))]);
private static (HeroPageModel Model, IMediaStore Store) Make(PublicHeroDto? hero)
{
var publicHero = Substitute.For<IPublicHeroService>();
publicHero.GetActiveAsync(default).ReturnsForAnyArgs(hero);
var store = Substitute.For<IMediaStore>();
store.ExistsAsync(default!, default).ReturnsForAnyArgs(true);
return (new HeroPageModel(publicHero, store, NullLogger<HeroPageModel>.Instance), store);
}
[Fact]
public async Task WhenTheFilesOfTheFirstSlideExist_TheHeroIsReturned()
{
var (model, _) = Make(Hero());
(await model.GetAsync(default)).Should().NotBeNull();
}
[Fact]
public async Task WhenAFileOfTheFirstSlideIsMissing_NothingIsReturned()
{
var (model, store) = Make(Hero());
store.ExistsAsync("hero/0123456789abcdef/tall-640.webp", default).Returns(false);
(await model.GetAsync(default)).Should().BeNull();
}
[Fact]
public async Task WithoutAHero_OrWithoutSlides_ThereIsNothingToCheck()
{
(await Make(null).Model.GetAsync(default)).Should().BeNull();
var textOnly = new PublicHeroDto("single", "Titel", null, null, [], []);
var (model, store) = Make(textOnly);
(await model.GetAsync(default)).Should().Be(textOnly);
await store.DidNotReceiveWithAnyArgs().ExistsAsync(default!, default);
}
[Fact]
public async Task TheDataContributor_WritesTheHero_OrNull()
{
var withHero = Make(Hero()).Model;
var without = Make(null).Model;
(await new HeroDataContributor(withHero).GetAsync(new HtmlTransformContext("https://x"), default)).Should().BeOfType<PublicHeroDto>();
(await new HeroDataContributor(without).GetAsync(new HtmlTransformContext("https://x"), default)).Should().BeNull();
new HeroDataContributor(withHero).Key.Should().Be("hero");
}
[Fact]
public async Task TheDataBlock_CarriesTheOfferingsFirst_ThenTheHero()
{
var offerings = Substitute.For<ICmsDataBlockContributor>();
offerings.Key.Returns("offerings");
offerings.Order.Returns(100);
offerings.GetAsync(default!, default).ReturnsForAnyArgs(new[] { 1 });
var block = new CmsDataBlockTransform([new HeroDataContributor(Make(Hero()).Model), offerings]);
var html = await block.ApplyAsync("<!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{}</script>", new HtmlTransformContext("https://x"), default);
var json = html["<script type=\"application/json\" id=\"cms-data\">".Length..^"</script>".Length];
using var doc = JsonDocument.Parse(json);
doc.RootElement.EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl", "offerings", "hero");
doc.RootElement.GetProperty("hero").GetProperty("slides")[0].GetProperty("image").GetProperty("variants").GetArrayLength().Should().Be(2);
}
}
@@ -0,0 +1,368 @@
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using FsCheck;
using FsCheck.Fluent;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
public class HeroTextsTests
{
[Fact]
public void TheTextFiles_MatchTheDeclaredKeys_InDutchAndEnglish()
=> SlpModularCms.Core.Texts.TextAudit.Verify(typeof(HeroTexts).Assembly, SlpModularCms.Core.Texts.TextAudit.KeysOf(typeof(HeroTexts)), "nl", "en").Should().BeEmpty();
}
public class ButtonTargetValidatorTests
{
[Theory]
[InlineData("#contact", ButtonTargetKind.Anchor)]
[InlineData("#pakketten", ButtonTargetKind.Anchor)]
[InlineData("#a-b_c9", ButtonTargetKind.Anchor)]
[InlineData("/over-mij", ButtonTargetKind.Path)]
[InlineData("/", ButtonTargetKind.Path)]
[InlineData("/a/b/c?x=1&y=2", ButtonTargetKind.Path)]
[InlineData("https://andreaslop.nl/portfolio", ButtonTargetKind.Https)]
[InlineData("https://sub.example.com:8443/p?q=1#top", ButtonTargetKind.Https)]
public void AcceptedTargets(string target, ButtonTargetKind kind) => ButtonTargetValidator.Validate(target).Should().Be(kind);
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("contact")]
[InlineData("#")]
[InlineData("#1abc")]
[InlineData("# contact")]
[InlineData("#a b")]
[InlineData("//evil.example/x")]
[InlineData("/\\evil.example")]
[InlineData("/a b")]
[InlineData("http://example.com")]
[InlineData("HTTPS://example.com")]
[InlineData("https://user:pw@example.com")]
[InlineData("https://")]
[InlineData("https:///path")]
[InlineData("javascript:alert(1)")]
[InlineData("JaVaScRiPt:alert(1)")]
[InlineData("data:text/html;base64,AAAA")]
[InlineData("mailto:a@b.nl")]
[InlineData("tel:+31612345678")]
[InlineData("ftp://example.com/x")]
[InlineData("/ok\"onmouseover=\"x")]
[InlineData("/ok<script>")]
[InlineData(" /leading-space")]
[InlineData("/trailing-space ")]
[InlineData("/tab\tx")]
[InlineData("/new\nline")]
public void RejectedTargets(string target) => ButtonTargetValidator.Validate(target).Should().Be(ButtonTargetKind.Invalid);
[Fact]
public void Null_IsInvalid() => ButtonTargetValidator.Validate(null).Should().Be(ButtonTargetKind.Invalid);
[Fact]
public void WhateverIsAccepted_IsOneOfTheThreeSafeForms()
=> Prop.ForAll(Gen.Elements("a", "#", "/", ":", "\\", " ", "\"", "<", ">", "h", "t", "p", "s", "x", ".", "-", "1", "\n", "\t", "%").ArrayOf().Select(p => string.Concat(p)).ToArbitrary(), text =>
{
var kind = ButtonTargetValidator.Validate(text);
if (kind != ButtonTargetKind.Invalid)
{
text.Should().NotMatchRegex("[\\s\\\\\"<>`]");
(text.StartsWith('#') || (text.StartsWith('/') && !text.StartsWith("//")) || text.StartsWith("https://")).Should().BeTrue();
}
return true;
}).Check(Config.QuickThrowOnFailure.WithMaxTest(3000));
}
public class HeroSaveRequestValidationTests
{
private static HeroSaveRequest Valid() => new(
"Websites,\napps\n& meer.", null, "Een lead.", "single",
[new HeroButtonRequest("Bekijk pakketten", "#pakketten"), new HeroButtonRequest("Plan een gesprek", "#contact")],
[]);
private static List<ValidationResult> Run(HeroSaveRequest request)
{
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, validateAllProperties: true);
return results;
}
private static string[] Members(List<ValidationResult> results) => results.SelectMany(r => r.MemberNames).ToArray();
[Fact]
public void AValidHero_HasNoErrors() => Run(Valid()).Should().BeEmpty();
[Fact]
public void TheTitle_IsRequired_AndBounded()
{
Members(Run(Valid() with { Title = " " })).Should().Contain("Title");
Members(Run(Valid() with { Title = new string('x', 201) })).Should().Contain("Title");
Members(Run(Valid() with { Title = "a\nb\nc\nd\ne\nf" })).Should().Contain("Title");
Run(Valid() with { Title = "a\nb\nc\nd\ne" }).Should().BeEmpty();
}
[Fact]
public void TheOtherTexts_AreBounded()
{
Members(Run(Valid() with { Tagline = new string('t', 121) })).Should().Contain("Tagline");
Members(Run(Valid() with { Lead = new string('l', 601) })).Should().Contain("Lead");
}
[Theory]
[InlineData("single", true)]
[InlineData("slideshow", true)]
[InlineData(null, true)]
[InlineData("carousel", false)]
[InlineData("SINGLE", false)]
public void TheDisplay_IsOneOfTheTwo(string? display, bool valid)
=> Run(Valid() with { Display = display }).Any(r => r.MemberNames.Contains("Display")).Should().Be(!valid);
[Fact]
public void Buttons_AreLimitedAndChecked()
{
var three = new[] { new HeroButtonRequest("a", "#a"), new HeroButtonRequest("b", "#b"), new HeroButtonRequest("c", "#c") };
Members(Run(Valid() with { Buttons = three })).Should().Contain("Buttons");
Members(Run(Valid() with { Buttons = [new HeroButtonRequest("Lees meer", null)] })).Should().Contain("Buttons[0]");
Members(Run(Valid() with { Buttons = [new HeroButtonRequest(null, "/x")] })).Should().Contain("Buttons[0]");
Members(Run(Valid() with { Buttons = [new HeroButtonRequest("x", "javascript:alert(1)")] })).Should().Contain("Buttons[0].Target");
Members(Run(Valid() with { Buttons = [new HeroButtonRequest(new string('l', 51), "/x")] })).Should().Contain("Buttons[0].Label");
Run(Valid() with { Buttons = [new HeroButtonRequest("", ""), new HeroButtonRequest(null, null)] }).Should().BeEmpty("empty buttons are ignored");
}
[Fact]
public void ASlide_NeedsAnImage_AndAnAltTextOrTheDecorativeFlag()
{
var image = Guid.NewGuid();
Members(Run(Valid() with { Slides = [new HeroSlideRequest(null, Guid.Empty, "alt")] })).Should().Contain("Slides[0].ImageId");
Members(Run(Valid() with { Slides = [new HeroSlideRequest(null, image, " ")] })).Should().Contain("Slides[0].Alt");
Members(Run(Valid() with { Slides = [new HeroSlideRequest(null, image, new string('a', 301))] })).Should().Contain("Slides[0].Alt");
Run(Valid() with { Slides = [new HeroSlideRequest(null, image, null, Decorative: true)] }).Should().BeEmpty();
Run(Valid() with { Slides = [new HeroSlideRequest(null, image, "Een foto")] }).Should().BeEmpty();
}
[Fact]
public void ThereIsNoAdministratorLimitOnTheNumberOfSlides()
{
var slides = Enumerable.Range(0, 120).Select(_ => new HeroSlideRequest(null, Guid.NewGuid(), "foto")).ToList();
Run(Valid() with { Slides = slides }).Should().BeEmpty();
}
[Fact]
public void FocusValues_AreChecked()
{
var ok = new FocusDto(0.5, 0.5, 1);
void Check(HeroFocusRequest r, bool valid)
{
var results = new List<ValidationResult>();
Validator.TryValidateObject(r, new ValidationContext(r), results, true);
results.Any().Should().Be(!valid);
}
Check(new HeroFocusRequest(ok), true);
Check(new HeroFocusRequest(ok, new FocusDto(0.2, 0.9, 2.5), new FocusDto(1, 0, 4)), true);
Check(new HeroFocusRequest(new FocusDto(-0.1, 0.5, 1)), false);
Check(new HeroFocusRequest(new FocusDto(0.5, 1.1, 1)), false);
Check(new HeroFocusRequest(new FocusDto(0.5, 0.5, 0.5)), false);
Check(new HeroFocusRequest(new FocusDto(0.5, 0.5, 4.5)), false);
Check(new HeroFocusRequest(ok, new FocusDto(double.NaN, 0.5, 1)), false);
}
}
public class HeroTextCleanTests
{
[Fact]
public void LineBreaksAreNormalised_AndControlCharactersRemoved()
{
HeroText.Clean("a\r\nb\rc\u0007d", keepLineBreaks: true).Should().Be("a\nb\ncd");
HeroText.Clean("a\nb", keepLineBreaks: false).Should().Be("a b");
HeroText.Clean(" x ", keepLineBreaks: false).Should().Be("x");
HeroText.Clean(null, keepLineBreaks: true).Should().BeEmpty();
HeroText.Clean("a\u2028b\u2029c", keepLineBreaks: true).Should().Be("abc");
}
[Fact]
public void HtmlStaysAsTypedText()
=> HeroText.Clean("<b>Hallo</b> & \"dag\"", keepLineBreaks: false).Should().Be("<b>Hallo</b> & \"dag\"");
[Fact]
public void CleaningIsIdempotent()
=> Prop.ForAll(Gen.Elements("a", " ", "\n", "\r", "\t", "<", "&", "\u0007", "\u2028", "é", "x").ArrayOf().Select(p => string.Concat(p)).ToArbitrary(), text =>
{
var once = HeroText.Clean(text, keepLineBreaks: true);
HeroText.Clean(once, keepLineBreaks: true).Should().Be(once);
return true;
}).Check(Config.QuickThrowOnFailure.WithMaxTest(1500));
}
public class HeroMappingTests
{
private static readonly DateTimeOffset Now = new(2026, 11, 16, 12, 0, 0, TimeSpan.Zero);
private static HeroImage Image(Guid? id = null, string set = "0123456789abcdef", string? wideFocus = null)
{
var image = new HeroImage
{
Id = id ?? Guid.NewGuid(),
SetHash = set,
Width = 1600,
Height = 1067,
Variants =
[
new HeroVariant("wide", 960, 640, "image/webp", "wide-960.webp", 10),
new HeroVariant("wide", 1600, 1067, "image/webp", "wide-1600.webp", 20),
new HeroVariant("wide", 1600, 1067, "image/avif", "wide-1600.avif", 15),
new HeroVariant("tall", 640, 800, "image/webp", "tall-640.webp", 8),
],
};
if (wideFocus is not null)
{
image.WideFocusX = 0.2;
image.WideFocusY = 0.3;
image.WideZoom = 2;
}
return image;
}
private static HeroSlide Slide(int order, bool active = true, string alt = "Een foto", bool decorative = false, HeroImage? image = null)
=> new() { Id = Guid.NewGuid(), SortOrder = order, IsActive = active, Alt = alt, Decorative = decorative, Image = image ?? Image(), ImageId = (image ?? Image()).Id };
private static HeroContent Hero(params HeroSlide[] slides) => new()
{
Id = Guid.NewGuid(),
Title = "Andrea Slop",
Tagline = "Lifestyle fotograaf",
Display = "slideshow",
Button1Label = "Lees meer",
Button1Target = "/over-mij",
Slides = [.. slides],
};
[Fact]
public void ALiveHero_IsPublished_WithItsSlidesInOrder()
{
var hero = Hero(Slide(2, alt: "derde"), Slide(0, alt: "eerste"), Slide(1, alt: "tweede"));
var pub = HeroMapping.ToPublic(hero, Now, 50)!;
pub.Slides.Select(s => s.Alt).Should().Equal("eerste", "tweede", "derde");
pub.Buttons.Should().ContainSingle().Which.Should().Be(new HeroButtonDto("Lees meer", "/over-mij"));
pub.Display.Should().Be("slideshow");
}
[Fact]
public void TheSrc_IsTheLargestWideWebP_AndAllVariantsAreListedWithMediaAddresses()
{
var pub = HeroMapping.ToPublic(Hero(Slide(0)), Now, 50)!;
var image = pub.Slides[0].Image;
image.Src.Should().Be("/media/hero/0123456789abcdef/wide-1600.webp");
image.Variants.Should().HaveCount(4);
image.Variants.Should().OnlyContain(v => v.Url.StartsWith("/media/hero/0123456789abcdef/"));
image.Width.Should().Be(1600);
}
[Fact]
public void WhenNothingIsWide_TheSrcFallsBackToTheLargestWebP()
{
var image = Image();
image.Variants = [new HeroVariant("tall", 640, 800, "image/webp", "tall-640.webp", 8), new HeroVariant("tall", 960, 1200, "image/webp", "tall-960.webp", 9)];
HeroMapping.SrcFor(image).Should().EndWith("tall-960.webp");
}
[Fact]
public void ADecorativeSlide_HasNoAlt_AndAnInactiveOrEmptyOneIsLeftOut()
{
var hero = Hero(
Slide(0, decorative: true, alt: "ignored"),
Slide(1, active: false),
Slide(2, alt: " "),
new HeroSlide { Id = Guid.NewGuid(), SortOrder = 3, Alt = "x", Image = new HeroImage { SetHash = "0123456789abcdef" }, ImageId = Guid.NewGuid() });
var pub = HeroMapping.ToPublic(hero, Now, 50)!;
pub.Slides.Should().ContainSingle().Which.Should().Match<PublicSlideDto>(s => s.Decorative && s.Alt == "");
}
[Fact]
public void ASafetyCeiling_LimitsThePublishedSlides_NotTheSavedOnes()
{
var hero = Hero(Enumerable.Range(0, 80).Select(i => Slide(i)).ToArray());
HeroMapping.ToPublic(hero, Now, 50)!.Slides.Should().HaveCount(50);
HeroMapping.ToPublic(hero, Now, 50)!.Slides.Select(s => s.Id).Should().Equal(hero.Slides.OrderBy(s => s.SortOrder).Take(50).Select(s => s.Id.ToString()));
hero.Slides.Should().HaveCount(80);
}
[Fact]
public void TheHeroIsAlwaysOn_ItIsLiveAsSoonAsItHasATitle()
{
var hero = Hero(Slide(0));
(HeroMapping.ToPublic(hero, Now, 50) is not null).Should().BeTrue();
HeroMapping.ToAdmin(hero, Now, 50).IsLive.Should().BeTrue();
}
[Fact]
public void AHeroWithoutATitle_IsNotPublished()
{
var hero = Hero(Slide(0));
hero.Title = " ";
HeroMapping.ToPublic(hero, Now, 50).Should().BeNull();
}
[Fact]
public void AHeroWithoutSlides_IsPublished_ForSitesThatShowOnlyText()
{
var hero = Hero();
hero.Button1Label = "Bekijk pakketten";
hero.Button1Target = "#pakketten";
hero.Button2Label = "Plan een gesprek";
hero.Button2Target = "#contact";
var pub = HeroMapping.ToPublic(hero, Now, 50)!;
pub.Slides.Should().BeEmpty();
pub.Buttons.Should().HaveCount(2);
}
[Fact]
public void FocusPerRatio_IsOnlyInTheDataWhenSet()
{
var plain = HeroMapping.ToPublic(Hero(Slide(0)), Now, 50)!.Slides[0].Image;
var withWide = HeroMapping.ToPublic(Hero(Slide(0, image: Image(wideFocus: "x"))), Now, 50)!.Slides[0].Image;
plain.FocusByRatio.Should().BeNull();
plain.Focus.Should().Be(new FocusDto(0.5, 0.5, 1));
withWide.FocusByRatio!["wide"].Should().Be(new FocusDto(0.2, 0.3, 2));
withWide.FocusByRatio.Should().NotContainKey("tall");
}
[Fact]
public void TheJson_DoesNotCarryTheScheduleOrSwitches_AndOmitsAnAbsentFocusByRatio()
{
var json = System.Text.Json.JsonSerializer.Serialize(HeroMapping.ToPublic(Hero(Slide(0)), Now, 50), new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web));
json.Should().NotContain("startsAt").And.NotContain("endsAt").And.NotContain("isActive").And.NotContain("focusByRatio");
json.Should().Contain("\"title\":\"Andrea Slop\"").And.Contain("\"lead\":null");
}
[Fact]
public void TheAdminView_ListsInactiveSlidesAndTheCeiling()
{
var hero = Hero(Slide(0), Slide(1, active: false));
var admin = HeroMapping.ToAdmin(hero, Now, 50);
admin.Slides.Should().HaveCount(2);
admin.MaxPublishedSlides.Should().Be(50);
HeroMapping.ToAdmin(null, Now, 50).Should().Match<HeroAdminDto>(d => d.Id == null && !d.IsLive && d.Slides.Count == 0 && d.Display == "single");
}
}
@@ -0,0 +1,54 @@
using System.Diagnostics;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Models;
using Xunit.Abstractions;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// A measurement, not a check: how long uploading and saving a hero with real photos takes. Runs only when
/// HERO_TIMING_DIR points to a folder with hero-1.jpg .. hero-8.jpg (the ASF exports); otherwise it passes at once.
/// </summary>
public class HeroSaveTimingTests(ITestOutputHelper output)
{
[Fact]
public async Task MeasureUploadAndSave_WithRealPhotos()
{
var dir = Environment.GetEnvironmentVariable("HERO_TIMING_DIR");
if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
{
return;
}
using var rig = new HeroTestRig();
var ids = new List<Guid>();
var watch = Stopwatch.StartNew();
for (var i = 1; i <= 8; i++)
{
watch.Restart();
var image = await rig.UploadWithoutRendering(await File.ReadAllBytesAsync(Path.Combine(dir, $"hero-{i}.jpg")));
ids.Add(Guid.Parse(image.Id));
output.WriteLine($"upload request hero-{i}.jpg: {watch.ElapsedMilliseconds} ms, {image.Width}x{image.Height}");
}
var slides = ids.Select(id => new HeroSlideRequest(null, id, "foto")).ToList();
var focus = ids.Select(id => new HeroImageFocusRequest(id, new FocusDto(0.3, 0.4, 1), null, null)).ToList();
watch.Restart();
await rig.Service.SaveAsync(new HeroSaveRequest("Andrea Slop", null, null, "slideshow", [], slides, focus), HeroTestRig.Admin, default);
output.WriteLine($"save request with 8 slides and 8 changed focus points: {watch.ElapsedMilliseconds} ms");
watch.Restart();
var cut = await rig.RenderPending();
output.WriteLine($"background renderer: {cut} images in {watch.ElapsedMilliseconds} ms ({watch.ElapsedMilliseconds / Math.Max(1, cut)} ms per image)");
watch.Restart();
var again = focus.Select(f => f with { Focus = new FocusDto(0.6, 0.4, 1) }).ToList();
await rig.Service.SaveAsync(new HeroSaveRequest("Andrea Slop", null, null, "slideshow", [], slides, again), HeroTestRig.Admin, default);
output.WriteLine($"save request with 8 changed focus points again: {watch.ElapsedMilliseconds} ms");
await rig.RenderPending();
watch.Restart();
await rig.Service.SaveAsync(new HeroSaveRequest("Andrea Slop", null, null, "slideshow", [], slides, null), HeroTestRig.Admin, default);
output.WriteLine($"save without focus changes: {watch.ElapsedMilliseconds} ms");
}
}
@@ -0,0 +1,458 @@
using FluentAssertions;
using NSubstitute;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
public class HeroImageServiceTests
{
[Fact]
public async Task AnUpload_IsProcessedIntoVariantsOfBothRatios_AndStored()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
(image.Width, image.Height).Should().Be((1600, 1067));
image.Variants.Select(v => v.Ratio).Distinct().Should().BeEquivalentTo("wide", "tall");
image.Variants.Should().Contain(v => v.Type == "image/webp" && v.Ratio == "wide" && v.Width == 1600);
image.Variants.Should().OnlyContain(v => v.Width <= 1600, "an image is never scaled up");
image.Src.Should().Be(image.Variants.Single(v => v.Ratio == "wide" && v.Width == 1600 && v.Type == "image/webp").Url);
foreach (var variant in image.Variants)
{
var key = variant.Url["/media/".Length..];
(await rig.Store.ExistsAsync(key, CancellationToken.None)).Should().BeTrue(key);
}
}
[Fact]
public async Task TheOriginal_IsKeptWithoutMetadata_AndNotPublic()
{
using var rig = new HeroTestRig();
await rig.Upload(HeroTestRig.Photo());
var stored = (await rig.Db.HeroImages.FindAsync(Guid.Parse((await rig.Repository.GetImageBySha256Async(rig.Db.HeroImages.Single().OriginalSha256, default))!.Id.ToString())))!;
stored.OriginalKey.Should().StartWith("originals/");
await using var file = (await rig.Store.OpenReadAsync(stored.OriginalKey, CancellationToken.None))!.Content;
var bytes = new MemoryStream();
await file.CopyToAsync(bytes);
using var back = new ImageMagick.MagickImage();
back.Ping(bytes.ToArray());
back.GetExifProfile().Should().BeNull("the camera tag was removed from the stored original");
SlpModularCms.Core.Media.MediaPaths.TryParse(stored.OriginalKey, out _).Should().BeFalse("originals are never an address");
}
[Fact]
public async Task TheSamePhotoTwice_IsOneImage()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo();
var first = await rig.Upload(photo);
var second = await rig.Upload(photo);
second.Id.Should().Be(first.Id);
rig.Db.HeroImages.Count().Should().Be(1);
}
[Fact]
public async Task TheSamePhotoAgain_RepairsLostFiles()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo();
var first = await rig.Upload(photo);
System.IO.Directory.Delete(rig.Directory, recursive: true);
var again = await rig.Upload(photo);
again.Id.Should().Be(first.Id);
rig.Db.HeroImages.Count().Should().Be(1);
foreach (var variant in again.Variants)
{
(await rig.Store.ExistsAsync(variant.Url["/media/".Length..], CancellationToken.None)).Should().BeTrue(variant.Url);
}
}
[Fact]
public async Task ADefaultFocus_IsTheCentre_ForEveryRatio()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
image.Focus.Should().Be(new FocusDto(0.5, 0.5, 1));
image.WideFocus.Should().BeNull();
image.TallFocus.Should().BeNull();
}
[Fact]
public async Task ChangingTheFocus_CutsNewVariants_AtNewAddresses_AndRetiresTheOldSet()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var oldSet = rig.Db.HeroImages.Single().SetHash;
var changed = await rig.SetFocus(Guid.Parse(image.Id), new HeroFocusRequest(new FocusDto(0.5, 0.5, 1), null, new FocusDto(1.0, 0.5, 1)));
var newSet = rig.Db.HeroImages.Single().SetHash;
newSet.Should().NotBe(oldSet);
changed.Variants.Should().OnlyContain(v => v.Url.Contains(newSet));
changed.TallFocus.Should().Be(new FocusDto(1.0, 0.5, 1));
changed.WideFocus.Should().BeNull();
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Select(s => s.SetHash).Should().BeEquivalentTo(oldSet, newSet);
rig.Db.HeroRetiredFiles.Should().ContainSingle(f => f.Kind == "set" && f.Key == oldSet);
rig.IndexCache.Received().Invalidate();
// The tall variant now shows the right (blue) side of the photo.
var tall = changed.Variants.First(v => v.Ratio == "tall" && v.Type == "image/webp");
await using var file = (await rig.Store.OpenReadAsync(tall.Url["/media/".Length..], CancellationToken.None))!.Content;
var bytes = new MemoryStream();
await file.CopyToAsync(bytes);
using var pixels = new ImageMagick.MagickImage(bytes.ToArray());
pixels.GetPixels().GetPixel((int)(pixels.Width / 2), (int)(pixels.Height / 2)).ToColor()!.ToString().Should().StartWith("#0000FF");
}
[Fact]
public async Task TheSameFocus_DoesNotMakeANewSet()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
await rig.SetFocus(Guid.Parse(image.Id), new HeroFocusRequest(new FocusDto(0.5, 0.5, 1)));
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().HaveCount(1);
rig.Db.HeroRetiredFiles.Should().BeEmpty();
}
[Fact]
public async Task ResettingARatioToTheGeneralPoint_ReturnsToTheSameSet()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var original = rig.Db.HeroImages.Single().SetHash;
await rig.SetFocus(Guid.Parse(image.Id), new HeroFocusRequest(new FocusDto(0.5, 0.5, 1), null, new FocusDto(0.9, 0.5, 1)));
await rig.SetFocus(Guid.Parse(image.Id), new HeroFocusRequest(new FocusDto(0.5, 0.5, 1)));
rig.Db.HeroImages.Single().SetHash.Should().Be(original);
}
[Fact]
public async Task ADeletedImage_IsRefused_WhileASlideUsesIt_AndRetiredOtherwise()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
await rig.Service.SaveAsync(new HeroSaveRequest("Titel", null, null, "single", [], [new HeroSlideRequest(null, Guid.Parse(image.Id), "foto")]), HeroTestRig.Admin, CancellationToken.None);
var inUse = async () => await rig.Images.DeleteAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None);
await inUse.Should().ThrowAsync<HeroImageInUseException>();
await rig.Service.SaveAsync(new HeroSaveRequest("Titel", null, null, "single", [], []), HeroTestRig.Admin, CancellationToken.None);
await rig.Images.DeleteAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None);
rig.Db.HeroImages.Should().BeEmpty();
rig.Db.HeroRetiredFiles.Select(f => f.Kind).Should().BeEquivalentTo("set", "original");
}
[Fact]
public async Task AnUnknownImage_IsNotFound()
{
using var rig = new HeroTestRig();
var focus = async () => await rig.SetFocus(Guid.NewGuid(), new HeroFocusRequest(new FocusDto(0.5, 0.5, 1)));
var delete = async () => await rig.Images.DeleteAsync(Guid.NewGuid(), HeroTestRig.Admin, CancellationToken.None);
await focus.Should().ThrowAsync<HeroSaveRejectedException>();
await delete.Should().ThrowAsync<HeroImageNotFoundException>();
}
[Theory]
[InlineData("<svg xmlns=\"http://www.w3.org/2000/svg\"><script>alert(1)</script></svg>", "unsupported_format")]
[InlineData("not an image at all", "unsupported_format")]
public async Task ARefusedUpload_StoresNothing(string text, string code)
{
using var rig = new HeroTestRig();
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
var act = async () => await rig.Upload(bytes);
(await act.Should().ThrowAsync<ImageRejectedException>()).Which.Code.Should().Be(code);
rig.Db.HeroImages.Should().BeEmpty();
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
[Fact]
public async Task ATruncatedPhoto_StoresNothing()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo();
var act = async () => await rig.Upload(photo.AsMemory(0, photo.Length / 4).ToArray());
await act.Should().ThrowAsync<ImageRejectedException>();
rig.Db.HeroImages.Should().BeEmpty();
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
[Fact]
public async Task WhenTheMediaStorageIsFull_TheUploadIsRefused_AndNothingIsStored()
{
using var rig = new HeroTestRig();
rig.Media.MaxTotalBytes = 1000;
var act = async () => await rig.Upload(HeroTestRig.Photo());
await act.Should().ThrowAsync<MediaStorageFullException>();
rig.Db.HeroImages.Should().BeEmpty();
}
[Fact]
public async Task APhoto_ThatIsTallerThanItIsWide_GetsSensibleVariants()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo(width: 800, height: 1200, seed: 1));
image.Variants.Where(v => v.Ratio == "wide").Should().OnlyContain(v => v.Width <= 800);
image.Variants.Where(v => v.Ratio == "tall").Should().OnlyContain(v => v.Width <= 800);
image.Variants.Should().NotBeEmpty();
}
}
public class HeroServiceTests
{
private static HeroSaveRequest Request(IReadOnlyList<HeroSlideRequest>? slides = null, string title = "Andrea Slop") => new(
title, "Lifestyle fotograaf", null, "slideshow", [new HeroButtonRequest("Lees meer", "/over-mij")], slides ?? []);
[Fact]
public async Task TheFirstSave_CreatesTheHero_WithItsSlidesInOrder()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
var saved = await rig.Service.SaveAsync(Request([new HeroSlideRequest(null, Guid.Parse(b.Id), "tweede foto"), new HeroSlideRequest(null, Guid.Parse(a.Id), "eerste foto")]), HeroTestRig.Admin, CancellationToken.None);
saved.Id.Should().NotBeNull();
saved.Slides.Select(s => s.Alt).Should().Equal("tweede foto", "eerste foto");
saved.Slides.Select(s => s.SortOrder).Should().Equal(0, 1);
saved.IsLive.Should().BeTrue();
rig.IndexCache.Received().Invalidate();
}
[Fact]
public async Task ASecondSave_ReplacesTheWholeHero_KeepingTheIdsOfTheSlidesItKeeps()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
var first = await rig.Service.SaveAsync(Request([new HeroSlideRequest(null, Guid.Parse(a.Id), "een"), new HeroSlideRequest(null, Guid.Parse(b.Id), "twee")]), HeroTestRig.Admin, CancellationToken.None);
var keep = Guid.Parse(first.Slides[1].Id);
var second = await rig.Service.SaveAsync(Request([new HeroSlideRequest(keep, Guid.Parse(b.Id), "twee, aangepast")], "Nieuwe titel"), HeroTestRig.Admin, CancellationToken.None);
second.Id.Should().Be(first.Id);
second.Title.Should().Be("Nieuwe titel");
second.Slides.Should().ContainSingle().Which.Should().Match<HeroSlideAdminDto>(s => s.Id == keep.ToString() && s.Alt == "twee, aangepast" && s.SortOrder == 0);
rig.Db.HeroSlides.Count().Should().Be(1);
rig.Db.Heroes.Count().Should().Be(1);
}
[Fact]
public async Task ReorderingSlides_ChangesTheirOrder_NotTheirIds()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
var first = await rig.Service.SaveAsync(Request([new HeroSlideRequest(null, Guid.Parse(a.Id), "een"), new HeroSlideRequest(null, Guid.Parse(b.Id), "twee")]), HeroTestRig.Admin, CancellationToken.None);
var ids = first.Slides.Select(s => Guid.Parse(s.Id)).ToArray();
var second = await rig.Service.SaveAsync(Request([new HeroSlideRequest(ids[1], Guid.Parse(b.Id), "twee"), new HeroSlideRequest(ids[0], Guid.Parse(a.Id), "een")]), HeroTestRig.Admin, CancellationToken.None);
second.Slides.Select(s => s.Id).Should().Equal(ids[1].ToString(), ids[0].ToString());
}
[Fact]
public async Task ADecorativeSlide_StoresNoAlt_AndAnInactiveOneIsKept_ButNotPublished()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
await rig.Service.SaveAsync(Request([new HeroSlideRequest(null, Guid.Parse(a.Id), "wordt genegeerd", Decorative: true), new HeroSlideRequest(null, Guid.Parse(b.Id), "uit", IsActive: false)]), HeroTestRig.Admin, CancellationToken.None);
var admin = await rig.Service.GetAsync(CancellationToken.None);
var pub = (await rig.Public.GetActiveAsync(CancellationToken.None))!;
admin.Slides.Should().HaveCount(2);
admin.Slides[0].Alt.Should().BeEmpty();
pub.Slides.Should().ContainSingle().Which.Decorative.Should().BeTrue();
}
[Fact]
public async Task AnUnknownImage_OrASlideOfAnotherHero_IsRejected()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo());
var unknownImage = async () => await rig.Service.SaveAsync(Request([new HeroSlideRequest(null, Guid.NewGuid(), "x")]), HeroTestRig.Admin, CancellationToken.None);
var unknownSlide = async () => await rig.Service.SaveAsync(Request([new HeroSlideRequest(Guid.NewGuid(), Guid.Parse(a.Id), "x")]), HeroTestRig.Admin, CancellationToken.None);
(await unknownImage.Should().ThrowAsync<HeroSaveRejectedException>()).Which.Field.Should().Be("Slides[0].ImageId");
(await unknownSlide.Should().ThrowAsync<HeroSaveRejectedException>()).Which.Field.Should().Be("Slides[0].Id");
}
[Fact]
public async Task TheTexts_AreStoredAsPlainText_WithoutControlCharacters()
{
using var rig = new HeroTestRig();
var saved = await rig.Service.SaveAsync(
new HeroSaveRequest("Websites,\r\napps\u0007\n& meer.", " <b>tag</b> ", "lead\nregel twee", "single", [new HeroButtonRequest(" Knop ", " #contact ")], []),
HeroTestRig.Admin, CancellationToken.None);
saved.Title.Should().Be("Websites,\napps\n& meer.");
saved.Tagline.Should().Be("<b>tag</b>");
saved.Lead.Should().Be("lead\nregel twee");
saved.Buttons.Should().ContainSingle().Which.Should().Be(new HeroButtonDto("Knop", "#contact"));
}
[Fact]
public async Task ASiteWithoutImages_CanHaveATextOnlyHero_WithTwoButtons()
{
using var rig = new HeroTestRig();
await rig.Service.SaveAsync(new HeroSaveRequest("Websites,\napps\n& meer.", null, "Lead.", "single",
[new HeroButtonRequest("Bekijk pakketten", "#pakketten"), new HeroButtonRequest("Plan een gesprek", "#contact")], null), HeroTestRig.Admin, CancellationToken.None);
var pub = (await rig.Public.GetActiveAsync(CancellationToken.None))!;
pub.Title.Should().Be("Websites,\napps\n& meer.");
pub.Tagline.Should().BeNull();
pub.Buttons.Select(b => b.Target).Should().Equal("#pakketten", "#contact");
pub.Slides.Should().BeEmpty();
}
[Fact]
public async Task WithoutAHero_TheAdminGetsEmptyDefaults_AndThePublicGetsNothing()
{
using var rig = new HeroTestRig();
var admin = await rig.Service.GetAsync(CancellationToken.None);
admin.Id.Should().BeNull();
admin.IsLive.Should().BeFalse();
(await rig.Public.GetActiveAsync(CancellationToken.None)).Should().BeNull();
}
[Fact]
public async Task ManySlides_AreSaved_ButOnlyTheCeilingIsPublished()
{
using var rig = new HeroTestRig(maxPublishedSlides: 3);
var image = await rig.Upload(HeroTestRig.Photo());
var slides = Enumerable.Range(0, 7).Select(i => new HeroSlideRequest(null, Guid.Parse(image.Id), "foto " + i)).ToList();
var saved = await rig.Service.SaveAsync(Request(slides), HeroTestRig.Admin, CancellationToken.None);
saved.Slides.Should().HaveCount(7);
(await rig.Public.GetActiveAsync(CancellationToken.None))!.Slides.Should().HaveCount(3);
}
}
public class HeroMediaCleanupTests
{
private static async Task<(string Set, string Original)> RetireAnImage(HeroTestRig rig)
{
var image = await rig.Upload(HeroTestRig.Photo());
var row = rig.Db.HeroImages.Single();
var keys = (row.SetHash, row.OriginalKey);
await rig.Images.DeleteAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None);
return keys;
}
[Fact]
public async Task RetiredFiles_StayForTheRetentionTime_ThenGo()
{
using var rig = new HeroTestRig();
var (set, original) = await RetireAnImage(rig);
(await rig.Cleanup.RunOnceAsync(CancellationToken.None)).Should().Be(0);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().ContainSingle(s => s.SetHash == set);
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeTrue();
rig.Time.Now = rig.Time.Now.AddDays(8);
(await rig.Cleanup.RunOnceAsync(CancellationToken.None)).Should().Be(2);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeFalse();
rig.Db.HeroRetiredFiles.Should().BeEmpty();
}
[Fact]
public async Task ARetiredSet_ThatIsUsedAgain_IsNeverDeleted()
{
using var rig = new HeroTestRig();
var photo = HeroTestRig.Photo();
var first = await rig.Upload(photo);
var set = rig.Db.HeroImages.Single().SetHash;
await rig.Images.DeleteAsync(Guid.Parse(first.Id), HeroTestRig.Admin, CancellationToken.None);
// The same photo with the same focus comes back: the same set again, referenced by a new row.
var again = await rig.Upload(photo);
rig.Db.HeroImages.Single().SetHash.Should().Be(set);
await rig.Service.SaveAsync(new HeroSaveRequest("Titel", null, null, "single", [], [new HeroSlideRequest(null, Guid.Parse(again.Id), "foto")]), HeroTestRig.Admin, CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(8);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().ContainSingle(s => s.SetHash == set);
again.Variants.Should().OnlyContain(v => v.Url.Contains(set));
}
[Fact]
public async Task AnImageNobodyUsed_IsRetiredAndRemoved_AfterTheRetentionTime()
{
using var rig = new HeroTestRig();
await rig.Upload(HeroTestRig.Photo());
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().HaveCount(1);
rig.Time.Now = rig.Time.Now.AddDays(8);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().BeEmpty();
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
[Fact]
public async Task AnImageASlideUses_IsNeverRemoved()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
await rig.Service.SaveAsync(new HeroSaveRequest("Titel", null, null, "single", [], [new HeroSlideRequest(null, Guid.Parse(image.Id), "foto")]), HeroTestRig.Admin, CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(30);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().HaveCount(1);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().HaveCount(1);
}
[Fact]
public async Task ASetNoRowKnows_AndIsOld_IsRemoved_ButARecentOneIsNot()
{
using var rig = new HeroTestRig();
await rig.Store.PutSetAsync("hero", "aaaaaaaaaaaaaaaa", new Dictionary<string, byte[]> { ["wide-960.webp"] = [1] }, CancellationToken.None);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().HaveCount(1);
rig.Time.Now = DateTimeOffset.UtcNow.AddDays(30);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().BeEmpty();
}
}
@@ -0,0 +1,128 @@
using ImageMagick;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Magick;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Repositories;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
public sealed class TestTime(DateTimeOffset now) : TimeProvider
{
public DateTimeOffset Now { get; set; } = now;
public override DateTimeOffset GetUtcNow() => Now;
}
/// <summary>A hero module over an in-memory database, a temporary media directory and the real image library.</summary>
public sealed class HeroTestRig : IDisposable
{
public static readonly Guid Admin = Guid.Parse("11111111-2222-3333-4444-555555555555");
public TestTime Time { get; } = new(new DateTimeOffset(2026, 11, 16, 12, 0, 0, TimeSpan.Zero));
public string Directory { get; } = Path.Combine(Path.GetTempPath(), "slp-hero-tests-" + Guid.NewGuid().ToString("N"));
public MediaOptions Media { get; }
public HeroDbContext Db { get; }
public IHeroRepository Repository { get; }
public FileSystemMediaStore Store { get; }
public MagickImageProcessor Processor { get; }
public MediaStatus Status { get; } = new();
public IIndexHtmlCache IndexCache { get; } = Substitute.For<IIndexHtmlCache>();
public HeroRenderSignal Signal { get; } = new();
public HeroImageService Images { get; }
public HeroService Service { get; }
public PublicHeroService Public { get; }
public HeroMediaCleanup Cleanup { get; }
public HeroOptions HeroOptions { get; } = new();
public HeroTestRig(int maxPublishedSlides = 50)
{
HeroOptions.MaxPublishedSlides = maxPublishedSlides;
Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 };
var mediaOptions = Options.Create(Media);
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options);
Repository = new HeroRepository(Db);
Store = new FileSystemMediaStore(mediaOptions);
Processor = new MagickImageProcessor(mediaOptions, NullLogger<MagickImageProcessor>.Instance);
Status.SetProcessor(true, Processor.SupportedInput, Processor.SupportedOutput);
Status.SetDirectory(true);
var gate = new MediaProcessingGate(mediaOptions);
var guard = new MediaUploadGuard(Processor, gate, Status, mediaOptions);
Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger<HeroImageService>.Instance, Time, IndexCache, Signal);
Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache);
Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time);
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time);
}
/// <summary>A photo-like fixture: the left half red, the right half blue, with a camera tag.</summary>
public static byte[] Photo(int width = 1600, int height = 1067, MagickFormat format = MagickFormat.Jpeg, byte seed = 0)
{
using var image = new MagickImage(MagickColors.Red, (uint)width, (uint)height);
using var blue = new MagickImage(MagickColors.Blue, (uint)(width / 2), (uint)height);
image.Composite(blue, width - width / 2, 0, CompositeOperator.Over);
if (seed > 0)
{
using var dot = new MagickImage(new MagickColor((byte)(seed * 20), 0, 0), 10, 10);
image.Composite(dot, 3, 3, CompositeOperator.Over);
}
var exif = new ExifProfile();
exif.SetValue(ExifTag.Software, "camera firmware");
image.SetProfile(exif);
using var stream = new MemoryStream();
image.Write(stream, format);
return stream.ToArray();
}
/// <summary>Changes the focus of an image the way the admin does: with the next save of the hero (the slides of the hero are left empty here).</summary>
public async Task<SlpModularCms.Modules.Hero.Models.HeroImageDto> SetFocus(Guid imageId, SlpModularCms.Modules.Hero.Models.HeroFocusRequest request)
{
await Service.SaveAsync(
new SlpModularCms.Modules.Hero.Models.HeroSaveRequest("Titel", null, null, "single", [], [],
[new SlpModularCms.Modules.Hero.Models.HeroImageFocusRequest(imageId, request.Focus, request.Wide, request.Tall)]),
Admin, CancellationToken.None);
await RenderPending();
return SlpModularCms.Modules.Hero.Services.HeroMapping.ToImageDto((await Repository.GetImageAsync(imageId, CancellationToken.None))!);
}
/// <summary>Uploads an image and lets the background renderer cut its variants (here: at once, the way the worker would).</summary>
public async Task<SlpModularCms.Modules.Hero.Models.HeroImageDto> Upload(byte[] bytes)
{
var dto = await UploadWithoutRendering(bytes);
await RenderPending();
return SlpModularCms.Modules.Hero.Services.HeroMapping.ToImageDto((await Repository.GetImageAsync(Guid.Parse(dto.Id), CancellationToken.None))!);
}
/// <summary>The upload request as such: the image is stored and its variants are still to be cut.</summary>
public Task<SlpModularCms.Modules.Hero.Models.HeroImageDto> UploadWithoutRendering(byte[] bytes)
=> Images.UploadAsync(new MemoryStream(bytes), bytes.Length, Admin, CancellationToken.None);
/// <summary>Does the work of the background renderer until nothing is pending.</summary>
public async Task<int> RenderPending()
{
var count = 0;
while (await Images.RenderNextPendingAsync(new HashSet<Guid>(), CancellationToken.None) is not null)
{
count++;
if (count > 100)
{
throw new InvalidOperationException("The renderer does not settle.");
}
}
return count;
}
public void Dispose()
{
Db.Dispose();
if (System.IO.Directory.Exists(Directory))
{
System.IO.Directory.Delete(Directory, recursive: true);
}
}
}
@@ -0,0 +1,131 @@
using FluentAssertions;
using NSubstitute;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// A hero without slides (SLP Software): when the site asks for it (<c>Hero:TextOnlyRegion</c>) the host writes the
/// title, the lead and the buttons into the first HTML; otherwise (ASF) the baked page stays.
/// </summary>
public class HeroTextOnlyTests
{
private static PublicHeroDto TextOnly(string title = "Websites,\napps\n& meer.", string? tagline = null, string? lead = "Een lead.", params HeroButtonDto[] buttons)
=> new("single", title, tagline, lead, buttons, []);
[Fact]
public void ADisplayedHeroWithoutSlides_IsATextOnlyRegion_WithNothingToPreload()
{
var hero = TextOnly(buttons: [new HeroButtonDto("Bekijk pakketten", "#pakketten"), new HeroButtonDto("Plan een gesprek", "#contact")]);
var injection = HeroPictureRenderer.Render(hero, textOnlyRegion: true);
injection.Region.Should().Be(
"<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-lines\">Websites,<br>apps<br>&amp; meer.</span></h1>" +
"<p class=\"cms-hero-lead\">Een lead.</p>" +
"<p class=\"cms-hero-buttons\"><a class=\"cms-hero-button\" href=\"#pakketten\">Bekijk pakketten</a><a class=\"cms-hero-button\" href=\"#contact\">Plan een gesprek</a></p>");
injection.Preload.Should().BeEmpty();
injection.IsEmpty.Should().BeFalse();
}
[Fact]
public void WithoutTheSiteAskingForIt_AHeroWithoutSlides_LeavesTheBakedHero()
{
var hero = TextOnly(buttons: [new HeroButtonDto("Knop", "#x")]);
HeroPictureRenderer.Render(hero).IsEmpty.Should().BeTrue();
HeroPictureRenderer.Render(hero, textOnlyRegion: false).IsEmpty.Should().BeTrue();
}
[Fact]
public void WithoutALead_OrButtons_ThosePartsAreLeftOut_AndATaglineComesFirst()
{
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true);
injection.Region.Should().Be("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-tagline\">Label</span> <span class=\"cms-hero-title-lines\">Titel</span></h1>");
}
[Fact]
public void TheLead_KeepsItsLineBreaks_AndEverythingIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("<b>x</b>", null, "a & b\nc \"d\"", new HeroButtonDto("<i>", "/p?a=1&b=2")), textOnlyRegion: true);
injection.Region.Should().Contain("&lt;b&gt;x&lt;/b&gt;").And.Contain("a &amp; b<br>c &quot;d&quot;").And.Contain("href=\"/p?a=1&amp;b=2\">&lt;i&gt;</a>");
injection.Region.Should().NotContain("<b>").And.NotContain("<i>");
}
[Fact]
public void AHeroWithSlidesThatAreAllUnusable_StaysEmpty_EvenWhenTheSiteAsksForText()
{
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]);
HeroPictureRenderer.Render(hero, textOnlyRegion: true).IsEmpty.Should().BeTrue();
}
[Fact]
public void ThePrefix_IsTheFirstHiddenPartOfTheH1_AndIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true, titlePrefix: "SLP & Co: ");
injection.Region.Should().Be("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-prefix\">SLP &amp; Co: </span><span class=\"cms-hero-tagline\">Label</span> <span class=\"cms-hero-title-lines\">Titel</span></h1>");
}
[Theory]
[InlineData("")]
[InlineData(" ")]
public void AnEmptyPrefix_ChangesNothing(string prefix)
{
var hero = TextOnly();
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: prefix).Region
.Should().Be(HeroPictureRenderer.Render(hero, textOnlyRegion: true).Region).And.NotContain("title-prefix");
}
[Fact]
public void APictureHero_NeverGetsThePrefix()
{
var variant = new HeroVariantDto("wide", 640, 427, "image/webp", "/media/hero/0123456789abcdef/wide-640.webp");
var image = new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-640.webp", [variant]);
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("alt", "", false, image)]);
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: "SLP: ").Region.Should().NotContain("title-prefix");
}
[Fact]
public async Task TheTransform_PassesTheConfiguredPrefix()
{
var model = Substitute.For<IHeroPageModel>();
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
var transform = new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = true, TitlePrefix = "SLP Software: " }));
var html = await transform.ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
html.Should().Contain("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-prefix\">SLP Software: </span><span class=\"cms-hero-title-lines\">");
}
private const string Page ="<head><!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD--></head><body><!--CMS:HERO-HTML-->baked<!--/CMS:HERO-HTML--></body>";
private static HeroHtmlTransform Transform(bool textOnly)
{
var model = Substitute.For<IHeroPageModel>();
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
return new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = textOnly }));
}
[Fact]
public async Task TheTransform_FillsTheTextRegion_WhenTheSiteAsksForIt_AndLeavesThePreloadRegionAlone()
{
var html = await Transform(textOnly: true).ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
html.Should().Contain("<!--CMS:HERO-HTML--><h1 class=\"cms-hero-title\">").And.NotContain("baked");
html.Should().Contain("<!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD-->");
}
[Fact]
public async Task TheTransform_LeavesTheWholePageAlone_WhenTheSiteDoesNotAskForText()
{
(await Transform(textOnly: false).ApplyAsync(Page, new HtmlTransformContext("https://x"), default)).Should().Be(Page);
}
}
@@ -0,0 +1,38 @@
<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>
<None Include="Golden\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,176 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Availability;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Texts;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Controllers;
/// <summary>
/// The hero of the site. <c>GET /hero</c> is what websites read (200 with the hero, 204 when there is none to show);
/// management (<c>GET /hero/all</c>, <c>PUT /hero</c>) is for administrators.
/// </summary>
[ApiController]
[Route("hero")]
public class HeroController(IHeroService service, IPublicHeroService publicService) : ControllerBase
{
[HttpGet]
[AllowAnonymous]
[PublicSiteEndpoint]
[EnableRateLimiting("hero-public")]
[ProducesResponseType<PublicHeroDto>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> Get(CancellationToken cancellationToken)
{
Response.Headers.CacheControl = "public, max-age=60";
var hero = await publicService.GetActiveAsync(cancellationToken);
return hero is null ? NoContent() : Ok(hero);
}
[HttpGet("all")]
[Authorize(Policy = "AdminOnly")]
[ProducesResponseType<HeroAdminDto>(StatusCodes.Status200OK)]
public async Task<IActionResult> GetForAdmin(CancellationToken cancellationToken)
=> Ok(await service.GetAsync(cancellationToken));
[HttpPut]
[Authorize(Policy = "AdminOnly")]
[ProducesResponseType<HeroAdminDto>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Save([FromBody] HeroSaveRequest request, CancellationToken cancellationToken)
{
try
{
return Ok(await service.SaveAsync(request, HeroCaller.Id(User), cancellationToken));
}
catch (HeroSaveRejectedException exception)
{
ModelState.AddModelError(exception.Field, exception.Message);
return ValidationProblem(ModelState);
}
catch (ImageRejectedException rejection)
{
// Cutting the variants for a changed focus needs the stored original and a free processing slot.
var status = rejection.Reason == ImageRejection.Busy ? StatusCodes.Status429TooManyRequests : StatusCodes.Status503ServiceUnavailable;
return StatusCode(status, new { error = rejection.Code, message = HttpTexts.From(HttpContext).Get(HeroTexts.Image.For(rejection.Reason)) });
}
}
}
/// <summary>Images of the hero: upload, focus and delete. Administrators only.</summary>
[ApiController]
[Route("hero/images")]
[Authorize(Policy = "AdminOnly")]
public class HeroImagesController(IHeroImageService service, IOptions<MediaOptions> media, MediaStatus mediaStatus) : ControllerBase
{
/// <summary>The body limit leaves room for the multipart envelope around the file limit of <see cref="MediaOptions.MaxUploadBytes"/>.</summary>
private const long RequestLimit = 12 * 1024 * 1024;
[HttpPost]
[RequestSizeLimit(RequestLimit)]
[EnableRateLimiting("hero-upload")]
[Consumes("multipart/form-data")]
[ProducesResponseType<HeroImageDto>(StatusCodes.Status201Created)]
public async Task<IActionResult> Upload(IFormFile file, CancellationToken cancellationToken)
{
try
{
await using var stream = file.OpenReadStream();
var image = await service.UploadAsync(stream, file.Length, HeroCaller.Id(User), cancellationToken);
return Created($"/api/v1/hero/images/{image.Id}", image);
}
catch (ImageRejectedException rejection)
{
return Rejected(rejection);
}
catch (MediaStorageFullException)
{
return StatusCode(StatusCodes.Status503ServiceUnavailable, new { error = "storage_full", message = Texts().Get(HeroTexts.Image.StorageFull) });
}
}
/// <summary>One image as the admin sees it now; the page asks for the images it uploaded but has not saved yet.</summary>
[HttpGet("{id:guid}")]
[ProducesResponseType<HeroImageDto>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(Guid id, CancellationToken cancellationToken)
{
var image = await service.GetImageAsync(id, cancellationToken);
return image is null ? NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) }) : Ok(image);
}
/// <summary>The formats and limits an upload has to meet on this host (what the validation uses).</summary>
[HttpGet("limits")]
[ProducesResponseType<HeroUploadLimitsDto>(StatusCodes.Status200OK)]
public ActionResult<HeroUploadLimitsDto> Limits() => HeroMapping.UploadLimits(mediaStatus, media.Value);
/// <summary>Lets the background renderer try an image it gave up on again.</summary>
[HttpPost("{id:guid}/retry")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Retry(Guid id, CancellationToken cancellationToken)
{
try
{
await service.RetryRenderAsync(id, cancellationToken);
return NoContent();
}
catch (HeroImageNotFoundException)
{
return NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) });
}
}
[HttpDelete("{id:guid}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
{
try
{
await service.DeleteAsync(id, HeroCaller.Id(User), cancellationToken);
return NoContent();
}
catch (HeroImageNotFoundException)
{
return NotFound(new { error = "not_found", message = Texts().Get(HeroTexts.Image.NotFound) });
}
catch (HeroImageInUseException)
{
return Conflict(new { error = "in_use", message = Texts().Get(HeroTexts.Image.InUse) });
}
}
private ITextCatalog Texts() => HttpTexts.From(HttpContext);
private IActionResult Rejected(ImageRejectedException rejection)
{
var status = rejection.Reason switch
{
ImageRejection.TooLarge => StatusCodes.Status413PayloadTooLarge,
ImageRejection.Busy => StatusCodes.Status429TooManyRequests,
ImageRejection.Unavailable => StatusCodes.Status503ServiceUnavailable,
_ => StatusCodes.Status400BadRequest,
};
var key = HeroTexts.Image.For(rejection.Reason);
var message = rejection.Reason == ImageRejection.TooLarge
? Texts().Get(key, args: ("max", media.Value.MaxUploadBytes / (1024 * 1024)))
: Texts().Get(key);
return StatusCode(status, new { error = rejection.Code, message });
}
}
internal static class HeroCaller
{
public static Guid Id(ClaimsPrincipal user)
{
var claim = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? user.FindFirstValue("sub");
return Guid.TryParse(claim, out var id) ? id : Guid.Empty;
}
}
@@ -0,0 +1,103 @@
using SlpModularCms.Core.Media;
namespace SlpModularCms.Modules.Hero.Data.Entities;
/// <summary>The hero of the site in one language: texts, up to two buttons, the display and the slides.</summary>
public class HeroContent
{
public Guid Id { get; set; }
/// <summary>Unique; only <see cref="HeroLanguages.Default"/> is exposed and edited now, the column keeps the model ready for more.</summary>
public string Language { get; set; } = HeroLanguages.Default;
public string Title { get; set; } = string.Empty;
public string? Tagline { get; set; }
public string? Lead { get; set; }
public string Display { get; set; } = HeroDisplays.Single;
public string? Button1Label { get; set; }
public string? Button1Target { get; set; }
public string? Button2Label { get; set; }
public string? Button2Target { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
public Guid LastModifiedByUserId { get; set; }
public List<HeroSlide> Slides { get; set; } = [];
}
public class HeroSlide
{
public Guid Id { get; set; }
public Guid HeroId { get; set; }
public Guid ImageId { get; set; }
public HeroImage? Image { get; set; }
public string Alt { get; set; } = string.Empty;
public bool Decorative { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
/// <summary>A stored image: the original (never served), where the subject is, and the current set of variants.</summary>
public class HeroImage
{
public Guid Id { get; set; }
/// <summary>SHA-256 of the file as uploaded (hex); the same photo uploaded twice is one image.</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 FocusX { get; set; } = 0.5;
public double FocusY { get; set; } = 0.5;
public double Zoom { get; set; } = 1.0;
public double? WideFocusX { get; set; }
public double? WideFocusY { get; set; }
public double? WideZoom { get; set; }
public double? TallFocusX { get; set; }
public double? TallFocusY { get; set; }
public double? TallZoom { get; set; }
public string SetHash { get; set; } = string.Empty;
public List<HeroVariant> Variants { get; set; } = [];
public DateTimeOffset CreatedAt { get; set; }
public Guid CreatedByUserId { get; set; }
public FocusSetting General => new FocusSetting(FocusX, FocusY, Zoom).Normalize();
public FocusSetting FocusFor(string ratio) => ratio switch
{
"wide" when WideFocusX is not null && WideFocusY is not null => new FocusSetting(WideFocusX.Value, WideFocusY.Value, WideZoom ?? Zoom).Normalize(),
"tall" when TallFocusX is not null && TallFocusY is not null => new FocusSetting(TallFocusX.Value, TallFocusY.Value, TallZoom ?? Zoom).Normalize(),
_ => General,
};
}
/// <summary>One file of the current set: the ratio, its pixel size, the type and the file name inside the set.</summary>
public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes);
/// <summary>A set or an original that nothing uses any more; the clean-up deletes it once the retention time has passed.</summary>
public class HeroRetiredFile
{
public Guid Id { get; set; }
/// <summary><c>set</c> (a set hash) or <c>original</c> (an original key).</summary>
public string Kind { get; set; } = string.Empty;
public string Key { get; set; } = string.Empty;
public DateTimeOffset RetiredAt { get; set; }
}
public static class HeroLanguages
{
public const string Default = "nl";
}
public static class HeroDisplays
{
public const string Single = "single";
public const string Slideshow = "slideshow";
public static bool IsValid(string? value) => value is Single or Slideshow;
}
@@ -0,0 +1,86 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using SlpModularCms.Modules.Hero.Data.Entities;
namespace SlpModularCms.Modules.Hero.Data;
public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(options)
{
public DbSet<HeroContent> Heroes => Set<HeroContent>();
public DbSet<HeroSlide> HeroSlides => Set<HeroSlide>();
public DbSet<HeroImage> HeroImages => Set<HeroImage>();
public DbSet<HeroRetiredFile> HeroRetiredFiles => Set<HeroRetiredFile>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HeroContent>(entity =>
{
entity.ToTable("Heroes");
entity.HasKey(e => e.Id);
entity.Property(e => e.Language).IsRequired().HasMaxLength(10);
entity.HasIndex(e => e.Language).IsUnique();
entity.Property(e => e.Title).IsRequired().HasMaxLength(1000);
entity.Property(e => e.Tagline).HasMaxLength(200);
entity.Property(e => e.Lead).HasMaxLength(1000);
entity.Property(e => e.Display).IsRequired().HasMaxLength(20);
entity.Property(e => e.Button1Label).HasMaxLength(100);
entity.Property(e => e.Button1Target).HasMaxLength(500);
entity.Property(e => e.Button2Label).HasMaxLength(100);
entity.Property(e => e.Button2Target).HasMaxLength(500);
entity.Property(e => e.UpdatedAt).IsRequired();
entity.Property(e => e.LastModifiedByUserId).IsRequired();
entity.HasMany(e => e.Slides).WithOne().HasForeignKey(s => s.HeroId).OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<HeroSlide>(entity =>
{
entity.ToTable("HeroSlides");
entity.HasKey(e => e.Id);
entity.Property(e => e.Alt).IsRequired().HasMaxLength(600);
entity.Property(e => e.SortOrder).IsRequired();
// Not unique: re-ordering updates several rows in one save and must not trip over itself.
entity.HasIndex(e => new { e.HeroId, e.SortOrder });
entity.HasIndex(e => e.ImageId);
// Restrict: an image that a slide still uses cannot be deleted (the service answers 409 first).
entity.HasOne(e => e.Image).WithMany().HasForeignKey(e => e.ImageId).OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<HeroImage>(entity =>
{
entity.ToTable("HeroImages");
entity.HasKey(e => e.Id);
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.SetHash).IsRequired().HasMaxLength(16);
entity.Property(e => e.CreatedAt).IsRequired();
entity.Property(e => e.CreatedByUserId).IsRequired();
entity.Ignore(e => e.General);
// The variants of the current set: a small list that is always replaced as a whole.
entity.Property(e => e.Variants)
.HasConversion(
variants => JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null),
json => JsonSerializer.Deserialize<List<HeroVariant>>(json, (JsonSerializerOptions?)null) ?? new List<HeroVariant>())
.HasColumnType("longtext")
.Metadata.SetValueComparer(new ValueComparer<List<HeroVariant>>(
(a, b) => (a ?? new List<HeroVariant>()).SequenceEqual(b ?? new List<HeroVariant>()),
v => v.Aggregate(0, (hash, item) => HashCode.Combine(hash, item.GetHashCode())),
v => v.ToList()));
});
modelBuilder.Entity<HeroRetiredFile>(entity =>
{
entity.ToTable("HeroRetiredFiles");
entity.HasKey(e => e.Id);
entity.Property(e => e.Kind).IsRequired().HasMaxLength(10);
entity.Property(e => e.Key).IsRequired().HasMaxLength(200);
entity.Property(e => e.RetiredAt).IsRequired();
});
}
}
@@ -0,0 +1,62 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SlpModularCms.Core.Hosting;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Magick;
using SlpModularCms.Core.Modules;
using SlpModularCms.Core.Texts;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Repositories;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero;
/// <summary>The manageable hero of a website: texts, buttons, slides with images; images are processed and served by the host.</summary>
[ExcludeFromCodeCoverage]
public class HeroModule : IModule
{
public string Name => "Hero";
public string Version => "1.0.0";
public void RegisterServices(IServiceCollection services)
{
services.AddDbContext<HeroDbContext>((serviceProvider, options) =>
{
var config = serviceProvider.GetRequiredService<IConfiguration>();
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
});
services.AddCmsTextsFrom(typeof(HeroModule).Assembly);
services.AddOptions<HeroOptions>().Configure<IConfiguration>((options, configuration) =>
configuration.GetSection(HeroOptions.SectionName).Bind(options));
// Image processing and the media area this module owns (served at /media/hero/...).
services.AddCmsMediaArea(HeroMapping.Area);
services.AddMagickImageProcessor();
services.AddScoped<IHeroRepository, HeroRepository>();
services.AddScoped<IHeroService, HeroService>();
services.AddScoped<IPublicHeroService, PublicHeroService>();
services.AddScoped<IHeroImageService, HeroImageService>();
services.AddScoped<IHeroPageModel, HeroPageModel>();
services.AddScoped<IHtmlPageTransform, HeroHtmlTransform>();
services.AddScoped<ICmsDataBlockContributor, HeroDataContributor>();
services.AddSingleton<HeroRenderSignal>();
services.AddHostedService<HeroRenderService>();
services.AddScoped<HeroMediaCleanup>();
services.AddHostedService<HeroMediaCleanupService>();
}
public void UseModule(IApplicationBuilder app)
{
using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
db.Database.Migrate();
}
}
@@ -0,0 +1,75 @@
using System.ComponentModel.DataAnnotations;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Texts;
namespace SlpModularCms.Modules.Hero;
/// <summary>The keys of the Hero module texts (<c>Texts/nl.json</c>, <c>Texts/en.json</c>, key path <c>hero.*</c>).</summary>
public static class HeroTexts
{
public static class Validation
{
public static class Title
{
public static readonly TextKey Required = new("hero.validation.title.required");
public static readonly TextKey TooLong = new("hero.validation.title.tooLong", "max");
public static readonly TextKey TooManyLines = new("hero.validation.title.tooManyLines", "max");
}
public static readonly TextKey TaglineTooLong = new("hero.validation.tagline.tooLong", "max");
public static readonly TextKey LeadTooLong = new("hero.validation.lead.tooLong", "max");
public static readonly TextKey DisplayInvalid = new("hero.validation.display.invalid");
public static class Button
{
public static readonly TextKey Incomplete = new("hero.validation.button.incomplete");
public static readonly TextKey LabelTooLong = new("hero.validation.button.labelTooLong", "max");
public static readonly TextKey TargetTooLong = new("hero.validation.button.targetTooLong", "max");
public static readonly TextKey TargetInvalid = new("hero.validation.button.targetInvalid");
public static readonly TextKey TooMany = new("hero.validation.button.tooMany", "max");
}
public static class Slide
{
public static readonly TextKey ImageRequired = new("hero.validation.slide.imageRequired");
public static readonly TextKey ImageUnknown = new("hero.validation.slide.imageUnknown");
public static readonly TextKey AltRequired = new("hero.validation.slide.altRequired");
public static readonly TextKey AltTooLong = new("hero.validation.slide.altTooLong", "max");
public static readonly TextKey IdUnknown = new("hero.validation.slide.idUnknown");
}
public static readonly TextKey FocusInvalid = new("hero.validation.focus.invalid", "maxZoom");
}
public static class Image
{
public static readonly TextKey InUse = new("hero.image.inUse");
public static readonly TextKey NotFound = new("hero.image.notFound");
public static readonly TextKey StorageFull = new("hero.image.storageFull");
public static readonly TextKey TooLarge = new("hero.image.rejected.tooLarge", "max");
public static readonly TextKey UnsupportedFormat = new("hero.image.rejected.unsupportedFormat");
public static readonly TextKey Corrupt = new("hero.image.rejected.corrupt");
public static readonly TextKey TooManyPixels = new("hero.image.rejected.tooManyPixels");
public static readonly TextKey Timeout = new("hero.image.rejected.timeout");
public static readonly TextKey Busy = new("hero.image.rejected.busy");
public static readonly TextKey Unavailable = new("hero.image.rejected.unavailable");
/// <summary>The text key for a rejection; the size limit is given in MB for the "too large" message.</summary>
public static TextKey For(ImageRejection reason) => reason switch
{
ImageRejection.TooLarge => TooLarge,
ImageRejection.UnsupportedFormat => UnsupportedFormat,
ImageRejection.Corrupt => Corrupt,
ImageRejection.TooManyPixels => TooManyPixels,
ImageRejection.Timeout => Timeout,
ImageRejection.Busy => Busy,
_ => Unavailable,
};
}
/// <summary>The catalogue of the request being validated, or the shared default one outside a request (unit tests).</summary>
public static ITextCatalog From(ValidationContext? context)
=> context?.GetService(typeof(ITextCatalog)) as ITextCatalog ?? TextCatalog.Default;
}
@@ -0,0 +1,256 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SlpModularCms.Modules.Hero.Data;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
[DbContext(typeof(HeroDbContext))]
[Migration("20261116142730_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.Hero.Data.Entities.HeroContent", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Button1Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button1Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Button2Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button2Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Display")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("varchar(20)");
b.Property<DateTimeOffset?>("EndsAt")
.HasColumnType("datetime");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<string>("Language")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("Lead")
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<DateTimeOffset?>("StartsAt")
.HasColumnType("datetime");
b.Property<string>("Tagline")
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("Language")
.IsUnique();
b.ToTable("Heroes", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("Bytes")
.HasColumnType("bigint");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<Guid>("CreatedByUserId")
.HasColumnType("char(36)");
b.Property<double>("FocusX")
.HasColumnType("double");
b.Property<double>("FocusY")
.HasColumnType("double");
b.Property<int>("Height")
.HasColumnType("int");
b.Property<string>("OriginalKey")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("OriginalSha256")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
b.Property<string>("SetHash")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<string>("SourceFormat")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<double?>("TallFocusX")
.HasColumnType("double");
b.Property<double?>("TallFocusY")
.HasColumnType("double");
b.Property<double?>("TallZoom")
.HasColumnType("double");
b.Property<string>("Variants")
.IsRequired()
.HasColumnType("longtext");
b.Property<double?>("WideFocusX")
.HasColumnType("double");
b.Property<double?>("WideFocusY")
.HasColumnType("double");
b.Property<double?>("WideZoom")
.HasColumnType("double");
b.Property<int>("Width")
.HasColumnType("int");
b.Property<double>("Zoom")
.HasColumnType("double");
b.HasKey("Id");
b.HasIndex("OriginalSha256")
.IsUnique();
b.ToTable("HeroImages", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Key")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Kind")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<DateTimeOffset>("RetiredAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.ToTable("HeroRetiredFiles", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Alt")
.IsRequired()
.HasMaxLength(600)
.HasColumnType("varchar(600)");
b.Property<bool>("Decorative")
.HasColumnType("tinyint(1)");
b.Property<Guid>("HeroId")
.HasColumnType("char(36)");
b.Property<Guid>("ImageId")
.HasColumnType("char(36)");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<int>("SortOrder")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("HeroId", "SortOrder");
b.ToTable("HeroSlides", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
.WithMany("Slides")
.HasForeignKey("HeroId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Image");
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Navigation("Slides");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,158 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "Heroes",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false),
Language = table.Column<string>(type: "varchar(10)", maxLength: 10, nullable: false),
Title = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: false),
Tagline = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true),
Lead = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: true),
Display = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false),
Button1Label = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true),
Button1Target = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
Button2Label = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true),
Button2Target = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
StartsAt = table.Column<DateTimeOffset>(type: "datetime", nullable: true),
EndsAt = table.Column<DateTimeOffset>(type: "datetime", nullable: true),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false),
UpdatedAt = table.Column<DateTimeOffset>(type: "datetime", nullable: false),
LastModifiedByUserId = table.Column<Guid>(type: "char(36)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Heroes", x => x.Id);
})
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "HeroImages",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false),
OriginalSha256 = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false),
OriginalKey = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false),
SourceFormat = table.Column<string>(type: "varchar(10)", maxLength: 10, nullable: false),
Width = table.Column<int>(type: "int", nullable: false),
Height = table.Column<int>(type: "int", nullable: false),
Bytes = table.Column<long>(type: "bigint", nullable: false),
FocusX = table.Column<double>(type: "double", nullable: false),
FocusY = table.Column<double>(type: "double", nullable: false),
Zoom = table.Column<double>(type: "double", nullable: false),
WideFocusX = table.Column<double>(type: "double", nullable: true),
WideFocusY = table.Column<double>(type: "double", nullable: true),
WideZoom = table.Column<double>(type: "double", nullable: true),
TallFocusX = table.Column<double>(type: "double", nullable: true),
TallFocusY = table.Column<double>(type: "double", nullable: true),
TallZoom = table.Column<double>(type: "double", nullable: true),
SetHash = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: false),
Variants = table.Column<string>(type: "longtext", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "datetime", nullable: false),
CreatedByUserId = table.Column<Guid>(type: "char(36)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_HeroImages", x => x.Id);
})
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "HeroRetiredFiles",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false),
Kind = table.Column<string>(type: "varchar(10)", maxLength: 10, nullable: false),
Key = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false),
RetiredAt = table.Column<DateTimeOffset>(type: "datetime", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_HeroRetiredFiles", x => x.Id);
})
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "HeroSlides",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false),
HeroId = table.Column<Guid>(type: "char(36)", nullable: false),
ImageId = table.Column<Guid>(type: "char(36)", nullable: false),
Alt = table.Column<string>(type: "varchar(600)", maxLength: 600, nullable: false),
Decorative = table.Column<bool>(type: "tinyint(1)", nullable: false),
SortOrder = table.Column<int>(type: "int", nullable: false),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_HeroSlides", x => x.Id);
table.ForeignKey(
name: "FK_HeroSlides_HeroImages_ImageId",
column: x => x.ImageId,
principalTable: "HeroImages",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_HeroSlides_Heroes_HeroId",
column: x => x.HeroId,
principalTable: "Heroes",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
})
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_Heroes_Language",
table: "Heroes",
column: "Language",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_HeroImages_OriginalSha256",
table: "HeroImages",
column: "OriginalSha256",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_HeroSlides_HeroId_SortOrder",
table: "HeroSlides",
columns: new[] { "HeroId", "SortOrder" });
migrationBuilder.CreateIndex(
name: "IX_HeroSlides_ImageId",
table: "HeroSlides",
column: "ImageId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "HeroRetiredFiles");
migrationBuilder.DropTable(
name: "HeroSlides");
migrationBuilder.DropTable(
name: "HeroImages");
migrationBuilder.DropTable(
name: "Heroes");
}
}
}
@@ -0,0 +1,247 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SlpModularCms.Modules.Hero.Data;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
[DbContext(typeof(HeroDbContext))]
[Migration("20261117071410_RemoveHeroScheduleAndSwitch")]
partial class RemoveHeroScheduleAndSwitch
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Button1Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button1Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Button2Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button2Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Display")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("varchar(20)");
b.Property<string>("Language")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("Lead")
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<string>("Tagline")
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("Language")
.IsUnique();
b.ToTable("Heroes", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("Bytes")
.HasColumnType("bigint");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<Guid>("CreatedByUserId")
.HasColumnType("char(36)");
b.Property<double>("FocusX")
.HasColumnType("double");
b.Property<double>("FocusY")
.HasColumnType("double");
b.Property<int>("Height")
.HasColumnType("int");
b.Property<string>("OriginalKey")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("OriginalSha256")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
b.Property<string>("SetHash")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<string>("SourceFormat")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<double?>("TallFocusX")
.HasColumnType("double");
b.Property<double?>("TallFocusY")
.HasColumnType("double");
b.Property<double?>("TallZoom")
.HasColumnType("double");
b.Property<string>("Variants")
.IsRequired()
.HasColumnType("longtext");
b.Property<double?>("WideFocusX")
.HasColumnType("double");
b.Property<double?>("WideFocusY")
.HasColumnType("double");
b.Property<double?>("WideZoom")
.HasColumnType("double");
b.Property<int>("Width")
.HasColumnType("int");
b.Property<double>("Zoom")
.HasColumnType("double");
b.HasKey("Id");
b.HasIndex("OriginalSha256")
.IsUnique();
b.ToTable("HeroImages", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Key")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Kind")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<DateTimeOffset>("RetiredAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.ToTable("HeroRetiredFiles", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Alt")
.IsRequired()
.HasMaxLength(600)
.HasColumnType("varchar(600)");
b.Property<bool>("Decorative")
.HasColumnType("tinyint(1)");
b.Property<Guid>("HeroId")
.HasColumnType("char(36)");
b.Property<Guid>("ImageId")
.HasColumnType("char(36)");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<int>("SortOrder")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("HeroId", "SortOrder");
b.ToTable("HeroSlides", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
.WithMany("Slides")
.HasForeignKey("HeroId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Image");
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Navigation("Slides");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,50 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
/// <inheritdoc />
public partial class RemoveHeroScheduleAndSwitch : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "EndsAt",
table: "Heroes");
migrationBuilder.DropColumn(
name: "IsActive",
table: "Heroes");
migrationBuilder.DropColumn(
name: "StartsAt",
table: "Heroes");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "EndsAt",
table: "Heroes",
type: "datetime",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsActive",
table: "Heroes",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "StartsAt",
table: "Heroes",
type: "datetime",
nullable: true);
}
}
}
@@ -0,0 +1,244 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SlpModularCms.Modules.Hero.Data;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
[DbContext(typeof(HeroDbContext))]
partial class HeroDbContextModelSnapshot : 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.Hero.Data.Entities.HeroContent", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Button1Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button1Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Button2Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button2Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Display")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("varchar(20)");
b.Property<string>("Language")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("Lead")
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<string>("Tagline")
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("Language")
.IsUnique();
b.ToTable("Heroes", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("Bytes")
.HasColumnType("bigint");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<Guid>("CreatedByUserId")
.HasColumnType("char(36)");
b.Property<double>("FocusX")
.HasColumnType("double");
b.Property<double>("FocusY")
.HasColumnType("double");
b.Property<int>("Height")
.HasColumnType("int");
b.Property<string>("OriginalKey")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("OriginalSha256")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
b.Property<string>("SetHash")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<string>("SourceFormat")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<double?>("TallFocusX")
.HasColumnType("double");
b.Property<double?>("TallFocusY")
.HasColumnType("double");
b.Property<double?>("TallZoom")
.HasColumnType("double");
b.Property<string>("Variants")
.IsRequired()
.HasColumnType("longtext");
b.Property<double?>("WideFocusX")
.HasColumnType("double");
b.Property<double?>("WideFocusY")
.HasColumnType("double");
b.Property<double?>("WideZoom")
.HasColumnType("double");
b.Property<int>("Width")
.HasColumnType("int");
b.Property<double>("Zoom")
.HasColumnType("double");
b.HasKey("Id");
b.HasIndex("OriginalSha256")
.IsUnique();
b.ToTable("HeroImages", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Key")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Kind")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<DateTimeOffset>("RetiredAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.ToTable("HeroRetiredFiles", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Alt")
.IsRequired()
.HasMaxLength(600)
.HasColumnType("varchar(600)");
b.Property<bool>("Decorative")
.HasColumnType("tinyint(1)");
b.Property<Guid>("HeroId")
.HasColumnType("char(36)");
b.Property<Guid>("ImageId")
.HasColumnType("char(36)");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<int>("SortOrder")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("HeroId", "SortOrder");
b.ToTable("HeroSlides", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
.WithMany("Slides")
.HasForeignKey("HeroId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Image");
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Navigation("Slides");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,57 @@
using System.Text.RegularExpressions;
namespace SlpModularCms.Modules.Hero.Models;
public enum ButtonTargetKind
{
Invalid = 0,
Anchor,
Path,
Https,
}
/// <summary>
/// The only kinds of button target the CMS accepts: a page anchor (<c>#contact</c>), a path of the site (<c>/over-mij</c>)
/// or an absolute https address. Nothing else, so a button can never carry <c>javascript:</c>, <c>data:</c>, a
/// protocol-relative address or a backslash trick. Pure.
/// </summary>
public static partial class ButtonTargetValidator
{
[GeneratedRegex("^#[A-Za-z][A-Za-z0-9_-]{0,99}$", RegexOptions.CultureInvariant)]
private static partial Regex AnchorPattern();
public static ButtonTargetKind Validate(string? target)
{
if (string.IsNullOrEmpty(target) || target != target.Trim())
{
return ButtonTargetKind.Invalid;
}
if (target.Any(c => char.IsWhiteSpace(c) || char.IsControl(c) || c == '\\' || c == '"' || c == '<' || c == '>' || c == '`'))
{
return ButtonTargetKind.Invalid;
}
if (target[0] == '#')
{
return AnchorPattern().IsMatch(target) ? ButtonTargetKind.Anchor : ButtonTargetKind.Invalid;
}
if (target[0] == '/')
{
// One leading slash, not "//host"; no scheme in the first segment ("/a:b" is fine, "/:" is not a concern here).
return target.Length == 1 || target[1] != '/' ? ButtonTargetKind.Path : ButtonTargetKind.Invalid;
}
if (Uri.TryCreate(target, UriKind.Absolute, out var uri)
&& uri.Scheme == Uri.UriSchemeHttps
&& !string.IsNullOrEmpty(uri.Host)
&& string.IsNullOrEmpty(uri.UserInfo)
&& target.StartsWith("https://", StringComparison.Ordinal))
{
return ButtonTargetKind.Https;
}
return ButtonTargetKind.Invalid;
}
}
@@ -0,0 +1,257 @@
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Json.Serialization;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
namespace SlpModularCms.Modules.Hero.Models;
public record FocusDto(double X, double Y, double Zoom)
{
public FocusSetting ToSetting() => new(X, Y, Zoom);
public static FocusDto From(FocusSetting focus) => new(focus.X, focus.Y, focus.Zoom);
}
public record HeroVariantDto(string Ratio, int Width, int Height, string Type, string Url);
public record HeroImageDto(
string Id,
int Width,
int Height,
FocusDto Focus,
FocusDto? WideFocus,
FocusDto? TallFocus,
string Src,
IReadOnlyList<HeroVariantDto> Variants,
bool Processing = false,
bool Failed = false,
int? RenderDone = null,
int? RenderTotal = null,
string? PreviewSrc = null,
bool Reused = false);
public record HeroButtonDto(string Label, string Target);
public record HeroSlideAdminDto(string Id, string ImageId, string Alt, bool Decorative, bool IsActive, int SortOrder, HeroImageDto Image);
/// <summary>What the admin edits. When no hero exists yet <see cref="Id"/> is null and the fields hold the defaults.</summary>
public record HeroAdminDto(
string? Id,
string Title,
string? Tagline,
string? Lead,
string Display,
IReadOnlyList<HeroButtonDto> Buttons,
IReadOnlyList<HeroSlideAdminDto> Slides,
bool IsLive,
DateTimeOffset? UpdatedAt,
int MaxPublishedSlides);
/// <summary>The image of a published slide. <see cref="Src"/> is the largest WebP of the wide ratio.</summary>
public record PublicImageDto(
int Width,
int Height,
FocusDto Focus,
string Src,
IReadOnlyList<HeroVariantDto> Variants,
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] IReadOnlyDictionary<string, FocusDto>? FocusByRatio = null);
public record PublicSlideDto(string Id, string Alt, bool Decorative, PublicImageDto Image);
/// <summary>What visitors and websites get: only content, never admin-only state. The same shape is written into <c>#cms-data</c>.</summary>
public record PublicHeroDto(
string Display,
string Title,
string? Tagline,
string? Lead,
IReadOnlyList<HeroButtonDto> Buttons,
IReadOnlyList<PublicSlideDto> Slides);
// ----- requests ----------------------------------------------------------------------------------------------------
public record HeroButtonRequest(string? Label, string? Target);
public record HeroSlideRequest(Guid? Id, Guid ImageId, string? Alt, bool Decorative = false, bool IsActive = true);
/// <summary>A changed focus of one image; it takes effect when the hero is saved (the admin keeps it locally until then).</summary>
public record HeroImageFocusRequest(Guid ImageId, FocusDto Focus, FocusDto? Wide = null, FocusDto? Tall = null);
public record HeroFocusRequest(FocusDto Focus, FocusDto? Wide = null, FocusDto? Tall = null) : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var texts = HeroTexts.From(validationContext);
foreach (var (name, value) in new[] { (nameof(Focus), Focus), (nameof(Wide), Wide), (nameof(Tall), Tall) })
{
if (value is not null && !value.ToSetting().IsValid)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.FocusInvalid, args: ("maxZoom", FocusSetting.MaxZoom)), [name]);
}
}
if (Focus is null)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.FocusInvalid, args: ("maxZoom", FocusSetting.MaxZoom)), [nameof(Focus)]);
}
}
}
/// <summary>The whole hero in one go (full replace). Nothing here is trusted: <see cref="Validate"/> is the one place that checks it.</summary>
public record HeroSaveRequest(
string? Title,
string? Tagline,
string? Lead,
string? Display,
IReadOnlyList<HeroButtonRequest>? Buttons,
IReadOnlyList<HeroSlideRequest>? Slides,
IReadOnlyList<HeroImageFocusRequest>? ImageFocus = null) : IValidatableObject
{
public const int TitleMax = 200;
public const int TitleMaxLines = 5;
public const int TaglineMax = 120;
public const int LeadMax = 600;
public const int ButtonLabelMax = 50;
public const int ButtonTargetMax = 500;
public const int ButtonsMax = 2;
public const int AltMax = 300;
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var texts = HeroTexts.From(validationContext);
var title = HeroText.Clean(Title, keepLineBreaks: true);
if (title.Length == 0)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Title.Required), [nameof(Title)]);
}
else
{
if (title.Length > TitleMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Title.TooLong, args: ("max", TitleMax)), [nameof(Title)]);
}
if (title.Count(c => c == '\n') + 1 > TitleMaxLines)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Title.TooManyLines, args: ("max", TitleMaxLines)), [nameof(Title)]);
}
}
if (HeroText.Clean(Tagline, keepLineBreaks: false).Length > TaglineMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.TaglineTooLong, args: ("max", TaglineMax)), [nameof(Tagline)]);
}
if (HeroText.Clean(Lead, keepLineBreaks: true).Length > LeadMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.LeadTooLong, args: ("max", LeadMax)), [nameof(Lead)]);
}
if (!HeroDisplays.IsValid(Display ?? HeroDisplays.Single))
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.DisplayInvalid), [nameof(Display)]);
}
var buttons = Buttons ?? [];
if (buttons.Count > ButtonsMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Button.TooMany, args: ("max", ButtonsMax)), [nameof(Buttons)]);
}
for (var i = 0; i < buttons.Count; i++)
{
var label = HeroText.Clean(buttons[i].Label, keepLineBreaks: false);
var target = (buttons[i].Target ?? string.Empty).Trim();
if (label.Length == 0 && target.Length == 0)
{
continue;
}
if (label.Length == 0 || target.Length == 0)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Button.Incomplete), [$"{nameof(Buttons)}[{i}]"]);
continue;
}
if (label.Length > ButtonLabelMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Button.LabelTooLong, args: ("max", ButtonLabelMax)), [$"{nameof(Buttons)}[{i}].Label"]);
}
if (target.Length > ButtonTargetMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Button.TargetTooLong, args: ("max", ButtonTargetMax)), [$"{nameof(Buttons)}[{i}].Target"]);
}
else if (ButtonTargetValidator.Validate(target) == ButtonTargetKind.Invalid)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Button.TargetInvalid), [$"{nameof(Buttons)}[{i}].Target"]);
}
}
var focusEdits = ImageFocus ?? [];
for (var i = 0; i < focusEdits.Count; i++)
{
var edit = focusEdits[i];
var valid = edit.ImageId != Guid.Empty
&& edit.Focus is not null && edit.Focus.ToSetting().IsValid
&& (edit.Wide is null || edit.Wide.ToSetting().IsValid)
&& (edit.Tall is null || edit.Tall.ToSetting().IsValid);
if (!valid)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.FocusInvalid, args: ("maxZoom", FocusSetting.MaxZoom)), [$"{nameof(ImageFocus)}[{i}]"]);
}
}
var slides = Slides ?? [];
for (var i = 0; i < slides.Count; i++)
{
var slide = slides[i];
if (slide.ImageId == Guid.Empty)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Slide.ImageRequired), [$"{nameof(Slides)}[{i}].ImageId"]);
}
var alt = HeroText.Clean(slide.Alt, keepLineBreaks: false);
if (!slide.Decorative && alt.Length == 0)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Slide.AltRequired), [$"{nameof(Slides)}[{i}].Alt"]);
}
else if (alt.Length > AltMax)
{
yield return new ValidationResult(texts.Get(HeroTexts.Validation.Slide.AltTooLong, args: ("max", AltMax)), [$"{nameof(Slides)}[{i}].Alt"]);
}
}
}
}
/// <summary>Plain text handling: stored as typed, minus control characters; encoding happens where the text is written.</summary>
public static class HeroText
{
public static string Clean(string? value, bool keepLineBreaks)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
var text = value.Replace("\r\n", "\n").Replace('\r', '\n');
var builder = new StringBuilder(text.Length);
foreach (var c in text)
{
if (c == '\n')
{
builder.Append(keepLineBreaks ? '\n' : ' ');
}
else if (!char.IsControl(c) && c != '\u2028' && c != '\u2029')
{
builder.Append(c);
}
}
return builder.ToString().Trim();
}
}
/// <summary>What an upload is allowed to be on this host, for the admin to show: derived from the same sources as the validation.</summary>
public record HeroUploadLimitsDto(IReadOnlyList<string> Formats, long MaxUploadBytes, int MaxMegapixels);
@@ -0,0 +1,122 @@
using Microsoft.EntityFrameworkCore;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Repositories;
public interface IHeroRepository
{
/// <summary>The hero of a language with its slides (and their images), tracked for update.</summary>
Task<HeroContent?> GetAsync(string language, CancellationToken cancellationToken);
/// <summary>The same, not tracked (read paths).</summary>
Task<HeroContent?> GetReadOnlyAsync(string language, CancellationToken cancellationToken);
void Add(HeroContent hero);
void AddSlide(HeroSlide slide);
void RemoveSlide(HeroSlide slide);
Task<HeroImage?> GetImageAsync(Guid id, CancellationToken cancellationToken);
Task<IReadOnlyList<HeroImage>> GetImagesAsync(IReadOnlyCollection<Guid> ids, CancellationToken cancellationToken);
/// <summary>Images whose set is missing or does not match their focus, oldest first (tracked).</summary>
Task<int> CountImagesAsync(CancellationToken cancellationToken);
Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken);
/// <summary>Images with a complete set made before the full-frame preview existed (tracked).</summary>
Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken);
/// <summary>The image as stored right now, not tracked.</summary>
Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken);
Task<HeroImage?> GetImageBySha256Async(string sha256, CancellationToken cancellationToken);
void AddImage(HeroImage image);
void RemoveImage(HeroImage image);
Task<bool> ImageInUseAsync(Guid imageId, CancellationToken cancellationToken);
Task<IReadOnlyList<HeroImage>> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
Task<HashSet<string>> GetReferencedSetHashesAsync(CancellationToken cancellationToken);
Task<HashSet<string>> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken);
void AddRetired(HeroRetiredFile file);
void RemoveRetired(HeroRetiredFile file);
Task<IReadOnlyList<HeroRetiredFile>> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
Task<HashSet<string>> GetRetiredSetHashesAsync(CancellationToken cancellationToken);
Task SaveChangesAsync(CancellationToken cancellationToken);
}
public class HeroRepository(HeroDbContext context) : IHeroRepository
{
public Task<HeroContent?> GetAsync(string language, CancellationToken cancellationToken)
=> context.Heroes.Include(h => h.Slides).ThenInclude(s => s.Image)
.FirstOrDefaultAsync(h => h.Language == language, cancellationToken);
public Task<HeroContent?> GetReadOnlyAsync(string language, CancellationToken cancellationToken)
=> context.Heroes.AsNoTracking().Include(h => h.Slides).ThenInclude(s => s.Image)
.FirstOrDefaultAsync(h => h.Language == language, cancellationToken);
public void Add(HeroContent hero) => context.Heroes.Add(hero);
public void AddSlide(HeroSlide slide) => context.HeroSlides.Add(slide);
public void RemoveSlide(HeroSlide slide) => context.HeroSlides.Remove(slide);
public Task<HeroImage?> GetImageAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
public async Task<IReadOnlyList<HeroImage>> GetImagesAsync(IReadOnlyCollection<Guid> ids, CancellationToken cancellationToken)
{
// The MySQL provider cannot map a Guid list parameter in an IN clause; there are only a few dozen images, so they
// are read and filtered here.
var wanted = ids.ToHashSet();
return (await context.HeroImages.ToListAsync(cancellationToken)).Where(i => wanted.Contains(i.Id)).ToList();
}
public Task<int> CountImagesAsync(CancellationToken cancellationToken) => context.HeroImages.CountAsync(cancellationToken);
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingRenderAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsRender).OrderBy(i => i.CreatedAt).ToList();
public async Task<IReadOnlyList<HeroImage>> GetImagesNeedingPreviewAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.ToListAsync(cancellationToken)).Where(HeroMapping.NeedsPreview).OrderBy(i => i.CreatedAt).ToList();
public Task<HeroImage?> GetImageFreshAsync(Guid id, CancellationToken cancellationToken)
=> context.HeroImages.AsNoTracking().FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
public Task<HeroImage?> GetImageBySha256Async(string sha256, CancellationToken cancellationToken)
=> context.HeroImages.FirstOrDefaultAsync(i => i.OriginalSha256 == sha256, cancellationToken);
public void AddImage(HeroImage image) => context.HeroImages.Add(image);
public void RemoveImage(HeroImage image) => context.HeroImages.Remove(image);
public Task<bool> ImageInUseAsync(Guid imageId, CancellationToken cancellationToken)
=> context.HeroSlides.AnyAsync(s => s.ImageId == imageId, cancellationToken);
public async Task<IReadOnlyList<HeroImage>> GetUnattachedImagesCreatedBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
{
var used = context.HeroSlides.Select(s => s.ImageId);
var candidates = await context.HeroImages.Where(i => !used.Contains(i.Id)).ToListAsync(cancellationToken);
return candidates.Where(i => i.CreatedAt < cutoff).ToList();
}
public async Task<HashSet<string>> GetReferencedSetHashesAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Select(i => i.SetHash).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
public async Task<HashSet<string>> GetReferencedOriginalKeysAsync(CancellationToken cancellationToken)
=> (await context.HeroImages.Select(i => i.OriginalKey).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
public void AddRetired(HeroRetiredFile file) => context.HeroRetiredFiles.Add(file);
public void RemoveRetired(HeroRetiredFile file) => context.HeroRetiredFiles.Remove(file);
public async Task<IReadOnlyList<HeroRetiredFile>> GetRetiredBeforeAsync(DateTimeOffset cutoff, CancellationToken cancellationToken)
=> (await context.HeroRetiredFiles.ToListAsync(cancellationToken)).Where(f => f.RetiredAt < cutoff).ToList();
public async Task<HashSet<string>> GetRetiredSetHashesAsync(CancellationToken cancellationToken)
=> (await context.HeroRetiredFiles.Where(f => f.Kind == "set").Select(f => f.Key).ToListAsync(cancellationToken)).ToHashSet(StringComparer.Ordinal);
public Task SaveChangesAsync(CancellationToken cancellationToken) => context.SaveChangesAsync(cancellationToken);
}
@@ -0,0 +1,380 @@
using System.Security.Cryptography;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Repositories;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>The image is still used by a slide (the administrator gets a 409 and a message).</summary>
public sealed class HeroImageInUseException() : Exception("The image is still used by a slide.");
/// <summary>Cutting the variants of one image failed; the id tells the background renderer which image to give up on after a few tries.</summary>
public sealed class HeroImageRenderException(Guid imageId, Exception inner) : Exception("Cutting the variants of a hero image failed.", inner)
{
public Guid ImageId { get; } = imageId;
}
public sealed class HeroImageNotFoundException() : Exception("The image does not exist.");
/// <summary>There is not enough room in the media directory for the image.</summary>
public sealed class MediaStorageFullException() : Exception("The media directory is full.");
public interface IHeroImageService
{
/// <summary>
/// Admits and stores an uploaded image and returns at once: the variants are cut afterwards by the background renderer
/// (<see cref="RenderNextPendingAsync"/>), so a slow machine never holds the request open. The same photo twice is one
/// image. Until its set exists the image has no variants and is not published.
/// </summary>
/// <exception cref="ImageRejectedException">The upload is refused; see the code.</exception>
Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken);
/// <summary>How many images exist, how many wait for their variants and how many only lack the full-frame preview (for the log).</summary>
Task<(int Total, int NeedRender, int NeedPreview)> GetRenderBacklogAsync(CancellationToken cancellationToken);
/// <summary>
/// Records a changed focus (general and optionally per ratio) on a tracked image. Nothing is cut here: the caller saves
/// the image together with the hero and then calls <see cref="NotifyPending"/>; the background renderer cuts the new set
/// into NEW files and only then switches the image to it (so the old set stays live until the new one is complete).
/// </summary>
void ApplyFocus(HeroImage image, HeroFocusRequest request);
/// <summary>The variant the renderer is busy with: (done, total), or null when the image is not being cut right now.</summary>
(int Done, int Total)? RenderProgress(Guid imageId);
/// <summary>One image as the admin sees it now (including whether it is still being prepared). Null when there is no such image.</summary>
Task<HeroImageDto?> GetImageAsync(Guid imageId, CancellationToken cancellationToken);
/// <summary>True when the background renderer gave up on the image after a few failed tries (until a retry or a restart).</summary>
bool IsRenderFailed(Guid imageId);
/// <summary>Lets the background renderer try an image it gave up on again.</summary>
/// <exception cref="HeroImageNotFoundException">There is no such image.</exception>
Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken);
/// <summary>Wakes the background renderer: there is an image whose set is missing or does not match its focus.</summary>
void NotifyPending();
/// <summary>
/// Cuts the set of the oldest image that needs one (new upload, repair or changed focus), skipping the given ids, and
/// switches the image to it. Returns the id of the image handled, or null when nothing is pending.
/// </summary>
Task<Guid?> RenderNextPendingAsync(IReadOnlySet<Guid> skip, CancellationToken cancellationToken);
/// <summary>Removes an image nothing uses; its files go after the retention time.</summary>
Task DeleteAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken);
}
public class HeroImageService(
IMediaUploadGuard guard,
IImageProcessor processor,
IMediaStore store,
MediaProcessingGate gate,
IHeroRepository repository,
IOptions<MediaOptions> mediaOptions,
ILogger<HeroImageService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
HeroRenderSignal? signal = null) : IHeroImageService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<HeroImageDto> UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken)
{
using var admitted = await guard.AdmitAsync(upload, declaredLength, cancellationToken);
var sha = Convert.ToHexStringLower(SHA256.HashData(admitted.Bytes));
// The same photo again is the same image. When its files are gone (a lost or restored media directory) the upload
// repairs them from the file that was just sent.
var existing = await repository.GetImageBySha256Async(sha, cancellationToken);
if (existing is not null && await FilesExistAsync(existing, cancellationToken))
{
// The same photo again also gives up the failed state: the renderer tries it again when it is still pending.
signal?.Reset(existing.Id);
signal?.Notify();
return HeroMapping.ToImageDto(existing) with { Reused = true };
}
await EnsureRoomAsync(admitted.Bytes.Length, cancellationToken);
// The original is kept without metadata (GPS and the like); formats that cannot be written back (HEIC) become JPEG.
var format = admitted.Info.Format;
var stripped = await processor.StripMetadataAsync(admitted.Bytes, format, cancellationToken);
var original = stripped ?? admitted.Bytes;
var storedFormat = stripped is not null && format == ImageFormat.Heic ? ImageFormat.Jpeg : format;
var image = existing ?? new HeroImage
{
Id = Guid.NewGuid(),
OriginalSha256 = sha,
OriginalKey = $"originals/{sha}.{storedFormat.Extension()}",
SourceFormat = storedFormat.Extension(),
Width = admitted.Info.Width,
Height = admitted.Info.Height,
Bytes = original.Length,
CreatedAt = _time.GetUtcNow(),
CreatedByUserId = callerId,
};
// The files first, the row last: an aborted request leaves at most an original nobody refers to (the clean-up
// removes it) and a repeated upload simply does it again. The set is cut afterwards by the background renderer.
await store.PutAsync(image.OriginalKey, original, cancellationToken);
image.SetHash = string.Empty;
image.Variants = [];
if (existing is null)
{
repository.AddImage(image);
}
await repository.SaveChangesAsync(cancellationToken);
signal?.Notify();
logger.LogInformation("Hero image {ImageId} {Action} by user {UserId} ({Width}x{Height}); its variants are cut in the background", image.Id, existing is null ? "uploaded" : "repaired", callerId, image.Width, image.Height);
return HeroMapping.ToImageDto(image);
}
private async Task<bool> FilesExistAsync(HeroImage image, CancellationToken cancellationToken)
{
if (!await store.ExistsAsync(image.OriginalKey, cancellationToken))
{
return false;
}
foreach (var variant in image.Variants)
{
if (!await store.ExistsAsync($"{HeroMapping.Area}/{image.SetHash}/{variant.FileName}", cancellationToken))
{
return false;
}
}
return true;
}
public void ApplyFocus(HeroImage image, HeroFocusRequest request)
{
var general = request.Focus.ToSetting().Normalize();
image.FocusX = general.X;
image.FocusY = general.Y;
image.Zoom = general.Zoom;
Apply(request.Wide, v => (image.WideFocusX, image.WideFocusY, image.WideZoom) = v);
Apply(request.Tall, v => (image.TallFocusX, image.TallFocusY, image.TallZoom) = v);
}
public void NotifyPending() => signal?.Notify();
public async Task<HeroImageDto?> GetImageAsync(Guid imageId, CancellationToken cancellationToken)
{
var image = await repository.GetImageFreshAsync(imageId, cancellationToken);
if (image is null)
{
return null;
}
var failed = IsRenderFailed(imageId);
var progress = RenderProgress(imageId);
return HeroMapping.ToImageDto(image) with { Failed = failed, RenderDone = progress?.Done, RenderTotal = progress?.Total };
}
public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false;
public (int Done, int Total)? RenderProgress(Guid imageId) => signal?.GetProgress(imageId);
public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken)
{
_ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
signal?.Reset(imageId);
signal?.Notify();
}
public async Task<(int Total, int NeedRender, int NeedPreview)> GetRenderBacklogAsync(CancellationToken cancellationToken)
=> (await repository.CountImagesAsync(cancellationToken), (await repository.GetImagesNeedingRenderAsync(cancellationToken)).Count, (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).Count);
public async Task<Guid?> RenderNextPendingAsync(IReadOnlySet<Guid> skip, CancellationToken cancellationToken)
{
var image = (await repository.GetImagesNeedingRenderAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id));
if (image is null)
{
// Nothing to cut: add the full-frame preview to complete sets that were made before it existed.
var old = (await repository.GetImagesNeedingPreviewAsync(cancellationToken)).FirstOrDefault(i => !skip.Contains(i.Id));
return old is null ? null : await AddPreviewAsync(old, cancellationToken);
}
try
{
return await RenderAsync(image, cancellationToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Hero image {ImageId}: cutting the variants failed.", image.Id);
throw new HeroImageRenderException(image.Id, exception);
}
finally
{
signal?.ClearProgress(image.Id);
}
}
/// <summary>Makes only the preview file of a complete set (one decode, one resize) and adds it to the set where it already is.</summary>
private async Task<Guid?> AddPreviewAsync(HeroImage image, CancellationToken cancellationToken)
{
try
{
using (await gate.EnterAsync(cancellationToken))
{
var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken)
?? throw new ImageRejectedException(ImageRejection.Unavailable, "The stored original is missing.");
using var memory = new MemoryStream();
await using (file.Content)
{
await file.Content.CopyToAsync(memory, cancellationToken);
}
var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.'));
var spec = VariantPlanner.Preview(image.Width, image.Height, processor.SupportedOutput);
var rendered = (await processor.RenderSetAsync(memory.ToArray(), storedFormat, [spec], null, cancellationToken))[0];
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
await store.PutAsync($"{HeroMapping.Area}/{image.SetHash}/{name}", rendered.Bytes, cancellationToken);
image.Variants = [.. image.Variants, new HeroVariant(spec.Ratio.Name, rendered.Width, rendered.Height, spec.Format.MimeType(), name, rendered.Bytes.LongLength)];
}
await repository.SaveChangesAsync(cancellationToken);
logger.LogInformation("Hero image {ImageId}: the full-frame preview was added to its set.", image.Id);
return image.Id;
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Hero image {ImageId}: adding the preview failed.", image.Id);
throw new HeroImageRenderException(image.Id, exception);
}
}
private async Task<Guid?> RenderAsync(HeroImage image, CancellationToken cancellationToken)
{
var target = HeroMapping.TargetSetHash(image);
var oldSet = image.SetHash;
Dictionary<string, byte[]> files;
List<HeroVariant> variants;
var watch = System.Diagnostics.Stopwatch.StartNew();
logger.LogInformation("Hero image {ImageId}: cutting the variants starts ({Width}x{Height}, {Bytes} bytes, {Reason}).", image.Id, image.Width, image.Height, image.Bytes, image.Variants.Count == 0 ? "new" : "focus changed");
using (await gate.EnterAsync(cancellationToken))
{
var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken)
?? throw new ImageRejectedException(ImageRejection.Unavailable, "The stored original is missing.");
using var memory = new MemoryStream();
await using (file.Content)
{
await file.Content.CopyToAsync(memory, cancellationToken);
}
var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.'));
var lastStep = watch.Elapsed;
(files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, (done, total) =>
{
signal?.SetProgress(image.Id, done, total);
logger.LogInformation("Hero image {ImageId}: variant {Done} of {Total} in {Milliseconds:0} ms.", image.Id, done, total, (watch.Elapsed - lastStep).TotalMilliseconds);
lastStep = watch.Elapsed;
}, cancellationToken);
}
await store.PutSetAsync(HeroMapping.Area, target, files, cancellationToken);
// The focus may have been changed while the set was being cut: then this set is not the one that is wanted and the
// image stays pending (the next round cuts the right one; this set is cleaned up as an unreferenced set).
var current = await repository.GetImageFreshAsync(image.Id, cancellationToken);
if (current is null || HeroMapping.TargetSetHash(current) != target)
{
logger.LogInformation("Hero image {ImageId}: recut superseded, the focus changed while its variants were being cut; the finished set is dropped and the image is cut again at the newest focus.", image.Id);
return image.Id;
}
image.SetHash = target;
image.Variants = variants;
if (oldSet.Length > 0 && oldSet != target)
{
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = oldSet, RetiredAt = _time.GetUtcNow() });
}
await repository.SaveChangesAsync(cancellationToken);
indexCache?.Invalidate();
logger.LogInformation("Hero image {ImageId}: {Variants} variants cut in {Seconds:0.0} s", image.Id, variants.Count, watch.Elapsed.TotalSeconds);
return image.Id;
}
public async Task DeleteAsync(Guid imageId, Guid callerId, CancellationToken cancellationToken)
{
var image = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException();
if (await repository.ImageInUseAsync(imageId, cancellationToken))
{
throw new HeroImageInUseException();
}
var now = _time.GetUtcNow();
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = now });
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = now });
repository.RemoveImage(image);
await repository.SaveChangesAsync(cancellationToken);
logger.LogInformation("Hero image {ImageId} removed by user {UserId}", imageId, callerId);
}
private static void Apply(FocusDto? value, Action<(double?, double?, double?)> assign)
{
if (value is null)
{
assign((null, null, null));
return;
}
var f = value.ToSetting().Normalize();
assign((f.X, f.Y, f.Zoom));
}
/// <summary>Cuts and encodes every planned variant of the image at its current focus (one decode for the whole set).</summary>
private async Task<(Dictionary<string, byte[]> Files, List<HeroVariant> Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, Action<int, int>? progress, CancellationToken cancellationToken)
{
var ratios = new[] { Ratio.Wide, Ratio.Tall }
.Select(r => new RatioPlan(r, image.FocusFor(r.Name), VariantPlanner.DefaultWidths(r)))
.ToList();
var specs = VariantPlanner.Plan(image.Width, image.Height, ratios, processor.SupportedOutput).ToList();
// The whole picture, for the focus dialog of the admin (never published to a website).
specs.Add(VariantPlanner.Preview(image.Width, image.Height, processor.SupportedOutput));
progress?.Invoke(0, specs.Count);
var rendered = await processor.RenderSetAsync(original, storedFormat, specs, progress, cancellationToken);
var files = new Dictionary<string, byte[]>();
var variants = new List<HeroVariant>();
for (var i = 0; i < specs.Count; i++)
{
var spec = specs[i];
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
files[name] = rendered[i].Bytes;
variants.Add(new HeroVariant(spec.Ratio.Name, rendered[i].Width, rendered[i].Height, spec.Format.MimeType(), name, rendered[i].Bytes.LongLength));
}
return (files, variants);
}
private async Task EnsureRoomAsync(long incomingBytes, CancellationToken cancellationToken)
{
var limits = mediaOptions.Value;
// A set is a few times the size of the original at most; keep a margin so the write cannot fill the disk.
var needed = incomingBytes * 4;
var total = await store.GetTotalBytesAsync(cancellationToken);
if (limits.MaxTotalBytes > 0 && total + needed > limits.MaxTotalBytes)
{
throw new MediaStorageFullException();
}
if (store.GetFreeBytes() is { } free && free < needed + 64L * 1024 * 1024)
{
throw new MediaStorageFullException();
}
}
}
@@ -0,0 +1,82 @@
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Models;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>
/// The hero as a page may show it: the public hero, but only when the files of its first slide are really in the media
/// store. When they are not (a missing or restored directory) nothing of the hero is written, so a page never gets a broken
/// first image and the baked hero stays. One answer for the region, the preload and the data block.
/// </summary>
public interface IHeroPageModel
{
Task<PublicHeroDto?> GetAsync(CancellationToken cancellationToken);
}
public sealed class HeroPageModel(IPublicHeroService publicHero, IMediaStore store, ILogger<HeroPageModel> logger) : IHeroPageModel
{
public async Task<PublicHeroDto?> GetAsync(CancellationToken cancellationToken)
{
var hero = await publicHero.GetActiveAsync(cancellationToken);
if (hero is null)
{
return null;
}
var first = hero.Slides.FirstOrDefault();
if (first is null)
{
return hero;
}
foreach (var url in new[] { first.Image.Src }.Concat(first.Image.Variants.Select(v => v.Url)).Distinct())
{
if (url.StartsWith(MediaPaths.UrlPrefix, StringComparison.Ordinal)
&& !await store.ExistsAsync(url[MediaPaths.UrlPrefix.Length..], cancellationToken))
{
logger.LogWarning("The first hero slide has a file missing in the media store ({Address}); the hero is left out of the page.", url);
return null;
}
}
return hero;
}
}
/// <summary>Fills the hero regions of a page: <c>&lt;!--CMS:HERO-HTML--&gt;</c> in the body and <c>&lt;!--CMS:HERO-PRELOAD--&gt;</c> in the head. Fail-open: no hero means the baked page stays.</summary>
public sealed class HeroHtmlTransform(IHeroPageModel model, Microsoft.Extensions.Options.IOptions<HeroOptions>? options = null) : IHtmlPageTransform
{
public const string HtmlRegion = "CMS:HERO-HTML";
public const string PreloadRegion = "CMS:HERO-PRELOAD";
public bool Wants(string html)
=> html.Contains("<!--" + HtmlRegion + "-->", StringComparison.Ordinal)
|| html.Contains("<!--" + PreloadRegion + "-->", StringComparison.Ordinal);
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
{
var rendered = HeroPictureRenderer.Render(await model.GetAsync(cancellationToken), options?.Value.TextOnlyRegion ?? false, options?.Value.TitlePrefix ?? string.Empty);
if (rendered.IsEmpty)
{
return html;
}
html = HtmlMarkerEditor.ReplaceRegion(html, HtmlRegion, rendered.Region);
// A text-only hero has nothing to preload: the page keeps whatever its build put there.
return rendered.Preload.Length == 0 ? html : HtmlMarkerEditor.ReplaceRegion(html, PreloadRegion, rendered.Preload);
}
}
/// <summary>The <c>hero</c> key of the page data block: the complete public hero (all slides), or null.</summary>
public sealed class HeroDataContributor(IHeroPageModel model) : ICmsDataBlockContributor
{
public string Key => "hero";
public int Order => 200;
public async Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken)
=> await model.GetAsync(cancellationToken);
}
@@ -0,0 +1,180 @@
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
namespace SlpModularCms.Modules.Hero.Services;
public sealed class HeroOptions
{
public const string SectionName = "Hero";
/// <summary>
/// A technical safety ceiling, not an administrator limit: the public hero, the injected page and the data block never
/// carry more slides than this, so the first HTML cannot grow without bound. Everything above stays saved.
/// </summary>
public int MaxPublishedSlides { get; set; } = 50;
/// <summary>
/// Whether a hero without any slide is written into the page as text (title lines, lead, buttons). On for a site
/// that has no images in its hero (SLP Software); off by default, so a site whose baked hero is a picture (ASF) keeps
/// that baked hero when the CMS hero has no slide (fail open).
/// </summary>
public bool TextOnlyRegion { get; set; }
/// <summary>
/// Brand text written as the first, visually hidden part of the h1 of the text-only region (SLP Software:
/// "SLP Software: "), so crawlers and screen readers read the full title. Empty (default) writes nothing.
/// </summary>
public string TitlePrefix { get; set; } = string.Empty;
}
/// <summary>Turns stored entities into the shapes of the API and the data block. Pure.</summary>
public static class HeroMapping
{
public const string Area = "hero";
/// <summary>The accepted upload formats (as the host allows them right now) and limits, in the words an administrator knows.</summary>
public static HeroUploadLimitsDto UploadLimits(MediaStatus status, MediaOptions limits)
=> new(
status.AcceptedInput.OrderBy(f => f).Select(DisplayName).ToList(),
limits.MaxUploadBytes,
(int)Math.Max(1, limits.MaxPixels / 1_000_000));
private static string DisplayName(ImageFormat format) => format switch
{
ImageFormat.Jpeg => "JPEG",
ImageFormat.WebP => "WebP",
_ => format.ToString().ToUpperInvariant(),
};
/// <summary>The variants websites and the admin lists use: everything except the full-frame preview.</summary>
public static IEnumerable<HeroVariant> Published(HeroImage image) => image.Variants.Where(v => v.Ratio != VariantPlanner.PreviewRatioName);
/// <summary>The address of the full-frame preview for the focus dialog, or null while the set has none yet.</summary>
public static string? PreviewSrc(HeroImage image)
=> image.Variants.FirstOrDefault(v => v.Ratio == VariantPlanner.PreviewRatioName) is { } preview
? MediaPaths.Url(Area, image.SetHash, preview.FileName)
: null;
/// <summary>A complete, current set that lacks the preview (made before it existed): only that file is added, quietly.</summary>
public static bool NeedsPreview(HeroImage image)
=> !NeedsRender(image) && image.Variants.All(v => v.Ratio != VariantPlanner.PreviewRatioName);
/// <summary>The set hash the image should have at its current focus.</summary>
public static string TargetSetHash(HeroImage image)
=> MediaPaths.SetHash(image.OriginalSha256, [("wide", image.FocusFor("wide")), ("tall", image.FocusFor("tall"))]);
/// <summary>True when the image has no set yet or its set was cut for another focus (the work of the background renderer).</summary>
public static bool NeedsRender(HeroImage image) => image.Variants.Count == 0 || image.SetHash != TargetSetHash(image);
public static HeroVariantDto ToDto(HeroImage image, HeroVariant variant)
=> new(variant.Ratio, variant.Width, variant.Height, variant.Type, MediaPaths.Url(Area, image.SetHash, variant.FileName));
/// <summary>The address the plain <c>img</c> uses: the largest WebP of the wide ratio, else the largest WebP, else the first variant.</summary>
public static string SrcFor(HeroImage image)
{
var webp = Published(image).Where(v => v.Type == ImageFormat.WebP.MimeType()).ToList();
var pick = webp.Where(v => v.Ratio == "wide").OrderByDescending(v => v.Width).FirstOrDefault()
?? webp.OrderByDescending(v => v.Width).FirstOrDefault()
?? Published(image).FirstOrDefault();
return pick is null ? string.Empty : MediaPaths.Url(Area, image.SetHash, pick.FileName);
}
public static HeroImageDto ToImageDto(HeroImage image)
=> new(
image.Id.ToString(),
image.Width,
image.Height,
FocusDto.From(image.General),
image.WideFocusX is null ? null : FocusDto.From(image.FocusFor("wide")),
image.TallFocusX is null ? null : FocusDto.From(image.FocusFor("tall")),
SrcFor(image),
Published(image).Select(v => ToDto(image, v)).ToList(),
NeedsRender(image),
PreviewSrc: PreviewSrc(image));
/// <summary>The hero is always on: it is live as soon as it has usable content (a title). There is no switch and no schedule (a deliberate choice; they may come back later, per slide).</summary>
public static bool IsLive(HeroContent hero) => !string.IsNullOrWhiteSpace(hero.Title);
private static List<HeroButtonDto> Buttons(HeroContent hero)
{
var buttons = new List<HeroButtonDto>();
if (!string.IsNullOrWhiteSpace(hero.Button1Label) && !string.IsNullOrWhiteSpace(hero.Button1Target))
{
buttons.Add(new HeroButtonDto(hero.Button1Label, hero.Button1Target));
}
if (!string.IsNullOrWhiteSpace(hero.Button2Label) && !string.IsNullOrWhiteSpace(hero.Button2Target))
{
buttons.Add(new HeroButtonDto(hero.Button2Label, hero.Button2Target));
}
return buttons;
}
/// <summary>The published hero, or null when it is not live. Inactive slides and slides without a stored image are left out.</summary>
public static PublicHeroDto? ToPublic(HeroContent hero, DateTimeOffset now, int maxPublishedSlides)
{
if (!IsLive(hero))
{
return null;
}
var slides = hero.Slides
.Where(s => s.IsActive && s.Image is { Variants.Count: > 0 } && (s.Decorative || !string.IsNullOrWhiteSpace(s.Alt)))
.OrderBy(s => s.SortOrder)
.Take(Math.Max(1, maxPublishedSlides))
.Select(s => new PublicSlideDto(s.Id.ToString(), s.Decorative ? string.Empty : s.Alt, s.Decorative, ToPublicImage(s.Image!)))
.ToList();
return new PublicHeroDto(hero.Display, hero.Title, hero.Tagline, hero.Lead, Buttons(hero), slides);
}
private static PublicImageDto ToPublicImage(HeroImage image)
{
Dictionary<string, FocusDto>? byRatio = null;
if (image.WideFocusX is not null)
{
(byRatio ??= new Dictionary<string, FocusDto>())["wide"] = FocusDto.From(image.FocusFor("wide"));
}
if (image.TallFocusX is not null)
{
(byRatio ??= new Dictionary<string, FocusDto>())["tall"] = FocusDto.From(image.FocusFor("tall"));
}
return new PublicImageDto(
image.Width,
image.Height,
FocusDto.From(image.General),
SrcFor(image),
Published(image).Select(v => ToDto(image, v)).ToList(),
byRatio);
}
public static HeroAdminDto ToAdmin(HeroContent? hero, DateTimeOffset now, int maxPublishedSlides)
{
if (hero is null)
{
return new HeroAdminDto(null, string.Empty, null, null, HeroDisplays.Single, [], [], false, null, maxPublishedSlides);
}
var slides = hero.Slides
.Where(s => s.Image is not null)
.OrderBy(s => s.SortOrder)
.Select(s => new HeroSlideAdminDto(s.Id.ToString(), s.ImageId.ToString(), s.Alt, s.Decorative, s.IsActive, s.SortOrder, ToImageDto(s.Image!)))
.ToList();
return new HeroAdminDto(
hero.Id.ToString(),
hero.Title,
hero.Tagline,
hero.Lead,
hero.Display,
Buttons(hero),
slides,
IsLive(hero),
hero.UpdatedAt,
maxPublishedSlides);
}
}
@@ -0,0 +1,111 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Media;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Repositories;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>
/// Removes files nothing uses any more, a retention time (7 days) after they stopped being used, so a site that still
/// holds older data keeps working for a while. Idempotent and safe to interrupt; a referenced file is never removed
/// (a retired set whose hash is used again, because the same photo and focus came back, is simply forgotten as retired).
/// </summary>
public class HeroMediaCleanup(
IHeroRepository repository,
IMediaStore store,
IOptions<MediaOptions> options,
ILogger<HeroMediaCleanup> logger,
TimeProvider? time = null)
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<int> RunOnceAsync(CancellationToken cancellationToken)
{
var now = _time.GetUtcNow();
var cutoff = now.AddDays(-Math.Max(0, options.Value.RetentionDays));
var removed = 0;
// 1. Images that were uploaded but never used by a slide: retire them (their files go on a later run).
foreach (var image in await repository.GetUnattachedImagesCreatedBeforeAsync(cutoff, cancellationToken))
{
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) });
repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) });
repository.RemoveImage(image);
}
await repository.SaveChangesAsync(cancellationToken);
// 2. Retired files whose retention has passed, unless an image uses them again.
var referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken);
var referencedOriginals = await repository.GetReferencedOriginalKeysAsync(cancellationToken);
foreach (var file in await repository.GetRetiredBeforeAsync(cutoff, cancellationToken))
{
if (file.Kind == "set" && !referencedSets.Contains(file.Key))
{
await store.DeleteSetAsync(HeroMapping.Area, file.Key, cancellationToken);
removed++;
}
else if (file.Kind == "original" && !referencedOriginals.Contains(file.Key))
{
await store.DeleteAsync(file.Key, cancellationToken);
removed++;
}
repository.RemoveRetired(file);
}
await repository.SaveChangesAsync(cancellationToken);
// 3. Sets in the store that no row knows (a crash between writing the files and saving the row).
var retiredSets = await repository.GetRetiredSetHashesAsync(cancellationToken);
referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken);
foreach (var set in await store.ListSetsAsync(HeroMapping.Area, cancellationToken))
{
if (set.CreatedUtc < cutoff && !referencedSets.Contains(set.SetHash) && !retiredSets.Contains(set.SetHash))
{
await store.DeleteSetAsync(HeroMapping.Area, set.SetHash, cancellationToken);
removed++;
}
}
if (removed > 0)
{
logger.LogInformation("Hero media clean-up removed {Removed} unused sets or originals.", removed);
}
return removed;
}
}
/// <summary>Runs <see cref="HeroMediaCleanup"/> shortly after start-up and then once a day.</summary>
public sealed class HeroMediaCleanupService(IServiceScopeFactory scopes, ILogger<HeroMediaCleanupService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
try
{
using var scope = scopes.CreateScope();
await scope.ServiceProvider.GetRequiredService<HeroMediaCleanup>().RunOnceAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "The hero media clean-up failed; it is tried again tomorrow.");
}
await Task.Delay(TimeSpan.FromHours(24), stoppingToken);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
}
@@ -0,0 +1,258 @@
using System.Globalization;
using System.Text;
using SlpModularCms.Modules.Hero.Models;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>The two pieces written into a page: the hero itself and the preload links for the head. Both empty = leave the baked page.</summary>
public sealed record HeroInjection(string Region, string Preload)
{
public static readonly HeroInjection None = new(string.Empty, string.Empty);
public bool IsEmpty => Region.Length == 0;
}
/// <summary>
/// The plain HTML of the hero for the first response, written exactly as the website's golden files expect
/// (aidlc-docs/features/hero-module, site contract; ASF commit 13f78c4). One place produces the picture and the preload,
/// so the <c>srcset</c>, <c>sizes</c> and <c>media</c> values are the same text in both. Pure; everything is HTML-encoded.
/// </summary>
/// <remarks>
/// The rules are those of the website: a hero needs a title and a usable first slide (an id, an own-host path, a size, and
/// an alt text or the decorative flag), only WebP is used for the first image (a preload cannot say "AVIF if supported"),
/// at most two buttons, and unusable parts are skipped, never repaired. They are deliberately a little wider than what the
/// CMS accepts when saving, because the renderer must also cope with data it did not validate itself.
/// </remarks>
public static class HeroPictureRenderer
{
private const string Webp = "image/webp";
private const string Portrait = "(orientation: portrait)";
private const string Landscape = "(orientation: landscape)";
public static HeroInjection Render(PublicHeroDto? hero, bool textOnlyRegion = false, string titlePrefix = "")
{
if (hero is null || string.IsNullOrWhiteSpace(hero.Title))
{
return HeroInjection.None;
}
// A hero without any slide is a text-only hero (SLP Software, when the site asks for it with
// Hero:TextOnlyRegion): the region holds the title lines, the lead and the buttons, and there is nothing to
// preload. Otherwise, and for a hero WITH slides of which none is usable, the region stays empty (ASF).
if ((hero.Slides ?? []).Count == 0)
{
return textOnlyRegion ? new HeroInjection(TextOnlyRegion(hero, titlePrefix), string.Empty) : HeroInjection.None;
}
var slide = (hero.Slides ?? []).FirstOrDefault(IsUsable);
if (slide is null)
{
return HeroInjection.None;
}
var image = slide.Image;
var variants = (image.Variants ?? []).Where(v => IsUsable(v) && v.Type == Webp).ToList();
var wide = SrcSet(variants, "wide");
var tall = SrcSet(variants, "tall");
return new HeroInjection(Region(hero, slide, wide, tall), Preload(image.Src, wide, tall));
}
private static string Region(PublicHeroDto hero, PublicSlideDto slide, string wide, string tall)
{
var image = slide.Image;
var html = new StringBuilder();
html.Append("<picture class=\"cms-hero-picture\">");
if (tall.Length > 0)
{
html.Append("<source media=\"").Append(Portrait).Append("\" srcset=\"").Append(Encode(tall)).Append("\" sizes=\"100vw\" type=\"image/webp\">");
}
html.Append("<img class=\"cms-hero-image\" src=\"").Append(Encode(image.Src)).Append('"');
if (wide.Length > 0)
{
html.Append(" srcset=\"").Append(Encode(wide)).Append("\" sizes=\"100vw\"");
}
html.Append(" width=\"").Append(image.Width.ToString(CultureInfo.InvariantCulture)).Append("\" height=\"").Append(image.Height.ToString(CultureInfo.InvariantCulture)).Append('"');
html.Append(" alt=\"").Append(slide.Decorative ? string.Empty : Encode(slide.Alt)).Append('"');
if (Position(image) is { } position)
{
html.Append(" style=\"object-position:").Append(position).Append('"');
}
html.Append(" fetchpriority=\"high\"></picture>");
html.Append("<h1 class=\"cms-hero-title\">");
if (!string.IsNullOrWhiteSpace(hero.Tagline))
{
html.Append("<span class=\"cms-hero-tagline\">").Append(Encode(hero.Tagline)).Append("</span> ");
}
html.Append("<span class=\"cms-hero-name\">").Append(Lines(hero.Title)).Append("</span></h1>");
AppendButtons(html, hero);
return html.ToString();
}
/// <summary>
/// The text-only hero: <c>h1.cms-hero-title</c> with the tagline (when there is one) and the title lines in
/// <c>span.cms-hero-title-lines</c> separated by <c>&lt;br&gt;</c> (no brand prefix: the site adds it to its own
/// screen-reader title), then <c>p.cms-hero-lead</c> and the buttons row.
/// </summary>
private static string TextOnlyRegion(PublicHeroDto hero, string titlePrefix)
{
var html = new StringBuilder();
html.Append("<h1 class=\"cms-hero-title\">");
// The brand prefix (Hero:TitlePrefix) is text for crawlers and screen readers; the site styles it visually hidden.
if (!string.IsNullOrWhiteSpace(titlePrefix))
{
html.Append("<span class=\"cms-hero-title-prefix\">").Append(Encode(titlePrefix)).Append("</span>");
}
if (!string.IsNullOrWhiteSpace(hero.Tagline))
{
html.Append("<span class=\"cms-hero-tagline\">").Append(Encode(hero.Tagline)).Append("</span> ");
}
html.Append("<span class=\"cms-hero-title-lines\">").Append(Lines(hero.Title)).Append("</span></h1>");
if (!string.IsNullOrWhiteSpace(hero.Lead))
{
html.Append("<p class=\"cms-hero-lead\">").Append(Lines(hero.Lead.Trim())).Append("</p>");
}
AppendButtons(html, hero);
return html.ToString();
}
private static string Lines(string text) => string.Join("<br>", text.Replace("\r\n", "\n").Split('\n').Select(Encode));
private static void AppendButtons(StringBuilder html, PublicHeroDto hero)
{
var buttons = (hero.Buttons ?? []).Where(IsUsable).Take(2).ToList();
if (buttons.Count == 0)
{
return;
}
html.Append("<p class=\"cms-hero-buttons\">");
foreach (var button in buttons)
{
html.Append("<a class=\"cms-hero-button\" href=\"").Append(Encode(button.Target)).Append("\">").Append(Encode(button.Label)).Append("</a>");
}
html.Append("</p>");
}
private static string Preload(string src, string wide, string tall)
{
const string head = "<link rel=\"preload\" as=\"image\"";
const string tail = " fetchpriority=\"high\">";
string Link(string set, string? media)
{
var link = new StringBuilder(head);
if (set.Length > 0)
{
link.Append(" imagesrcset=\"").Append(Encode(set)).Append("\" imagesizes=\"100vw\"");
}
else
{
link.Append(" href=\"").Append(Encode(src)).Append('"');
}
if (media is not null)
{
link.Append(" media=\"").Append(media).Append('"');
}
return link.Append(tail).ToString();
}
return tall.Length > 0 ? Link(tall, Portrait) + Link(wide, Landscape) : Link(wide, null);
}
// ----- what counts as usable (the rules of the website) -------------------------------------------------------
private static bool IsUsable(PublicSlideDto? slide)
=> slide is { Image: not null }
&& !string.IsNullOrEmpty(slide.Id)
&& IsHostPath(slide.Image.Src)
&& slide.Image.Width > 0
&& slide.Image.Height > 0
&& (slide.Decorative || !string.IsNullOrWhiteSpace(slide.Alt));
private static bool IsUsable(HeroVariantDto variant)
=> !string.IsNullOrEmpty(variant.Ratio) && !string.IsNullOrEmpty(variant.Type) && variant.Width > 0 && IsHostPath(variant.Url);
private static bool IsUsable(HeroButtonDto button)
=> !string.IsNullOrWhiteSpace(button.Label) && IsButtonTarget(button.Target);
/// <summary>A path of this host: one leading slash, not a protocol-relative address, no backslash.</summary>
private static bool IsHostPath(string? value)
=> !string.IsNullOrEmpty(value) && value[0] == '/' && !value.StartsWith("//", StringComparison.Ordinal) && !value.Contains('\\');
private static bool IsButtonTarget(string? target)
{
if (string.IsNullOrEmpty(target))
{
return false;
}
if (target[0] == '#')
{
return target.Length > 1 && !target.Any(char.IsWhiteSpace);
}
if (target[0] == '/')
{
return IsHostPath(target);
}
return target.StartsWith("https://", StringComparison.Ordinal)
&& Uri.TryCreate(target, UriKind.Absolute, out var uri)
&& uri.Scheme == Uri.UriSchemeHttps
&& !string.IsNullOrEmpty(uri.Host);
}
private static string SrcSet(IEnumerable<HeroVariantDto> variants, string ratio)
=> string.Join(", ", variants.Where(v => v.Ratio == ratio).OrderBy(v => v.Width).Select(v => v.Url + " " + v.Width.ToString(CultureInfo.InvariantCulture) + "w"));
/// <summary><c>X% Y%</c> from a focus point inside the image (at most two decimals, no trailing zeros), or null without a valid one.</summary>
private static string? Position(PublicImageDto image)
{
var focus = image.Focus;
if (focus is null || focus.X is < 0 or > 1 || focus.Y is < 0 or > 1 || double.IsNaN(focus.X) || double.IsNaN(focus.Y))
{
return null;
}
return Percent(focus.X) + "% " + Percent(focus.Y) + "%";
}
private static string Percent(double fraction)
=> Math.Round(fraction * 100, 2, MidpointRounding.AwayFromZero).ToString("0.##", CultureInfo.InvariantCulture);
private static string Encode(string? value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
var builder = new StringBuilder(value.Length + 8);
foreach (var c in value)
{
switch (c)
{
case '&': builder.Append("&amp;"); break;
case '<': builder.Append("&lt;"); break;
case '>': builder.Append("&gt;"); break;
case '"': builder.Append("&quot;"); break;
case '\'': builder.Append("&#39;"); break;
default: builder.Append(c); break;
}
}
return builder.ToString();
}
}
@@ -0,0 +1,151 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Media;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>Wakes the background renderer after an upload or a save. A singleton shared by the request and the worker.</summary>
public sealed class HeroRenderSignal
{
private readonly SemaphoreSlim _signal = new(0, 1);
public void Notify()
{
try
{
_signal.Release();
}
catch (SemaphoreFullException)
{
// Already signalled; the worker will look at everything that is pending.
}
}
/// <summary>How often the renderer may try an image before it gives up on it (until a retry or a restart).</summary>
public const int MaxAttempts = 3;
private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, int> _failures = new();
public int RecordFailure(Guid imageId) => _failures.AddOrUpdate(imageId, 1, (_, n) => n + 1);
public bool IsGivenUp(Guid imageId) => _failures.TryGetValue(imageId, out var n) && n >= MaxAttempts;
public IReadOnlySet<Guid> GivenUp() => _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet();
private readonly System.Collections.Concurrent.ConcurrentDictionary<Guid, (int Done, int Total)> _progress = new();
public void SetProgress(Guid imageId, int done, int total) => _progress[imageId] = (done, total);
public (int Done, int Total)? GetProgress(Guid imageId) => _progress.TryGetValue(imageId, out var p) ? p : null;
public void ClearProgress(Guid imageId) => _progress.TryRemove(imageId, out _);
/// <summary>Forgets the failures of an image (after a success, a retry or a new upload of the same photo).</summary>
public void Reset(Guid imageId) => _failures.TryRemove(imageId, out _);
/// <summary>Waits for a signal or for the timeout, whichever comes first.</summary>
public Task<bool> WaitAsync(TimeSpan timeout, CancellationToken cancellationToken) => _signal.WaitAsync(timeout, cancellationToken);
}
/// <summary>
/// Cuts the variants of images that have none yet (upload) or whose set no longer matches their focus (a saved focus
/// change), one image at a time, so an upload or a save never waits for the slow part. An image goes live (or switches to
/// its new set) the moment its set is complete; until then the old set, or nothing, is published. Looks again when woken
/// and at least every minute, and at start-up, so work interrupted by a restart is picked up.
/// </summary>
public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, IOptions<MediaOptions> media, ILogger<HeroRenderService> logger) : BackgroundService
{
private bool _loggedFirstPass;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
// Let the start-up (migrations, health check) finish first; whatever was still pending is picked up then.
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
logger.LogInformation("Hero image renderer started.");
while (!stoppingToken.IsCancellationRequested)
{
try
{
await RunPassAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
// Whatever happens, the renderer does not stop: it logs and looks again at the next wake-up.
logger.LogError(exception, "A round of the hero image renderer failed; it goes on.");
}
await signal.WaitAsync(TimeSpan.FromMinutes(1), stoppingToken);
}
}
catch (OperationCanceledException)
{
// Shutting down.
}
}
private async Task RunPassAsync(CancellationToken stoppingToken)
{
// An image that failed in this pass waits for the next pass (a minute later or the next wake-up): one difficult photo
// must not hold up the others, and it is not retried straight away.
var failedInThisPass = new HashSet<Guid>();
try
{
using var scope = scopes.CreateScope();
var (total, needRender, needPreview) = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().GetRenderBacklogAsync(stoppingToken);
if (needRender + needPreview > 0 || !_loggedFirstPass)
{
logger.LogInformation("Hero image renderer: {Total} images, {NeedRender} waiting for their variants, {NeedPreview} lacking the full-frame preview.", total, needRender, needPreview);
}
_loggedFirstPass = true;
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogWarning(exception, "Counting the hero images that wait for the renderer failed.");
}
while (!stoppingToken.IsCancellationRequested)
{
var skip = signal.GivenUp().Concat(failedInThisPass).ToHashSet();
Guid? handled = null;
try
{
using var scope = scopes.CreateScope();
// A hard limit around one image, a little above the time the image library itself is allowed: nothing can
// keep the renderer waiting for ever.
using var limit = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
limit.CancelAfter(TimeSpan.FromSeconds(Math.Max(30, media.Value.RenderSetTimeoutSeconds) + 60));
handled = await scope.ServiceProvider.GetRequiredService<IHeroImageService>().RenderNextPendingAsync(skip, limit.Token);
}
catch (OperationCanceledException) when (!stoppingToken.IsCancellationRequested)
{
logger.LogError("Cutting a hero image took longer than the hard limit; the round is stopped and tried again at the next wake-up.");
return;
}
catch (HeroImageRenderException failure)
{
failedInThisPass.Add(failure.ImageId);
var attempts = signal.RecordFailure(failure.ImageId);
logger.LogError(failure.InnerException, "Cutting the variants of hero image {ImageId} failed (attempt {Attempt} of {Max}).", failure.ImageId, attempts, HeroRenderSignal.MaxAttempts);
continue;
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Looking for hero images to cut failed; trying again at the next wake-up.");
return;
}
if (handled is null)
{
return;
}
signal.Reset(handled.Value);
}
}
}
@@ -0,0 +1,188 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Core.Texts;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Repositories;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>A save that refers to something that is not there (an image or a slide of another hero). The message is for the administrator.</summary>
public sealed class HeroSaveRejectedException(string field, string message) : Exception(message)
{
public string Field { get; } = field;
}
public interface IHeroService
{
/// <summary>The hero for the admin (empty defaults when none exists yet), with whether it is live right now.</summary>
Task<HeroAdminDto> GetAsync(CancellationToken cancellationToken);
/// <summary>Replaces the whole hero: texts, buttons, display and the ordered slide list.</summary>
Task<HeroAdminDto> SaveAsync(HeroSaveRequest request, Guid callerId, CancellationToken cancellationToken);
}
public class HeroService(
IHeroRepository repository,
IHeroImageService imageService,
IOptions<HeroOptions> options,
ILogger<HeroService> logger,
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
ITextCatalog? texts = null) : IHeroService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
public async Task<HeroAdminDto> GetAsync(CancellationToken cancellationToken)
=> WithFailures(HeroMapping.ToAdmin(await repository.GetReadOnlyAsync(HeroLanguages.Default, cancellationToken), _time.GetUtcNow(), options.Value.MaxPublishedSlides));
/// <summary>Adds what only the running renderer knows (gave up on the image, which variant it is cutting); it lives in memory, not in the database.</summary>
private HeroAdminDto WithFailures(HeroAdminDto dto)
=> dto with
{
Slides = dto.Slides.Select(s =>
{
var id = Guid.Parse(s.ImageId);
var progress = imageService.RenderProgress(id);
var failed = imageService.IsRenderFailed(id);
return failed || progress is not null
? s with { Image = s.Image with { Failed = failed, RenderDone = progress?.Done, RenderTotal = progress?.Total } }
: s;
}).ToList(),
};
public async Task<HeroAdminDto> SaveAsync(HeroSaveRequest request, Guid callerId, CancellationToken cancellationToken)
{
var now = _time.GetUtcNow();
var slideRequests = request.Slides ?? [];
var focusRequests = request.ImageFocus ?? [];
var imageIds = slideRequests.Select(s => s.ImageId).Concat(focusRequests.Select(f => f.ImageId)).Distinct().ToList();
var images = (await repository.GetImagesAsync(imageIds, cancellationToken)).ToDictionary(i => i.Id);
for (var i = 0; i < focusRequests.Count; i++)
{
if (!images.ContainsKey(focusRequests[i].ImageId))
{
throw new HeroSaveRejectedException($"ImageFocus[{i}].ImageId", _texts.Get(HeroTexts.Validation.Slide.ImageUnknown));
}
}
for (var i = 0; i < slideRequests.Count; i++)
{
if (!images.ContainsKey(slideRequests[i].ImageId))
{
throw new HeroSaveRejectedException($"Slides[{i}].ImageId", _texts.Get(HeroTexts.Validation.Slide.ImageUnknown));
}
}
var hero = await repository.GetAsync(HeroLanguages.Default, cancellationToken);
var created = hero is null;
if (hero is null)
{
hero = new HeroContent { Id = Guid.NewGuid(), Language = HeroLanguages.Default };
repository.Add(hero);
}
var existing = hero.Slides.ToDictionary(s => s.Id);
for (var i = 0; i < slideRequests.Count; i++)
{
if (slideRequests[i].Id is { } id && !existing.ContainsKey(id))
{
throw new HeroSaveRejectedException($"Slides[{i}].Id", _texts.Get(HeroTexts.Validation.Slide.IdUnknown));
}
}
Apply(hero, request, now, callerId);
var keep = new HashSet<Guid>();
for (var i = 0; i < slideRequests.Count; i++)
{
var r = slideRequests[i];
HeroSlide slide;
if (r.Id is { } id)
{
slide = existing[id];
}
else
{
slide = new HeroSlide { Id = Guid.NewGuid(), HeroId = hero.Id };
hero.Slides.Add(slide);
repository.AddSlide(slide);
}
keep.Add(slide.Id);
slide.ImageId = r.ImageId;
slide.Image = images[r.ImageId];
slide.Decorative = r.Decorative;
slide.Alt = r.Decorative ? string.Empty : HeroText.Clean(r.Alt, keepLineBreaks: false);
slide.IsActive = r.IsActive;
slide.SortOrder = i;
}
foreach (var removed in hero.Slides.Where(s => !keep.Contains(s.Id)).ToList())
{
hero.Slides.Remove(removed);
if (!created)
{
repository.RemoveSlide(removed);
}
}
// A changed focus is only recorded here: the new variants are cut in the background into new files and the image
// switches to them when they are complete, so saving is quick and nothing live changes half-way.
foreach (var edit in focusRequests)
{
imageService.ApplyFocus(images[edit.ImageId], new HeroFocusRequest(edit.Focus, edit.Wide, edit.Tall));
}
await repository.SaveChangesAsync(cancellationToken);
indexCache?.Invalidate();
imageService.NotifyPending();
logger.LogInformation("Hero {HeroId} {Action} by user {LastModifiedByUserId} ({Slides} slides)", hero.Id, created ? "Created" : "Updated", callerId, keep.Count);
return WithFailures(HeroMapping.ToAdmin(hero, now, options.Value.MaxPublishedSlides));
}
private static void Apply(HeroContent hero, HeroSaveRequest r, DateTimeOffset now, Guid callerId)
{
hero.Title = HeroText.Clean(r.Title, keepLineBreaks: true);
hero.Tagline = NullIfEmpty(HeroText.Clean(r.Tagline, keepLineBreaks: false));
hero.Lead = NullIfEmpty(HeroText.Clean(r.Lead, keepLineBreaks: true));
hero.Display = r.Display ?? HeroDisplays.Single;
var buttons = (r.Buttons ?? [])
.Select(b => (Label: HeroText.Clean(b.Label, keepLineBreaks: false), Target: (b.Target ?? string.Empty).Trim()))
.Where(b => b.Label.Length > 0 && b.Target.Length > 0)
.Take(HeroSaveRequest.ButtonsMax)
.ToList();
hero.Button1Label = buttons.Count > 0 ? buttons[0].Label : null;
hero.Button1Target = buttons.Count > 0 ? buttons[0].Target : null;
hero.Button2Label = buttons.Count > 1 ? buttons[1].Label : null;
hero.Button2Target = buttons.Count > 1 ? buttons[1].Target : null;
hero.UpdatedAt = now;
hero.LastModifiedByUserId = callerId;
}
private static string? NullIfEmpty(string value) => value.Length == 0 ? null : value;
}
public interface IPublicHeroService
{
/// <summary>The hero as websites get it, or null (no hero or without a title).</summary>
Task<PublicHeroDto?> GetActiveAsync(CancellationToken cancellationToken);
}
public class PublicHeroService(IHeroRepository repository, IOptions<HeroOptions> options, TimeProvider? time = null) : IPublicHeroService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
public async Task<PublicHeroDto?> GetActiveAsync(CancellationToken cancellationToken)
{
var hero = await repository.GetReadOnlyAsync(HeroLanguages.Default, cancellationToken);
return hero is null ? null : HeroMapping.ToPublic(hero, _time.GetUtcNow(), options.Value.MaxPublishedSlides);
}
}
@@ -0,0 +1,24 @@
<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.Hero.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
<ProjectReference Include="..\SlpModularCms.Core.Media.Magick\SlpModularCms.Core.Media.Magick.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Texts\*.json" WithCulture="false" />
</ItemGroup>
</Project>
@@ -0,0 +1,51 @@
{
"hero": {
"validation": {
"title": {
"required": "A title is required.",
"tooLong": "The title can have at most {max} characters.",
"tooManyLines": "The title can have at most {max} lines."
},
"tagline": {
"tooLong": "The short text above the title can have at most {max} characters."
},
"lead": {
"tooLong": "The introduction can have at most {max} characters."
},
"display": {
"invalid": "Choose ‘Single’ or ‘Slideshow’."
},
"button": {
"incomplete": "A button needs both a label and a target.",
"labelTooLong": "The button label can have at most {max} characters.",
"targetTooLong": "The button target can have at most {max} characters.",
"targetInvalid": "Choose a part of the page (for example #contact), a page of the site (for example /privacy) or a full web address (https://…).",
"tooMany": "At most {max} buttons are possible."
},
"slide": {
"imageRequired": "Every image in the list needs a photo.",
"imageUnknown": "An image in the list does not exist (any more).",
"altRequired": "Give a description or choose ‘Decoration only’.",
"altTooLong": "The description can have at most {max} characters.",
"idUnknown": "An image in the list does not belong to this hero."
},
"focus": {
"invalid": "The point of the subject must be inside the photo and the zoom between 1 and {maxZoom}."
}
},
"image": {
"inUse": "This image is still used in the hero.",
"notFound": "This image does not exist.",
"rejected": {
"tooLarge": "The image is too large (at most {max} MB).",
"unsupportedFormat": "This file type is not supported. The types that work are listed above the upload button.",
"corrupt": "The image could not be read or is damaged.",
"tooManyPixels": "The image has too many pixels. Choose a smaller image.",
"timeout": "Processing the image took too long. Try a smaller image.",
"busy": "Other images are being processed. Try again in a moment.",
"unavailable": "Uploading images is not available at the moment."
},
"storageFull": "There is not enough storage space for this image."
}
}
}
@@ -0,0 +1,51 @@
{
"hero": {
"validation": {
"title": {
"required": "Een titel is verplicht.",
"tooLong": "De titel mag maximaal {max} tekens bevatten.",
"tooManyLines": "De titel mag maximaal {max} regels hebben."
},
"tagline": {
"tooLong": "De korte tekst boven de titel mag maximaal {max} tekens bevatten."
},
"lead": {
"tooLong": "De introductietekst mag maximaal {max} tekens bevatten."
},
"display": {
"invalid": "Kies ‘Enkel’ of ‘Diavoorstelling’."
},
"button": {
"incomplete": "Een knop heeft zowel een tekst als een doel nodig.",
"labelTooLong": "De knoptekst mag maximaal {max} tekens bevatten.",
"targetTooLong": "Het doel van een knop mag maximaal {max} tekens bevatten.",
"targetInvalid": "Kies een onderdeel van de pagina (bijvoorbeeld #contact), een pagina van de site (bijvoorbeeld /privacy) of een volledig webadres (https://…).",
"tooMany": "Er zijn maximaal {max} knoppen mogelijk."
},
"slide": {
"imageRequired": "Elke afbeelding in de lijst heeft een foto nodig.",
"imageUnknown": "Een afbeelding in de lijst bestaat niet (meer).",
"altRequired": "Geef een beschrijving of kies ‘Alleen versiering’.",
"altTooLong": "De beschrijving mag maximaal {max} tekens bevatten.",
"idUnknown": "Een afbeelding in de lijst hoort niet bij deze hero."
},
"focus": {
"invalid": "Het punt van het onderwerp moet binnen de foto liggen en het inzoomen tussen 1 en {maxZoom}."
}
},
"image": {
"inUse": "Deze afbeelding wordt nog gebruikt in de hero.",
"notFound": "Deze afbeelding bestaat niet.",
"rejected": {
"tooLarge": "De afbeelding is te groot (maximaal {max} MB).",
"unsupportedFormat": "Dit bestandstype wordt niet ondersteund. Boven de uploadknop staat welke typen kunnen.",
"corrupt": "De afbeelding kon niet worden gelezen of is beschadigd.",
"tooManyPixels": "De afbeelding heeft te veel pixels. Kies een kleinere afbeelding.",
"timeout": "Het verwerken van de afbeelding duurde te lang. Probeer een kleinere afbeelding.",
"busy": "Er worden nu andere afbeeldingen verwerkt. Probeer het zo opnieuw.",
"unavailable": "Afbeeldingen uploaden is op dit moment niet beschikbaar."
},
"storageFull": "Er is te weinig opslagruimte voor deze afbeelding."
}
}
}
@@ -16,6 +16,9 @@ public class OfferingsHtmlTransformTests
private OfferingsHtmlTransform Sut() => new(_offerings, _banners);
// The data block is composed by the Core transform from the contributors of the Offerings module (same output as before).
private CmsDataBlockTransform DataBlock() => new([new OfferingsDataContributor(_offerings), new BannersDataContributor(_banners)]);
private static OfferingDto Offering(string slug, string title, string price, int order = 0, bool featured = false) => new(
Guid.NewGuid().ToString(), slug, title, "Beschrijving", price, "eenmalig", ["1 uur fotografie", "Online galerij"], "Boek", featured, order);
@@ -92,7 +95,7 @@ public class OfferingsHtmlTransformTests
Banner("top", top);
Banner("bottom", null);
var html = await Sut().ApplyAsync("<head><!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>", Context, default);
var html = await DataBlock().ApplyAsync("<head><!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{\"baked\":true}</script></head>", Context, default);
html.Should().StartWith("<head><script type=\"application/json\" id=\"cms-data\">").And.EndWith("</script></head>");
var json = html["<head><script type=\"application/json\" id=\"cms-data\">".Length..^"</script></head>".Length];
@@ -114,7 +117,7 @@ public class OfferingsHtmlTransformTests
Banner("top", null);
Banner("bottom", null);
var html = await Sut().ApplyAsync("<!--CMS:DATA-->", Context, default);
var html = await DataBlock().ApplyAsync("<!--CMS:DATA-->", Context, default);
var inner = html["<script type=\"application/json\" id=\"cms-data\">".Length..^"</script>".Length];
inner.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028");
@@ -34,6 +34,8 @@ public class OfferingsModule : IModule
services.AddScoped<IOfferingsService, OfferingsService>();
services.AddScoped<IIndexHtmlMarkerProvider, OfferingsIndexMarkers>();
services.AddScoped<IHtmlPageTransform, OfferingsHtmlTransform>();
services.AddScoped<ICmsDataBlockContributor, OfferingsDataContributor>();
services.AddScoped<ICmsDataBlockContributor, BannersDataContributor>();
services.AddScoped<IOfferingBannerRepository, OfferingBannerRepository>();
services.AddScoped<IOfferingBannersService, OfferingBannersService>();
services.AddScoped<IOfferingCategoryRepository, OfferingCategoryRepository>();
@@ -0,0 +1,30 @@
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Offerings.Data.Entities;
namespace SlpModularCms.Modules.Offerings.Services;
/// <summary>The <c>offerings</c> key of the page data block: the same list the public endpoint answers.</summary>
public sealed class OfferingsDataContributor(IOfferingsService offerings) : ICmsDataBlockContributor
{
public string Key => "offerings";
public int Order => 100;
public async Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken)
=> await offerings.GetOfferingsAsync();
}
/// <summary>The <c>banners</c> key of the page data block: the active top and bottom banner (null when there is none).</summary>
public sealed class BannersDataContributor(IOfferingBannersService banners) : ICmsDataBlockContributor
{
public string Key => "banners";
public int Order => 110;
public async Task<object?> GetAsync(HtmlTransformContext context, CancellationToken cancellationToken)
{
var top = await banners.GetActiveAsync(OfferingBannerPositions.Top);
var bottom = await banners.GetActiveAsync(OfferingBannerPositions.Bottom);
return new { top, bottom };
}
}
@@ -18,14 +18,9 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
public const string OfferingsRegion = "CMS:OFFERINGS-HTML";
public const string BannerTopRegion = "CMS:BANNER-TOP-HTML";
public const string BannerBottomRegion = "CMS:BANNER-BOTTOM-HTML";
public const string DataMarker = "CMS:DATA";
public const string DataBlockId = "cms-data";
public const string JsonLdMarker = "CMS:OFFER-JSON-LD";
public const string PriceAttribute = "data-cms-price";
// The shape of GET /api/v1/offerings and /offerings/banner: camelCase, nulls written.
private static readonly JsonSerializerOptions ApiJson = new(JsonSerializerDefaults.Web);
private static readonly JsonSerializerOptions LdJson = new()
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
@@ -35,13 +30,11 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
public async Task<string> ApplyAsync(string html, HtmlTransformContext context, CancellationToken cancellationToken)
{
var wantsData = html.Contains("<!--" + DataMarker + "-->", StringComparison.Ordinal);
var wantsOfferings = wantsData
|| html.Contains("<!--" + OfferingsRegion + "-->", StringComparison.Ordinal)
var wantsOfferings = html.Contains("<!--" + OfferingsRegion + "-->", StringComparison.Ordinal)
|| html.Contains("<!--" + JsonLdMarker, StringComparison.Ordinal)
|| html.Contains(PriceAttribute + "=", StringComparison.Ordinal);
var wantsTop = wantsData || html.Contains("<!--" + BannerTopRegion + "-->", StringComparison.Ordinal);
var wantsBottom = wantsData || html.Contains("<!--" + BannerBottomRegion + "-->", StringComparison.Ordinal);
var wantsTop = html.Contains("<!--" + BannerTopRegion + "-->", StringComparison.Ordinal);
var wantsBottom = html.Contains("<!--" + BannerBottomRegion + "-->", StringComparison.Ordinal);
var live = wantsOfferings ? await offerings.GetOfferingsAsync() : [];
var top = wantsTop ? await banners.GetActiveAsync(OfferingBannerPositions.Top) : null;
@@ -65,22 +58,9 @@ public sealed class OfferingsHtmlTransform(IOfferingsService offerings, IOfferin
html = HtmlMarkerEditor.ReplaceRegion(html, BannerBottomRegion, CmsPageHtml.Banner(bottom, OfferingBannerPositions.Bottom));
}
if (wantsData)
{
html = HtmlMarkerEditor.ReplaceDataBlock(html, DataMarker, DataBlockId, DataBlock(context.SiteUrl, live, top, bottom));
}
return html;
}
/// <summary>The data block: the answers of the public API plus the address of the site, safe inside a script element.</summary>
public static string DataBlock(string siteUrl, IReadOnlyList<OfferingDto> live, PublicOfferingBannerDto? top, PublicOfferingBannerDto? bottom)
{
var json = JsonSerializer.Serialize(
new { siteUrl, offerings = live, banners = new { top, bottom } }, ApiJson);
return "<script type=\"application/json\" id=\"" + DataBlockId + "\">" + HtmlMarkerEditor.EscapeForScript(json) + "</script>";
}
private static int? FindPrice(IReadOnlyList<OfferingDto> live, string slug)
=> live.FirstOrDefault(o => string.Equals(o.Slug, slug, StringComparison.Ordinal)) is { } offering
? CmsPageHtml.WholeEuros(offering.Price)