reject forbidden file typed

This commit is contained in:
krut_ni 2025-06-24 10:17:44 +02:00
parent 04543540f8
commit 53f8257a01
2 changed files with 20 additions and 5 deletions

View File

@ -18,15 +18,28 @@ public partial class ReadFileContent : MSGComponentBase
private async Task SelectFile()
{
var txtFile = await this.RustService.SelectFile("Select Text file");
if (txtFile.UserCancelled)
var selectedFile = await this.RustService.SelectFile("Select Text file");
if (selectedFile.UserCancelled)
return;
if(!File.Exists(txtFile.SelectedFilePath))
if(!File.Exists(selectedFile.SelectedFilePath))
return;
var txtContent = await this.RustService.ReadArbitraryFileData(txtFile.SelectedFilePath, int.MaxValue);
var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.');
await this.FileContentChanged.InvokeAsync(txtContent);
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendError(new(@Icons.Material.Filled.AppBlocking, "Executables are not allowed"));
return;
}
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendWarning(new(@Icons.Material.Filled.ImageNotSupported, "Images are not supported yet"));
return;
}
var fileContent = await this.RustService.ReadArbitraryFileData(selectedFile.SelectedFilePath, int.MaxValue);
await this.FileContentChanged.InvokeAsync(fileContent);
}
}

View File

@ -20,4 +20,6 @@ public readonly record struct FileTypeFilter(string FilterName, string[] FilterE
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"]);
public static FileTypeFilter Executables => new(TB("Executable Files"), ["exe", "app", "bin"]);
}