Files
EnvelopeGenerator/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/PdfRenderController.cs
TekH 0fbaaf0b92 Refactor ReceiverPage for WebAssembly and state persistence
Switched from server-side Blazor (`InteractiveServer`) to client-side WebAssembly (`InteractiveWebAssembly`) rendering for `ReceiverPage` and `ReceiverRejectedPage`.

Refactored dependency injection:
- Removed: `IMediator`, `EnvelopeReceiverAuthorizationService`, `EnvelopeReceiverPageDataService`, `IPdfPageImageRenderer`.
- Added: `DocumentService`, `EnvelopeReceiverService`, `PdfRenderService`, `PersistentComponentState`.

Implemented state persistence using `PersistentComponentState` to improve user experience by restoring page state. Added `ReceiverPageInitialState` class and implemented `IDisposable` for cleanup.

Simplified authorization by replacing claims-based checks with `AuthService.CheckEnvelopeAccessAsync`. Updated service usage to streamline data fetching and PDF rendering.

Refactored `IsRejected` logic to rely solely on `EnvelopeStatus`. Cleaned up unused namespaces and modernized code patterns for better readability and maintainability.

Updated `PdfRenderController` to allow anonymous access to the `RenderPages` endpoint, enabling broader use cases.

Overall, these changes improve maintainability, simplify dependencies, and align the application with a more scalable client-side architecture.
2026-11-24 14:47:46 +01:00

42 lines
1.7 KiB
C#

using DigitalData.Auth.Claims;
using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.Common.Interfaces.Doc;
using EnvelopeGenerator.Domain.Constants;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Security.Cryptography;
namespace EnvelopeGenerator.Server.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PdfRenderController(IPdfPageImageRenderer renderer, IMemoryCache memoryCache) : ControllerBase
{
private static readonly TimeSpan RenderCacheDuration = TimeSpan.FromMinutes(5);
// TODO(perf): Replace JSON/base64 transport for PdfBytes with application/octet-stream (or multipart) and stream-based request parsing to reduce payload size and allocation pressure for large PDFs.
[HttpPost("pages")]
[AllowAnonymous]
public async Task<IActionResult> RenderPages([FromBody] PdfRenderRequestDto? request, CancellationToken cancel)
{
if (request?.PdfBytes is not { Length: > 0 } bytes)
return BadRequest("Missing PDF bytes.");
var cacheKey = BuildCacheKey(bytes);
if (memoryCache.TryGetValue(cacheKey, out IReadOnlyList<PdfPageImageDto>? cachedPages) && cachedPages is not null)
return Ok(cachedPages);
IReadOnlyList<PdfPageImageDto> pages = await renderer.RenderPagesAsImagesAsync(bytes, cancel);
memoryCache.Set(cacheKey, pages, RenderCacheDuration);
return Ok(pages);
}
private static string BuildCacheKey(byte[] pdfBytes)
{
var hash = Convert.ToHexString(SHA256.HashData(pdfBytes));
return $"pdf-render:pages:v1:dpi110:{hash}";
}
}