-
Notifications
You must be signed in to change notification settings - Fork 10
/
PluginHandler.cs
68 lines (59 loc) · 2.28 KB
/
PluginHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
namespace Todo.PluginDownloader
{
class PluginHandler
{
private string _hostUrl = "http://localhost:21021";
private string _pluginObjectPath =
"/api/Plugin/GetPluginObjectsResult";
private string _pluginDownloadPath = "/api/Plugin/Download";
private string _rootSavePath = "wwwroot/";
public PluginHandler()
{
}
public PluginHandler(string[] args)
{
initializePluginHandler(args);
}
public void Execute(string[] args)
{
initializePluginHandler(args);
DownloadPlugins().GetAwaiter().GetResult();
}
public async Task DownloadPlugins()
{
var pluginDownloader = new WebDownloader(_hostUrl);
var pluginObjects = await pluginDownloader.GetPluginObjects(_pluginObjectPath);
foreach (var pluginObject in pluginObjects)
{
var pluginName = pluginObject.Title;
var fileContentResults = await pluginDownloader.GetFileContentResults(_pluginDownloadPath, pluginName);
foreach (var fileContentResult in fileContentResults)
{
var saveFilePath = Path.Combine(_rootSavePath, fileContentResult.FileDownloadName);
File.WriteAllBytes(saveFilePath, fileContentResult.FileContents);
if (File.Exists(saveFilePath) && saveFilePath.EndsWith(".zip"))
{
var newDir = Path.Combine(_rootSavePath, pluginObject.Url.Replace("/", ""));
if (Directory.Exists(newDir))
{
Directory.Delete(newDir, true);
}
ZipFile.ExtractToDirectory(saveFilePath, _rootSavePath);
File.Delete(saveFilePath);
}
}
}
}
private void initializePluginHandler(string[] args)
{
_rootSavePath = Path.GetFullPath(_rootSavePath);
if (!Directory.Exists(_rootSavePath))
{
Directory.CreateDirectory(_rootSavePath);
}
}
}
}