From 2bd368dcce6c54b1b0a132f78f64bc29453318a2 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Wed, 30 Apr 2025 21:32:36 +0300 Subject: [PATCH] Added Add Local plugin option --- .../PluginManager.cs | 6 + WebUI/Components/Pages/Plugins/AddLocal.razor | 103 ++++++++++++++++++ WebUI/Components/Pages/Plugins/Local.razor | 27 +++-- WebUI/WebUI.csproj | 1 + 4 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 WebUI/Components/Pages/Plugins/AddLocal.razor diff --git a/DiscordBotCore.PluginManagement/PluginManager.cs b/DiscordBotCore.PluginManagement/PluginManager.cs index b104cfa..118a276 100644 --- a/DiscordBotCore.PluginManagement/PluginManager.cs +++ b/DiscordBotCore.PluginManagement/PluginManager.cs @@ -91,6 +91,12 @@ public sealed class PluginManager : IPluginManager { pluginData.ListOfExecutableDependencies[dependency.Key] = dependency.Value; } + + if (installedPlugins.Any(plugin => plugin.PluginName == pluginData.PluginName)) + { + _Logger.Log($"Plugin {pluginData.PluginName} already exists in the database. Updating...", this, LogType.Info); + installedPlugins.RemoveAll(p => p.PluginName == pluginData.PluginName); + } installedPlugins.Add(pluginData); await JsonManager.SaveToJsonFile(pluginDatabaseFile, installedPlugins); diff --git a/WebUI/Components/Pages/Plugins/AddLocal.razor b/WebUI/Components/Pages/Plugins/AddLocal.razor new file mode 100644 index 0000000..418ada1 --- /dev/null +++ b/WebUI/Components/Pages/Plugins/AddLocal.razor @@ -0,0 +1,103 @@ +@page "/plugins/add-local" +@using System.ComponentModel.DataAnnotations + +@using DiscordBotCore.Configuration +@using DiscordBotCore.Logging +@using DiscordBotCore.PluginManagement +@using DiscordBotCore.PluginManagement.Models + +@inject NavigationManager Navigation + +@inject IPluginManager PluginManager +@inject IConfiguration Configuration +@inject ILogger Logger + +@rendermode InteractiveServer + +

Upload Plugin

+ + + + + +
+ + +
+ +
+ + + @if (!IsValidVersion) + { +
Version must be in format X.X.X
+ } +
+ +
+ Enable Plugin +
+ +
+ + + @if (selectedFileName != null) + { +
Selected: @selectedFileName
+ } +
+ + +
+ +@code { + private PluginUploadModel pluginModel = new(); + private IBrowserFile? selectedFile; + private string? selectedFileName; + + private bool IsValidVersion => System.Text.RegularExpressions.Regex.IsMatch(pluginModel.Version ?? "", @"^\d+\.\d+\.\d+$"); + + private async Task HandleFileSelected(InputFileChangeEventArgs e) + { + selectedFile = e.File; + selectedFileName = selectedFile.Name; + } + + private async Task HandleValidSubmit() + { + if (!IsValidVersion || selectedFile is null || string.IsNullOrEmpty(selectedFileName)) + { + return; + } + + string? pluginsFolder = Configuration.Get("PluginFolder"); + + if (string.IsNullOrEmpty(pluginsFolder)) + { + Logger.Log("Plugins folder is not set in the configuration.", this, LogType.Error); + return; + } + + string filePath = Path.Combine(pluginsFolder, selectedFileName); + + await using var stream = selectedFile.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024); + await using var fileStream = File.Create(filePath); + await stream.CopyToAsync(fileStream); + + LocalPlugin plugin = new LocalPlugin(pluginModel.Name, pluginModel.Version, filePath, new(), true, pluginModel.IsEnabled); + await PluginManager.AppendPluginToDatabase(plugin); + + Navigation.NavigateTo("/"); + } + + public class PluginUploadModel + { + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public string Version { get; set; } = string.Empty; + + public bool IsEnabled { get; set; } + } +} diff --git a/WebUI/Components/Pages/Plugins/Local.razor b/WebUI/Components/Pages/Plugins/Local.razor index a54749d..b80ceb0 100644 --- a/WebUI/Components/Pages/Plugins/Local.razor +++ b/WebUI/Components/Pages/Plugins/Local.razor @@ -6,6 +6,19 @@ @using DiscordBotCore.PluginManagement.Loading @using DiscordBotCore.PluginManagement.Models +@inject IPluginManager PluginManager +@inject ILogger Logger +@inject IPluginLoader PluginLoader + +@inject NavigationManager Navigation + +
+ +
+ +

Installed Plugins

@@ -56,14 +69,6 @@ @code { - [Inject] - public IPluginManager PluginManager { get; set; } - - [Inject] - public ILogger Logger { get; set; } - - [Inject] - public IPluginLoader PluginLoader { get; set; } private readonly List _installedPlugins = new List(); @@ -150,4 +155,10 @@ public string Version { get; set; } public bool IsOfflineAdded { get; set; } } + + private void NavigateToAddPlugin() + { + Navigation.NavigateTo("/plugins/add-local"); + } + } \ No newline at end of file diff --git a/WebUI/WebUI.csproj b/WebUI/WebUI.csproj index c263ed2..e2caac1 100644 --- a/WebUI/WebUI.csproj +++ b/WebUI/WebUI.csproj @@ -7,6 +7,7 @@ +