diff --git a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor index 90f889bd..4f222b92 100644 --- a/app/MindWork AI Studio/Chat/ContentBlockComponent.razor +++ b/app/MindWork AI Studio/Chat/ContentBlockComponent.razor @@ -107,9 +107,21 @@ break; case ContentType.IMAGE: - if (this.Content is ContentImage { SourceType: ContentImageSource.URL or ContentImageSource.LOCAL_PATH } imageContent) + if (this.Content is ContentImage imageContent) { - + var imageSrc = imageContent.SourceType switch + { + ContentImageSource.BASE64 => ImageHelpers.ToDataUrl(imageContent.Source), + ContentImageSource.URL => imageContent.Source, + ContentImageSource.LOCAL_PATH => imageContent.Source, + + _ => string.Empty + }; + + if (!string.IsNullOrWhiteSpace(imageSrc)) + { + + } } break; diff --git a/app/MindWork AI Studio/Tools/ImageHelpers.cs b/app/MindWork AI Studio/Tools/ImageHelpers.cs new file mode 100644 index 00000000..21227a4b --- /dev/null +++ b/app/MindWork AI Studio/Tools/ImageHelpers.cs @@ -0,0 +1,62 @@ +namespace AIStudio.Tools; + +/// +/// Helper methods for image handling, particularly for Base64 images. +/// +public static class ImageHelpers +{ + /// + /// Detects the MIME type of an image from its Base64-encoded header. + /// + /// The Base64-encoded image string. + /// The detected MIME type (e.g., "image/png", "image/jpeg"). + public static string DetectMimeType(ReadOnlySpan base64ImageString) + { + if (base64ImageString.IsWhiteSpace() || base64ImageString.Length < 10) + return "image"; // Fallback + + var header = base64ImageString[..Math.Min(20, base64ImageString.Length)]; + + // + // See https://en.wikipedia.org/wiki/List_of_file_signatures + // + + // PNG: iVBORw0KGgo + if (header.StartsWith("iVBORw0KGgo", StringComparison.Ordinal)) + return "image/png"; + + // JPEG: /9j/ + if (header.StartsWith("/9j/", StringComparison.Ordinal)) + return "image/jpeg"; + + // GIF: R0lGOD + if (header.StartsWith("R0lGOD", StringComparison.Ordinal)) + return "image/gif"; + + // WebP: UklGR + if (header.StartsWith("UklGR", StringComparison.Ordinal)) + return "image/webp"; + + // BMP: Qk + if (header.StartsWith("Qk", StringComparison.Ordinal)) + return "image/bmp"; + + // Default fallback: + return "image"; + } + + /// + /// Converts a Base64 string to a data URL suitable for use in HTML img src attributes. + /// + /// The Base64-encoded image string. + /// Optional MIME type. If not provided, it will be auto-detected. + /// A data URL in the format "data:image/type;base64,..." + public static string ToDataUrl(string base64String, string? mimeType = null) + { + if (string.IsNullOrEmpty(base64String)) + return string.Empty; + + var detectedMimeType = mimeType ?? DetectMimeType(base64String); + return $"data:{detectedMimeType};base64,{base64String}"; + } +}