Domain layer: - PdfAMetadata value object (isValid, pdfVersion, pageCount, encrypted, pdfaVersion, pdfaCompliant, errors, warnings) Infrastructure layer: - IPdfProcessor.ValidatePdfAAsync() interface method - DevExpressPdfProcessor.ValidatePdfAAsync() implementation - DetectEncryption() - scans PDF raw data for /Encrypt keyword - DetectPdfAConformance() - parses XMP metadata (pdfaid:part, pdfaid:conformance) - Validation: encrypted PDF cannot be PDF/A compliant Application layer: - ValidatePdfAQuery + ValidatePdfAQueryHandler (co-located) - ValidatePdfAQueryValidator (FluentValidation: PdfBytes XOR Base64Pdf) - PdfAValidationResult DTO - AutoMapper: PdfAMetadata -> PdfAValidationResult API layer: - PdfValidationController.ValidatePdfAFromFile() (multipart/form-data) - PdfValidationController.ValidatePdfAFromBase64() (application/json) - XML documentation with response codes Result: - POST /api/pdf/validation/validate-pdfa (both multipart and JSON) - Returns: conformance level, errors, warnings - Build: 0 errors, 4 warnings (DevExpress eval) - Tests: 20/20 passing Next: Integration tests + Swagger test case
145 lines
6.2 KiB
C#
145 lines
6.2 KiB
C#
using DocumentOperator.Application.Common.DTOs;
|
|
using DocumentOperator.Application.ValidatePdf.Queries;
|
|
using DocumentOperator.Application.ValidatePdfA.Queries;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DocumentOperator.API.Controllers;
|
|
|
|
/// <summary>
|
|
/// PDF validation operations
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/pdf/validation")]
|
|
[Produces("application/json")]
|
|
public class PdfValidationController(IMediator Mediator) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Validates a PDF document and returns metadata (multipart/form-data)
|
|
/// </summary>
|
|
/// <param name="file">PDF file to validate</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>
|
|
/// <response code="400">Invalid PDF or file format</response>
|
|
/// <response code="500">Internal server error during validation</response>
|
|
[HttpPost("validate")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(PdfValidationResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ValidateFromFile(
|
|
IFormFile file,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
return BadRequest(new ProblemDetails
|
|
{
|
|
Title = "Invalid file",
|
|
Detail = "File is required and cannot be empty",
|
|
Status = StatusCodes.Status400BadRequest
|
|
});
|
|
}
|
|
|
|
// Convert IFormFile to byte array
|
|
using var memoryStream = new MemoryStream();
|
|
await file.CopyToAsync(memoryStream, cancellationToken);
|
|
byte[] pdfBytes = memoryStream.ToArray();
|
|
|
|
// Direct pass-through to MediatR
|
|
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
|
var result = await Mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a PDF document and returns metadata (Base64 JSON)
|
|
/// </summary>
|
|
/// <param name="query">PDF as Base64 string</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>
|
|
/// <response code="400">Invalid PDF or Base64 format</response>
|
|
/// <response code="500">Internal server error during validation</response>
|
|
[HttpPost("validate")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(PdfValidationResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ValidateFromBase64(
|
|
[FromBody] ValidatePdfQuery query,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Direct pass-through to MediatR
|
|
var result = await Mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a PDF/A document and checks conformance level (multipart/form-data)
|
|
/// </summary>
|
|
/// <param name="file">PDF file to validate</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>
|
|
/// <response code="400">Invalid PDF or file format</response>
|
|
/// <response code="500">Internal server error during validation</response>
|
|
[HttpPost("validate-pdfa")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(PdfAValidationResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ValidatePdfAFromFile(
|
|
IFormFile file,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
return BadRequest(new ProblemDetails
|
|
{
|
|
Title = "Invalid file",
|
|
Detail = "File is required and cannot be empty",
|
|
Status = StatusCodes.Status400BadRequest
|
|
});
|
|
}
|
|
|
|
// Convert IFormFile to byte array
|
|
using var memoryStream = new MemoryStream();
|
|
await file.CopyToAsync(memoryStream, cancellationToken);
|
|
byte[] pdfBytes = memoryStream.ToArray();
|
|
|
|
// Direct pass-through to MediatR
|
|
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
|
var result = await Mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a PDF/A document and checks conformance level (Base64 JSON)
|
|
/// </summary>
|
|
/// <param name="query">PDF as Base64 string</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>
|
|
/// <response code="400">Invalid PDF or Base64 format</response>
|
|
/// <response code="500">Internal server error during validation</response>
|
|
[HttpPost("validate-pdfa")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(PdfAValidationResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ValidatePdfAFromBase64(
|
|
[FromBody] ValidatePdfAQuery query,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Direct pass-through to MediatR
|
|
var result = await Mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
}
|