From 4c181ea924b5fdd75588d132175fc617d9f2ee3c Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 29 Dec 2025 17:33:58 +0100 Subject: [PATCH] Add DetermineMimeType method for improved MIME type detection based on image source --- .../Chat/IImageSourceExtensions.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/app/MindWork AI Studio/Chat/IImageSourceExtensions.cs b/app/MindWork AI Studio/Chat/IImageSourceExtensions.cs index 62631c68..41706047 100644 --- a/app/MindWork AI Studio/Chat/IImageSourceExtensions.cs +++ b/app/MindWork AI Studio/Chat/IImageSourceExtensions.cs @@ -6,6 +6,64 @@ public static class IImageSourceExtensions { private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(IImageSourceExtensions).Namespace, nameof(IImageSourceExtensions)); + public static string DetermineMimeType(this IImageSource image) + { + switch (image.SourceType) + { + case ContentImageSource.BASE64: + { + // Try to detect the mime type from the base64 string: + var base64Data = image.Source; + if (base64Data.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) + { + var mimeEnd = base64Data.IndexOf(';'); + if (mimeEnd > 5) + { + return base64Data[5..mimeEnd]; + } + } + + // Fallback: + return "application/octet-stream"; + } + + case ContentImageSource.URL: + { + // Try to detect the mime type from the URL extension: + var uri = new Uri(image.Source); + var extension = Path.GetExtension(uri.AbsolutePath).ToLowerInvariant(); + return extension switch + { + ".png" => "image/png", + ".jpg" or ".jpeg" => "image/jpeg", + ".gif" => "image/gif", + ".bmp" => "image/bmp", + ".webp" => "image/webp", + + _ => "application/octet-stream" + }; + } + + case ContentImageSource.LOCAL_PATH: + { + var extension = Path.GetExtension(image.Source).ToLowerInvariant(); + return extension switch + { + ".png" => "image/png", + ".jpg" or ".jpeg" => "image/jpeg", + ".gif" => "image/gif", + ".bmp" => "image/bmp", + ".webp" => "image/webp", + + _ => "application/octet-stream" + }; + } + + default: + return "application/octet-stream"; + } + } + /// /// Read the image content as a base64 string. ///