@@ -40,12 +40,37 @@ public static ClassicProfile ToClassicProfile(ProfileEntity profile)
4040 {
4141 AssetType . Text => text ?? string . Empty ,
4242 AssetType . Remote => text ?? string . Empty ,
43- AssetType . Image => data != null
44- ? $ "data:{ ImageHelper . DetectMimeType ( data ) } ;base64,{ Convert . ToBase64String ( data ) } "
45- : string . Empty ,
43+ AssetType . Style => text ?? string . Empty ,
44+ AssetType . Image => data != null ? CreateImageDataUrl ( data ) : string . Empty ,
4645 _ => string . Empty
4746 } ;
4847
48+ private static string CreateImageDataUrl ( byte [ ] data )
49+ {
50+ var mimeType = ImageHelper . DetectMimeType ( data ) ;
51+ var base64Length = GetBase64EncodedLength ( data . Length ) ;
52+ var totalLength = 5 + mimeType . Length + 8 + base64Length ; // "data:" + mimeType + ";base64," + base64
53+
54+ return string . Create ( totalLength , ( data , mimeType ) , static ( span , state ) =>
55+ {
56+ var current = span ;
57+
58+ "data:" . AsSpan ( ) . CopyTo ( current ) ;
59+ current = current [ 5 ..] ;
60+
61+ state . mimeType . AsSpan ( ) . CopyTo ( current ) ;
62+ current = current [ state . mimeType . Length ..] ;
63+
64+ ";base64," . AsSpan ( ) . CopyTo ( current ) ;
65+ current = current [ 8 ..] ;
66+
67+ Convert . TryToBase64Chars ( state . data , current , out _ ) ;
68+ } ) ;
69+ }
70+
71+ private static int GetBase64EncodedLength ( int byteLength ) => ( ( byteLength + 2 ) / 3 ) * 4 ;
72+
73+
4974 private static ClassicContact ToClassicContact ( ContactItemEntity c )
5075 {
5176 var value = c . ImageType . HasValue
0 commit comments