refactor(infrastructure): implement Stream-based PDF processing

DevExpressPdfProcessor:
- ValidateAsync, ValidatePdfAAsync, CheckAttachmentsAsync: Stream parameters
- Defensive Position=0 validation (BadRequestException for seekable streams not at beginning)
- Remove unsafe Position reset (non-seekable stream compatibility)
- Remove PdfProcessingException wrapping (let DevExpress exceptions propagate naturally)

DevExpressSwissQrCodeProcessor:
- ExtractSwissQrCodeAsync: Stream parameter
- Defensive Position=0 validation
- Remove unsafe Position reset

Memory optimization: MemoryStream.TryGetBuffer fast path for byte[] extraction
This commit is contained in:
2026-07-20 16:31:47 +02:00
parent 5dc2e38507
commit 1af158840e
2 changed files with 46 additions and 11 deletions

View File

@@ -24,19 +24,27 @@ public sealed class DevExpressSwissQrCodeProcessor : ISwissQrCodeProcessor
/// <inheritdoc />
public async Task<(Bill Bill, string[] RawLines)> ExtractSwissQrCodeAsync(
byte[] pdfBytes,
Stream pdfStream,
int[]? pageNumbers = null,
CancellationToken cancellationToken = default)
{
if (pdfBytes.Length == 0)
throw new ArgumentException("PDF document contains no byte data.", nameof(pdfBytes));
ArgumentNullException.ThrowIfNull(pdfStream, nameof(pdfStream));
if (pdfStream.Length == 0)
throw new ArgumentException("PDF stream is empty.", nameof(pdfStream));
// Defensive validation: Seekable streams must be at Position = 0
// Non-seekable streams (e.g., NetworkStream) are not checked
if (pdfStream.CanSeek && pdfStream.Position != 0)
throw new BadRequestException(null, new ArgumentException(
"PDF stream must be positioned at the beginning (Position = 0).",
nameof(pdfStream)));
using var pdfDocument = new PdfDocumentProcessor();
using var pdfStream = new MemoryStream(pdfBytes);
pdfDocument.LoadDocument(pdfStream);
if (pdfDocument.Document.Pages.Count == 0)
throw new ArgumentException("PDF document contains no pages.", nameof(pdfBytes));
throw new ArgumentException("PDF document contains no pages.", nameof(pdfStream));
// Determine which pages to scan
int[] pagesToScan = DeterminePageNumbers(pdfDocument.Document.Pages.Count, pageNumbers);