Skip to content

Commit 5193421

Browse files
committed
move download and unzip to utilities class
1 parent d8191f7 commit 5193421

File tree

2 files changed

+57
-54
lines changed

2 files changed

+57
-54
lines changed

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

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
using Flow.Launcher.Infrastructure.Logger;
44
using Flow.Launcher.Infrastructure.UserSettings;
55
using Flow.Launcher.Plugin.PluginsManager.Models;
6-
using ICSharpCode.SharpZipLib.Zip;
7-
using Newtonsoft.Json;
86
using System;
97
using System.Collections.Generic;
108
using System.IO;
119
using System.Linq;
1210
using System.Net;
13-
using System.Text;
1411
using System.Windows;
1512

1613
namespace Flow.Launcher.Plugin.PluginsManager
@@ -37,29 +34,23 @@ internal void PluginInstall(UserPlugin plugin)
3734
}
3835

3936
var filePath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}.zip");
40-
PluginDownload(plugin.UrlDownload, filePath);
41-
Application.Current.Dispatcher.Invoke(() => Install(plugin, filePath));
42-
}
43-
44-
private void PluginDownload(string downloadUrl, string toFilePath)
45-
{
37+
4638
try
4739
{
48-
using (var wc = new WebClient { Proxy = Http.WebProxy() })
49-
{
50-
wc.DownloadFile(downloadUrl, toFilePath);
51-
}
40+
Utilities.Download(plugin.UrlDownload, filePath);
5241

5342
context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
54-
context.API.GetTranslation("plugin_pluginsmanager_download_success"));
43+
context.API.GetTranslation("plugin_pluginsmanager_download_success"));
5544
}
56-
catch(Exception e)
45+
catch (Exception e)
5746
{
5847
context.API.ShowMsg(context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
5948
context.API.GetTranslation("plugin_pluginsmanager_download_success"));
6049

6150
Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "PluginDownload");
6251
}
52+
53+
Application.Current.Dispatcher.Invoke(() => Install(plugin, filePath));
6354
}
6455

6556
internal void PluginUpdate()
@@ -127,7 +118,7 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
127118

128119
File.Move(downloadedFilePath, zipFilePath);
129120

130-
UnZip(zipFilePath, tempPluginFolderPath, true);
121+
Utilities.UnZip(zipFilePath, tempPluginFolderPath, true);
131122

132123
var unzippedParentFolderPath = tempPluginFolderPath;
133124

@@ -203,43 +194,5 @@ private void Install(UserPlugin plugin, string downloadedFilePath)
203194
}
204195
}
205196
}
206-
207-
/// <summary>
208-
/// unzip plugin contents to the given directory.
209-
/// </summary>
210-
/// <param name="zipFilePath">The path to the zip file.</param>
211-
/// <param name="strDirectory">The output directory.</param>
212-
/// <param name="overwrite">overwrite</param>
213-
private void UnZip(string zipFilePath, string strDirectory, bool overwrite)
214-
{
215-
if (strDirectory == "")
216-
strDirectory = Directory.GetCurrentDirectory();
217-
218-
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
219-
{
220-
ZipEntry theEntry;
221-
222-
while ((theEntry = zipStream.GetNextEntry()) != null)
223-
{
224-
var pathToZip = theEntry.Name;
225-
var directoryName = string.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip);
226-
var fileName = Path.GetFileName(pathToZip);
227-
var destinationDir = Path.Combine(strDirectory, directoryName);
228-
var destinationFile = Path.Combine(destinationDir, fileName);
229-
230-
Directory.CreateDirectory(destinationDir);
231-
232-
if (string.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overwrite))
233-
continue;
234-
235-
using (FileStream streamWriter = File.Create(destinationFile))
236-
{
237-
zipStream.CopyTo(streamWriter);
238-
}
239-
}
240-
}
241-
}
242-
243-
//delete the zip file when done
244197
}
245198
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Flow.Launcher.Infrastructure.Http;
2+
using ICSharpCode.SharpZipLib.Zip;
3+
using System.IO;
4+
using System.Net;
5+
6+
namespace Flow.Launcher.Plugin.PluginsManager
7+
{
8+
internal static class Utilities
9+
{
10+
/// <summary>
11+
/// Unzip contents to the given directory.
12+
/// </summary>
13+
/// <param name="zipFilePath">The path to the zip file.</param>
14+
/// <param name="strDirectory">The output directory.</param>
15+
/// <param name="overwrite">overwrite</param>
16+
internal static void UnZip(string zipFilePath, string strDirectory, bool overwrite)
17+
{
18+
if (strDirectory == "")
19+
strDirectory = Directory.GetCurrentDirectory();
20+
21+
using var zipStream = new ZipInputStream(File.OpenRead(zipFilePath));
22+
23+
ZipEntry theEntry;
24+
25+
while ((theEntry = zipStream.GetNextEntry()) != null)
26+
{
27+
var pathToZip = theEntry.Name;
28+
var directoryName = string.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip);
29+
var fileName = Path.GetFileName(pathToZip);
30+
var destinationDir = Path.Combine(strDirectory, directoryName);
31+
var destinationFile = Path.Combine(destinationDir, fileName);
32+
33+
Directory.CreateDirectory(destinationDir);
34+
35+
if (string.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overwrite))
36+
continue;
37+
38+
using var streamWriter = File.Create(destinationFile);
39+
zipStream.CopyTo(streamWriter);
40+
}
41+
}
42+
43+
internal static void Download(string downloadUrl, string toFilePath)
44+
{
45+
using var wc = new WebClient { Proxy = Http.WebProxy() };
46+
47+
wc.DownloadFile(downloadUrl, toFilePath);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)