Flow.Launcher/Flow.Launcher.Core/Plugin/PluginInstaller.cs

288 lines
10 KiB
C#
Raw Normal View History

2025-06-29 07:48:08 +00:00
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
2025-06-29 14:35:14 +00:00
namespace Flow.Launcher.Core.Plugin;
2025-06-29 07:48:08 +00:00
/// <summary>
/// Helper class for installing, updating, and uninstalling plugins.
/// </summary>
2025-06-29 14:35:14 +00:00
public static class PluginInstaller
2025-06-29 07:48:08 +00:00
{
2025-06-29 14:35:14 +00:00
private static readonly string ClassName = nameof(PluginInstaller);
2025-06-29 07:48:08 +00:00
private static readonly Settings Settings = Ioc.Default.GetRequiredService<Settings>();
2025-06-29 14:35:14 +00:00
// We should not initialize API in static constructor because it will create another API instance
private static IPublicAPI api = null;
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
2025-06-29 07:48:08 +00:00
public static async Task InstallPluginAndCheckRestartAsync(UserPlugin newPlugin)
{
2025-06-29 14:35:14 +00:00
if (API.ShowMsgBox(
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation("InstallPromptSubtitle"),
2025-06-29 07:48:08 +00:00
newPlugin.Name, newPlugin.Author, Environment.NewLine),
2025-06-29 14:35:14 +00:00
API.GetTranslation("InstallPromptTitle"),
2025-06-29 07:48:08 +00:00
button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;
try
{
// at minimum should provide a name, but handle plugin that is not downloaded from plugins manifest and is a url download
var downloadFilename = string.IsNullOrEmpty(newPlugin.Version)
? $"{newPlugin.Name}-{Guid.NewGuid()}.zip"
: $"{newPlugin.Name}-{newPlugin.Version}.zip";
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
using var cts = new CancellationTokenSource();
if (!newPlugin.IsFromLocalInstallPath)
{
await DownloadFileAsync(
2025-06-29 14:35:14 +00:00
$"{API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}",
2025-06-29 07:48:08 +00:00
newPlugin.UrlDownload, filePath, cts);
}
else
{
filePath = newPlugin.LocalInstallPath;
}
// check if user cancelled download before installing plugin
if (cts.IsCancellationRequested)
{
return;
}
2025-06-29 07:58:52 +00:00
if (!File.Exists(filePath))
2025-06-29 07:48:08 +00:00
{
2025-06-29 07:58:52 +00:00
throw new FileNotFoundException($"Plugin {newPlugin.ID} zip file not found at {filePath}", filePath);
}
2025-06-29 07:48:08 +00:00
2025-06-29 14:35:14 +00:00
API.InstallPlugin(newPlugin, filePath);
2025-06-29 07:48:08 +00:00
2025-06-29 07:58:52 +00:00
if (!newPlugin.IsFromLocalInstallPath)
{
File.Delete(filePath);
2025-06-29 07:48:08 +00:00
}
}
catch (Exception e)
{
2025-06-29 14:35:14 +00:00
API.LogException(ClassName, "Failed to install plugin", e);
API.ShowMsgError(API.GetTranslation("ErrorInstallingPlugin"));
2025-06-29 07:48:08 +00:00
return; // dont restart on failure
}
if (Settings.AutoRestartAfterChanging)
{
2025-06-29 14:35:14 +00:00
API.RestartApp();
2025-06-29 07:48:08 +00:00
}
else
{
2025-06-29 14:35:14 +00:00
API.ShowMsg(
API.GetTranslation("installbtn"),
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation(
2025-06-29 07:48:08 +00:00
"InstallSuccessNoRestart"),
newPlugin.Name));
}
}
public static async Task InstallPluginAndCheckRestartAsync(string filePath)
{
UserPlugin plugin;
try
{
using ZipArchive archive = ZipFile.OpenRead(filePath);
2025-06-29 08:09:06 +00:00
var pluginJsonEntry = archive.Entries.FirstOrDefault(x => x.Name == "plugin.json") ??
2025-06-29 07:48:08 +00:00
throw new FileNotFoundException("The zip file does not contain a plugin.json file.");
using Stream stream = pluginJsonEntry.Open();
plugin = JsonSerializer.Deserialize<UserPlugin>(stream);
plugin.IcoPath = "Images\\zipfolder.png";
plugin.LocalInstallPath = filePath;
}
catch (Exception e)
{
2025-06-29 14:35:14 +00:00
API.LogException(ClassName, "Failed to validate zip file", e);
API.ShowMsgError(API.GetTranslation("ZipFileNotHavePluginJson"));
2025-06-29 07:48:08 +00:00
return;
}
if (Settings.ShowUnknownSourceWarning)
{
if (!InstallSourceKnown(plugin.Website)
2025-06-29 14:35:14 +00:00
&& API.ShowMsgBox(string.Format(
API.GetTranslation("InstallFromUnknownSourceSubtitle"), Environment.NewLine),
API.GetTranslation("InstallFromUnknownSourceTitle"),
2025-06-29 07:48:08 +00:00
MessageBoxButton.YesNo) == MessageBoxResult.No)
return;
}
await InstallPluginAndCheckRestartAsync(plugin);
}
public static async Task UninstallPluginAndCheckRestartAsync(PluginMetadata oldPlugin)
{
2025-06-29 14:35:14 +00:00
if (API.ShowMsgBox(
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation("UninstallPromptSubtitle"),
2025-06-29 07:48:08 +00:00
oldPlugin.Name, oldPlugin.Author, Environment.NewLine),
2025-06-29 14:35:14 +00:00
API.GetTranslation("UninstallPromptTitle"),
2025-06-29 07:48:08 +00:00
button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;
2025-06-29 14:35:14 +00:00
var removePluginSettings = API.ShowMsgBox(
API.GetTranslation("KeepPluginSettingsSubtitle"),
API.GetTranslation("KeepPluginSettingsTitle"),
2025-06-29 07:48:08 +00:00
button: MessageBoxButton.YesNo) == MessageBoxResult.No;
try
{
2025-06-29 14:35:14 +00:00
await API.UninstallPluginAsync(oldPlugin, removePluginSettings);
2025-06-29 07:48:08 +00:00
}
catch (Exception e)
{
2025-06-29 14:35:14 +00:00
API.LogException(ClassName, "Failed to uninstall plugin", e);
API.ShowMsgError(API.GetTranslation("ErrorUninstallingPlugin"));
2025-06-29 07:48:08 +00:00
return; // dont restart on failure
}
if (Settings.AutoRestartAfterChanging)
{
2025-06-29 14:35:14 +00:00
API.RestartApp();
2025-06-29 07:48:08 +00:00
}
else
{
2025-06-29 14:35:14 +00:00
API.ShowMsg(
API.GetTranslation("uninstallbtn"),
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation(
2025-06-29 07:48:08 +00:00
"UninstallSuccessNoRestart"),
oldPlugin.Name));
}
}
public static async Task UpdatePluginAndCheckRestartAsync(UserPlugin newPlugin, PluginMetadata oldPlugin)
{
2025-06-29 14:35:14 +00:00
if (API.ShowMsgBox(
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation("UpdatePromptSubtitle"),
2025-06-29 07:48:08 +00:00
oldPlugin.Name, oldPlugin.Author, Environment.NewLine),
2025-06-29 14:35:14 +00:00
API.GetTranslation("UpdatePromptTitle"),
2025-06-29 07:48:08 +00:00
button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;
try
{
var filePath = Path.Combine(Path.GetTempPath(), $"{newPlugin.Name}-{newPlugin.Version}.zip");
using var cts = new CancellationTokenSource();
if (!newPlugin.IsFromLocalInstallPath)
{
await DownloadFileAsync(
2025-06-29 14:35:14 +00:00
$"{API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}",
2025-06-29 07:48:08 +00:00
newPlugin.UrlDownload, filePath, cts);
}
else
{
filePath = newPlugin.LocalInstallPath;
}
// check if user cancelled download before installing plugin
if (cts.IsCancellationRequested)
{
return;
}
2025-06-29 07:58:52 +00:00
2025-06-29 14:35:14 +00:00
await API.UpdatePluginAsync(oldPlugin, newPlugin, filePath);
2025-06-29 07:48:08 +00:00
}
catch (Exception e)
{
2025-06-29 14:35:14 +00:00
API.LogException(ClassName, "Failed to update plugin", e);
API.ShowMsgError(API.GetTranslation("ErrorUpdatingPlugin"));
2025-06-29 07:48:08 +00:00
return; // dont restart on failure
}
if (Settings.AutoRestartAfterChanging)
{
2025-06-29 14:35:14 +00:00
API.RestartApp();
2025-06-29 07:48:08 +00:00
}
else
{
2025-06-29 14:35:14 +00:00
API.ShowMsg(
API.GetTranslation("updatebtn"),
2025-06-29 07:48:08 +00:00
string.Format(
2025-06-29 14:35:14 +00:00
API.GetTranslation(
2025-06-29 07:48:08 +00:00
"UpdateSuccessNoRestart"),
newPlugin.Name));
}
}
private static async Task DownloadFileAsync(string prgBoxTitle, string downloadUrl, string filePath, CancellationTokenSource cts, bool deleteFile = true, bool showProgress = true)
{
if (deleteFile && File.Exists(filePath))
File.Delete(filePath);
if (showProgress)
{
var exceptionHappened = false;
2025-06-29 14:35:14 +00:00
await API.ShowProgressBoxAsync(prgBoxTitle,
2025-06-29 07:48:08 +00:00
async (reportProgress) =>
{
if (reportProgress == null)
{
2025-06-29 10:20:47 +00:00
// when reportProgress is null, it means there is exception with the progress box
2025-06-29 07:48:08 +00:00
// so we record it with exceptionHappened and return so that progress box will close instantly
exceptionHappened = true;
return;
}
else
{
2025-06-29 14:35:14 +00:00
await API.HttpDownloadAsync(downloadUrl, filePath, reportProgress, cts.Token).ConfigureAwait(false);
2025-06-29 07:48:08 +00:00
}
}, cts.Cancel);
// if exception happened while downloading and user does not cancel downloading,
// we need to redownload the plugin
if (exceptionHappened && (!cts.IsCancellationRequested))
2025-06-29 14:35:14 +00:00
await API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false);
2025-06-29 07:48:08 +00:00
}
else
{
2025-06-29 14:35:14 +00:00
await API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false);
2025-06-29 07:48:08 +00:00
}
}
private static bool InstallSourceKnown(string url)
{
2025-06-29 08:29:45 +00:00
if (string.IsNullOrEmpty(url))
return false;
2025-06-29 07:48:08 +00:00
var pieces = url.Split('/');
if (pieces.Length < 4)
return false;
var author = pieces[3];
2025-06-29 09:06:29 +00:00
var acceptedHost = "github.com";
2025-06-29 07:48:08 +00:00
var acceptedSource = "https://github.com";
var constructedUrlPart = string.Format("{0}/{1}/", acceptedSource, author);
2025-06-29 09:06:29 +00:00
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || uri.Host != acceptedHost)
return false;
2025-06-29 14:35:14 +00:00
return API.GetAllPlugins().Any(x =>
2025-06-29 09:06:29 +00:00
!string.IsNullOrEmpty(x.Metadata.Website) &&
x.Metadata.Website.StartsWith(constructedUrlPart)
);
2025-06-29 07:48:08 +00:00
}
}