Added download and progress endpoints
This commit is contained in:
@@ -12,12 +12,21 @@ public class ApiManager
|
||||
{
|
||||
private bool IsRunning { get; set; }
|
||||
private List<IEndpoint> ApiEndpoints { get; }
|
||||
|
||||
|
||||
public ApiManager()
|
||||
{
|
||||
ApiEndpoints = new List<IEndpoint>();
|
||||
}
|
||||
|
||||
internal void AddBaseEndpoints()
|
||||
{
|
||||
AddEndpoint(new HomeEndpoint());
|
||||
AddEndpoint(new PluginListEndpoint());
|
||||
AddEndpoint(new PluginListInstalledEndpoint());
|
||||
AddEndpoint(new PluginInstallEndpoint());
|
||||
AddEndpoint(new PluginInstallGetProgressEndpoint());
|
||||
}
|
||||
|
||||
public Result AddEndpoint(IEndpoint endpoint)
|
||||
{
|
||||
if (ApiEndpoints.Contains(endpoint) || ApiEndpoints.Exists(x => x.Path == endpoint.Path))
|
||||
@@ -38,12 +47,6 @@ public class ApiManager
|
||||
{
|
||||
return this.ApiEndpoints.Exists(endpoint => endpoint.Path == endpointPath);
|
||||
}
|
||||
|
||||
internal void AddBaseEndpoints()
|
||||
{
|
||||
AddEndpoint(new HomeEndpoint());
|
||||
AddEndpoint(new PluginListEndpoint());
|
||||
}
|
||||
|
||||
public async Task InitializeApi()
|
||||
{
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.API;
|
||||
using DiscordBotCore.Others;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace DiscordBotCore.API.Endpoints;
|
||||
|
||||
|
||||
@@ -4,12 +4,10 @@ using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.API;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Plugin;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
|
||||
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
||||
|
||||
public class InstallPluginEndpoint : IEndpoint
|
||||
public class PluginInstallEndpoint : IEndpoint
|
||||
{
|
||||
public string Path => "/api/plugin/install";
|
||||
public EndpointType HttpMethod => EndpointType.Put;
|
||||
@@ -25,7 +23,7 @@ public class InstallPluginEndpoint : IEndpoint
|
||||
return ApiResponse.Fail("Plugin not found.");
|
||||
}
|
||||
|
||||
await Application.CurrentApplication.PluginManager.InstallPlugin(pluginInfo, null);
|
||||
await Application.CurrentApplication.PluginManager.InstallPluginWithNoProgress(pluginInfo);
|
||||
return ApiResponse.Ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.API;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
||||
|
||||
public class PluginInstallGetProgressEndpoint : IEndpoint
|
||||
{
|
||||
public string Path => "/api/plugin/install/progress";
|
||||
public EndpointType HttpMethod => EndpointType.Get;
|
||||
public async Task<ApiResponse> HandleRequest(string? jsonRequest)
|
||||
{
|
||||
if (!Application.CurrentApplication.PluginManager.InstallingPluginInformation.IsInstalling)
|
||||
{
|
||||
return ApiResponse.Fail("No plugin is currently being installed.");
|
||||
}
|
||||
|
||||
var progress = Application.CurrentApplication.PluginManager.InstallingPluginInformation.InstallationProgress;
|
||||
string stringProgress = progress.ToString(CultureInfo.InvariantCulture);
|
||||
var response = new Dictionary<string, string>
|
||||
{
|
||||
{"progress", stringProgress},
|
||||
{"pluginName", Application.CurrentApplication.PluginManager.InstallingPluginInformation.PluginName}
|
||||
};
|
||||
return ApiResponse.From(await JsonManager.ConvertToJsonString(response), true);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.API;
|
||||
using DiscordBotCore.Others;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
||||
|
||||
|
||||
@@ -11,7 +11,4 @@
|
||||
<ItemGroup>
|
||||
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="API\Services\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -11,10 +11,11 @@ using DiscordBotCore.Updater.Plugins;
|
||||
|
||||
namespace DiscordBotCore.Online;
|
||||
|
||||
public class PluginManager
|
||||
public sealed class PluginManager
|
||||
{
|
||||
private PluginRepository _PluginRepository;
|
||||
|
||||
private readonly PluginRepository _PluginRepository;
|
||||
internal InstallingPluginInformation InstallingPluginInformation { get; private set; }
|
||||
|
||||
public PluginManager(PluginRepository pluginRepository)
|
||||
{
|
||||
_PluginRepository = pluginRepository;
|
||||
@@ -189,13 +190,56 @@ public class PluginManager
|
||||
return relative;
|
||||
}
|
||||
|
||||
public async Task InstallPlugin(PluginOnlineInfo pluginData, IProgress<float>? installProgress)
|
||||
public async Task InstallPluginWithNoProgress(PluginOnlineInfo pluginData)
|
||||
{
|
||||
InstallingPluginInformation = new InstallingPluginInformation() {PluginName = pluginData.Name};
|
||||
|
||||
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
|
||||
|
||||
float stepProgress = 1f / totalSteps;
|
||||
float currentProgress = 0f;
|
||||
|
||||
InstallingPluginInformation.IsInstalling = true;
|
||||
|
||||
IProgress<float> downloadProgress = new Progress<float>(f => InstallingPluginInformation.InstallationProgress = currentProgress + stepProgress * f);
|
||||
|
||||
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink,
|
||||
$"{Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder")}/{pluginData.Name}.dll",
|
||||
downloadProgress
|
||||
);
|
||||
|
||||
if (pluginData.HasFileDependencies)
|
||||
foreach (var dependency in pluginData.Dependencies)
|
||||
{
|
||||
string dependencyLocation = GenerateDependencyRelativePath(pluginData.Name, dependency.DownloadLocation);
|
||||
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependencyLocation, downloadProgress);
|
||||
|
||||
currentProgress += stepProgress;
|
||||
}
|
||||
|
||||
if (pluginData.HasScriptDependencies)
|
||||
foreach (var scriptDependency in pluginData.ScriptDependencies)
|
||||
{
|
||||
|
||||
string console = OperatingSystem.IsWindows() ? "start cmd.exe" : "bash";
|
||||
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
|
||||
|
||||
await ServerCom.RunConsoleCommand(console, arguments);
|
||||
}
|
||||
|
||||
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
|
||||
|
||||
await AppendPluginToDatabase(pluginInfo);
|
||||
|
||||
InstallingPluginInformation.IsInstalling = false;
|
||||
}
|
||||
|
||||
public async Task InstallPluginWithProgressBar(PluginOnlineInfo pluginData, IProgress<float>? installProgress)
|
||||
{
|
||||
installProgress?.Report(0f);
|
||||
|
||||
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
|
||||
totalSteps += pluginData.HasScriptDependencies ? pluginData.ScriptDependencies.Count : 0;
|
||||
|
||||
|
||||
float stepProgress = 1f / totalSteps;
|
||||
|
||||
float currentProgress = 0f;
|
||||
@@ -224,7 +268,6 @@ public class PluginManager
|
||||
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
|
||||
|
||||
await ServerCom.RunConsoleCommand(console, arguments);
|
||||
currentProgress += stepProgress;
|
||||
}
|
||||
|
||||
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
|
||||
|
||||
10
DiscordBotCore/Plugin/InstallingPluginInformation.cs
Normal file
10
DiscordBotCore/Plugin/InstallingPluginInformation.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace DiscordBotCore.Plugin;
|
||||
|
||||
public class InstallingPluginInformation
|
||||
{
|
||||
public bool IsInstalling { get; set; }
|
||||
public required string PluginName { get; set; }
|
||||
public float InstallationProgress { get; set; } = 0.0f;
|
||||
}
|
||||
@@ -36,7 +36,7 @@
|
||||
if (pluginInfo is null)
|
||||
return;
|
||||
|
||||
await Application.CurrentApplication.PluginManager.InstallPlugin(pluginInfo, downloadProgress);
|
||||
await Application.CurrentApplication.PluginManager.InstallPluginWithProgressBar(pluginInfo, downloadProgress);
|
||||
|
||||
_ProgressBarVisible = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user