Mediabibliotheek: instelling Media:AutoCleanupUnattached (gestapeld op #22) #23

Open
Sluijsens wants to merge 2 commits from feature/media-library-retention into feature/media-library-screen
5 changed files with 46 additions and 3 deletions
@@ -6,7 +6,7 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|---|---|---|---|---|
| 1 | Which **uses** (places and ratios) exist? | Hero `wide` (3:2) and `tall` (2:3), ASF shoot-page **mosaic** tiles (a small fixed set of ratios per page, to be listed by the ASF session), **masonry** (natural ratio, no crop), **thumb** (library thumbnails, 1:1 or natural). A use is `(module, place, ratio set)`. | Other ratios only add rows to the use definitions; a free-form ratio per tile would need a ratio editor and more variants per photo. | Application design section 4 (`MediaUseDefinition`) |
| 2 | **Several formats/variants per photo**: who decides the set? | The **use** decides the ratios and widths (as `VariantPlanner` does for Hero); the library never cuts a fixed set per photo. Formats stay WebP and AVIF as today. | A fixed set per photo costs more disk and CPU on the Pi and cuts variants nobody uses. | `VariantPlanner`, application design section 4 |
| 3 | The **7-day automatic clean-up** of unattached Hero images until the library is built. | Switch it off behind a setting (`Media:AutoCleanupUnattached`, default `true` until the library screen exists, then default `false`), so nothing is lost silently once the library can show and delete photos. Retired-file removal (after a replaced set) stays: that is housekeeping of generated files, not a person's photos. | Keep it on until phase C; or switch off now (risk: unused uploads pile up with no screen to remove them). | `HeroMediaCleanup`, `MediaOptions` |
| 3 | The **7-day automatic clean-up** of unattached Hero images until the library is built. | **Built (2026-11-17, safe: default unchanged)**: the setting `Media:AutoCleanupUnattached` exists, default `true` (today's behaviour); set `Media__AutoCleanupUnattached=false` per host to keep every upload. Plan: switch it off (default `false`) once the library screen and delete are accepted by Bryan. Switch it off behind a setting (`Media:AutoCleanupUnattached`, default `true` until the library screen exists, then default `false`), so nothing is lost silently once the library can show and delete photos. Retired-file removal (after a replaced set) stays: that is housekeeping of generated files, not a person's photos. | Keep it on until phase C; or switch off now (risk: unused uploads pile up with no screen to remove them). | `HeroMediaCleanup`, `MediaOptions` |
| 4 | **Rights per module**: who may see, use and delete which photos? | One shared library for Owner and Administrator (like Hero and Offerings). No per-module visibility yet. | Per-module visibility needs an owner field on a photo and a filter; the contract has room for it (`ModuleKey` on a use). | `[Authorize(Policy = "AdminOnly")]`, `MediaUse.Module` |
| 5 | **Public addresses**: per module (`/media/<module>/...`) or a shared path? | Keep `/media/{area}/{setHash}/{name}`; `area` stays the module (`hero`; a gallery would use `gallery`). The set hash still comes from the original's hash plus the crops of that **use**. A photo used in two modules is two sets (two addresses), cached separately. | A shared path would change addresses and golden files: not allowed without alignment. | `MediaPaths`, `MediaEndpoint` |
| 6 | **Usage register**: do modules report, or does the library keep it centrally? | Modules report: `IMediaUsageProvider` per module answers "where is this photo used" and "detach this photo" from the module's own data. Computed, never copied, so it cannot go stale. The library warns when a module has no provider. | A central register table keeps the answer fast but must be kept in step on every module save (risk of drift); can be added later as a cache. | Application design section 3, `IMediaUsageProvider` |
+1 -1
View File
@@ -26,7 +26,7 @@ De admin toont alleen een badge Live of Niet live.
| Instelling | Betekenis |
|---|---|
| `Media__Directory` | De map voor de afbeeldingen, **buiten de release** en buiten de webroot. Zonder deze instelling (of als de map niet beschrijfbaar is) staan uploads uit; bestaande bestanden en de publieke hero blijven werken. Niet in een committed appsettings-bestand. |
| `Media__MaxUploadBytes` (10 MB), `Media__MaxPixels` (64 miljoen), `Media__MaxParallelJobs` (2), `Media__DecodeTimeoutSeconds` (20), `Media__MaxTotalBytes` (2 GB), `Media__RetentionDays` (7) | Grenzen en bewaartijd; standaardwaarden zijn bedoeld om te blijven. |
| `Media__MaxUploadBytes` (10 MB), `Media__MaxPixels` (64 miljoen), `Media__MaxParallelJobs` (2), `Media__DecodeTimeoutSeconds` (20), `Media__MaxTotalBytes` (2 GB), `Media__RetentionDays` (7), `Media__AutoCleanupUnattached` (standaard `true`; `false` bewaart ongebruikte uploads tot iemand ze verwijdert, voor de mediabibliotheek) | Grenzen en bewaartijd; standaardwaarden zijn bedoeld om te blijven. |
| `Hero__TextOnlyRegion` | `true` voor een site waarvan de hero geen afbeeldingen heeft (SLP Software; staat in de committed instellingen van die site): een hero zonder slides wordt dan als tekst (titelregels, lead, knoppen) in de eerste HTML geschreven. Uit (standaard, ook voor ASF): een hero zonder slides laat de pagina ongemoeid. |
| `Hero__TitlePrefix` | Merktekst die in de tekst-only-regio als eerste, visueel te verbergen deel van de h1 wordt geschreven (`<span class="cms-hero-title-prefix">`), zodat crawlers en schermlezers de volledige titel lezen. SLP Software: `"SLP Software: "` (committed instelling); leeg (standaard, ook ASF) schrijft niets. De site verbergt de klasse zelf visueel. |
| `Media__RenderSetTimeoutSeconds` (600), `Media__StripTimeoutSeconds` (60) | De tijd die het knippen van alle varianten van één foto in de achtergrond mag duren, en het verwijderen van metadata bij een upload. Ruim gekozen: een grote foto op een Raspberry Pi kost minuten. |
@@ -65,6 +65,14 @@ public sealed class MediaOptions
public long MaxTotalBytes { get; set; } = 2L * 1024 * 1024 * 1024;
public int RetentionDays { get; set; } = 7;
/// <summary>
/// Whether photos that were uploaded but never used are removed automatically after <see cref="RetentionDays"/>.
/// The media library keeps everything until a person removes it, so set this to <c>false</c> (<c>Media__AutoCleanupUnattached=false</c>)
/// once the library can show and delete photos. Retired files of photos a person removed stay housekeeping either way.
/// Default <c>true</c> keeps today's behaviour (assumption 3 of the media-library list).
/// </summary>
public bool AutoCleanupUnattached { get; set; } = true;
}
/// <summary>
@@ -412,6 +412,37 @@ public class HeroMediaCleanupTests
again.Variants.Should().OnlyContain(v => v.Url.Contains(set));
}
[Fact]
public async Task AnImageNobodyUsed_IsKept_WhenAutomaticCleanupIsOff()
{
using var rig = new HeroTestRig();
rig.Media.AutoCleanupUnattached = false;
await rig.Upload(HeroTestRig.Photo());
rig.Time.Now = rig.Time.Now.AddDays(400);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(400);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().HaveCount(1);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().HaveCount(1);
rig.Db.HeroRetiredFiles.Should().BeEmpty();
}
[Fact]
public async Task APhotoAPersonRemoved_StillLeavesAfterTheRetentionTime_WhenAutomaticCleanupIsOff()
{
using var rig = new HeroTestRig();
rig.Media.AutoCleanupUnattached = false;
var (set, original) = await RetireAnImage(rig);
rig.Time.Now = rig.Time.Now.AddDays(8);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().NotContain(s => s.SetHash == set);
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeFalse();
}
[Fact]
public async Task AnImageNobodyUsed_IsRetiredAndRemoved_AfterTheRetentionTime()
{
@@ -29,7 +29,11 @@ public class HeroMediaCleanup(
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))
// Skipped when the media library keeps every upload until a person removes it (Media:AutoCleanupUnattached = false).
var unattached = options.Value.AutoCleanupUnattached
? await repository.GetUnattachedImagesCreatedBeforeAsync(cutoff, cancellationToken)
: [];
foreach (var image in unattached)
{
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) });