Refactoring of file drop

This commit is contained in:
PaulKoudelka 2026-03-04 18:14:06 +01:00
parent 721d5c9070
commit 0007b80a3a
13 changed files with 202 additions and 27 deletions

View File

@ -6343,6 +6343,39 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Text Fil
-- All Office Files
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "All Office Files"
-- Text
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1041509726"] = "Text"
-- Office Files
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1063218378"] = "Office Files"
-- Executable
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1364437037"] = "Executable"
-- Image
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1494001562"] = "Image"
-- Video
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T1533528076"] = "Video"
-- 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"
-- Document
UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPES::T4165204724"] = "Document"
-- Pandoc Installation
UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc Installation"

View File

@ -58,11 +58,14 @@ public record FileAttachment(FileAttachmentType Type, string FileName, string Fi
/// extracting the filename, and reading the file size.
/// </summary>
/// <param name="filePath">The full path to the file.</param>
/// <param name="allowedTypes">Optional: The allowed file types.</param>
/// <returns>A FileAttachment instance with populated properties.</returns>
public static FileAttachment FromPath(string filePath)
public static FileAttachment FromPath(string filePath, FileType[]? allowedTypes=null)
{
var fileName = Path.GetFileName(filePath);
var fileSize = File.Exists(filePath) ? new FileInfo(filePath).Length : 0;
if (allowedTypes != null && !IsAllowed(filePath, allowedTypes))
return new FileAttachment(FileAttachmentType.FORBIDDEN, fileName, filePath, fileSize);
var type = DetermineFileType(filePath);
return type switch
@ -76,7 +79,7 @@ public record FileAttachment(FileAttachmentType Type, string FileName, string Fi
/// <summary>
/// Determines the file attachment type based on the file extension.
/// Uses centrally defined file type filters from <see cref="FileTypeFilter"/>.
/// Uses centrally defined file types from <see cref="FileTypes"/>.
/// </summary>
/// <param name="filePath">The file path to analyze.</param>
/// <returns>The corresponding FileAttachmentType.</returns>
@ -85,21 +88,28 @@ public record FileAttachment(FileAttachmentType Type, string FileName, string Fi
var extension = Path.GetExtension(filePath).TrimStart('.').ToLowerInvariant();
// Check if it's an image file:
if (FileTypeFilter.AllImages.FilterExtensions.Contains(extension))
if (FileTypes.OnlyAllowTypes(FileTypes.IMAGE).Contains(extension))
{
return FileAttachmentType.IMAGE;
}
// Check if it's an audio file:
if (FileTypeFilter.AllAudio.FilterExtensions.Contains(extension))
if (FileTypes.OnlyAllowTypes(FileTypes.AUDIO).Contains(extension))
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))
if (FileTypes.OnlyAllowTypes(FileTypes.DOCUMENT).Contains(extension))
{
return FileAttachmentType.DOCUMENT;
}
// All other file types are forbidden:
return FileAttachmentType.FORBIDDEN;
}
private static bool IsAllowed(string filePath, FileType[] allowedTypes)
{
var extension = Path.GetExtension(filePath).TrimStart('.').ToLowerInvariant();
return FileTypes.OnlyAllowTypes(allowedTypes).Contains(extension);
}
}

View File

@ -48,6 +48,9 @@ public partial class AttachDocuments : MSGComponentBase
[Parameter]
public bool UseSmallForm { get; set; }
[Parameter]
public FileType[]? AllowedFileTypes { get; set; }
/// <summary>
/// When true, validate media file types before attaching. Default is true. That means that
/// the user cannot attach unsupported media file types when the provider or model does not
@ -181,8 +184,7 @@ public partial class AttachDocuments : MSGComponentBase
{
if(!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.ATTACHING_CONTENT, path, this.ValidateMediaFileTypes, this.Provider))
continue;
this.DocumentPaths.Add(FileAttachment.FromPath(path));
this.DocumentPaths.Add(FileAttachment.FromPath(path, this.AllowedFileTypes));
}
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
@ -226,7 +228,7 @@ public partial class AttachDocuments : MSGComponentBase
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.ATTACHING_CONTENT, selectedFilePath, this.ValidateMediaFileTypes, this.Provider))
continue;
this.DocumentPaths.Add(FileAttachment.FromPath(selectedFilePath));
this.DocumentPaths.Add(FileAttachment.FromPath(selectedFilePath, this.AllowedFileTypes));
}
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);

View File

@ -23,7 +23,7 @@ public partial class SelectFile : MSGComponentBase
public string FileDialogTitle { get; set; } = "Select File";
[Parameter]
public FileTypeFilter? Filter { get; set; }
public FileType[]? Filter { get; set; }
[Parameter]
public Func<string, string?> Validation { get; set; } = _ => null;

View File

@ -1,5 +1,6 @@
@using AIStudio.Provider
@using AIStudio.Provider.SelfHosted
@using AIStudio.Tools.Rust
@inherits MSGComponentBase
<MudDialog>
@ -124,6 +125,7 @@
Validation="@this.providerValidation.ValidatingInstanceName"
UserAttributes="@SPELLCHECK_ATTRIBUTES"
/>
<AttachDocuments Name="File Attachments" Layer="@DropLayers.PAGES" @bind-DocumentPaths="@this.chatDocumentPaths" CatchAllDocuments="true" UseSmallForm="true" ValidateMediaFileTypes="true" AllowedFileTypes="[FileTypes.JSON]"/>
</MudForm>
<Issues IssuesData="@this.dataIssues"/>

View File

@ -1,3 +1,4 @@
using AIStudio.Chat;
using AIStudio.Components;
using AIStudio.Provider;
using AIStudio.Settings;
@ -96,7 +97,8 @@ public partial class EmbeddingProviderDialog : MSGComponentBase, ISecretId
private readonly List<Model> availableModels = new();
private readonly Encryption encryption = Program.ENCRYPTION;
private readonly ProviderValidation providerValidation;
private HashSet<FileAttachment> chatDocumentPaths = [];
public EmbeddingProviderDialog()
{
this.providerValidation = new()

View File

@ -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<bool> 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.");

View File

@ -0,0 +1,41 @@
namespace AIStudio.Tools.Rust;
/// <summary>
/// Represents a file type that can optionally contain child file types.
/// Use the static helpers <see cref="Leaf"/>, <see cref="Parent"/> and <see cref="Composite"/> to build readable trees.
/// </summary>
/// <param name="FilterName">Display name of the type (e.g., "Document").</param>
/// <param name="FilterExtensions">File extensions belonging to this type (without dot).</param>
/// <param name="Children">Nested file types that are included when this type is selected.</param>
public sealed record FileType(string FilterName, string[] FilterExtensions, IReadOnlyList<FileType> Children)
{
/// <summary>
/// Factory for a leaf node.
/// Example: <c>FileType.Leaf(".NET", "cs", "razor")</c>
/// </summary>
public static FileType Leaf(string name, params string[] extensions) =>
new(name, extensions, []);
/// <summary>
/// Factory for a parent node that only has children.
/// Example: <c>FileType.Parent("Source Code", dotnet, java)</c>
/// </summary>
public static FileType Parent(string name, params FileType[]? children) =>
new(name, [], children ?? []);
/// <summary>
/// Factory for a composite node that has its own extensions in addition to children.
/// </summary>
public static FileType Composite(string name, string[] extensions, params FileType[] children) =>
new(name, extensions, children);
/// <summary>
/// Collects all extensions for this type, including children.
/// </summary>
public IEnumerable<string> FlattenExtensions()
{
return this.FilterExtensions
.Concat(this.Children.SelectMany(child => child.FlattenExtensions()))
.Distinct(StringComparer.OrdinalIgnoreCase);
}
}

View File

@ -0,0 +1,84 @@
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tools.Rust;
/// <summary>
/// Central definition of supported file types with parent/child relationships and helpers
/// to build extension whitelists (e.g., for file pickers or validation).
/// </summary>
public static class FileTypes
{
private static string TB(string fallbackEn) => I18N.I.T(fallbackEn, typeof(FileType).Namespace, nameof(FileType));
// Source code hierarchy: SourceCode -> (.NET, Java, Python, Web, C/C++, Config, ...)
public static readonly FileType DOTNET = FileType.Leaf(".NET", "cs", "razor", "vb", "fs", "aspx", "cshtml", "csproj");
public static readonly FileType JAVA = FileType.Leaf("Java", "java");
public static readonly FileType PYTHON = FileType.Leaf("Python", "py");
public static readonly FileType JAVASCRIPT = FileType.Leaf("JavaScript/TypeScript", "js", "ts");
public static readonly FileType CFAMILY = FileType.Leaf("C/C++", "c", "cpp", "h", "hpp");
public static readonly FileType RUBY = FileType.Leaf("Ruby", "rb");
public static readonly FileType GO = FileType.Leaf("Go", "go");
public static readonly FileType RUST = FileType.Leaf("Rust", "rs");
public static readonly FileType LUA = FileType.Leaf("Lua", "lua");
public static readonly FileType PHP = FileType.Leaf("PHP", "php");
public static readonly FileType WEB = FileType.Leaf("HTML/CSS", "html", "css");
public static readonly FileType APP = FileType.Leaf("Swift/Kotlin", "swift", "kt");
public static readonly FileType SHELL = FileType.Leaf("Shell", "sh", "bash", "zsh");
public static readonly FileType LOG = FileType.Leaf("Log", "log");
public static readonly FileType JSON = FileType.Leaf("JSON", "json");
public static readonly FileType XML = FileType.Leaf("XML", "xml");
public static readonly FileType YAML = FileType.Leaf("YAML", "yaml", "yml");
public static readonly FileType CONFIG = FileType.Leaf(TB("Config"), "ini", "cfg", "toml", "plist");
public static readonly FileType SOURCE_CODE = FileType.Parent(TB("Source Code"),
DOTNET, JAVA, PYTHON, JAVASCRIPT, CFAMILY, RUBY, GO, RUST, LUA, PHP, WEB, APP, SHELL, LOG, JSON, XML, YAML, CONFIG);
// Document hierarchy
public static readonly FileType PDF = FileType.Leaf("PDF", "pdf");
public static readonly FileType TEXT = FileType.Leaf(TB("Text"), "txt", "md");
public static readonly FileType MS_WORD = FileType.Leaf("Microsoft Word", "docx");
public static readonly FileType WORD = FileType.Composite("Word", ["docx"], MS_WORD);
public static readonly FileType EXCEL = FileType.Leaf("Excel", "xls", "xlsx");
public static readonly FileType POWER_POINT = FileType.Leaf("PowerPoint", "ppt", "pptx");
public static readonly FileType OFFICE_FILES = FileType.Parent(TB("Office Files"),
WORD, EXCEL, POWER_POINT, PDF);
public static readonly FileType DOCUMENT = FileType.Parent(TB("Document"),
TEXT, OFFICE_FILES, SOURCE_CODE);
// Media hierarchy
public static readonly FileType IMAGE = FileType.Leaf(TB("Image"),
"jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg", "webp", "heic");
public static readonly FileType AUDIO = FileType.Leaf(TB("Audio"),
"mp3", "wav", "wave", "aac", "flac", "ogg", "m4a", "wma", "alac", "aiff", "m4b");
public static readonly FileType VIDEO = FileType.Leaf(TB("Video"),
"mp4", "m4v", "avi", "mkv", "mov", "wmv", "flv", "webm");
public static readonly FileType MEDIA = FileType.Parent(TB("Media"), IMAGE, AUDIO, VIDEO);
// Other standalone types
public static readonly FileType EXECUTABLES = FileType.Leaf(TB("Executable"), "exe", "app", "bin", "appimage");
/// <summary>
/// Builds a distinct, lower-cased list of extensions allowed for the provided types.
/// Accepts both composite types (e.g., Document) and leaves (e.g., Pdf).
/// </summary>
public static string[] OnlyAllowTypes(params FileType[] types)
{
if (types.Length == 0)
return [];
return types
.SelectMany(t => t.FlattenExtensions())
.Select(ext => ext.ToLowerInvariant())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
}
public static FileType? AsOneFileType(params FileType[]? types)
{
if (types == null || types.Length == 0)
return null;
return FileType.Composite(TB("Custom"), OnlyAllowTypes(types));
}
}

View File

@ -6,5 +6,5 @@ public class SaveFileOptions
public PreviousFile? PreviousFile { get; init; }
public FileTypeFilter? Filter { get; init; }
public FileType? Filter { get; init; }
}

View File

@ -6,5 +6,5 @@ public sealed class SelectFileOptions
public PreviousFile? PreviousFile { get; init; }
public FileTypeFilter? Filter { get; init; }
public FileType? Filter { get; init; }
}

View File

@ -17,13 +17,13 @@ public sealed partial class RustService
return await result.Content.ReadFromJsonAsync<DirectorySelectionResponse>(this.jsonRustSerializerOptions);
}
public async Task<FileSelectionResponse> SelectFile(string title, FileTypeFilter? filter = null, string? initialFile = null)
public async Task<FileSelectionResponse> SelectFile(string title, FileType[]? 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<FileSelectionResponse>(this.jsonRustSerializerOptions);
}
public async Task<FilesSelectionResponse> SelectFiles(string title, FileTypeFilter? filter = null, string? initialFile = null)
public async Task<FilesSelectionResponse> SelectFiles(string title, FileType[]? 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);
@ -63,13 +63,13 @@ public sealed partial class RustService
/// <param name="initialFile">An optional initial file path to pre-fill in the dialog.</param>
/// <returns>A <see cref="FileSaveResponse"/> object containing information about whether the user canceled the
/// operation and whether the select operation was successful.</returns>
public async Task<FileSaveResponse> SaveFile(string title, FileTypeFilter? filter = null, string? initialFile = null)
public async Task<FileSaveResponse> SaveFile(string title, FileType[]? 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);

View File

@ -44,7 +44,7 @@ public static class FileExtensionValidation
public static async Task<bool> 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.EXECUTABLES.FlattenExtensions().Contains(ext))
{
await MessageBus.INSTANCE.SendError(new(
Icons.Material.Filled.AppBlocking,
@ -53,7 +53,7 @@ public static class FileExtensionValidation
}
var capabilities = provider?.GetModelCapabilities() ?? new();
if (FileTypeFilter.AllImages.FilterExtensions.Contains(ext))
if (FileTypes.IMAGE.FlattenExtensions().Contains(ext))
{
switch (useCae)
{
@ -88,7 +88,7 @@ public static class FileExtensionValidation
}
}
if(FileTypeFilter.AllVideos.FilterExtensions.Contains(ext))
if(FileTypes.VIDEO.FlattenExtensions().Contains(ext))
{
await MessageBus.INSTANCE.SendWarning(new(
Icons.Material.Filled.FeaturedVideo,
@ -96,7 +96,7 @@ public static class FileExtensionValidation
return false;
}
if(FileTypeFilter.AllAudio.FilterExtensions.Contains(ext))
if(FileTypes.AUDIO.FlattenExtensions().Contains(ext))
{
await MessageBus.INSTANCE.SendWarning(new(
Icons.Material.Filled.AudioFile,
@ -123,7 +123,7 @@ public static class FileExtensionValidation
return false;
}
if (!Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
if (FileTypes.IMAGE.FlattenExtensions().Any(x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendError(new(
Icons.Material.Filled.ImageNotSupported,