Skip to content

Commit 22c5d10

Browse files
Filtered out unsupported audio files in file handling (#592)
1 parent 9966b7b commit 22c5d10

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

app/MindWork AI Studio/Components/AttachDocuments.razor.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AIStudio.Dialogs;
22
using AIStudio.Tools.Rust;
33
using AIStudio.Tools.Services;
4+
using AIStudio.Tools.Validation;
45

56
using Microsoft.AspNetCore.Components;
67

@@ -91,7 +92,7 @@ protected override async Task OnInitializedAsync()
9192

9293
foreach (var path in paths)
9394
{
94-
if(!await this.IsFileExtensionValid(path))
95+
if(!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(path))
9596
continue;
9697

9798
this.DocumentPaths.Add(path);
@@ -133,38 +134,14 @@ private async Task AddFilesManually()
133134
if (!File.Exists(selectedFile.SelectedFilePath))
134135
return;
135136

136-
if (!await this.IsFileExtensionValid(selectedFile.SelectedFilePath))
137+
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(selectedFile.SelectedFilePath))
137138
return;
138139

139140
this.DocumentPaths.Add(selectedFile.SelectedFilePath);
140141
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
141142
await this.OnChange(this.DocumentPaths);
142143
}
143144

144-
private async Task<bool> IsFileExtensionValid(string selectedFile)
145-
{
146-
var ext = Path.GetExtension(selectedFile).TrimStart('.');
147-
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
148-
{
149-
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.AppBlocking, this.T("Executables are not allowed")));
150-
return false;
151-
}
152-
153-
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
154-
{
155-
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.ImageNotSupported, this.T("Images are not supported yet")));
156-
return false;
157-
}
158-
159-
if (Array.Exists(FileTypeFilter.AllVideos.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
160-
{
161-
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.FeaturedVideo, this.T("Videos are not supported yet")));
162-
return false;
163-
}
164-
165-
return true;
166-
}
167-
168145
private async Task ClearAllFiles()
169146
{
170147
this.DocumentPaths.Clear();

app/MindWork AI Studio/Components/ReadFileContent.razor.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using AIStudio.Tools.Rust;
21
using AIStudio.Tools.Services;
2+
using AIStudio.Tools.Validation;
33

44
using Microsoft.AspNetCore.Components;
55

@@ -55,28 +55,12 @@ private async Task SelectFile()
5555
return;
5656
}
5757

58-
var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.');
59-
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
58+
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(selectedFile.SelectedFilePath))
6059
{
61-
this.Logger.LogWarning("User attempted to load executable file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext);
62-
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.AppBlocking, T("Executables are not allowed")));
60+
this.Logger.LogWarning("User attempted to load unsupported file: {FilePath}", selectedFile.SelectedFilePath);
6361
return;
6462
}
6563

66-
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
67-
{
68-
this.Logger.LogWarning("User attempted to load image file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext);
69-
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.ImageNotSupported, T("Images are not supported yet")));
70-
return;
71-
}
72-
73-
if (Array.Exists(FileTypeFilter.AllVideos.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
74-
{
75-
this.Logger.LogWarning("User attempted to load video file: {FilePath} with extension: {Extension}", selectedFile.SelectedFilePath, ext);
76-
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.FeaturedVideo, this.T("Videos are not supported yet")));
77-
return;
78-
}
79-
8064
try
8165
{
8266
var fileContent = await UserFile.LoadFileData(selectedFile.SelectedFilePath, this.RustService, this.DialogService);

app/MindWork AI Studio/Tools/Rust/FileTypeFilter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public readonly record struct FileTypeFilter(string FilterName, string[] FilterE
2121

2222
public static FileTypeFilter AllImages => new(TB("All Image Files"), ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "svg", "webp", "heic"]);
2323

24-
public static FileTypeFilter AllVideos => new(TB("All Video Files"), ["mp4", "avi", "mkv", "mov", "wmv", "flv", "webm"]);
24+
public static FileTypeFilter AllVideos => new(TB("All Video Files"), ["mp4", "m4v", "avi", "mkv", "mov", "wmv", "flv", "webm"]);
25+
26+
public static FileTypeFilter AllAudio => new(TB("All Audio Files"), ["mp3", "wav", "wave", "aac", "flac", "ogg", "m4a", "wma", "alac", "aiff", "m4b"]);
2527

2628
public static FileTypeFilter Executables => new(TB("Executable Files"), ["exe", "app", "bin", "appimage"]);
2729
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using AIStudio.Tools.PluginSystem;
2+
using AIStudio.Tools.Rust;
3+
4+
namespace AIStudio.Tools.Validation;
5+
6+
/// <summary>
7+
/// Provides centralized validation for file extensions.
8+
/// </summary>
9+
public static class FileExtensionValidation
10+
{
11+
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(FileExtensionValidation).Namespace, nameof(FileExtensionValidation));
12+
13+
/// <summary>
14+
/// Validates the file extension and sends appropriate MessageBus notifications when invalid.
15+
/// </summary>
16+
/// <param name="filePath">The file path to validate.</param>
17+
/// <returns>True if valid, false if invalid (error/warning already sent via MessageBus).</returns>
18+
public static async Task<bool> IsExtensionValidWithNotifyAsync(string filePath)
19+
{
20+
var ext = Path.GetExtension(filePath).TrimStart('.');
21+
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
22+
{
23+
await MessageBus.INSTANCE.SendError(new(
24+
Icons.Material.Filled.AppBlocking,
25+
TB("Executables are not allowed")));
26+
return false;
27+
}
28+
29+
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
30+
{
31+
await MessageBus.INSTANCE.SendWarning(new(
32+
Icons.Material.Filled.ImageNotSupported,
33+
TB("Images are not supported yet")));
34+
return false;
35+
}
36+
37+
if (Array.Exists(FileTypeFilter.AllVideos.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
38+
{
39+
await MessageBus.INSTANCE.SendWarning(new(
40+
Icons.Material.Filled.FeaturedVideo,
41+
TB("Videos are not supported yet")));
42+
return false;
43+
}
44+
45+
if (Array.Exists(FileTypeFilter.AllAudio.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
46+
{
47+
await MessageBus.INSTANCE.SendWarning(new(
48+
Icons.Material.Filled.AudioFile,
49+
TB("Audio files are not supported yet")));
50+
return false;
51+
}
52+
53+
return true;
54+
}
55+
}

app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Improved error handling, logging, and code quality.
1010
- Improved error handling for Microsoft Word export.
1111
- Improved file reading, e.g. for the translation, summarization, and legal assistants, by performing the Pandoc validation in the first step. This prevents unnecessary selection of files that cannot be processed.
12+
- Improved the file selection for file attachments in chat and assistant file loading by filtering out audio files. Audio attachments are not yet supported.
1213
- Fixed a bug in the local data sources info dialog (preview feature) for data directories that could cause the app to crash. The error was caused by a background thread producing data while the frontend attempted to display it.
1314
- Fixed a visual bug where a function's preview status was misaligned. You might have seen it in document analysis or the ERI server assistant.
1415
- Fixed a rare bug in the Microsoft Word export for huge documents.

0 commit comments

Comments
 (0)