using AutoMapper;
using DocumentOperator.Application.Common.DTOs;
using DocumentOperator.Application.Common.Interfaces;
using MediatR;
namespace DocumentOperator.Application.ValidatePdfA.Queries;
///
/// Query for PDF/A validation (supports both byte array and Base64 input)
///
public record ValidatePdfAQuery : IRequest
{
///
/// PDF as byte array (direct upload)
///
public byte[]? PdfBytes { get; init; }
///
/// PDF as Base64 string (API clients)
///
public string? Base64Pdf { get; init; }
}
///
/// Handler for ValidatePdfAQuery
/// Orchestrates PDF/A validation using IPdfProcessor and AutoMapper
///
public class ValidatePdfAQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper)
: IRequestHandler
{
///
/// Validates PDF/A and returns metadata with conformance level
///
public async Task Handle(ValidatePdfAQuery request, CancellationToken cancellationToken)
{
// Use byte[] if available, otherwise convert Base64
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
// Convert to stream for IPdfProcessor
using var pdfStream = new MemoryStream(pdfBytes);
// Call DevExpress service (exceptions propagate naturally)
var metadata = await PdfProcessor.ValidatePdfAAsync(pdfStream);
// Map DTO to response DTO using AutoMapper
return Mapper.Map(metadata);
}
}