H3: mediaopslag in een map buiten de release, /media-endpoint (immutable, 404 voor al het andere), mapcontrole en snelheidsbeleid
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,19 +13,79 @@ public static class MediaServiceCollectionExtensions
|
||||
/// 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, IConfiguration configuration)
|
||||
public static IServiceCollection AddCmsMedia(this IServiceCollection services)
|
||||
{
|
||||
services.AddOptions<MediaOptions>().Bind(configuration.GetSection(MediaOptions.SectionName));
|
||||
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
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var registration in registrations)
|
||||
{
|
||||
areas.Add(registration.Area);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user