@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
@inject NavigationManager Navigation
@inject NotificationService NotificationService
@inject IPluginManager PluginManager
@inject IConfiguration Configuration
@inject ILogger Logger
@rendermode InteractiveServer
Upload Plugin
Enable Plugin
@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))
{
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, 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);
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; }
}
}