implement plugins section
This commit is contained in:
49
TorchRemote.Plugin/Controllers/PluginDownloadsController.cs
Normal file
49
TorchRemote.Plugin/Controllers/PluginDownloadsController.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using EmbedIO;
|
||||
using EmbedIO.Routing;
|
||||
using EmbedIO.WebApi;
|
||||
using Torch.API.WebAPI;
|
||||
using TorchRemote.Models.Responses;
|
||||
using TorchRemote.Plugin.Utils;
|
||||
|
||||
namespace TorchRemote.Plugin.Controllers;
|
||||
|
||||
public class PluginDownloadsController : WebApiController
|
||||
{
|
||||
private const string RootPath = "/plugins/downloads";
|
||||
|
||||
[Route(HttpVerbs.Get, RootPath)]
|
||||
public async Task<IEnumerable<PluginInfo>> GetAsync()
|
||||
{
|
||||
var response = await PluginQuery.Instance.QueryAll();
|
||||
return response.Plugins.Select(b => new PluginItemInfo(Guid.Parse(b.ID), b.Name, b.LatestVersion, b.Author));
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Get, $"{RootPath}/{{id}}")]
|
||||
public async Task<FullPluginItemInfo> GetFullAsync(Guid id)
|
||||
{
|
||||
var response = await PluginQuery.Instance.QueryOne(id);
|
||||
|
||||
if (response is null)
|
||||
throw HttpException.NotFound("Plugin not found", id);
|
||||
|
||||
return new(Guid.Parse(response.ID), response.Name, response.Description, response.LatestVersion,
|
||||
response.Author);
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Post, $"{RootPath}/{{id}}/install")]
|
||||
public async Task InstallAsync(Guid id)
|
||||
{
|
||||
if (Statics.PluginManager.Plugins.ContainsKey(id))
|
||||
throw HttpException.BadRequest("Plugin with given id already exists", id);
|
||||
|
||||
var response = await PluginQuery.Instance.QueryOne(id);
|
||||
|
||||
if (response is null)
|
||||
throw HttpException.NotFound("Plugin not found", id);
|
||||
|
||||
if (!await PluginQuery.Instance.DownloadPlugin(response))
|
||||
throw HttpException.InternalServerError();
|
||||
|
||||
Statics.Torch.Config.Plugins.Add(id);
|
||||
}
|
||||
}
|
66
TorchRemote.Plugin/Controllers/PluginsController.cs
Normal file
66
TorchRemote.Plugin/Controllers/PluginsController.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.IO;
|
||||
using EmbedIO;
|
||||
using EmbedIO.Routing;
|
||||
using EmbedIO.WebApi;
|
||||
using HttpMultipartParser;
|
||||
using Swan;
|
||||
using Torch.API.Managers;
|
||||
using Torch.Managers;
|
||||
using TorchRemote.Models.Responses;
|
||||
using TorchRemote.Plugin.Utils;
|
||||
|
||||
namespace TorchRemote.Plugin.Controllers;
|
||||
|
||||
public class PluginsController : WebApiController
|
||||
{
|
||||
private const string RootPath = "/plugins";
|
||||
|
||||
[Route(HttpVerbs.Get, RootPath)]
|
||||
public IEnumerable<InstalledPluginInfo> Get()
|
||||
{
|
||||
return Statics.PluginManager.Select(plugin => new InstalledPluginInfo(plugin.Id, plugin.Name, plugin.Version,
|
||||
Statics.SettingManager.PluginSettings!
|
||||
.GetValueOrDefault(plugin.Id)));
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Delete, $"{RootPath}/{{id}}")]
|
||||
public void Uninstall(Guid id)
|
||||
{
|
||||
foreach (var zip in Directory.EnumerateFiles(Statics.PluginManager.PluginDir, "*.zip"))
|
||||
{
|
||||
var manifest = PluginManifestUtils.ReadFromZip(zip);
|
||||
if (manifest.Guid != id)
|
||||
continue;
|
||||
|
||||
File.Delete(zip);
|
||||
return;
|
||||
}
|
||||
|
||||
throw HttpException.NotFound("Plugin zip with given id not found", id);
|
||||
}
|
||||
|
||||
[Route(HttpVerbs.Put, RootPath)]
|
||||
public async Task<IEnumerable<PluginInfo>> InstallAsync()
|
||||
{
|
||||
var payload = await MultipartFormDataParser.ParseAsync(Request.InputStream);
|
||||
|
||||
var pluginsToInstall = payload.Files.ToDictionary(f => PluginManifestUtils.ReadFromZip(f.Data));
|
||||
|
||||
if (pluginsToInstall.Keys.FirstOrDefault(m => Statics.PluginManager.Plugins.ContainsKey(m.Guid)) is { } m)
|
||||
throw HttpException.BadRequest("Plugin with given id already exists", m.Guid);
|
||||
|
||||
return pluginsToInstall.Select(b =>
|
||||
{
|
||||
var (manifest, file) = b;
|
||||
|
||||
var path = Path.Combine(Statics.PluginManager.PluginDir, $"{manifest.Name}.zip");
|
||||
|
||||
using var zipStream = File.Create(path);
|
||||
|
||||
file.Data.Position = 0;
|
||||
file.Data.CopyTo(zipStream);
|
||||
|
||||
return new PluginInfo(manifest.Guid, manifest.Name, manifest.Version);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user