refactor(api): migrate controllers to Stream API with Base64 validation
PdfValidationController: - ValidateFromFile: IFormFile.OpenReadStream() direct usage (no byte[] copy) - ValidateFromBase64: try-catch Convert.FromBase64String → BadRequestException - ValidatePdfAFromFile: IFormFile.OpenReadStream() direct usage - ValidatePdfAFromBase64: try-catch Convert.FromBase64String → BadRequestException PdfAttachmentController: - CheckAttachmentsFromBase64: try-catch Convert.FromBase64String → BadRequestException SwissQrCodeController: - ExtractFromBase64: try-catch Convert.FromBase64String → BadRequestException All controllers: Add using DocumentOperator.Domain.Common.Exceptions for BadRequestException
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using DocumentOperator.Application.Common.DTOs;
|
||||
using DocumentOperator.Application.ValidatePdf.Queries;
|
||||
using DocumentOperator.Application.ValidatePdfA.Queries;
|
||||
using DocumentOperator.Domain.Common.Exceptions;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -42,13 +43,11 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
});
|
||||
}
|
||||
|
||||
// Convert IFormFile to byte array
|
||||
using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream, cancellationToken);
|
||||
byte[] pdfBytes = memoryStream.ToArray();
|
||||
|
||||
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
||||
using var pdfStream = file.OpenReadStream();
|
||||
|
||||
// Direct pass-through to MediatR
|
||||
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
||||
var query = new ValidatePdfQuery { PdfStream = pdfStream };
|
||||
var result = await Mediator.Send(query, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
@@ -57,7 +56,7 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
/// <summary>
|
||||
/// Validates a PDF document and returns metadata (Base64 JSON)
|
||||
/// </summary>
|
||||
/// <param name="query">PDF as Base64 string</param>
|
||||
/// <param name="request">Request containing Base64-encoded PDF</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>PDF metadata (page count, file size, PDF version, attachments)</returns>
|
||||
/// <response code="200">PDF is valid, metadata returned</response>
|
||||
@@ -69,10 +68,24 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> ValidateFromBase64(
|
||||
[FromBody] ValidatePdfQuery query,
|
||||
[FromBody] ValidatePdfBase64Request request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Convert Base64 to stream (wrap in try-catch to throw BadRequestException)
|
||||
byte[] pdfBytes;
|
||||
try
|
||||
{
|
||||
pdfBytes = Convert.FromBase64String(request.Base64Pdf);
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
throw new BadRequestException("Invalid Base64 format: " + ex.Message);
|
||||
}
|
||||
|
||||
using var pdfStream = new MemoryStream(pdfBytes);
|
||||
|
||||
// Direct pass-through to MediatR
|
||||
var query = new ValidatePdfQuery { PdfStream = pdfStream };
|
||||
var result = await Mediator.Send(query, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
@@ -106,13 +119,11 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
});
|
||||
}
|
||||
|
||||
// Convert IFormFile to byte array
|
||||
using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream, cancellationToken);
|
||||
byte[] pdfBytes = memoryStream.ToArray();
|
||||
|
||||
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
||||
using var pdfStream = file.OpenReadStream();
|
||||
|
||||
// Direct pass-through to MediatR
|
||||
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
||||
var query = new ValidatePdfAQuery { PdfStream = pdfStream };
|
||||
var result = await Mediator.Send(query, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
@@ -121,7 +132,7 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
/// <summary>
|
||||
/// Validates a PDF/A document and checks conformance level (Base64 JSON)
|
||||
/// </summary>
|
||||
/// <param name="query">PDF as Base64 string</param>
|
||||
/// <param name="request">Request containing Base64-encoded PDF</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>PDF/A metadata (conformance level, errors, warnings)</returns>
|
||||
/// <response code="200">PDF/A validation completed, results returned</response>
|
||||
@@ -133,12 +144,50 @@ public class PdfValidationController(IMediator Mediator) : ControllerBase
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> ValidatePdfAFromBase64(
|
||||
[FromBody] ValidatePdfAQuery query,
|
||||
[FromBody] ValidatePdfABase64Request request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Convert Base64 to stream (wrap in try-catch to throw BadRequestException)
|
||||
byte[] pdfBytes;
|
||||
try
|
||||
{
|
||||
pdfBytes = Convert.FromBase64String(request.Base64Pdf);
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
throw new BadRequestException("Invalid Base64 format: " + ex.Message);
|
||||
}
|
||||
|
||||
using var pdfStream = new MemoryStream(pdfBytes);
|
||||
|
||||
// Direct pass-through to MediatR
|
||||
var query = new ValidatePdfAQuery { PdfStream = pdfStream };
|
||||
var result = await Mediator.Send(query, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request DTO for Base64-encoded PDF validation
|
||||
/// </summary>
|
||||
public record ValidatePdfBase64Request
|
||||
{
|
||||
/// <summary>
|
||||
/// PDF document encoded as Base64 string
|
||||
/// </summary>
|
||||
/// <example>JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c...</example>
|
||||
public string Base64Pdf { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request DTO for Base64-encoded PDF/A validation
|
||||
/// </summary>
|
||||
public record ValidatePdfABase64Request
|
||||
{
|
||||
/// <summary>
|
||||
/// PDF document encoded as Base64 string
|
||||
/// </summary>
|
||||
/// <example>JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c...</example>
|
||||
public string Base64Pdf { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user