Skip to content

Commit f6a4942

Browse files
Refactor plugin zip logic
- Download zip to temp folder - Unzip to unique folder
1 parent 50449de commit f6a4942

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,28 @@ public static bool PluginModified(string uuid)
366366
return _modifiedPlugins.Contains(uuid);
367367
}
368368

369-
public static void UpdatePlugin(PluginMetadata existingVersion, UserPlugin newVersion, string downloadedFilePath)
369+
370+
/// <summary>
371+
/// Update a plugin to new version, from a zip file. Will Delete zip after updating.
372+
/// </summary>
373+
public static void UpdatePlugin(PluginMetadata existingVersion, UserPlugin newVersion, string zipFilePath)
370374
{
371-
InstallPlugin(newVersion, downloadedFilePath, checkModified:false);
375+
InstallPlugin(newVersion, zipFilePath, checkModified:false);
372376
UninstallPlugin(existingVersion, removeSettings:false, checkModified:false);
373377
_modifiedPlugins.Add(existingVersion.ID);
374378
}
375379

376-
public static void InstallPlugin(UserPlugin plugin, string downloadedFilePath)
380+
/// <summary>
381+
/// Install a plugin. Will Delete zip after updating.
382+
/// </summary>
383+
public static void InstallPlugin(UserPlugin plugin, string zipFilePath)
377384
{
378-
InstallPlugin(plugin, downloadedFilePath, true);
385+
InstallPlugin(plugin, zipFilePath, true);
379386
}
380387

388+
/// <summary>
389+
/// Uninstall a plugin.
390+
/// </summary>
381391
public static void UninstallPlugin(PluginMetadata plugin, bool removeSettings = true)
382392
{
383393
UninstallPlugin(plugin, removeSettings, true);
@@ -387,29 +397,18 @@ public static void UninstallPlugin(PluginMetadata plugin, bool removeSettings =
387397

388398
#region Internal functions
389399

390-
internal static void InstallPlugin(UserPlugin plugin, string downloadedFilePath, bool checkModified)
400+
internal static void InstallPlugin(UserPlugin plugin, string zipFilePath, bool checkModified)
391401
{
392402
if (checkModified && PluginModified(plugin.ID))
393403
{
394404
// Distinguish exception from installing same or less version
395405
throw new ArgumentException($"Plugin {plugin.Name} {plugin.ID} has been modified.", nameof(plugin));
396406
}
397407

398-
var tempFolderPath = Path.Combine(Path.GetTempPath(), "flowlauncher");
399-
var tempFolderPluginPath = Path.Combine(tempFolderPath, "plugin");
400-
401-
if (Directory.Exists(tempFolderPath))
402-
Directory.Delete(tempFolderPath, true);
403-
404-
Directory.CreateDirectory(tempFolderPath);
405-
406-
var zipFilePath = Path.Combine(tempFolderPath, Path.GetFileName(downloadedFilePath));
407-
408-
File.Copy(downloadedFilePath, zipFilePath);
409-
410-
File.Delete(downloadedFilePath);
411-
408+
// Unzip plugin files to temp folder
409+
var tempFolderPluginPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
412410
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, tempFolderPluginPath);
411+
File.Delete(zipFilePath);
413412

414413
var pluginFolderPath = GetContainingFolderPathAfterUnzip(tempFolderPluginPath);
415414

@@ -454,7 +453,7 @@ internal static void InstallPlugin(UserPlugin plugin, string downloadedFilePath,
454453

455454
FilesFolders.CopyAll(pluginFolderPath, newPluginPath);
456455

457-
Directory.Delete(pluginFolderPath, true);
456+
Directory.Delete(tempFolderPluginPath, true);
458457

459458
if (checkModified)
460459
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
139139
? $"{plugin.Name}-{Guid.NewGuid()}.zip"
140140
: $"{plugin.Name}-{plugin.Version}.zip";
141141

142-
var filePath = Path.Combine(DataLocation.PluginsDirectory, downloadFilename);
142+
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
143143

144144
try
145145
{
@@ -240,7 +240,7 @@ where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
240240
Context.API.GetTranslation("plugin_pluginsmanager_update_title"),
241241
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
242242
{
243-
var downloadToFilePath = Path.Combine(DataLocation.PluginsDirectory,
243+
var downloadToFilePath = Path.Combine(Path.GetTempPath(),
244244
$"{x.Name}-{x.NewVersion}.zip");
245245

246246
_ = Task.Run(async delegate
@@ -414,6 +414,7 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
414414
try
415415
{
416416
PluginManager.InstallPlugin(plugin, downloadedFilePath);
417+
File.Delete(downloadedFilePath);
417418
}
418419
catch (FileNotFoundException e)
419420
{

0 commit comments

Comments
 (0)