diff --git a/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs b/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs index 3c6f7b33..152cb02b 100644 --- a/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs +++ b/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs @@ -40,7 +40,9 @@ public class MappingProfile : Profile // DTO to Entity mappings CreateMap(); CreateMap(); - CreateMap().MapChangedWhen(); + CreateMap() + .ForMember(dest => dest.Ink, opt => opt.MapFrom(src => src.DataUrl.MapDataUrlToRequiredBytes())) + .MapChangedWhen(); CreateMap(); CreateMap(); CreateMap(); diff --git a/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs b/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs index 72f2abef..ca1667e2 100644 --- a/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs +++ b/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs @@ -1,5 +1,6 @@ using AutoMapper; using EnvelopeGenerator.Domain.Interfaces.Auditing; +using System; namespace EnvelopeGenerator.Application.Common.Extensions; @@ -21,4 +22,21 @@ public static class AutoMapperAuditingExtensions public static IMappingExpression MapChangedWhen(this IMappingExpression expression) where TDestination : IHasChangedWhen => expression.ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.Now)); + + /// + /// Converts a base64 data URL string to a byte array. + /// Handles data URLs in the format: "data:image/png;base64,iVBORw0KG..." + /// + /// The base64 data URL string from Canvas.toDataURL() + /// The decoded byte array, or null if the input is null or empty + 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); + } } \ No newline at end of file