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:
@@ -28,6 +28,12 @@ public class DevExpressPdfProcessor : IPdfProcessor
|
||||
throw new BadRequestException("PDF stream cannot be empty");
|
||||
}
|
||||
|
||||
// Defensive validation: Seekable streams must be at Position = 0
|
||||
if (pdfStream.CanSeek && pdfStream.Position != 0)
|
||||
{
|
||||
throw new BadRequestException("PDF stream must be positioned at the beginning (Position = 0).");
|
||||
}
|
||||
|
||||
// 2. Read stream to byte array for raw data analysis
|
||||
// (DevExpress needs byte[] for some operations like attachment detection)
|
||||
byte[] pdfBytes;
|
||||
@@ -39,14 +45,18 @@ public class DevExpressPdfProcessor : IPdfProcessor
|
||||
else
|
||||
{
|
||||
// Slow path: copy stream to byte array
|
||||
pdfStream.Position = 0;
|
||||
using var memoryStream = new MemoryStream();
|
||||
await pdfStream.CopyToAsync(memoryStream);
|
||||
pdfBytes = memoryStream.ToArray();
|
||||
}
|
||||
|
||||
// 3. Load PDF with DevExpress Document API
|
||||
pdfStream.Position = 0;
|
||||
// Reset position for DevExpress (seekable streams only)
|
||||
if (pdfStream.CanSeek)
|
||||
{
|
||||
pdfStream.Position = 0;
|
||||
}
|
||||
|
||||
using var processor = new PdfDocumentProcessor();
|
||||
processor.LoadDocument(pdfStream);
|
||||
|
||||
@@ -87,6 +97,12 @@ public class DevExpressPdfProcessor : IPdfProcessor
|
||||
throw new BadRequestException("PDF stream cannot be empty");
|
||||
}
|
||||
|
||||
// Defensive validation: Seekable streams must be at Position = 0
|
||||
if (pdfStream.CanSeek && pdfStream.Position != 0)
|
||||
{
|
||||
throw new BadRequestException("PDF stream must be positioned at the beginning (Position = 0).");
|
||||
}
|
||||
|
||||
// 2. Read stream to byte array for raw data analysis
|
||||
byte[] pdfBytes;
|
||||
if (pdfStream is MemoryStream ms && ms.TryGetBuffer(out var buffer))
|
||||
@@ -95,14 +111,18 @@ public class DevExpressPdfProcessor : IPdfProcessor
|
||||
}
|
||||
else
|
||||
{
|
||||
pdfStream.Position = 0;
|
||||
using var memoryStream = new MemoryStream();
|
||||
await pdfStream.CopyToAsync(memoryStream);
|
||||
pdfBytes = memoryStream.ToArray();
|
||||
}
|
||||
|
||||
// 3. Load PDF with DevExpress Document API
|
||||
pdfStream.Position = 0;
|
||||
// Reset position for DevExpress (seekable streams only)
|
||||
if (pdfStream.CanSeek)
|
||||
{
|
||||
pdfStream.Position = 0;
|
||||
}
|
||||
|
||||
using var processor = new PdfDocumentProcessor();
|
||||
processor.LoadDocument(pdfStream);
|
||||
|
||||
@@ -175,8 +195,15 @@ public class DevExpressPdfProcessor : IPdfProcessor
|
||||
throw new BadRequestException("PDF stream cannot be empty");
|
||||
}
|
||||
|
||||
// 2. Load PDF with DevExpress Document API (exceptions propagate naturally)
|
||||
pdfStream.Position = 0;
|
||||
// Defensive validation: Seekable streams must be at Position = 0
|
||||
if (pdfStream.CanSeek && pdfStream.Position != 0)
|
||||
{
|
||||
throw new BadRequestException("PDF stream must be positioned at the beginning (Position = 0).");
|
||||
}
|
||||
|
||||
// 2. Load PDF with DevExpress Document API
|
||||
// DevExpress LoadDocument may throw exceptions for corrupted PDFs - let them propagate naturally
|
||||
// Middleware will catch and convert to 500 Internal Server Error
|
||||
using var processor = new PdfDocumentProcessor();
|
||||
processor.LoadDocument(pdfStream);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user