From 741f72bc5a3888a65ddf41d0d30c2ebe19344273 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 16 Dec 2025 21:24:35 +0100 Subject: [PATCH] Add method to validate and load images from file paths --- app/MindWork AI Studio/Chat/ContentImage.cs | 18 +++++++++++++ .../Validation/FileExtensionValidation.cs | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/MindWork AI Studio/Chat/ContentImage.cs b/app/MindWork AI Studio/Chat/ContentImage.cs index 6026e554..a6f385b8 100644 --- a/app/MindWork AI Studio/Chat/ContentImage.cs +++ b/app/MindWork AI Studio/Chat/ContentImage.cs @@ -1,6 +1,7 @@ using System.Text.Json.Serialization; using AIStudio.Provider; +using AIStudio.Tools.Validation; namespace AIStudio.Chat; @@ -50,6 +51,23 @@ public sealed class ContentImage : IContent, IImageSource #endregion + /// + /// Creates a ContentImage from a local file path. + /// + /// The path to the image file. + /// A new ContentImage instance if the file is valid, null otherwise. + public static async Task CreateFromFileAsync(string filePath) + { + if (!await FileExtensionValidation.IsImageExtensionValidWithNotifyAsync(filePath)) + return null; + + return new ContentImage + { + SourceType = ContentImageSource.LOCAL_PATH, + Source = filePath, + }; + } + /// /// The type of the image source. /// diff --git a/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs b/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs index cc40c959..741e596a 100644 --- a/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs +++ b/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs @@ -52,4 +52,31 @@ public static class FileExtensionValidation return true; } + + /// + /// Validates that the file is a supported image format and sends appropriate MessageBus notifications when invalid. + /// + /// The file path to validate. + /// True if valid image, false if invalid (error already sent via MessageBus). + public static async Task IsImageExtensionValidWithNotifyAsync(string filePath) + { + var ext = Path.GetExtension(filePath).TrimStart('.'); + if (string.IsNullOrWhiteSpace(ext)) + { + await MessageBus.INSTANCE.SendError(new( + Icons.Material.Filled.ImageNotSupported, + TB("File has no extension"))); + return false; + } + + if (!Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase))) + { + await MessageBus.INSTANCE.SendError(new( + Icons.Material.Filled.ImageNotSupported, + TB("Unsupported image format"))); + return false; + } + + return true; + } }