2020-10-21 14:02:20 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
|
|
|
using BTCPayServer.Abstractions.Models;
|
2020-10-21 14:02:20 +02:00
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
using BTCPayServer.Plugins;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
public partial class ServerController
|
|
|
|
{
|
|
|
|
[HttpGet("server/plugins")]
|
|
|
|
public async Task<IActionResult> ListPlugins(
|
|
|
|
[FromServices] PluginService pluginService,
|
2020-11-05 10:21:09 +01:00
|
|
|
[FromServices] BTCPayServerOptions btcPayServerOptions)
|
2020-10-21 14:02:20 +02:00
|
|
|
{
|
|
|
|
IEnumerable<PluginService.AvailablePlugin> availablePlugins;
|
|
|
|
try
|
|
|
|
{
|
2020-11-05 10:21:09 +01:00
|
|
|
availablePlugins = await pluginService.GetRemotePlugins();
|
2020-10-21 14:02:20 +02:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Severity = StatusMessageModel.StatusSeverity.Error,
|
2020-10-22 10:58:22 +02:00
|
|
|
Message = "Remote plugins lookup failed. Try again later."
|
2020-10-21 14:02:20 +02:00
|
|
|
});
|
|
|
|
availablePlugins = Array.Empty<PluginService.AvailablePlugin>();
|
|
|
|
}
|
|
|
|
var res = new ListPluginsViewModel()
|
|
|
|
{
|
|
|
|
Installed = pluginService.LoadedPlugins,
|
|
|
|
Available = availablePlugins,
|
|
|
|
Commands = pluginService.GetPendingCommands(),
|
|
|
|
CanShowRestart = btcPayServerOptions.DockerDeployment
|
|
|
|
};
|
|
|
|
return View(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ListPluginsViewModel
|
|
|
|
{
|
|
|
|
public IEnumerable<IBTCPayServerPlugin> Installed { get; set; }
|
|
|
|
public IEnumerable<PluginService.AvailablePlugin> Available { get; set; }
|
|
|
|
public (string command, string plugin)[] Commands { get; set; }
|
|
|
|
public bool CanShowRestart { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("server/plugins/uninstall")]
|
|
|
|
public IActionResult UnInstallPlugin(
|
|
|
|
[FromServices] PluginService pluginService, string plugin)
|
|
|
|
{
|
|
|
|
pluginService.UninstallPlugin(plugin);
|
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
2020-10-22 10:58:22 +02:00
|
|
|
Message = "Plugin scheduled to be uninstalled.",
|
2020-10-21 14:02:20 +02:00
|
|
|
Severity = StatusMessageModel.StatusSeverity.Success
|
|
|
|
});
|
|
|
|
|
|
|
|
return RedirectToAction("ListPlugins");
|
|
|
|
}
|
|
|
|
[HttpPost("server/plugins/cancel")]
|
|
|
|
public IActionResult CancelPluginCommands(
|
|
|
|
[FromServices] PluginService pluginService, string plugin)
|
|
|
|
{
|
|
|
|
pluginService.CancelCommands(plugin);
|
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
2020-10-22 10:58:22 +02:00
|
|
|
Message = "Plugin action cancelled.",
|
2020-10-21 14:02:20 +02:00
|
|
|
Severity = StatusMessageModel.StatusSeverity.Success
|
|
|
|
});
|
|
|
|
|
|
|
|
return RedirectToAction("ListPlugins");
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("server/plugins/install")]
|
|
|
|
public async Task<IActionResult> InstallPlugin(
|
2020-11-05 15:43:14 +01:00
|
|
|
[FromServices] PluginService pluginService, string plugin , bool update = false)
|
2020-10-21 14:02:20 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-05 10:21:09 +01:00
|
|
|
await pluginService.DownloadRemotePlugin(plugin);
|
2020-11-05 15:43:14 +01:00
|
|
|
if (update)
|
|
|
|
{
|
|
|
|
pluginService.UpdatePlugin(plugin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
pluginService.InstallPlugin(plugin);
|
|
|
|
}
|
2020-10-21 14:02:20 +02:00
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Message = "Plugin scheduled to be installed.",
|
|
|
|
Severity = StatusMessageModel.StatusSeverity.Success
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
TempData.SetStatusMessageModel(new StatusMessageModel()
|
|
|
|
{
|
|
|
|
Message = "The plugin could not be downloaded. Try again later.", Severity = StatusMessageModel.StatusSeverity.Error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToAction("ListPlugins");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("server/plugins/upload")]
|
|
|
|
public async Task<IActionResult> UploadPlugin([FromServices] PluginService pluginService,
|
|
|
|
List<IFormFile> files)
|
|
|
|
{
|
2021-03-19 10:55:07 +01:00
|
|
|
foreach (var formFile in files.Where(file => file.Length > 0).Where(file => file.FileName.IsValidFileName()))
|
2020-10-21 14:02:20 +02:00
|
|
|
{
|
|
|
|
await pluginService.UploadPlugin(formFile);
|
|
|
|
pluginService.InstallPlugin(formFile.FileName.TrimEnd(PluginManager.BTCPayPluginSuffix,
|
|
|
|
StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToAction("ListPlugins",
|
|
|
|
new {StatusMessage = "Files uploaded, restart server to load plugins"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|