refactor(application): migrate all queries to Stream-based API
- Replace byte[] and Base64String with required Stream PdfStream - Simplify validators: remove XOR/Base64 validation, only check NotNull - Affected queries: ValidatePdfQuery, ValidatePdfAQuery, CheckPdfAttachmentsQuery, ExtractSwissQrCodeQuery - Memory efficiency: direct stream usage, no intermediate byte[] copies
This commit is contained in:
@@ -6,19 +6,14 @@ using MediatR;
|
|||||||
namespace DocumentOperator.Application.CheckPdfAttachments.Queries;
|
namespace DocumentOperator.Application.CheckPdfAttachments.Queries;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query for checking PDF attachments (supports both byte array and Base64 input)
|
/// Query for checking PDF attachments (Stream-based)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record CheckPdfAttachmentsQuery : IRequest<AttachmentCheckResult>
|
public record CheckPdfAttachmentsQuery : IRequest<AttachmentCheckResult>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PDF as byte array (direct upload via multipart/form-data)
|
/// PDF as stream (caller is responsible for disposal)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[]? PdfBytes { get; init; }
|
public required Stream PdfStream { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// PDF as Base64 string (for API clients using application/json)
|
|
||||||
/// </summary>
|
|
||||||
public string? Base64Pdf { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -33,14 +28,8 @@ public class CheckPdfAttachmentsQueryHandler(IPdfProcessor PdfProcessor, IMapper
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<AttachmentCheckResult> Handle(CheckPdfAttachmentsQuery request, CancellationToken cancellationToken)
|
public async Task<AttachmentCheckResult> Handle(CheckPdfAttachmentsQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// Use byte[] if available, otherwise convert Base64
|
// Call DevExpress service directly with stream (exceptions propagate naturally)
|
||||||
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
|
var attachmentInfo = await PdfProcessor.CheckAttachmentsAsync(request.PdfStream);
|
||||||
|
|
||||||
// Convert to stream for IPdfProcessor
|
|
||||||
using var pdfStream = new MemoryStream(pdfBytes);
|
|
||||||
|
|
||||||
// Call DevExpress service (exceptions propagate naturally)
|
|
||||||
var attachmentInfo = await PdfProcessor.CheckAttachmentsAsync(pdfStream);
|
|
||||||
|
|
||||||
// Map DTO to response DTO using AutoMapper
|
// Map DTO to response DTO using AutoMapper
|
||||||
return Mapper.Map<AttachmentCheckResult>(attachmentInfo);
|
return Mapper.Map<AttachmentCheckResult>(attachmentInfo);
|
||||||
|
|||||||
@@ -4,41 +4,15 @@ namespace DocumentOperator.Application.CheckPdfAttachments.Queries;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validator for CheckPdfAttachmentsQuery
|
/// Validator for CheckPdfAttachmentsQuery
|
||||||
/// Ensures exactly one input type (PdfBytes OR Base64Pdf) is provided
|
/// Ensures PdfStream is not null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CheckPdfAttachmentsQueryValidator : AbstractValidator<CheckPdfAttachmentsQuery>
|
public class CheckPdfAttachmentsQueryValidator : AbstractValidator<CheckPdfAttachmentsQuery>
|
||||||
{
|
{
|
||||||
public CheckPdfAttachmentsQueryValidator()
|
public CheckPdfAttachmentsQueryValidator()
|
||||||
{
|
{
|
||||||
// Rule 1: Exactly ONE input must be provided (XOR logic)
|
// Rule: PdfStream must be provided and non-empty
|
||||||
RuleFor(x => x)
|
RuleFor(x => x.PdfStream)
|
||||||
.Must(x => (x.PdfBytes != null && x.PdfBytes.Length > 0) ^
|
.NotNull()
|
||||||
(!string.IsNullOrWhiteSpace(x.Base64Pdf)))
|
.WithMessage("PdfStream is required");
|
||||||
.WithMessage("Either PdfBytes or Base64Pdf must be provided, but not both");
|
|
||||||
|
|
||||||
// Rule 2: Base64 format validation (if provided)
|
|
||||||
RuleFor(x => x.Base64Pdf)
|
|
||||||
.Must(BeValidBase64)
|
|
||||||
.When(x => !string.IsNullOrWhiteSpace(x.Base64Pdf))
|
|
||||||
.WithMessage("Base64Pdf must be a valid Base64 string");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Validates if a string is a valid Base64 format
|
|
||||||
/// </summary>
|
|
||||||
private bool BeValidBase64(string? base64)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(base64))
|
|
||||||
return true; // Skip validation if null/empty (handled by Rule 1)
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Convert.FromBase64String(base64);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (FormatException)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,14 @@ using MediatR;
|
|||||||
namespace DocumentOperator.Application.SwissQrCode.Queries;
|
namespace DocumentOperator.Application.SwissQrCode.Queries;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query for extracting Swiss QR Code from PDF (supports both byte array and Base64 input)
|
/// Query for extracting Swiss QR Code from PDF (Stream-based)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record ExtractSwissQrCodeQuery : IRequest<SwissQrCodeExtractionResult>
|
public record ExtractSwissQrCodeQuery : IRequest<SwissQrCodeExtractionResult>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PDF as byte array (direct upload)
|
/// PDF as stream (caller is responsible for disposal)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[]? PdfBytes { get; init; }
|
public required Stream PdfStream { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// PDF as Base64 string (API clients)
|
|
||||||
/// </summary>
|
|
||||||
public string? Base64Pdf { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -34,11 +29,8 @@ public class ExtractSwissQrCodeQueryHandler(ISwissQrCodeProcessor qrCodeProcesso
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<SwissQrCodeExtractionResult> Handle(ExtractSwissQrCodeQuery request, CancellationToken cancellationToken)
|
public async Task<SwissQrCodeExtractionResult> Handle(ExtractSwissQrCodeQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// Use byte[] if available, otherwise convert Base64
|
// Extract: returns (Bill, RawLines) - pass stream directly
|
||||||
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
|
var (bill, rawLines) = await qrCodeProcessor.ExtractSwissQrCodeAsync(request.PdfStream, pageNumbers: null, cancellationToken);
|
||||||
|
|
||||||
// Extract: returns (Bill, RawLines)
|
|
||||||
var (bill, rawLines) = await qrCodeProcessor.ExtractSwissQrCodeAsync(pdfBytes, pageNumbers: null, cancellationToken);
|
|
||||||
|
|
||||||
// Map Codecrete Bill to DTO using AutoMapper
|
// Map Codecrete Bill to DTO using AutoMapper
|
||||||
var billDto = mapper.Map<SwissQrBillDto>(bill);
|
var billDto = mapper.Map<SwissQrBillDto>(bill);
|
||||||
|
|||||||
@@ -5,55 +5,15 @@ namespace DocumentOperator.Application.SwissQrCode.Queries;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates ExtractSwissQrCodeQuery before handler execution.
|
/// Validates ExtractSwissQrCodeQuery before handler execution.
|
||||||
/// Ensures exactly ONE input method is provided (either PdfBytes OR Base64Pdf, not both, not none).
|
/// Ensures PdfStream is not null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class ExtractSwissQrCodeQueryValidator : AbstractValidator<ExtractSwissQrCodeQuery>
|
public sealed class ExtractSwissQrCodeQueryValidator : AbstractValidator<ExtractSwissQrCodeQuery>
|
||||||
{
|
{
|
||||||
public ExtractSwissQrCodeQueryValidator()
|
public ExtractSwissQrCodeQueryValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x)
|
// Rule: PdfStream must be provided
|
||||||
.Must(HasExactlyOneInput)
|
RuleFor(x => x.PdfStream)
|
||||||
.WithMessage("Either PdfBytes or Base64Pdf must be provided, but not both");
|
.NotNull()
|
||||||
|
.WithMessage("PdfStream is required");
|
||||||
// Validate Base64 format if provided
|
|
||||||
When(x => !string.IsNullOrWhiteSpace(x.Base64Pdf), () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.Base64Pdf)
|
|
||||||
.Must(BeValidBase64)
|
|
||||||
.WithMessage("Invalid Base64 format");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Validate byte array if provided
|
|
||||||
When(x => x.PdfBytes != null, () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.PdfBytes)
|
|
||||||
.NotEmpty()
|
|
||||||
.WithMessage("PdfBytes cannot be empty");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool HasExactlyOneInput(ExtractSwissQrCodeQuery request)
|
|
||||||
{
|
|
||||||
var hasPdfBytes = request.PdfBytes != null && request.PdfBytes.Length > 0;
|
|
||||||
var hasBase64 = !string.IsNullOrWhiteSpace(request.Base64Pdf);
|
|
||||||
|
|
||||||
// XOR: exactly one must be true
|
|
||||||
return hasPdfBytes ^ hasBase64;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool BeValidBase64(string? base64)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(base64))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Convert.FromBase64String(base64);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (FormatException)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,14 @@ using MediatR;
|
|||||||
namespace DocumentOperator.Application.ValidatePdf.Queries;
|
namespace DocumentOperator.Application.ValidatePdf.Queries;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query for PDF validation (supports both byte array and Base64 input)
|
/// Query for PDF validation (Stream-based)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record ValidatePdfQuery : IRequest<PdfValidationResult>
|
public record ValidatePdfQuery : IRequest<PdfValidationResult>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PDF as byte array (direct upload)
|
/// PDF as stream (caller is responsible for disposal)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[]? PdfBytes { get; init; }
|
public required Stream PdfStream { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// PDF as Base64 string (API clients)
|
|
||||||
/// </summary>
|
|
||||||
public string? Base64Pdf { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -33,14 +28,8 @@ public class ValidatePdfQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper)
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<PdfValidationResult> Handle(ValidatePdfQuery request, CancellationToken cancellationToken)
|
public async Task<PdfValidationResult> Handle(ValidatePdfQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// Use byte[] if available, otherwise convert Base64
|
// Call DevExpress service directly with stream (exceptions propagate naturally)
|
||||||
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
|
var metadata = await PdfProcessor.ValidateAsync(request.PdfStream);
|
||||||
|
|
||||||
// Convert to stream for IPdfProcessor (using MemoryStream)
|
|
||||||
using var pdfStream = new MemoryStream(pdfBytes);
|
|
||||||
|
|
||||||
// Call DevExpress service (exceptions propagate naturally)
|
|
||||||
var metadata = await PdfProcessor.ValidateAsync(pdfStream);
|
|
||||||
|
|
||||||
// Map DTO to response DTO using AutoMapper
|
// Map DTO to response DTO using AutoMapper
|
||||||
return Mapper.Map<PdfValidationResult>(metadata);
|
return Mapper.Map<PdfValidationResult>(metadata);
|
||||||
|
|||||||
@@ -5,55 +5,15 @@ namespace DocumentOperator.Application.ValidatePdf.Queries;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validator for ValidatePdfQuery
|
/// Validator for ValidatePdfQuery
|
||||||
/// Ensures exactly ONE input method is provided (either PdfBytes OR Base64Pdf, not both, not none)
|
/// Ensures PdfStream is not null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ValidatePdfQueryValidator : AbstractValidator<ValidatePdfQuery>
|
public class ValidatePdfQueryValidator : AbstractValidator<ValidatePdfQuery>
|
||||||
{
|
{
|
||||||
public ValidatePdfQueryValidator()
|
public ValidatePdfQueryValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x)
|
// Rule: PdfStream must be provided
|
||||||
.Must(HasExactlyOneInput)
|
RuleFor(x => x.PdfStream)
|
||||||
.WithMessage("Either PdfBytes or Base64Pdf must be provided, but not both");
|
.NotNull()
|
||||||
|
.WithMessage("PdfStream is required");
|
||||||
// Validate Base64 format if provided
|
|
||||||
When(x => !string.IsNullOrWhiteSpace(x.Base64Pdf), () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.Base64Pdf)
|
|
||||||
.Must(BeValidBase64)
|
|
||||||
.WithMessage("Invalid Base64 format");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Validate byte array if provided
|
|
||||||
When(x => x.PdfBytes != null, () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.PdfBytes)
|
|
||||||
.NotEmpty()
|
|
||||||
.WithMessage("PdfBytes cannot be empty");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool HasExactlyOneInput(ValidatePdfQuery request)
|
|
||||||
{
|
|
||||||
var hasPdfBytes = request.PdfBytes != null && request.PdfBytes.Length > 0;
|
|
||||||
var hasBase64 = !string.IsNullOrWhiteSpace(request.Base64Pdf);
|
|
||||||
|
|
||||||
// XOR: exactly one must be true
|
|
||||||
return hasPdfBytes ^ hasBase64;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool BeValidBase64(string? base64)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(base64))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Convert.FromBase64String(base64);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (FormatException)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,19 +6,14 @@ using MediatR;
|
|||||||
namespace DocumentOperator.Application.ValidatePdfA.Queries;
|
namespace DocumentOperator.Application.ValidatePdfA.Queries;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query for PDF/A validation (supports both byte array and Base64 input)
|
/// Query for PDF/A validation (Stream-based)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record ValidatePdfAQuery : IRequest<PdfAValidationResult>
|
public record ValidatePdfAQuery : IRequest<PdfAValidationResult>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PDF as byte array (direct upload)
|
/// PDF as stream (caller is responsible for disposal)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[]? PdfBytes { get; init; }
|
public required Stream PdfStream { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// PDF as Base64 string (API clients)
|
|
||||||
/// </summary>
|
|
||||||
public string? Base64Pdf { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -33,14 +28,8 @@ public class ValidatePdfAQueryHandler(IPdfProcessor PdfProcessor, IMapper Mapper
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<PdfAValidationResult> Handle(ValidatePdfAQuery request, CancellationToken cancellationToken)
|
public async Task<PdfAValidationResult> Handle(ValidatePdfAQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// Use byte[] if available, otherwise convert Base64
|
// Call DevExpress service directly with stream (exceptions propagate naturally)
|
||||||
byte[] pdfBytes = request.PdfBytes ?? Convert.FromBase64String(request.Base64Pdf!);
|
var metadata = await PdfProcessor.ValidatePdfAAsync(request.PdfStream);
|
||||||
|
|
||||||
// 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
|
// Map DTO to response DTO using AutoMapper
|
||||||
return Mapper.Map<PdfAValidationResult>(metadata);
|
return Mapper.Map<PdfAValidationResult>(metadata);
|
||||||
|
|||||||
@@ -4,45 +4,15 @@ namespace DocumentOperator.Application.ValidatePdfA.Validators;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validator for ValidatePdfAQuery
|
/// Validator for ValidatePdfAQuery
|
||||||
/// Ensures exactly ONE input format is provided (PdfBytes XOR Base64Pdf)
|
/// Ensures PdfStream is not null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ValidatePdfAQueryValidator : AbstractValidator<Queries.ValidatePdfAQuery>
|
public class ValidatePdfAQueryValidator : AbstractValidator<Queries.ValidatePdfAQuery>
|
||||||
{
|
{
|
||||||
public ValidatePdfAQueryValidator()
|
public ValidatePdfAQueryValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x)
|
// Rule: PdfStream must be provided
|
||||||
.Must(x => (x.PdfBytes != null && x.PdfBytes.Length > 0) ^
|
RuleFor(x => x.PdfStream)
|
||||||
!string.IsNullOrWhiteSpace(x.Base64Pdf))
|
.NotNull()
|
||||||
.WithMessage("Either PdfBytes or Base64Pdf must be provided, but not both");
|
.WithMessage("PdfStream is required");
|
||||||
|
|
||||||
When(x => !string.IsNullOrWhiteSpace(x.Base64Pdf), () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.Base64Pdf!)
|
|
||||||
.Must(BeValidBase64)
|
|
||||||
.WithMessage("Base64Pdf must be a valid Base64 string");
|
|
||||||
});
|
|
||||||
|
|
||||||
When(x => x.PdfBytes != null, () =>
|
|
||||||
{
|
|
||||||
RuleFor(x => x.PdfBytes!)
|
|
||||||
.Must(bytes => bytes.Length > 0)
|
|
||||||
.WithMessage("PdfBytes cannot be empty");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool BeValidBase64(string base64)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(base64))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Convert.FromBase64String(base64);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (FormatException)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user