Skip to content

Commit 94a2393

Browse files
committed
refactor Core.Plugin.PluginInstaller
1 parent 4ac1925 commit 94a2393

File tree

1 file changed

+28
-47
lines changed

1 file changed

+28
-47
lines changed

Flow.Launcher.Core/Plugin/PluginInstaller.cs

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ internal static void Install(string path)
1515
{
1616
if (File.Exists(path))
1717
{
18-
string tempFoler = Path.Combine(Path.GetTempPath(), "flowlauncher\\plugins");
19-
if (Directory.Exists(tempFoler))
18+
string tempFolder = Path.Combine(Path.GetTempPath(), "flowlauncher", "plugins");
19+
if (Directory.Exists(tempFolder))
2020
{
21-
Directory.Delete(tempFoler, true);
21+
Directory.Delete(tempFolder, true);
2222
}
23-
UnZip(path, tempFoler, true);
23+
UnZip(path, tempFolder, true);
2424

25-
string iniPath = Path.Combine(tempFoler, Constant.PluginMetadataFileName);
26-
if (!File.Exists(iniPath))
25+
string jsonPath = Path.Combine(tempFolder, Constant.PluginMetadataFileName);
26+
if (!File.Exists(jsonPath))
2727
{
2828
MessageBox.Show("Install failed: plugin config is missing");
2929
return;
3030
}
3131

32-
PluginMetadata plugin = GetMetadataFromJson(tempFoler);
32+
PluginMetadata plugin = GetMetadataFromJson(tempFolder);
3333
if (plugin == null || plugin.Name == null)
3434
{
3535
MessageBox.Show("Install failed: plugin config is invalid");
3636
return;
3737
}
3838

39-
string pluginFolerPath = Infrastructure.UserSettings.DataLocation.PluginsDirectory;
39+
string pluginFolderPath = Infrastructure.UserSettings.DataLocation.PluginsDirectory;
4040

4141
string newPluginName = plugin.Name
4242
.Replace("/", "_")
@@ -48,7 +48,9 @@ internal static void Install(string path)
4848
.Replace("*", "_")
4949
.Replace("|", "_")
5050
+ "-" + Guid.NewGuid();
51-
string newPluginPath = Path.Combine(pluginFolerPath, newPluginName);
51+
52+
string newPluginPath = Path.Combine(pluginFolderPath, newPluginName);
53+
5254
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
5355
$"Name: {plugin.Name}{Environment.NewLine}" +
5456
$"Version: {plugin.Version}{Environment.NewLine}" +
@@ -73,8 +75,7 @@ internal static void Install(string path)
7375
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
7476
}
7577

76-
UnZip(path, newPluginPath, true);
77-
Directory.Delete(tempFoler, true);
78+
Directory.Move(tempFolder, newPluginPath);
7879

7980
//exsiting plugins may be has loaded by application,
8081
//if we try to delelte those kind of plugins, we will get a error that indicate the
@@ -130,58 +131,38 @@ private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
130131
}
131132

132133
/// <summary>
133-
/// unzip
134+
/// unzip plugin contents to the given directory.
134135
/// </summary>
135-
/// <param name="zipedFile">The ziped file.</param>
136-
/// <param name="strDirectory">The STR directory.</param>
136+
/// <param name="zipFile">The path to the zip file.</param>
137+
/// <param name="strDirectory">The output directory.</param>
137138
/// <param name="overWrite">overwirte</param>
138-
private static void UnZip(string zipedFile, string strDirectory, bool overWrite)
139+
private static void UnZip(string zipFile, string strDirectory, bool overWrite)
139140
{
140141
if (strDirectory == "")
141142
strDirectory = Directory.GetCurrentDirectory();
142-
if (!strDirectory.EndsWith("\\"))
143-
strDirectory = strDirectory + "\\";
144143

145-
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
144+
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFile)))
146145
{
147146
ZipEntry theEntry;
148147

149-
while ((theEntry = s.GetNextEntry()) != null)
148+
while ((theEntry = zipStream.GetNextEntry()) != null)
150149
{
151-
string directoryName = "";
152-
string pathToZip = "";
153-
pathToZip = theEntry.Name;
150+
var pathToZip = theEntry.Name;
151+
var directoryName = String.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip);
152+
var fileName = Path.GetFileName(pathToZip);
153+
var destinationDir = Path.Combine(strDirectory, directoryName);
154+
var destinationFile = Path.Combine(destinationDir, fileName);
154155

155-
if (pathToZip != "")
156-
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
156+
Directory.CreateDirectory(destinationDir);
157157

158-
string fileName = Path.GetFileName(pathToZip);
158+
if (String.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overWrite))
159+
continue;
159160

160-
Directory.CreateDirectory(strDirectory + directoryName);
161-
162-
if (fileName != "")
161+
using (FileStream streamWriter = File.Create(destinationFile))
163162
{
164-
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
165-
{
166-
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
167-
{
168-
byte[] data = new byte[2048];
169-
while (true)
170-
{
171-
int size = s.Read(data, 0, data.Length);
172-
173-
if (size > 0)
174-
streamWriter.Write(data, 0, size);
175-
else
176-
break;
177-
}
178-
streamWriter.Close();
179-
}
180-
}
163+
zipStream.CopyTo(streamWriter);
181164
}
182165
}
183-
184-
s.Close();
185166
}
186167
}
187168
}

0 commit comments

Comments
 (0)