Skip to content

Commit d8191f7

Browse files
committed
adjust temporary working directory path
1 parent 8c13658 commit d8191f7

File tree

1 file changed

+53
-74
lines changed

1 file changed

+53
-74
lines changed

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

Lines changed: 53 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Flow.Launcher.Infrastructure.UserSettings;
55
using Flow.Launcher.Plugin.PluginsManager.Models;
66
using ICSharpCode.SharpZipLib.Zip;
7+
using Newtonsoft.Json;
78
using System;
89
using System.Collections.Generic;
910
using System.IO;
@@ -37,7 +38,7 @@ internal void PluginInstall(UserPlugin plugin)
3738

3839
var filePath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}.zip");
3940
PluginDownload(plugin.UrlDownload, filePath);
40-
context.API.InstallPlugin(filePath);
41+
Application.Current.Dispatcher.Invoke(() => Install(plugin, filePath));
4142
}
4243

4344
private void PluginDownload(string downloadUrl, string toFilePath)
@@ -108,51 +109,62 @@ internal List<Result> PluginsSearch(string searchName)
108109
.ToList();
109110
}
110111

111-
private void Install(string path)
112+
private void Install(UserPlugin plugin, string downloadedFilePath)
112113
{
113-
if (File.Exists(path))
114+
if (File.Exists(downloadedFilePath))
114115
{
115-
string tempFolder = Path.Combine(Path.GetTempPath(), "flowlauncher", "plugins");
116-
if (Directory.Exists(tempFolder))
116+
var tempFolderPath = Path.Combine(Path.GetTempPath(), "flowlauncher");
117+
var tempPluginFolderPath = Path.Combine(tempFolderPath, "plugin");
118+
119+
if (Directory.Exists(tempFolderPath))
117120
{
118-
Directory.Delete(tempFolder, true);
121+
Directory.Delete(tempFolderPath, true);
119122
}
120-
UnZip(path, tempFolder, true);
121123

122-
string jsonPath = Path.Combine(tempFolder, Constant.PluginMetadataFileName);
123-
if (!File.Exists(jsonPath))
124-
{
125-
MessageBox.Show("Install failed: plugin config is missing");
126-
return;
127-
}
124+
Directory.CreateDirectory(tempFolderPath);
125+
126+
var zipFilePath = Path.Combine(tempFolderPath, Path.GetFileName(downloadedFilePath));
127+
128+
File.Move(downloadedFilePath, zipFilePath);
129+
130+
UnZip(zipFilePath, tempPluginFolderPath, true);
131+
132+
var unzippedParentFolderPath = tempPluginFolderPath;
133+
134+
var metadataJsonFilePath = string.Empty;
135+
136+
var pluginFolderPath = string.Empty;
128137

129-
PluginMetadata plugin = GetMetadataFromJson(tempFolder);
130-
if (plugin == null || plugin.Name == null)
138+
var unzippedFolderCount = Directory.GetDirectories(unzippedParentFolderPath).Length;
139+
var unzippedFilesCount = Directory.GetFiles(unzippedParentFolderPath).Length;
140+
141+
// addjust path depending on how the plugin is zipped up
142+
// the recommended should be to zip up the folder not the contents
143+
if (unzippedFolderCount == 1 && unzippedFilesCount == 0)
144+
// folder is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/pluginFolderName/
145+
pluginFolderPath = Directory.GetDirectories(unzippedParentFolderPath)[0];
146+
147+
if (unzippedFilesCount > 1)
148+
// content is zipped up, unzipped plugin directory structure: tempPath/unzippedParentPluginFolder/
149+
pluginFolderPath = unzippedParentFolderPath;
150+
151+
if (File.Exists(Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName)))
152+
metadataJsonFilePath = Path.Combine(pluginFolderPath, Constant.PluginMetadataFileName);
153+
154+
if (string.IsNullOrEmpty(metadataJsonFilePath) || string.IsNullOrEmpty(pluginFolderPath))
131155
{
132-
MessageBox.Show("Install failed: plugin config is invalid");
156+
MessageBox.Show("Install failed: unable to find the plugin.json metadata file");
133157
return;
134158
}
135159

136-
string pluginFolderPath = Infrastructure.UserSettings.DataLocation.PluginsDirectory;
137-
138-
string newPluginName = plugin.Name
139-
.Replace("/", "_")
140-
.Replace("\\", "_")
141-
.Replace(":", "_")
142-
.Replace("<", "_")
143-
.Replace(">", "_")
144-
.Replace("?", "_")
145-
.Replace("*", "_")
146-
.Replace("|", "_")
147-
+ "-" + Guid.NewGuid();
148-
149-
string newPluginPath = Path.Combine(pluginFolderPath, newPluginName);
160+
string newPluginPath = Path.Combine(DataLocation.PluginsDirectory, $"{plugin.Name}{plugin.ID}");
150161

151162
string content = $"Do you want to install following plugin?{Environment.NewLine}{Environment.NewLine}" +
152163
$"Name: {plugin.Name}{Environment.NewLine}" +
153164
$"Version: {plugin.Version}{Environment.NewLine}" +
154165
$"Author: {plugin.Author}";
155-
PluginPair existingPlugin = PluginManager.GetPluginForId(plugin.ID);
166+
167+
var existingPlugin = context.API.GetAllPlugins().Where(x => x.Metadata.ID == plugin.ID).FirstOrDefault();
156168

157169
if (existingPlugin != null)
158170
{
@@ -172,7 +184,7 @@ private void Install(string path)
172184
File.Create(Path.Combine(existingPlugin.Metadata.PluginDirectory, "NeedDelete.txt")).Close();
173185
}
174186

175-
Directory.Move(tempFolder, newPluginPath);
187+
Directory.Move(pluginFolderPath, newPluginPath);
176188

177189
//exsiting plugins may be has loaded by application,
178190
//if we try to delelte those kind of plugins, we will get a error that indicate the
@@ -186,73 +198,38 @@ private void Install(string path)
186198
"Restart Flow Launcher to take effect?",
187199
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
188200
{
189-
PluginManager.API.RestartApp();
201+
context.API.RestartApp();
190202
}
191203
}
192204
}
193205
}
194206

195-
private PluginMetadata GetMetadataFromJson(string pluginDirectory)
196-
{
197-
string configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
198-
PluginMetadata metadata;
199-
200-
if (!File.Exists(configPath))
201-
{
202-
return null;
203-
}
204-
205-
try
206-
{
207-
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
208-
metadata.PluginDirectory = pluginDirectory;
209-
}
210-
catch (Exception e)
211-
{
212-
Log.Exception($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: invalid json format", e);
213-
return null;
214-
}
215-
216-
if (!AllowedLanguage.IsAllowed(metadata.Language))
217-
{
218-
Log.Error($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: invalid language {metadata.Language}");
219-
return null;
220-
}
221-
if (!File.Exists(metadata.ExecuteFilePath))
222-
{
223-
Log.Error($"|PluginInstaller.GetMetadataFromJson|plugin config {configPath} failed: file {metadata.ExecuteFilePath} doesn't exist");
224-
return null;
225-
}
226-
227-
return metadata;
228-
}
229-
230207
/// <summary>
231208
/// unzip plugin contents to the given directory.
232209
/// </summary>
233-
/// <param name="zipFile">The path to the zip file.</param>
210+
/// <param name="zipFilePath">The path to the zip file.</param>
234211
/// <param name="strDirectory">The output directory.</param>
235-
/// <param name="overWrite">overwirte</param>
236-
private void UnZip(string zipFile, string strDirectory, bool overWrite)
212+
/// <param name="overwrite">overwrite</param>
213+
private void UnZip(string zipFilePath, string strDirectory, bool overwrite)
237214
{
238215
if (strDirectory == "")
239216
strDirectory = Directory.GetCurrentDirectory();
240217

241-
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFile)))
218+
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
242219
{
243220
ZipEntry theEntry;
244221

245222
while ((theEntry = zipStream.GetNextEntry()) != null)
246223
{
247224
var pathToZip = theEntry.Name;
248-
var directoryName = String.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip);
225+
var directoryName = string.IsNullOrEmpty(pathToZip) ? "" : Path.GetDirectoryName(pathToZip);
249226
var fileName = Path.GetFileName(pathToZip);
250227
var destinationDir = Path.Combine(strDirectory, directoryName);
251228
var destinationFile = Path.Combine(destinationDir, fileName);
252229

253230
Directory.CreateDirectory(destinationDir);
254231

255-
if (String.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overWrite))
232+
if (string.IsNullOrEmpty(fileName) || (File.Exists(destinationFile) && !overwrite))
256233
continue;
257234

258235
using (FileStream streamWriter = File.Create(destinationFile))
@@ -262,5 +239,7 @@ private void UnZip(string zipFile, string strDirectory, bool overWrite)
262239
}
263240
}
264241
}
242+
243+
//delete the zip file when done
265244
}
266245
}

0 commit comments

Comments
 (0)