Updated the `ComposeSignaturePreviewPng` method in the `IPdfPageImageRenderer` interface to return a `Stream` instead of a `byte[]` for improved memory efficiency when handling large image data. - Added `ComposeSignaturePreviewPngBytes` extension method to convert the `Stream` to a `byte[]` for backward compatibility. - Updated `PdfPageImageRenderer` to return a `Stream` and adjusted its internal logic to reset the `MemoryStream` position before returning. - Modified `DevSignatureInkController` to use the `Stream`-based implementation for HTTP responses. - Made minor formatting adjustments in `PdfPageImageRenderer`. These changes optimize memory usage while maintaining compatibility with existing workflows that require `byte[]`.
67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using EnvelopeGenerator.Domain.Interfaces;
|
|
|
|
namespace EnvelopeGenerator.Application.Common.Interfaces.Doc;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public interface IPdfPageImageRenderer
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="pdfBytes"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
Task<IReadOnlyList<PdfPageImageDto>> RenderPagesAsImagesAsync(byte[] pdfBytes, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="element"></param>
|
|
/// <returns></returns>
|
|
Stream ComposeSignaturePreviewPng(ISignatureInkPayload element);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <returns></returns>
|
|
public bool IsSupportedImageFormat(byte[] bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class PdfPageImageRendererExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="renderer"></param>
|
|
/// <param name="inkBytes"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="BadRequestException"></exception>
|
|
public static byte[] EnsureSupportedImageBytes(this IPdfPageImageRenderer renderer, byte[] inkBytes)
|
|
{
|
|
if (!renderer.IsSupportedImageFormat(inkBytes))
|
|
throw new BadRequestException("Only PNG and JPEG signature formats are supported.");
|
|
|
|
return inkBytes;
|
|
}
|
|
|
|
public static byte[] ComposeSignaturePreviewPngBytes(this IPdfPageImageRenderer renderer, ISignatureInkPayload element)
|
|
{
|
|
using var stream = renderer.ComposeSignaturePreviewPng(element);
|
|
|
|
if (stream is MemoryStream memoryStream)
|
|
return memoryStream.ToArray();
|
|
|
|
using var copy = new MemoryStream();
|
|
stream.CopyTo(copy);
|
|
return copy.ToArray();
|
|
}
|
|
}
|