AI-Studio/app/MindWork AI Studio/Dialogs/PandocDialog.razor.cs

167 lines
5.6 KiB
C#
Raw Normal View History

2025-05-30 20:39:16 +00:00
using System.Reflection;
using AIStudio.Components;
using AIStudio.Tools.Metadata;
using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
2025-05-30 20:39:16 +00:00
using SharedTools;
namespace AIStudio.Dialogs;
2025-05-30 20:39:16 +00:00
public partial class PandocDialog : MSGComponentBase
{
2025-05-30 20:39:16 +00:00
private static readonly Assembly ASSEMBLY = Assembly.GetExecutingAssembly();
private static readonly MetaDataArchitectureAttribute META_DATA_ARCH = ASSEMBLY.GetCustomAttribute<MetaDataArchitectureAttribute>()!;
private static readonly RID CPU_ARCHITECTURE = META_DATA_ARCH.Architecture.ToRID();
[Parameter]
public bool ShowInstallationPage { get; set; }
[Parameter]
public bool ShowInitialResultInSnackbar { get; set; } = true;
[Inject]
private HttpClient HttpClient { get; set; } = null!;
[Inject]
private RustService RustService { get; init; } = null!;
[Inject]
private IDialogService DialogService { get; init; } = null!;
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = null!;
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger("PandocDialog");
2025-05-30 20:39:16 +00:00
private static readonly string LICENCE_URI = "https://raw.githubusercontent.com/jgm/pandoc/refs/heads/main/COPYING.md";
private static string LATEST_PANDOC_VERSION = string.Empty;
2025-05-30 20:39:16 +00:00
private PandocInstallation pandocInstallation;
private string? licenseText;
2025-05-30 20:39:16 +00:00
private bool isLoadingLicence;
private bool isInstallationInProgress;
private int selectedInstallerIndex = SelectInstallerIndex();
private int selectedArchiveIndex = SelectArchiveIndex();
private string downloadUrlArchive = string.Empty;
private string downloadUrlInstaller = string.Empty;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
2025-05-30 20:39:16 +00:00
LATEST_PANDOC_VERSION = await Pandoc.FetchLatestVersionAsync();
await this.CheckPandocAvailabilityAsync(this.ShowInitialResultInSnackbar);
}
#endregion
private void Cancel() => this.MudDialog.Cancel();
2025-05-30 20:39:16 +00:00
private async Task CheckPandocAvailabilityAsync(bool useSnackbar)
{
2025-05-30 20:39:16 +00:00
this.pandocInstallation = await Pandoc.CheckAvailabilityAsync(this.RustService, useSnackbar);
await this.InvokeAsync(this.StateHasChanged);
}
private async Task InstallPandocAsync()
{
2025-05-30 20:39:16 +00:00
this.isInstallationInProgress = true;
this.StateHasChanged();
await Pandoc.InstallAsync(this.RustService);
2025-05-30 20:39:16 +00:00
this.isInstallationInProgress = false;
this.MudDialog.Close(DialogResult.Ok(true));
2025-05-30 20:39:16 +00:00
await this.DialogService.ShowAsync<PandocDialog>("Pandoc Installation", DialogOptions.FULLSCREEN);
}
2025-05-30 20:39:16 +00:00
private void ProceedToInstallation() => this.ShowInstallationPage = true;
private async Task RejectLicense()
{
2025-05-30 20:39:16 +00:00
var message = T("Pandoc is open-source and free, but if you reject its license, you can't install it and some MindWork AI Studio features will be limited (like the integration of Office files) or unavailable (like the generation of Office files). You can change your decision anytime. Are you sure you want to reject the license?");
var dialogParameters = new DialogParameters
{
{ "Message", message },
};
2025-05-30 20:39:16 +00:00
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>(T("Reject Pandoc's Licence"), dialogParameters, DialogOptions.FULLSCREEN);
var dialogResult = await dialogReference.Result;
if (dialogResult is null || dialogResult.Canceled)
dialogReference.Close();
else
this.Cancel();
}
2025-05-30 20:39:16 +00:00
private async Task WhenExpandingManualInstallation(bool isExpanded)
{
2025-05-30 20:39:16 +00:00
if(string.IsNullOrWhiteSpace(this.downloadUrlArchive))
this.downloadUrlArchive = await Pandoc.GenerateArchiveUriAsync();
if(string.IsNullOrWhiteSpace(this.downloadUrlInstaller))
this.downloadUrlInstaller = await Pandoc.GenerateInstallerUriAsync();
}
2025-05-30 20:39:16 +00:00
private async Task WhenExpandingLicence(bool isExpanded)
{
if (isExpanded)
{
2025-05-30 20:39:16 +00:00
this.isLoadingLicence = true;
try
{
this.licenseText = await this.LoadLicenseTextAsync();
}
catch (Exception ex)
{
2025-05-30 20:39:16 +00:00
this.licenseText = T("Error loading license text, please consider following the links to read the GPL.");
LOG.LogError("Error loading GPL license text: {ErrorMessage}", ex.Message);
}
finally
{
2025-05-30 20:39:16 +00:00
this.isLoadingLicence = false;
}
}
else
{
this.licenseText = string.Empty;
}
}
private async Task<string> LoadLicenseTextAsync()
{
var response = await this.HttpClient.GetAsync(LICENCE_URI);
response.EnsureSuccessStatusCode();
2025-05-30 20:39:16 +00:00
return await response.Content.ReadAsStringAsync();
}
2025-05-30 20:39:16 +00:00
// ReSharper disable RedundantSwitchExpressionArms
private static int SelectInstallerIndex() => CPU_ARCHITECTURE switch
{
RID.OSX_ARM64 => 1,
RID.OSX_X64 => 2,
RID.WIN_ARM64 => 0,
RID.WIN_X64 => 0,
_ => 0,
};
private static int SelectArchiveIndex() => CPU_ARCHITECTURE switch
{
RID.OSX_ARM64 => 1,
RID.OSX_X64 => 1,
RID.WIN_ARM64 => 0,
RID.WIN_X64 => 0,
RID.LINUX_ARM64 => 2,
RID.LINUX_X64 => 2,
_ => 0,
};
// ReSharper restore RedundantSwitchExpressionArms
}