diff --git a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua index 7bf5327a..17788f24 100644 --- a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua +++ b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua @@ -7135,29 +7135,47 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T304 -- AI source selection with AI retrieval context validation UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T3775725978"] = "AI source selection with AI retrieval context validation" --- Executable Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2217313358"] = "Executable Files" +-- Text +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1041509726"] = "Text" --- All Source Code Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2460199369"] = "All Source Code Files" +-- Office Files +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1063218378"] = "Office Files" --- All Audio Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2575722901"] = "All Audio Files" +-- Executable +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1364437037"] = "Executable" --- All Video Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2850789856"] = "All Video Files" +-- Mail +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1399880782"] = "Mail" --- PDF Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T3108466742"] = "PDF Files" +-- Source like +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1487238587"] = "Source like" --- All Image Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T4086723714"] = "All Image Files" +-- Image +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1494001562"] = "Image" --- Text Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Text Files" +-- Video +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1533528076"] = "Video" --- All Office Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "All Office Files" +-- Source Code +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1569048941"] = "Source Code" + +-- Config +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1779622119"] = "Config" + +-- Audio +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2291602489"] = "Audio" + +-- Custom +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2502277006"] = "Custom" + +-- Media +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T3507473059"] = "Media" + +-- Source like prefix +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T378481461"] = "Source like prefix" + +-- Document +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T4165204724"] = "Document" -- Pandoc Installation UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc Installation" diff --git a/app/MindWork AI Studio/Chat/FileAttachment.cs b/app/MindWork AI Studio/Chat/FileAttachment.cs index f364ed8f..10e506d8 100644 --- a/app/MindWork AI Studio/Chat/FileAttachment.cs +++ b/app/MindWork AI Studio/Chat/FileAttachment.cs @@ -76,34 +76,28 @@ public record FileAttachment(FileAttachmentType Type, string FileName, string Fi /// /// Determines the file attachment type based on the file extension. - /// Uses centrally defined file type filters from . + /// Uses centrally defined file type filters from . /// /// The file path to analyze. /// The corresponding FileAttachmentType. private static FileAttachmentType DetermineFileType(string filePath) { - var extension = Path.GetExtension(filePath).TrimStart('.').ToLowerInvariant(); - - if (FileTypeFilter.Executables.FilterExtensions.Contains(extension)) + // Check if it's an executable: + if (FileTypes.IsAllowedPath(filePath, FileTypes.EXECUTABLES)) return FileAttachmentType.FORBIDDEN; // Check if it's an image file: - if (FileTypeFilter.AllImages.FilterExtensions.Contains(extension)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.IMAGE)) return FileAttachmentType.IMAGE; // Check if it's an audio file: - if (FileTypeFilter.AllAudio.FilterExtensions.Contains(extension)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.AUDIO)) return FileAttachmentType.AUDIO; - // Check if it's an allowed document file (PDF, Text, or Office): - if (FileTypeFilter.PDF.FilterExtensions.Contains(extension) || - FileTypeFilter.Text.FilterExtensions.Contains(extension) || - FileTypeFilter.AllOffice.FilterExtensions.Contains(extension) || - FileTypeFilter.AllSourceCode.FilterExtensions.Contains(extension) || - FileTypeFilter.IsAllowedSourceLikeFileName(filePath)) + // Check if it's an allowed document file (PDF, Text, LaTeX, or Office): + if (FileTypes.IsAllowedPath(filePath, FileTypes.DOCUMENT)) return FileAttachmentType.DOCUMENT; - // All other file types are forbidden: return FileAttachmentType.FORBIDDEN; } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/SelectFile.razor.cs b/app/MindWork AI Studio/Components/SelectFile.razor.cs index 9caf3cd7..91c7a667 100644 --- a/app/MindWork AI Studio/Components/SelectFile.razor.cs +++ b/app/MindWork AI Studio/Components/SelectFile.razor.cs @@ -23,7 +23,7 @@ public partial class SelectFile : MSGComponentBase public string FileDialogTitle { get; set; } = "Select File"; [Parameter] - public FileTypeFilter? Filter { get; set; } + public FileTypeFilter[]? Filter { get; set; } [Parameter] public Func Validation { get; set; } = _ => null; @@ -32,7 +32,7 @@ public partial class SelectFile : MSGComponentBase public RustService RustService { get; set; } = null!; [Inject] - protected ILogger Logger { get; init; } = null!; + protected ILogger Logger { get; init; } = null!; private static readonly Dictionary SPELLCHECK_ATTRIBUTES = new(); diff --git a/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua b/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua index 8d8cadb5..f2fd08fa 100644 --- a/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua +++ b/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua @@ -7137,29 +7137,47 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T304 -- AI-based data source selection with AI retrieval context validation UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T3775725978"] = "KI-basierte Datenquellen-Auswahl mit Validierung des Abrufkontexts" --- Executable Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2217313358"] = "Ausführbare Dateien" +-- Text +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1041509726"] = "Text" --- All Source Code Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2460199369"] = "Alle Quellcodedateien" +-- Office Files +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1063218378"] = "Office-Dateien" --- All Audio Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2575722901"] = "Alle Audiodateien" +-- Executable +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1364437037"] = "Ausführbare Dateien" --- All Video Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2850789856"] = "Alle Videodateien" +-- Mail +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1399880782"] = "E-Mail" --- PDF Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T3108466742"] = "PDF-Dateien" +-- Source like +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1487238587"] = "Source Code ähnlich" --- All Image Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T4086723714"] = "Alle Bilddateien" +-- Image +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1494001562"] = "Bild" --- Text Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Textdateien" +-- Video +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1533528076"] = "Video" --- All Office Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "Alle Office-Dateien" +-- Source Code +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1569048941"] = "Quellcode" + +-- Config +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1779622119"] = "Konfiguration" + +-- Audio +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2291602489"] = "Audio" + +-- Custom +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2502277006"] = "Benutzerdefiniert" + +-- Media +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T3507473059"] = "Medien" + +-- Source like prefix +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T378481461"] = "Source Code ähnlicher Prefix" + +-- Document +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T4165204724"] = "Dokument" -- Pandoc Installation UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc-Installation" diff --git a/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua b/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua index 1fd09c3d..130f0b25 100644 --- a/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua +++ b/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua @@ -7137,29 +7137,47 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T304 -- AI-based data source selection with AI retrieval context validation UI_TEXT_CONTENT["AISTUDIO::TOOLS::RAG::RAGPROCESSES::AISRCSELWITHRETCTXVAL::T3775725978"] = "AI-based data source selection with AI retrieval context validation" --- Executable Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2217313358"] = "Executable Files" +-- Text +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1041509726"] = "Text" --- All Source Code Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2460199369"] = "All Source Code Files" +-- Office Files +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1063218378"] = "Office Files" --- All Audio Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2575722901"] = "All Audio Files" +-- Executable +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1364437037"] = "Executable" --- All Video Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T2850789856"] = "All Video Files" +-- Mail +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1399880782"] = "Mail" --- PDF Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T3108466742"] = "PDF Files" +-- Source like +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1487238587"] = "Source like" --- All Image Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T4086723714"] = "All Image Files" +-- Image +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1494001562"] = "Image" --- Text Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Text Files" +-- Video +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1533528076"] = "Video" --- All Office Files -UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "All Office Files" +-- Source Code +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1569048941"] = "Source Code" + +-- Config +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1779622119"] = "Config" + +-- Audio +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2291602489"] = "Audio" + +-- Custom +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T2502277006"] = "Custom" + +-- Media +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T3507473059"] = "Media" + +-- Source like prefix +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T378481461"] = "Source like prefix" + +-- Document +UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T4165204724"] = "Document" -- Pandoc Installation UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc Installation" diff --git a/app/MindWork AI Studio/Tools/PandocExport.cs b/app/MindWork AI Studio/Tools/PandocExport.cs index 27e5244e..e57afdd8 100644 --- a/app/MindWork AI Studio/Tools/PandocExport.cs +++ b/app/MindWork AI Studio/Tools/PandocExport.cs @@ -2,6 +2,7 @@ using AIStudio.Chat; using AIStudio.Dialogs; using AIStudio.Tools.PluginSystem; +using AIStudio.Tools.Rust; using AIStudio.Tools.Services; using DialogOptions = AIStudio.Dialogs.DialogOptions; @@ -16,7 +17,7 @@ public static class PandocExport public static async Task ToMicrosoftWord(RustService rustService, IDialogService dialogService, string dialogTitle, IContent markdownContent) { - var response = await rustService.SaveFile(dialogTitle, new("Microsoft Word", ["docx"])); + var response = await rustService.SaveFile(dialogTitle, [FileTypes.MS_WORD]); if (response.UserCancelled) { LOGGER.LogInformation("User cancelled the save dialog."); diff --git a/app/MindWork AI Studio/Tools/Rust/FileTypeFilter.cs b/app/MindWork AI Studio/Tools/Rust/FileTypeFilter.cs index d93f44e0..f4cd1c7e 100644 --- a/app/MindWork AI Studio/Tools/Rust/FileTypeFilter.cs +++ b/app/MindWork AI Studio/Tools/Rust/FileTypeFilter.cs @@ -1,125 +1,49 @@ -// ReSharper disable NotAccessedPositionalProperty.Global - -using AIStudio.Tools.PluginSystem; - namespace AIStudio.Tools.Rust; /// -/// Represents a file type filter for file selection dialogs. +/// Represents a file type that can optionally contain child file types. +/// Use the static helpers , and to build readable trees. /// -/// The name of the filter. -/// The file extensions associated with the filter. -public readonly record struct FileTypeFilter(string FilterName, string[] FilterExtensions) +/// Display name of the type (e.g., "Document"). +/// File extensions belonging to this type (without dot). +/// Nested file types that are included when this type is selected. +public sealed record FileTypeFilter(string FilterName, string[] FilterExtensions, IReadOnlyList Children) { - private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(FileTypeFilter).Namespace, nameof(FileTypeFilter)); + /// + /// Factory for a leaf node. + /// Example: FileType.Leaf(".NET", "cs", "razor") + /// + public static FileTypeFilter Leaf(string name, params string[] extensions) => + new(name, extensions, []); - private static string[] AllowedSourceLikeFileNames => - [ - "Dockerfile", - "Containerfile", - "Jenkinsfile", - "Makefile", - "GNUmakefile", - "Procfile", - "Vagrantfile", - "Tiltfile", - "Justfile", - "Brewfile", - "Caddyfile", - "Gemfile", - "Podfile", - "Fastfile", - "Appfile", - "Rakefile", - "Dangerfile", - "BUILD", - "WORKSPACE", - "BUCK", - ]; + /// + /// Factory for a parent node that only has children. + /// Example: FileType.Parent("Source Code", dotnet, java) + /// + public static FileTypeFilter Parent(string name, params FileTypeFilter[]? children) => + new(name, [], children ?? []); - private static string[] AllowedSourceLikeFileNamePrefixes => - [ - "Dockerfile", - "Containerfile", - "Jenkinsfile", - "Procfile", - "Caddyfile", - ]; - - public static bool IsAllowedSourceLikeFileName(string filePath) + /// + /// Factory for a composite node that has its own extensions in addition to children. + /// + public static FileTypeFilter Composite(string name, string[] extensions, params FileTypeFilter[] children) => + new(name, extensions, children); + + /// + /// Collects all extensions for this type, including children. + /// + public IEnumerable FlattenExtensions() { - var fileName = Path.GetFileName(filePath); - if (string.IsNullOrWhiteSpace(fileName)) - return false; - - if (AllowedSourceLikeFileNames.Any(name => string.Equals(name, fileName, StringComparison.OrdinalIgnoreCase))) - return true; - - return AllowedSourceLikeFileNamePrefixes.Any(prefix => fileName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)); + return this.FilterExtensions + .Concat(this.Children.SelectMany(child => child.FlattenExtensions())) + .Distinct(StringComparer.OrdinalIgnoreCase); } - public static FileTypeFilter PDF => new(TB("PDF Files"), ["pdf"]); - - public static FileTypeFilter Text => new(TB("Text Files"), ["txt", "md"]); - - public static FileTypeFilter AllOffice => new(TB("All Office Files"), ["docx", "xlsx", "pptx", "doc", "xls", "ppt", "pdf"]); - - public static FileTypeFilter AllImages => new(TB("All Image Files"), ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg", "webp", "heic"]); - - public static FileTypeFilter AllVideos => new(TB("All Video Files"), ["mp4", "m4v", "avi", "mkv", "mov", "wmv", "flv", "webm"]); - - public static FileTypeFilter AllAudio => new(TB("All Audio Files"), ["mp3", "wav", "wave", "aac", "flac", "ogg", "m4a", "wma", "alac", "aiff", "m4b"]); - - public static FileTypeFilter AllSourceCode => new(TB("All Source Code Files"), - [ - // .NET - "cs", "vb", "fs", "razor", "aspx", "cshtml", "csproj", - - // Java: - "java", - - // Python: - "py", - - // JavaScript/TypeScript: - "js", "ts", - - // C/C++: - "c", "cpp", "h", "hpp", - - // Ruby: - "rb", - - // Go: - "go", - - // Rust: - "rs", - - // Lua: - "lua", - - // PHP: - "php", - - // HTML/CSS: - "html", "css", - - // Swift/Kotlin: - "swift", "kt", - - // Shell scripts: - "sh", "bash", - - // Logging files: - "log", - - // JSON/YAML/XML: - "json", "yaml", "yml", "xml", - - // Config files: - "ini", "cfg", "toml", "plist", - ]); - - public static FileTypeFilter Executables => new(TB("Executable Files"), ["exe", "app", "bin", "appimage"]); + public bool ContainsType(FileTypeFilter target) + { + if (this == target) + return true; + + return this.Children.Any(child => child.ContainsType(target)); + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/Rust/FileTypes.cs b/app/MindWork AI Studio/Tools/Rust/FileTypes.cs new file mode 100644 index 00000000..87a551b2 --- /dev/null +++ b/app/MindWork AI Studio/Tools/Rust/FileTypes.cs @@ -0,0 +1,130 @@ +using AIStudio.Tools.PluginSystem; +// ReSharper disable MemberCanBePrivate.Global + +namespace AIStudio.Tools.Rust; + +/// +/// Central definition of supported file types with parent/child relationships and helpers +/// to build extension whitelists (e.g., for file pickers or validation). +/// +public static class FileTypes +{ + private static string TB(string fallbackEn) => I18N.I.T(fallbackEn, typeof(FileTypeFilter).Namespace, nameof(FileTypeFilter)); + + public static readonly FileTypeFilter SOURCE_LIKE_FILE_NAMES = FileTypeFilter.Leaf(TB("Source like"), + "Dockerfile", "Containerfile", "Jenkinsfile", "Makefile", "GNUmakefile", "Procfile", "Vagrantfile", + "Tiltfile", "Justfile", "Brewfile", "Caddyfile", "Gemfile", "Podfile", "Fastfile", "Appfile", "Rakefile", "Dangerfile", + "BUILD", "WORKSPACE", "BUCK"); + + public static readonly FileTypeFilter SOURCE_LIKE_FILE_NAME_PREFIXES = FileTypeFilter.Leaf(TB("Source like prefix"), + "Dockerfile", "Containerfile", "Jenkinsfile", "Procfile", "Caddyfile"); + + // Source code hierarchy: SourceCode -> (.NET, Java, Python, Web, C/C++, Config, ...) + public static readonly FileTypeFilter DOTNET = FileTypeFilter.Leaf(".NET", "cs", "razor", "vb", "fs", "aspx", "cshtml", "csproj"); + public static readonly FileTypeFilter JAVA = FileTypeFilter.Leaf("Java", "java"); + public static readonly FileTypeFilter PYTHON = FileTypeFilter.Leaf("Python", "py"); + public static readonly FileTypeFilter JAVASCRIPT = FileTypeFilter.Leaf("JavaScript/TypeScript", "js", "ts"); + public static readonly FileTypeFilter CFAMILY = FileTypeFilter.Leaf("C/C++", "c", "cpp", "h", "hpp"); + public static readonly FileTypeFilter RUBY = FileTypeFilter.Leaf("Ruby", "rb"); + public static readonly FileTypeFilter GO = FileTypeFilter.Leaf("Go", "go"); + public static readonly FileTypeFilter RUST = FileTypeFilter.Leaf("Rust", "rs"); + public static readonly FileTypeFilter LUA = FileTypeFilter.Leaf("Lua", "lua"); + public static readonly FileTypeFilter PHP = FileTypeFilter.Leaf("PHP", "php"); + public static readonly FileTypeFilter WEB = FileTypeFilter.Leaf("HTML/CSS", "html", "css"); + public static readonly FileTypeFilter APP = FileTypeFilter.Leaf("Swift/Kotlin", "swift", "kt"); + public static readonly FileTypeFilter SHELL = FileTypeFilter.Leaf("Shell", "sh", "bash", "zsh"); + public static readonly FileTypeFilter LOG = FileTypeFilter.Leaf("Log", "log"); + public static readonly FileTypeFilter JSON = FileTypeFilter.Leaf("JSON", "json"); + public static readonly FileTypeFilter XML = FileTypeFilter.Leaf("XML", "xml"); + public static readonly FileTypeFilter YAML = FileTypeFilter.Leaf("YAML", "yaml", "yml"); + public static readonly FileTypeFilter CONFIG = FileTypeFilter.Leaf(TB("Config"), "ini", "cfg", "toml", "plist"); + + public static readonly FileTypeFilter SOURCE_CODE = FileTypeFilter.Parent(TB("Source Code"), + DOTNET, JAVA, PYTHON, JAVASCRIPT, CFAMILY, RUBY, GO, RUST, LUA, PHP, WEB, APP, SHELL, LOG, JSON, XML, YAML, CONFIG, SOURCE_LIKE_FILE_NAMES, SOURCE_LIKE_FILE_NAME_PREFIXES); + + // Document hierarchy + public static readonly FileTypeFilter PDF = FileTypeFilter.Leaf("PDF", "pdf"); + public static readonly FileTypeFilter TEXT = FileTypeFilter.Leaf(TB("Text"), "txt", "md", "rtf"); + public static readonly FileTypeFilter MS_WORD = FileTypeFilter.Leaf("Microsoft Word", "docx", "doc"); + public static readonly FileTypeFilter WORD = FileTypeFilter.Composite("Word", ["odt"], MS_WORD); + public static readonly FileTypeFilter EXCEL = FileTypeFilter.Leaf("Excel", "xls", "xlsx"); + public static readonly FileTypeFilter POWER_POINT = FileTypeFilter.Leaf("PowerPoint", "ppt", "pptx"); + public static readonly FileTypeFilter MAIL = FileTypeFilter.Leaf(TB("Mail"), "eml", "msg", "mbox"); + public static readonly FileTypeFilter LATEX = FileTypeFilter.Leaf("LaTeX", "tex", "bib", "sty", "cls", "log"); + + public static readonly FileTypeFilter OFFICE_FILES = FileTypeFilter.Parent(TB("Office Files"), + WORD, EXCEL, POWER_POINT, PDF); + public static readonly FileTypeFilter DOCUMENT = FileTypeFilter.Parent(TB("Document"), + TEXT, OFFICE_FILES, SOURCE_CODE, LATEX); + + // Media hierarchy + public static readonly FileTypeFilter IMAGE = FileTypeFilter.Leaf(TB("Image"), + "jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg", "webp", "heic"); + public static readonly FileTypeFilter AUDIO = FileTypeFilter.Leaf(TB("Audio"), + "mp3", "wav", "wave", "aac", "flac", "ogg", "m4a", "wma", "alac", "aiff", "m4b"); + public static readonly FileTypeFilter VIDEO = FileTypeFilter.Leaf(TB("Video"), + "mp4", "m4v", "avi", "mkv", "mov", "wmv", "flv", "webm"); + + public static readonly FileTypeFilter MEDIA = FileTypeFilter.Parent(TB("Media"), IMAGE, AUDIO, VIDEO); + + // Other standalone types + public static readonly FileTypeFilter EXECUTABLES = FileTypeFilter.Leaf(TB("Executable"), "exe", "app", "bin", "appimage"); + + public static FileTypeFilter? AsOneFileType(params FileTypeFilter[]? types) + { + if (types == null || types.Length == 0) + return null; + + if (types.Length == 1) + return types[0]; + + return FileTypeFilter.Composite(TB("Custom"), OnlyAllowTypes(types)); + } + + public static string[] OnlyAllowTypes(params FileTypeFilter[] types) + { + if (types.Length == 0) + return []; + + return types + .Where(t => t != SOURCE_LIKE_FILE_NAMES && t != SOURCE_LIKE_FILE_NAME_PREFIXES) + .SelectMany(t => t.FlattenExtensions()) + .Select(ext => ext.ToLowerInvariant()) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + + /// + /// Validates a file path against the provided filters. + /// Supports extension-based matching and source-like file names (e.g. Dockerfile). + /// + public static bool IsAllowedPath(string filePath, params FileTypeFilter[]? types) + { + if (types == null || types.Length == 0 || string.IsNullOrWhiteSpace(filePath)) + return false; + + var extension = Path.GetExtension(filePath).TrimStart('.'); + if (!string.IsNullOrWhiteSpace(extension)) + { + if (OnlyAllowTypes(types).Contains(extension, StringComparer.OrdinalIgnoreCase)) + return true; + } + + var fileName = Path.GetFileName(filePath); + if (string.IsNullOrWhiteSpace(fileName)) + return false; + + if (types.Any(t => t.ContainsType(SOURCE_LIKE_FILE_NAMES))) + { + if (SOURCE_LIKE_FILE_NAMES.FilterExtensions.Contains(fileName)) + return true; + } + + if (types.Any(t => t.ContainsType(SOURCE_LIKE_FILE_NAME_PREFIXES))){ + if (SOURCE_LIKE_FILE_NAME_PREFIXES.FilterExtensions.Any(prefix => fileName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Tools/Services/RustService.FileSystem.cs b/app/MindWork AI Studio/Tools/Services/RustService.FileSystem.cs index 4a498b01..e44dfa7f 100644 --- a/app/MindWork AI Studio/Tools/Services/RustService.FileSystem.cs +++ b/app/MindWork AI Studio/Tools/Services/RustService.FileSystem.cs @@ -17,13 +17,13 @@ public sealed partial class RustService return await result.Content.ReadFromJsonAsync(this.jsonRustSerializerOptions); } - public async Task SelectFile(string title, FileTypeFilter? filter = null, string? initialFile = null) + public async Task SelectFile(string title, FileTypeFilter[]? filter = null, string? initialFile = null) { var payload = new SelectFileOptions { Title = title, PreviousFile = initialFile is null ? null : new (initialFile), - Filter = filter + Filter = FileTypes.AsOneFileType(filter) }; var result = await this.http.PostAsJsonAsync("/select/file", payload, this.jsonRustSerializerOptions); @@ -36,13 +36,13 @@ public sealed partial class RustService return await result.Content.ReadFromJsonAsync(this.jsonRustSerializerOptions); } - public async Task SelectFiles(string title, FileTypeFilter? filter = null, string? initialFile = null) + public async Task SelectFiles(string title, FileTypeFilter[]? filter = null, string? initialFile = null) { var payload = new SelectFileOptions { Title = title, PreviousFile = initialFile is null ? null : new (initialFile), - Filter = filter + Filter = FileTypes.AsOneFileType(filter) }; var result = await this.http.PostAsJsonAsync("/select/files", payload, this.jsonRustSerializerOptions); @@ -59,17 +59,17 @@ public sealed partial class RustService /// Initiates a dialog to let the user select a file for a writing operation. /// /// The title of the save file dialog. - /// An optional file type filter for filtering specific file formats. + /// Optional file type filters for filtering specific file formats. /// An optional initial file path to pre-fill in the dialog. /// A object containing information about whether the user canceled the /// operation and whether the select operation was successful. - public async Task SaveFile(string title, FileTypeFilter? filter = null, string? initialFile = null) + public async Task SaveFile(string title, FileTypeFilter[]? filter = null, string? initialFile = null) { var payload = new SaveFileOptions { Title = title, PreviousFile = initialFile is null ? null : new (initialFile), - Filter = filter + Filter = FileTypes.AsOneFileType(filter) }; var result = await this.http.PostAsJsonAsync("/save/file", payload, this.jsonRustSerializerOptions); diff --git a/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs b/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs index 02a978d1..d38a8c08 100644 --- a/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs +++ b/app/MindWork AI Studio/Tools/Validation/FileExtensionValidation.cs @@ -43,8 +43,7 @@ public static class FileExtensionValidation /// True if valid, false if invalid (error/warning already sent via MessageBus). public static async Task IsExtensionValidWithNotifyAsync(UseCase useCae, string filePath, bool validateMediaFileTypes = true, Settings.Provider? provider = null) { - var ext = Path.GetExtension(filePath).TrimStart('.').ToLowerInvariant(); - if(FileTypeFilter.Executables.FilterExtensions.Contains(ext)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.EXECUTABLES)) { await MessageBus.INSTANCE.SendError(new( Icons.Material.Filled.AppBlocking, @@ -53,7 +52,7 @@ public static class FileExtensionValidation } var capabilities = provider?.GetModelCapabilities() ?? new(); - if (FileTypeFilter.AllImages.FilterExtensions.Contains(ext)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.IMAGE)) { switch (useCae) { @@ -88,7 +87,7 @@ public static class FileExtensionValidation } } - if(FileTypeFilter.AllVideos.FilterExtensions.Contains(ext)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.VIDEO)) { await MessageBus.INSTANCE.SendWarning(new( Icons.Material.Filled.FeaturedVideo, @@ -96,7 +95,7 @@ public static class FileExtensionValidation return false; } - if(FileTypeFilter.AllAudio.FilterExtensions.Contains(ext)) + if (FileTypes.IsAllowedPath(filePath, FileTypes.AUDIO)) { await MessageBus.INSTANCE.SendWarning(new( Icons.Material.Filled.AudioFile, @@ -123,7 +122,7 @@ public static class FileExtensionValidation return false; } - if (!Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase))) + if (FileTypes.IsAllowedPath(filePath, FileTypes.IMAGE)) { await MessageBus.INSTANCE.SendError(new( Icons.Material.Filled.ImageNotSupported,