Added download and progress endpoints
This commit is contained in:
@@ -18,6 +18,15 @@ public class ApiManager
|
|||||||
ApiEndpoints = new List<IEndpoint>();
|
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)
|
public Result AddEndpoint(IEndpoint endpoint)
|
||||||
{
|
{
|
||||||
if (ApiEndpoints.Contains(endpoint) || ApiEndpoints.Exists(x => x.Path == endpoint.Path))
|
if (ApiEndpoints.Contains(endpoint) || ApiEndpoints.Exists(x => x.Path == endpoint.Path))
|
||||||
@@ -39,12 +48,6 @@ public class ApiManager
|
|||||||
return this.ApiEndpoints.Exists(endpoint => endpoint.Path == endpointPath);
|
return this.ApiEndpoints.Exists(endpoint => endpoint.Path == endpointPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddBaseEndpoints()
|
|
||||||
{
|
|
||||||
AddEndpoint(new HomeEndpoint());
|
|
||||||
AddEndpoint(new PluginListEndpoint());
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task InitializeApi()
|
public async Task InitializeApi()
|
||||||
{
|
{
|
||||||
if (IsRunning)
|
if (IsRunning)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DiscordBotCore.Interfaces.API;
|
using DiscordBotCore.Interfaces.API;
|
||||||
using DiscordBotCore.Others;
|
using DiscordBotCore.Others;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
namespace DiscordBotCore.API.Endpoints;
|
namespace DiscordBotCore.API.Endpoints;
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -4,12 +4,10 @@ using System.Threading.Tasks;
|
|||||||
using DiscordBotCore.Interfaces.API;
|
using DiscordBotCore.Interfaces.API;
|
||||||
using DiscordBotCore.Others;
|
using DiscordBotCore.Others;
|
||||||
using DiscordBotCore.Plugin;
|
using DiscordBotCore.Plugin;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
|
||||||
|
|
||||||
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
||||||
|
|
||||||
public class InstallPluginEndpoint : IEndpoint
|
public class PluginInstallEndpoint : IEndpoint
|
||||||
{
|
{
|
||||||
public string Path => "/api/plugin/install";
|
public string Path => "/api/plugin/install";
|
||||||
public EndpointType HttpMethod => EndpointType.Put;
|
public EndpointType HttpMethod => EndpointType.Put;
|
||||||
@@ -25,7 +23,7 @@ public class InstallPluginEndpoint : IEndpoint
|
|||||||
return ApiResponse.Fail("Plugin not found.");
|
return ApiResponse.Fail("Plugin not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
await Application.CurrentApplication.PluginManager.InstallPlugin(pluginInfo, null);
|
await Application.CurrentApplication.PluginManager.InstallPluginWithNoProgress(pluginInfo);
|
||||||
return ApiResponse.Ok();
|
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 System.Threading.Tasks;
|
||||||
using DiscordBotCore.Interfaces.API;
|
using DiscordBotCore.Interfaces.API;
|
||||||
using DiscordBotCore.Others;
|
using DiscordBotCore.Others;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
namespace DiscordBotCore.API.Endpoints.PluginManagement;
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,4 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml" />
|
<UpToDateCheckInput Remove="UI\Controls\MessageBox.axaml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="API\Services\" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -11,9 +11,10 @@ using DiscordBotCore.Updater.Plugins;
|
|||||||
|
|
||||||
namespace DiscordBotCore.Online;
|
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)
|
public PluginManager(PluginRepository pluginRepository)
|
||||||
{
|
{
|
||||||
@@ -189,12 +190,55 @@ public class PluginManager
|
|||||||
return relative;
|
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);
|
installProgress?.Report(0f);
|
||||||
|
|
||||||
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
|
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
|
||||||
totalSteps += pluginData.HasScriptDependencies ? pluginData.ScriptDependencies.Count : 0;
|
|
||||||
|
|
||||||
float stepProgress = 1f / totalSteps;
|
float stepProgress = 1f / totalSteps;
|
||||||
|
|
||||||
@@ -224,7 +268,6 @@ public class PluginManager
|
|||||||
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
|
string arguments = OperatingSystem.IsWindows() ? $"/c {scriptDependency.ScriptContent}" : scriptDependency.ScriptContent;
|
||||||
|
|
||||||
await ServerCom.RunConsoleCommand(console, arguments);
|
await ServerCom.RunConsoleCommand(console, arguments);
|
||||||
currentProgress += stepProgress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
|
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
|
||||||
|
|||||||
@@ -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)
|
if (pluginInfo is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await Application.CurrentApplication.PluginManager.InstallPlugin(pluginInfo, downloadProgress);
|
await Application.CurrentApplication.PluginManager.InstallPluginWithProgressBar(pluginInfo, downloadProgress);
|
||||||
|
|
||||||
_ProgressBarVisible = false;
|
_ProgressBarVisible = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user