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:
@@ -4,45 +4,15 @@ namespace DocumentOperator.Application.ValidatePdfA.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Validator for ValidatePdfAQuery
|
||||
/// Ensures exactly ONE input format is provided (PdfBytes XOR Base64Pdf)
|
||||
/// Ensures PdfStream is not null
|
||||
/// </summary>
|
||||
public class ValidatePdfAQueryValidator : AbstractValidator<Queries.ValidatePdfAQuery>
|
||||
{
|
||||
public ValidatePdfAQueryValidator()
|
||||
{
|
||||
RuleFor(x => x)
|
||||
.Must(x => (x.PdfBytes != null && x.PdfBytes.Length > 0) ^
|
||||
!string.IsNullOrWhiteSpace(x.Base64Pdf))
|
||||
.WithMessage("Either PdfBytes or Base64Pdf must be provided, but not both");
|
||||
|
||||
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;
|
||||
}
|
||||
// Rule: PdfStream must be provided
|
||||
RuleFor(x => x.PdfStream)
|
||||
.NotNull()
|
||||
.WithMessage("PdfStream is required");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user