Consolidated DTOs under `DocumentService.Application.Common.DTOs.Requests` to centralize and align them with the application layer. Introduced `PdfBase64RequestBase` to encapsulate shared properties, reducing redundancy across request DTOs. Updated controllers, clients, and tests to use the new DTO structure. Removed redundant DTOs and cleaned up unused namespaces and comments. Improved documentation and ensured consistent naming conventions across the codebase.
293 lines
13 KiB
C#
293 lines
13 KiB
C#
using DocumentService.Application.AddAttachments;
|
|
using DocumentService.Application.CheckPdfAttachments.Queries;
|
|
using DocumentService.Application.Common.DTOs;
|
|
using DocumentService.Application.ExtractPdfAttachments;
|
|
using DocumentService.Application.Common.DTOs.Requests;
|
|
using DocumentService.Domain.Common.Exceptions;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DocumentService.API.Controllers;
|
|
|
|
/// <summary>
|
|
/// Controller for PDF attachment operations (detection, extraction, embedding)
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/pdf/attachments")]
|
|
[Produces("application/json")]
|
|
public class PdfAttachmentController(IMediator mediator) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Checks if a PDF contains embedded files (attachments) and returns their metadata.
|
|
/// Supports multipart/form-data file upload.
|
|
/// </summary>
|
|
/// <param name="file">The PDF file to check for attachments</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Attachment check result with metadata for all found attachments</returns>
|
|
/// <response code="200">PDF successfully checked - returns attachment details</response>
|
|
/// <response code="400">Invalid input (file missing, not a PDF, or corrupted)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[HttpPost("check")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(AttachmentCheckResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CheckAttachmentsFromFile(
|
|
IFormFile file,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
|
using var pdfStream = file.OpenReadStream();
|
|
|
|
// Send query to MediatR (ValidationBehavior runs automatically)
|
|
var query = new CheckPdfAttachmentsQuery { PdfStream = pdfStream };
|
|
var result = await mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if a PDF contains embedded files (attachments) and returns their metadata.
|
|
/// Supports Base64-encoded PDF via JSON payload.
|
|
/// </summary>
|
|
/// <param name="request">Request containing Base64-encoded PDF</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Attachment check result with metadata for all found attachments</returns>
|
|
/// <response code="200">PDF successfully checked - returns attachment details</response>
|
|
/// <response code="400">Invalid input (Base64 format error, not a PDF, or corrupted)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[HttpPost("check")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(AttachmentCheckResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CheckAttachmentsFromBase64(
|
|
[FromBody] CheckPdfAttachmentsRequest 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);
|
|
|
|
// Send query to MediatR (ValidationBehavior runs automatically)
|
|
var query = new CheckPdfAttachmentsQuery { PdfStream = pdfStream };
|
|
var result = await mediator.Send(query, cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extracts all embedded files from a PDF and returns them as a ZIP archive.
|
|
/// Supports multipart/form-data file upload.
|
|
/// </summary>
|
|
/// <param name="file">The PDF file to extract attachments from</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>ZIP archive containing all extracted attachments</returns>
|
|
/// <response code="200">Attachments extracted successfully - returns ZIP file</response>
|
|
/// <response code="400">Invalid input (file missing, not a PDF, or corrupted)</response>
|
|
/// <response code="404">PDF contains no attachments</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[HttpPost("extract")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ExtractAttachmentsFromFile(
|
|
IFormFile file,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
|
using var pdfStream = file.OpenReadStream();
|
|
|
|
// Send command to MediatR
|
|
var command = new ExtractPdfAttachmentsCommand { PdfStream = pdfStream };
|
|
byte[] zipBytes = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return ZIP file
|
|
return File(zipBytes, "application/zip", "attachments.zip");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extracts all embedded files from a PDF and returns them as a ZIP archive.
|
|
/// Supports Base64-encoded PDF via JSON payload.
|
|
/// </summary>
|
|
/// <param name="request">Request containing Base64-encoded PDF</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>ZIP archive containing all extracted attachments</returns>
|
|
/// <response code="200">Attachments extracted successfully - returns ZIP file</response>
|
|
/// <response code="400">Invalid input (Base64 format error, not a PDF, or corrupted)</response>
|
|
/// <response code="404">PDF contains no attachments</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[HttpPost("extract")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ExtractAttachmentsFromBase64(
|
|
[FromBody] ExtractPdfAttachmentsRequest 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);
|
|
|
|
// Send command to MediatR
|
|
var command = new ExtractPdfAttachmentsCommand { PdfStream = pdfStream };
|
|
byte[] zipBytes = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return ZIP file
|
|
return File(zipBytes, "application/zip", "attachments.zip");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Embeds one or more files as attachments in a PDF document (supports PDF/A-3).
|
|
/// Supports multipart/form-data file upload.
|
|
/// </summary>
|
|
/// <param name="pdfFile">The PDF file to add attachments to</param>
|
|
/// <param name="attachmentFiles">Files to embed as attachments (one or more)</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>PDF with embedded attachments</returns>
|
|
/// <response code="200">Attachments added successfully - returns PDF</response>
|
|
/// <response code="400">Invalid input (file missing, not a PDF, or no attachments provided)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("add")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> AddAttachmentsFromFile(
|
|
IFormFile pdfFile,
|
|
List<IFormFile> attachmentFiles,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (pdfFile == null || pdfFile.Length == 0)
|
|
{
|
|
throw new BadRequestException("PDF file is required");
|
|
}
|
|
|
|
if (attachmentFiles == null || attachmentFiles.Count == 0)
|
|
{
|
|
throw new BadRequestException("At least one attachment file is required");
|
|
}
|
|
|
|
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
|
using var pdfStream = pdfFile.OpenReadStream();
|
|
|
|
// Convert attachment files to AttachmentFile records
|
|
var attachments = new List<AttachmentFile>();
|
|
foreach (var file in attachmentFiles)
|
|
{
|
|
using var ms = new MemoryStream();
|
|
await file.CopyToAsync(ms, cancellationToken);
|
|
|
|
attachments.Add(new AttachmentFile
|
|
{
|
|
FileName = file.FileName,
|
|
Content = ms.ToArray(),
|
|
MimeType = file.ContentType
|
|
});
|
|
}
|
|
|
|
// Send command to MediatR
|
|
var command = new AddAttachmentsCommand
|
|
{
|
|
PdfStream = pdfStream,
|
|
Attachments = attachments
|
|
};
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return PDF with attachments
|
|
return File(resultPdf, "application/pdf", "with-attachments.pdf");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Embeds one or more files as attachments in a PDF document (supports PDF/A-3).
|
|
/// Supports Base64-encoded PDF and attachments via JSON payload.
|
|
/// </summary>
|
|
/// <param name="request">Request containing Base64-encoded PDF and attachments</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>PDF with embedded attachments</returns>
|
|
/// <response code="200">Attachments added successfully - returns PDF</response>
|
|
/// <response code="400">Invalid input (Base64 format error, not a PDF, or no attachments provided)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("add")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> AddAttachmentsFromBase64(
|
|
[FromBody] AddAttachmentsRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Convert Base64 PDF to stream
|
|
byte[] pdfBytes;
|
|
try
|
|
{
|
|
pdfBytes = Convert.FromBase64String(request.Base64Pdf);
|
|
}
|
|
catch (FormatException ex)
|
|
{
|
|
throw new BadRequestException("Invalid Base64 PDF format: " + ex.Message);
|
|
}
|
|
|
|
using var pdfStream = new MemoryStream(pdfBytes);
|
|
|
|
// Convert Base64 attachments to AttachmentFile records
|
|
var attachments = new List<AttachmentFile>();
|
|
foreach (var att in request.Attachments)
|
|
{
|
|
byte[] attBytes;
|
|
try
|
|
{
|
|
attBytes = Convert.FromBase64String(att.Base64Content);
|
|
}
|
|
catch (FormatException ex)
|
|
{
|
|
throw new BadRequestException($"Invalid Base64 format for attachment '{att.FileName}': " + ex.Message);
|
|
}
|
|
|
|
attachments.Add(new AttachmentFile
|
|
{
|
|
FileName = att.FileName,
|
|
Content = attBytes,
|
|
MimeType = att.MimeType
|
|
});
|
|
}
|
|
|
|
// Send command to MediatR
|
|
var command = new AddAttachmentsCommand
|
|
{
|
|
PdfStream = pdfStream,
|
|
Attachments = attachments
|
|
};
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return PDF with attachments
|
|
return File(resultPdf, "application/pdf", "with-attachments.pdf");
|
|
}
|
|
}
|
|
|