From 519342178f0bc1730e2df34b8605f8e6e3bcab13 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 10 Dec 2020 13:29:47 +1100 Subject: [PATCH] move download and unzip to utilities class --- .../PluginsManager.cs | 61 +++---------------- .../Utilities.cs | 50 +++++++++++++++ 2 files changed, 57 insertions(+), 54 deletions(-) create mode 100644 Plugins/Flow.Launcher.Plugin.PluginManager/Utilities.cs diff --git a/Plugins/Flow.Launcher.Plugin.PluginManager/PluginsManager.cs b/Plugins/Flow.Launcher.Plugin.PluginManager/PluginsManager.cs index 4290767c3..e7e61460e 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginManager/PluginsManager.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginManager/PluginsManager.cs @@ -3,14 +3,11 @@ using Flow.Launcher.Infrastructure.Http; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin.PluginsManager.Models; -using ICSharpCode.SharpZipLib.Zip; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Text; using System.Windows; namespace Flow.Launcher.Plugin.PluginsManager @@ -37,29 +34,23 @@ namespace Flow.Launcher.Plugin.PluginsManager } var filePath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}.zip"); - PluginDownload(plugin.UrlDownload, filePath); - Application.Current.Dispatcher.Invoke(() => Install(plugin, filePath)); - } - - private void PluginDownload(string downloadUrl, string toFilePath) - { + try { - using (var wc = new WebClient { Proxy = Http.WebProxy() }) - { - wc.DownloadFile(downloadUrl, toFilePath); - } + Utilities.Download(plugin.UrlDownload, filePath); context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), - context.API.GetTranslation("plugin_pluginsmanager_download_success")); + context.API.GetTranslation("plugin_pluginsmanager_download_success")); } - catch(Exception e) + catch (Exception e) { context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), context.API.GetTranslation("plugin_pluginsmanager_download_success")); Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "PluginDownload"); } + + Application.Current.Dispatcher.Invoke(() => Install(plugin, filePath)); } internal void PluginUpdate() @@ -127,7 +118,7 @@ namespace Flow.Launcher.Plugin.PluginsManager File.Move(downloadedFilePath, zipFilePath); - UnZip(zipFilePath, tempPluginFolderPath, true); + Utilities.UnZip(zipFilePath, tempPluginFolderPath, true); var unzippedParentFolderPath = tempPluginFolderPath; @@ -203,43 +194,5 @@ namespace Flow.Launcher.Plugin.PluginsManager } } } - - /// - /// unzip plugin contents to the given directory. - /// - /// The path to the zip file. - /// The output directory. - /// overwrite - private void UnZip(string zipFilePath, string strDirectory, bool overwrite) - { - if (strDirectory == "") - strDirectory = Directory.GetCurrentDirectory(); - - using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath))) - { - ZipEntry theEntry; - - while ((theEntry = zipStream.GetNextEntry()) != null) - { - var pathToZip = theEntry.Name; - var directoryName = string.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip); - var fileName = Path.GetFileName(pathToZip); - var destinationDir = Path.Combine(strDirectory, directoryName); - var destinationFile = Path.Combine(destinationDir, fileName); - - Directory.CreateDirectory(destinationDir); - - if (string.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overwrite)) - continue; - - using (FileStream streamWriter = File.Create(destinationFile)) - { - zipStream.CopyTo(streamWriter); - } - } - } - } - - //delete the zip file when done } } diff --git a/Plugins/Flow.Launcher.Plugin.PluginManager/Utilities.cs b/Plugins/Flow.Launcher.Plugin.PluginManager/Utilities.cs new file mode 100644 index 000000000..1ff42fdbd --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.PluginManager/Utilities.cs @@ -0,0 +1,50 @@ +using Flow.Launcher.Infrastructure.Http; +using ICSharpCode.SharpZipLib.Zip; +using System.IO; +using System.Net; + +namespace Flow.Launcher.Plugin.PluginsManager +{ + internal static class Utilities + { + /// + /// Unzip contents to the given directory. + /// + /// The path to the zip file. + /// The output directory. + /// overwrite + internal static void UnZip(string zipFilePath, string strDirectory, bool overwrite) + { + if (strDirectory == "") + strDirectory = Directory.GetCurrentDirectory(); + + using var zipStream = new ZipInputStream(File.OpenRead(zipFilePath)); + + ZipEntry theEntry; + + while ((theEntry = zipStream.GetNextEntry()) != null) + { + var pathToZip = theEntry.Name; + var directoryName = string.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip); + var fileName = Path.GetFileName(pathToZip); + var destinationDir = Path.Combine(strDirectory, directoryName); + var destinationFile = Path.Combine(destinationDir, fileName); + + Directory.CreateDirectory(destinationDir); + + if (string.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overwrite)) + continue; + + using var streamWriter = File.Create(destinationFile); + zipStream.CopyTo(streamWriter); + } + } + + internal static void Download(string downloadUrl, string toFilePath) + { + using var wc = new WebClient { Proxy = Http.WebProxy() }; + + wc.DownloadFile(downloadUrl, toFilePath); + } + } +}