2025-06-23 12:15:08 +00:00
|
|
|
using AIStudio.Tools.Rust;
|
|
|
|
using AIStudio.Tools.Services;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
|
|
|
|
public partial class ReadFileContent : MSGComponentBase
|
|
|
|
{
|
|
|
|
[Parameter]
|
|
|
|
public string FileContent { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
public EventCallback<string> FileContentChanged { get; set; }
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
private RustService RustService { get; init; } = null!;
|
|
|
|
|
|
|
|
private async Task SelectFile()
|
|
|
|
{
|
2025-06-24 08:17:44 +00:00
|
|
|
var selectedFile = await this.RustService.SelectFile("Select Text file");
|
|
|
|
if (selectedFile.UserCancelled)
|
2025-06-23 12:15:08 +00:00
|
|
|
return;
|
|
|
|
|
2025-06-24 08:17:44 +00:00
|
|
|
if(!File.Exists(selectedFile.SelectedFilePath))
|
2025-06-23 12:15:08 +00:00
|
|
|
return;
|
|
|
|
|
2025-06-24 08:17:44 +00:00
|
|
|
var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.');
|
2025-06-23 12:15:08 +00:00
|
|
|
|
2025-06-24 08:17:44 +00:00
|
|
|
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);
|
2025-06-23 12:15:08 +00:00
|
|
|
}
|
|
|
|
}
|