Media: instelling Media:AutoCleanupUnattached (standaard true) om ongebruikte uploads te bewaren
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 13s
Continuous Integration / frontend-prepare (pull_request) Skipped
Continuous Integration / frontend-build (pull_request) Skipped
Continuous Integration / frontend-test (pull_request) Skipped
Continuous Integration / frontend-lint (pull_request) Skipped
Continuous Integration / config-asf (pull_request) Successful in 5s
Continuous Integration / compile-sites (pull_request) Successful in 1m31s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m18s
Continuous Integration / backend-build (pull_request) Successful in 3m10s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-test (pull_request) Canceled after 1m25s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 14:17:28 +01:00
co-authored by Claude Sonnet 5.5
parent caa178a58c
commit a71db84632
5 changed files with 46 additions and 3 deletions
@@ -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) });