Updated `MappingProfile` to map `Signature.DataUrl` to `DocReceiverElement.Ink` using the new `MapDataUrlToRequiredBytes` extension method. Added `MapDataUrlToRequiredBytes` to handle base64-encoded data URLs, converting them to byte arrays. Introduced a `using System;` directive in `AutoMapperAuditingExtensions.cs` to support `DateTime`. Retained `MapChangedWhen` functionality while extending mapping capabilities for handling base64 data URLs.
42 lines
1.9 KiB
C#
42 lines
1.9 KiB
C#
using AutoMapper;
|
|
using EnvelopeGenerator.Domain.Interfaces.Auditing;
|
|
using System;
|
|
|
|
namespace EnvelopeGenerator.Application.Common.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extension methods for applying auditing timestamps during AutoMapper mappings.
|
|
/// </summary>
|
|
public static class AutoMapperAuditingExtensions
|
|
{
|
|
/// <summary>
|
|
/// Maps <see cref="IHasAddedWhen.AddedWhen"/> to the current UTC time.
|
|
/// </summary>
|
|
public static IMappingExpression<TSource, TDestination> MapAddedWhen<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
|
|
where TDestination : IHasAddedWhen
|
|
=> expression.ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.Now));
|
|
|
|
/// <summary>
|
|
/// Maps <see cref="IHasChangedWhen.ChangedWhen"/> to the current UTC time.
|
|
/// </summary>
|
|
public static IMappingExpression<TSource, TDestination> MapChangedWhen<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
|
|
where TDestination : IHasChangedWhen
|
|
=> expression.ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.Now));
|
|
|
|
/// <summary>
|
|
/// Converts a base64 data URL string to a byte array.
|
|
/// Handles data URLs in the format: "data:image/png;base64,iVBORw0KG..."
|
|
/// </summary>
|
|
/// <param name="dataUrl">The base64 data URL string from Canvas.toDataURL()</param>
|
|
/// <returns>The decoded byte array, or null if the input is null or empty</returns>
|
|
public static byte[]? MapDataUrlToRequiredBytes(this string dataUrl)
|
|
{
|
|
// Remove data URL prefix (e.g., "data:image/png;base64,")
|
|
var base64Index = dataUrl.IndexOf(',', StringComparison.Ordinal);
|
|
if (base64Index == -1)
|
|
throw new ArgumentException("Invalid data URL format. Unable to extract base64 data.", nameof(dataUrl));
|
|
|
|
var base64Data = dataUrl[(base64Index + 1)..];
|
|
return Convert.FromBase64String(base64Data);
|
|
}
|
|
} |