2025-06-30 16:56:48 +00:00
|
|
|
using AIStudio.Tools.Services;
|
2025-12-15 12:26:55 +00:00
|
|
|
using AIStudio.Tools.Validation;
|
2025-06-30 16:56:48 +00:00
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
|
|
|
|
|
|
public partial class ReadFileContent : MSGComponentBase
|
|
|
|
|
{
|
2025-11-24 11:37:18 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public string Text { get; set; } = string.Empty;
|
|
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public string FileContent { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<string> FileContentChanged { get; set; }
|
2026-02-01 13:13:30 +00:00
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
2025-06-30 16:56:48 +00:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private RustService RustService { get; init; } = null!;
|
|
|
|
|
|
2025-11-14 12:34:30 +00:00
|
|
|
[Inject]
|
|
|
|
|
private IDialogService DialogService { get; init; } = null!;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private ILogger<ReadFileContent> Logger { get; init; } = null!;
|
2025-12-10 12:48:13 +00:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private PandocAvailabilityService PandocAvailabilityService { get; init; } = null!;
|
2025-11-14 12:34:30 +00:00
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
private async Task SelectFile()
|
|
|
|
|
{
|
2026-02-01 13:13:30 +00:00
|
|
|
if (this.Disabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-12-15 11:45:54 +00:00
|
|
|
// Ensure that Pandoc is installed and ready:
|
|
|
|
|
var pandocState = await this.PandocAvailabilityService.EnsureAvailabilityAsync(
|
|
|
|
|
showSuccessMessage: false,
|
|
|
|
|
showDialog: true);
|
|
|
|
|
|
|
|
|
|
// Check if Pandoc is available after the check / installation:
|
|
|
|
|
if (!pandocState.IsAvailable)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogWarning("The user cancelled the Pandoc installation or Pandoc is not available. Aborting file selection.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
var selectedFile = await this.RustService.SelectFile(T("Select file to read its content"));
|
|
|
|
|
if (selectedFile.UserCancelled)
|
2025-12-08 20:15:45 +00:00
|
|
|
{
|
|
|
|
|
this.Logger.LogInformation("User cancelled the file selection");
|
2025-06-30 16:56:48 +00:00
|
|
|
return;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
if(!File.Exists(selectedFile.SelectedFilePath))
|
2025-12-08 20:15:45 +00:00
|
|
|
{
|
|
|
|
|
this.Logger.LogWarning("Selected file does not exist: '{FilePath}'", selectedFile.SelectedFilePath);
|
2025-06-30 16:56:48 +00:00
|
|
|
return;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 17:30:32 +00:00
|
|
|
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.DIRECTLY_LOADING_CONTENT, selectedFile.SelectedFilePath))
|
2025-06-30 16:56:48 +00:00
|
|
|
{
|
2025-12-15 12:26:55 +00:00
|
|
|
this.Logger.LogWarning("User attempted to load unsupported file: {FilePath}", selectedFile.SelectedFilePath);
|
2025-06-30 16:56:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-12-08 20:15:45 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fileContent = await UserFile.LoadFileData(selectedFile.SelectedFilePath, this.RustService, this.DialogService);
|
|
|
|
|
await this.FileContentChanged.InvokeAsync(fileContent);
|
|
|
|
|
this.Logger.LogInformation("Successfully loaded file content: {FilePath}", selectedFile.SelectedFilePath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogError(ex, "Failed to load file content: {FilePath}", selectedFile.SelectedFilePath);
|
|
|
|
|
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Error, T("Failed to load file content")));
|
|
|
|
|
}
|
2025-06-30 16:56:48 +00:00
|
|
|
}
|
2026-02-01 13:13:30 +00:00
|
|
|
}
|