H1: beeldpijplijn in Core.Media (Magick.NET achter IImageProcessor), pure functies met property-tests, uploadwacht
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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, IConfiguration configuration)
|
||||
{
|
||||
services.AddOptions<MediaOptions>().Bind(configuration.GetSection(MediaOptions.SectionName));
|
||||
services.TryAddSingleton<MediaProcessingGate>();
|
||||
services.TryAddSingleton<MediaStatus>();
|
||||
services.TryAddSingleton<IMediaUploadGuard, MediaUploadGuard>();
|
||||
if (!services.Any(d => d.ImplementationType == typeof(ImageProcessorSelfTest)))
|
||||
{
|
||||
services.AddHostedService<ImageProcessorSelfTest>();
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
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 Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
Reference in New Issue
Block a user