From ebf9ef1d8026d7ce1af84300ef42b0ac3698340e Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 21 Nov 2021 13:31:43 +1100 Subject: [PATCH] add check downloaded plugin zip has same or lesser version --- .../Languages/en.xaml | 1 + .../PluginsManager.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml index fc4ff95ab..89ed9acf3 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml @@ -14,6 +14,7 @@ Plugin installation in progress. Please wait... Plugin successfully installed. Restarting Flow, please wait... Unable to find the plugin.json metadata file from the extracted zip file. + Error: A plugin which has the same or greater version with {0} already exists. Error installing plugin Error occured while trying to install {0} No update available diff --git a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs index f13e8d8ac..adc4beb6c 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -438,6 +439,17 @@ namespace Flow.Launcher.Plugin.PluginsManager string.Format("Unable to find plugin.json from the extracted zip file, or this path {0} does not exist", pluginFolderPath)); } + if (SameOrLesserPluginVersionExists(metadataJsonFilePath)) + { + MessageBox.Show(string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_duplicate"), plugin.Name), + Context.API.GetTranslation("plugin_pluginsmanager_install_error_title")); + + throw new InvalidOperationException( + string.Format("A plugin with the same ID and version already exists, " + + "or the version is greater than this downloaded plugin {0}", + plugin.Name)); + } + var directory = string.IsNullOrEmpty(plugin.Version) ? $"{plugin.Name}-{Guid.NewGuid()}" : $"{plugin.Name}-{plugin.Version}"; var newPluginPath = Path.Combine(DataLocation.PluginsDirectory, directory); @@ -532,5 +544,14 @@ namespace Flow.Launcher.Plugin.PluginsManager return new List(); } + + private bool SameOrLesserPluginVersionExists(string metadataPath) + { + var newMetadata = JsonSerializer.Deserialize(File.ReadAllText(metadataPath)); + return Context.API.GetAllPlugins() + .Any(x => x.Metadata.ID == newMetadata.ID + && (x.Metadata.Version == newMetadata.Version) + || newMetadata.Version.CompareTo(x.Metadata.Version) < 0); + } } }