Introduced a new `ReadDocumentDetailsQuery` to fetch document metadata (`DocumentDetailsDto`) without the binary payload. Updated `MappingProfile` to map `Document` to `DocumentDetailsDto`. Refactored `DocumentService` to handle separate fetching of metadata and binary data, combining them into `DocumentDto`. Changed the metadata endpoint to `/api/Document/details`. Added a new `GetDocumentDetails` endpoint in `DocumentController` to return metadata without binary data. Updated `EnvelopeReceiverPageDataService` with a new method `GetDocumentMetadataOfSenderAsync` for fetching metadata. These changes improve efficiency by allowing clients to load metadata without transferring large binary data when unnecessary.
79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
using DigitalData.Auth.Claims;
|
|
using EnvelopeGenerator.Server.Controllers.Interfaces;
|
|
using EnvelopeGenerator.Server.Extensions;
|
|
using EnvelopeGenerator.Application.Documents.Queries;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace EnvelopeGenerator.Server.Controllers;
|
|
|
|
/// <summary>
|
|
/// Provides access to envelope documents for authenticated receivers.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="DocumentController"/> class.
|
|
/// </remarks>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class DocumentController(IMediator mediator, IAuthorizationService authService, ILogger<DocumentController> logger) : ControllerBase, IAuthController
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public IAuthorizationService AuthService => authService;
|
|
|
|
/// <summary>
|
|
/// Returns the document bytes receiver.
|
|
/// </summary>
|
|
/// <param name="query">Encoded envelope key.</param>
|
|
/// <param name="cancel">Cancellation token.</param>
|
|
[HttpGet]
|
|
[Authorize(AuthenticationSchemes = AuthScheme.Sender)]
|
|
public async Task<IActionResult> GetDocument(CancellationToken cancel, [FromQuery] ReadDocumentQuery query)
|
|
{
|
|
var senderDoc = await mediator.Send(query, cancel);
|
|
return senderDoc?.ByteData is byte[] senderDocByte
|
|
? File(senderDocByte, "application/octet-stream")
|
|
: NotFound("Document is empty.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns sender document metadata and element details (without binary payload) for editor loading.
|
|
/// </summary>
|
|
/// <param name="query">Document query with EnvelopeId.</param>
|
|
/// <param name="cancel">Cancellation token.</param>
|
|
[HttpGet("details")]
|
|
[Authorize(AuthenticationSchemes = AuthScheme.Sender)]
|
|
public async Task<IActionResult> GetDocumentDetails(CancellationToken cancel, [FromQuery] ReadDocumentDetailsQuery query)
|
|
{
|
|
var senderDoc = await mediator.Send(query with { UserId = User.GetId() }, cancel);
|
|
if (senderDoc is null)
|
|
return NotFound("Document is empty.");
|
|
|
|
return Ok(senderDoc);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the document for the specified envelope key.
|
|
/// </summary>
|
|
/// <param name="envelopeKey"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
[Authorize(Policy = AuthPolicy.Receiver)]
|
|
[HttpGet("{envelopeKey}")]
|
|
public async Task<IActionResult> GetDocumentOfReceiver(string envelopeKey, CancellationToken cancel)
|
|
{
|
|
int envelopeId = User.EnvelopeId();
|
|
|
|
var senderDoc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel);
|
|
|
|
if (senderDoc.ByteData is not byte[] senderDocByte)
|
|
return NotFound("Document is empty.");
|
|
|
|
Response.Headers.ContentDisposition = $"inline; filename=\"{envelopeKey}.pdf\"";
|
|
return File(senderDocByte, "application/pdf");
|
|
}
|
|
}
|