diff --git a/DocumentOperator.API/Controllers/PdfAttachmentController.cs b/DocumentOperator.API/Controllers/PdfAttachmentController.cs new file mode 100644 index 0000000..a3f4324 --- /dev/null +++ b/DocumentOperator.API/Controllers/PdfAttachmentController.cs @@ -0,0 +1,85 @@ +using DocumentOperator.Application.CheckPdfAttachments.Queries; +using DocumentOperator.Application.Common.DTOs; +using MediatR; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace DocumentOperator.API.Controllers; + +/// +/// Controller for PDF attachment operations (detection, extraction, embedding) +/// +[ApiController] +[Route("api/pdf/attachments")] +[Produces("application/json")] +public class PdfAttachmentController(IMediator mediator) : ControllerBase +{ + /// + /// Checks if a PDF contains embedded files (attachments) and returns their metadata. + /// Supports multipart/form-data file upload. + /// + /// The PDF file to check for attachments + /// Cancellation token + /// Attachment check result with metadata for all found attachments + /// PDF successfully checked - returns attachment details + /// Invalid input (file missing, not a PDF, or corrupted) + /// Internal server error during PDF processing + [HttpPost("check")] + [Consumes("multipart/form-data")] + [ProducesResponseType(typeof(AttachmentCheckResult), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public async Task CheckAttachmentsFromFile( + IFormFile file, + CancellationToken cancellationToken) + { + // Convert IFormFile to byte array + using var memoryStream = new MemoryStream(); + await file.CopyToAsync(memoryStream, cancellationToken); + byte[] pdfBytes = memoryStream.ToArray(); + + // Send query to MediatR (ValidationBehavior runs automatically) + var query = new CheckPdfAttachmentsQuery { PdfBytes = pdfBytes }; + var result = await mediator.Send(query, cancellationToken); + + return Ok(result); + } + + /// + /// Checks if a PDF contains embedded files (attachments) and returns their metadata. + /// Supports Base64-encoded PDF via JSON payload. + /// + /// Request containing Base64-encoded PDF + /// Cancellation token + /// Attachment check result with metadata for all found attachments + /// PDF successfully checked - returns attachment details + /// Invalid input (Base64 format error, not a PDF, or corrupted) + /// Internal server error during PDF processing + [HttpPost("check")] + [Consumes("application/json")] + [ProducesResponseType(typeof(AttachmentCheckResult), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public async Task CheckAttachmentsFromBase64( + [FromBody] CheckPdfAttachmentsRequest request, + CancellationToken cancellationToken) + { + // Send query to MediatR (ValidationBehavior runs automatically) + var query = new CheckPdfAttachmentsQuery { Base64Pdf = request.Base64Pdf }; + var result = await mediator.Send(query, cancellationToken); + + return Ok(result); + } +} + +/// +/// Request DTO for Base64-encoded PDF attachment check +/// +public record CheckPdfAttachmentsRequest +{ + /// + /// PDF document encoded as Base64 string + /// + /// JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c... + public string Base64Pdf { get; init; } = string.Empty; +}