@page "/plugins/add-local" @using System.ComponentModel.DataAnnotations @using DiscordBotCore.Configuration @using DiscordBotCore.Logging @using DiscordBotCore.PluginManagement @using DiscordBotCore.PluginManagement.Models @using WebUI.Models @using WebUI.Services @using WebUI.Components.Shared @inject NavigationManager Navigation @inject NotificationService NotificationService @inject IPluginManager PluginManager @inject IConfiguration Configuration @inject ILogger Logger @rendermode InteractiveServer
@if (_message is not null) {

@_message

}
@code { private PluginUploadModel pluginModel = new(); private IBrowserFile? selectedFile; private List allowedFileTypes { get; set; } = new List { ".dll"}; private string? _message; private bool IsValidVersion => System.Text.RegularExpressions.Regex.IsMatch(pluginModel.Version ?? "", @"^\d+\.\d+\.\d+$"); private async Task HandleUploadedFile(IBrowserFile file) { var buffer = new byte[file.Size]; await file.OpenReadStream().ReadAsync(buffer); _message = $"Uploaded file: {file.Name} ({file.Size} bytes)"; } private async Task HandleValidSubmit() { if (!IsValidVersion || selectedFile is null || string.IsNullOrEmpty(selectedFile.Name)) { NotificationService.Notify("Invalid field values. Please check the form.", NotificationType.Error); return; } string? pluginsFolder = Configuration.Get("PluginFolder"); if (string.IsNullOrEmpty(pluginsFolder)) { Logger.Log("Plugins folder is not set in the configuration.", this, LogType.Error); NotificationService.Notify("Plugins folder is not set in the configuration.", NotificationType.Error); return; } string filePath = Path.Combine(pluginsFolder, selectedFile.Name); 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); NotificationService.Notify($"Plugin {pluginModel.Name} uploaded successfully!", NotificationType.Success); 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; } } }