btcpayserver/BTCPayServer/Controllers/UIServerController.Plugins.cs

154 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Abstractions.Models;
using BTCPayServer.Configuration;
using BTCPayServer.Plugins;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static BTCPayServer.Plugins.PluginService;
namespace BTCPayServer.Controllers
{
2022-01-07 04:32:00 +01:00
public partial class UIServerController
{
[HttpGet("server/plugins")]
public async Task<IActionResult> ListPlugins(
[FromServices] PluginService pluginService,
2024-10-09 13:47:11 +02:00
[FromServices] BTCPayServerOptions btcPayServerOptions,
string search = null)
{
IEnumerable<PluginService.AvailablePlugin> availablePlugins;
try
{
2024-10-09 13:47:11 +02:00
availablePlugins = await pluginService.GetRemotePlugins(search);
}
catch (Exception)
{
TempData.SetStatusMessageModel(new StatusMessageModel
{
Severity = StatusMessageModel.StatusSeverity.Error,
Message = StringLocalizer["Remote plugins lookup failed. Try again later."].Value
});
availablePlugins = Array.Empty<PluginService.AvailablePlugin>();
}
var availablePluginsByIdentifier = new Dictionary<string, AvailablePlugin>();
foreach (var p in availablePlugins)
availablePluginsByIdentifier.TryAdd(p.Identifier, p);
var res = new ListPluginsViewModel()
{
Installed = pluginService.LoadedPlugins,
Available = availablePlugins,
Commands = pluginService.GetPendingCommands(),
Disabled = pluginService.GetDisabledPlugins(),
CanShowRestart = true,
DownloadedPluginsByIdentifier = availablePluginsByIdentifier
};
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; }
public Dictionary<string, Version> Disabled { get; set; }
public Dictionary<string, AvailablePlugin> DownloadedPluginsByIdentifier { get; set; } = new Dictionary<string, AvailablePlugin>();
}
[HttpPost("server/plugins/uninstall-all")]
public IActionResult UnInstallAllDisabledPlugin(
[FromServices] PluginService pluginService, string plugin)
{
var disabled = pluginService.GetDisabledPlugins();
foreach (var d in disabled)
pluginService.UninstallPlugin(d.Key);
return RedirectToAction(nameof(ListPlugins));
}
[HttpPost("server/plugins/uninstall")]
public IActionResult UnInstallPlugin(
[FromServices] PluginService pluginService, string plugin)
{
pluginService.UninstallPlugin(plugin);
TempData.SetStatusMessageModel(new StatusMessageModel
{
Message = StringLocalizer["Plugin scheduled to be uninstalled."].Value,
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
{
Message = StringLocalizer["Plugin action cancelled."].Value,
Severity = StatusMessageModel.StatusSeverity.Success
});
return RedirectToAction("ListPlugins");
}
[HttpPost("server/plugins/install")]
public async Task<IActionResult> InstallPlugin(
[FromServices] PluginService pluginService, string plugin, bool update = false, string version = null)
{
try
{
await pluginService.DownloadRemotePlugin(plugin, version);
if (update)
{
pluginService.UpdatePlugin(plugin);
}
else
{
pluginService.InstallPlugin(plugin);
}
TempData.SetStatusMessageModel(new StatusMessageModel
{
Message = StringLocalizer["Plugin scheduled to be installed."].Value,
Severity = StatusMessageModel.StatusSeverity.Success
});
}
catch (Exception)
{
TempData.SetStatusMessageModel(new StatusMessageModel
{
Message = StringLocalizer["The plugin could not be downloaded. Try again later."].Value,
2021-12-31 08:59:02 +01:00
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()))
{
await pluginService.UploadPlugin(formFile);
pluginService.InstallPlugin(formFile.FileName.TrimEnd(PluginManager.BTCPayPluginSuffix,
StringComparison.InvariantCultureIgnoreCase));
}
TempData.SetStatusMessageModel(new StatusMessageModel
2022-03-23 16:03:39 +01:00
{
Message = StringLocalizer["Files uploaded, restart server to load plugins"].Value,
2022-03-23 16:03:39 +01:00
Severity = StatusMessageModel.StatusSeverity.Success
});
return RedirectToAction("ListPlugins");
}
}
}