Skip to content

Commit ebf9ef1

Browse files
committed
add check downloaded plugin zip has same or lesser version
1 parent a796ac4 commit ebf9ef1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Plugins/Flow.Launcher.Plugin.PluginsManager/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<system:String x:Key="plugin_pluginsmanager_install_in_progress">Plugin installation in progress. Please wait...</system:String>
1515
<system:String x:Key="plugin_pluginsmanager_install_success_restart">Plugin successfully installed. Restarting Flow, please wait...</system:String>
1616
<system:String x:Key="plugin_pluginsmanager_install_errormetadatafile">Unable to find the plugin.json metadata file from the extracted zip file.</system:String>
17+
<system:String x:Key="plugin_pluginsmanager_install_error_duplicate">Error: A plugin which has the same or greater version with {0} already exists.</system:String>
1718
<system:String x:Key="plugin_pluginsmanager_install_error_title">Error installing plugin</system:String>
1819
<system:String x:Key="plugin_pluginsmanager_install_error_subtitle">Error occured while trying to install {0}</system:String>
1920
<system:String x:Key="plugin_pluginsmanager_update_noresult_title">No update available</system:String>

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Linq;
1212
using System.Net.Http;
13+
using System.Text.Json;
1314
using System.Threading;
1415
using System.Threading.Tasks;
1516
using System.Windows;
@@ -438,6 +439,17 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
438439
string.Format("Unable to find plugin.json from the extracted zip file, or this path {0} does not exist", pluginFolderPath));
439440
}
440441

442+
if (SameOrLesserPluginVersionExists(metadataJsonFilePath))
443+
{
444+
MessageBox.Show(string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_duplicate"), plugin.Name),
445+
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"));
446+
447+
throw new InvalidOperationException(
448+
string.Format("A plugin with the same ID and version already exists, " +
449+
"or the version is greater than this downloaded plugin {0}",
450+
plugin.Name));
451+
}
452+
441453
var directory = string.IsNullOrEmpty(plugin.Version) ? $"{plugin.Name}-{Guid.NewGuid()}" : $"{plugin.Name}-{plugin.Version}";
442454
var newPluginPath = Path.Combine(DataLocation.PluginsDirectory, directory);
443455

@@ -532,5 +544,14 @@ private List<Result> AutoCompleteReturnAllResults(string search, string hotkey,
532544

533545
return new List<Result>();
534546
}
547+
548+
private bool SameOrLesserPluginVersionExists(string metadataPath)
549+
{
550+
var newMetadata = JsonSerializer.Deserialize<PluginMetadata>(File.ReadAllText(metadataPath));
551+
return Context.API.GetAllPlugins()
552+
.Any(x => x.Metadata.ID == newMetadata.ID
553+
&& (x.Metadata.Version == newMetadata.Version)
554+
|| newMetadata.Version.CompareTo(x.Metadata.Version) < 0);
555+
}
535556
}
536557
}

0 commit comments

Comments
 (0)