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.
183 lines
7.7 KiB
C#
183 lines
7.7 KiB
C#
using DocumentService.Application.ConvertFromPdfA;
|
|
using DocumentService.Application.ConvertToPdfA;
|
|
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 conversion operations (PDF ? PDF/A)
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/pdf/conversion")]
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
public class PdfConversionController(IMediator mediator) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Converts a standard PDF to PDF/A format.
|
|
/// Supports multipart/form-data file upload.
|
|
/// </summary>
|
|
/// <param name="file">The PDF file to convert</param>
|
|
/// <param name="pdfALevel">Target PDF/A level (e.g., "PDF/A-1b", "PDF/A-2b", "PDF/A-3b")</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>PDF/A compliant document</returns>
|
|
/// <response code="200">PDF converted to PDF/A successfully</response>
|
|
/// <response code="400">Invalid input (file missing, not a PDF, or invalid PDF/A level)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("to-pdfa")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ConvertToPdfAFromFile(
|
|
IFormFile file,
|
|
[FromQuery] string pdfALevel = "PDF/A-3b",
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
throw new BadRequestException("PDF file is required");
|
|
}
|
|
|
|
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
|
using var pdfStream = file.OpenReadStream();
|
|
|
|
// Send command to MediatR
|
|
var command = new ConvertToPdfACommand
|
|
{
|
|
PdfStream = pdfStream,
|
|
PdfALevel = pdfALevel
|
|
};
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return PDF/A file
|
|
return File(resultPdf, "application/pdf", "converted-pdfa.pdf");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a standard PDF to PDF/A format.
|
|
/// Supports Base64-encoded PDF via JSON payload.
|
|
/// </summary>
|
|
/// <param name="request">Request containing Base64-encoded PDF and PDF/A level</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>PDF/A compliant document</returns>
|
|
/// <response code="200">PDF converted to PDF/A successfully</response>
|
|
/// <response code="400">Invalid input (Base64 format error, not a PDF, or invalid PDF/A level)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("to-pdfa")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ConvertToPdfAFromBase64(
|
|
[FromBody] ConvertToPdfARequest request,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
// 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);
|
|
|
|
// Send command to MediatR
|
|
var command = new ConvertToPdfACommand
|
|
{
|
|
PdfStream = pdfStream,
|
|
PdfALevel = request.PdfALevel ?? "PDF/A-3b"
|
|
};
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return PDF/A file
|
|
return File(resultPdf, "application/pdf", "converted-pdfa.pdf");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a PDF/A document to a standard PDF (removes PDF/A restrictions).
|
|
/// Supports multipart/form-data file upload.
|
|
/// </summary>
|
|
/// <param name="file">The PDF/A file to convert</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Standard PDF document</returns>
|
|
/// <response code="200">PDF/A converted to standard PDF successfully</response>
|
|
/// <response code="400">Invalid input (file missing, not a PDF)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("from-pdfa")]
|
|
[Consumes("multipart/form-data")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ConvertFromPdfAFromFile(
|
|
IFormFile file,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
throw new BadRequestException("PDF/A file is required");
|
|
}
|
|
|
|
// Use IFormFile stream directly (no intermediate byte[] conversion)
|
|
using var pdfStream = file.OpenReadStream();
|
|
|
|
// Send command to MediatR
|
|
var command = new ConvertFromPdfACommand { PdfStream = pdfStream };
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return standard PDF file
|
|
return File(resultPdf, "application/pdf", "converted-pdf.pdf");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a PDF/A document to a standard PDF (removes PDF/A restrictions).
|
|
/// Supports Base64-encoded PDF via JSON payload.
|
|
/// </summary>
|
|
/// <param name="request">Request containing Base64-encoded PDF/A</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
/// <returns>Standard PDF document</returns>
|
|
/// <response code="200">PDF/A converted to standard PDF successfully</response>
|
|
/// <response code="400">Invalid input (Base64 format error, not a PDF)</response>
|
|
/// <response code="500">Internal server error during PDF processing</response>
|
|
[Obsolete("This endpoint is not implemented yet.")]
|
|
[HttpPost("from-pdfa")]
|
|
[Consumes("application/json")]
|
|
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> ConvertFromPdfAFromBase64(
|
|
[FromBody] ConvertFromPdfARequest request,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
// 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);
|
|
|
|
// Send command to MediatR
|
|
var command = new ConvertFromPdfACommand { PdfStream = pdfStream };
|
|
byte[] resultPdf = await mediator.Send(command, cancellationToken);
|
|
|
|
// Return standard PDF file
|
|
return File(resultPdf, "application/pdf", "converted-pdf.pdf");
|
|
}
|
|
}
|