using DocumentService.Application.Common.Interfaces; using FluentValidation; using MediatR; namespace DocumentService.Application.ConvertToPdfA; /// /// Command to convert a standard PDF to PDF/A format /// public record ConvertToPdfACommand : IRequest { /// /// PDF document stream. Must be positioned at the beginning (Position = 0). /// public required Stream PdfStream { get; init; } /// /// Target PDF/A level (e.g., "PDF/A-1b", "PDF/A-2b", "PDF/A-3b") /// public required string PdfALevel { get; init; } } /// /// Handler for ConvertToPdfACommand /// public class ConvertToPdfACommandHandler(IPdfProcessor pdfProcessor) : IRequestHandler { public async Task Handle(ConvertToPdfACommand request, CancellationToken cancellationToken) { return await pdfProcessor.ConvertToPdfAAsync(request.PdfStream, request.PdfALevel); } } /// /// Validator for ConvertToPdfACommand /// public class ConvertToPdfACommandValidator : AbstractValidator { private static readonly string[] ValidPdfALevels = { "PDF/A-1b", "PDF/A-1a", "PDF/A-2b", "PDF/A-2u", "PDF/A-2a", "PDF/A-3b", "PDF/A-3u", "PDF/A-3a" }; public ConvertToPdfACommandValidator() { RuleFor(x => x.PdfStream) .NotNull() .WithMessage("PDF stream is required"); RuleFor(x => x.PdfALevel) .NotEmpty() .WithMessage("PDF/A level is required") .Must(level => ValidPdfALevels.Contains(level, StringComparer.OrdinalIgnoreCase)) .WithMessage($"Invalid PDF/A level. Valid values: {string.Join(", ", ValidPdfALevels)}"); } }