-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathOSSStrategy.cs
More file actions
125 lines (107 loc) · 4.57 KB
/
Copy pathOSSStrategy.cs
File metadata and controls
125 lines (107 loc) · 4.57 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeneralUpdate.Core.Compress;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core.Download;
using GeneralUpdate.Core.JsonContext;
using GeneralUpdate.Core;
using GeneralUpdate.Core.Configuration;
namespace GeneralUpdate.Core.Strategy
{
public class OSSStrategy
{
#region Private Members
private readonly string _appPath = AppDomain.CurrentDomain.BaseDirectory;
private const int TimeOut = 60;
private GlobalConfigInfoOSS? _parameter;
#endregion Private Members
#region Public Methods
public void Create(GlobalConfigInfoOSS parameter)
=> _parameter = parameter;
public async Task ExecuteAsync()
{
try
{
//1.Download the JSON version configuration file.
GeneralTracer.Debug("1.Download the JSON version configuration file.");
var jsonPath = Path.Combine(_appPath, _parameter.VersionFileName);
if (!File.Exists(jsonPath))
throw new FileNotFoundException(jsonPath);
//2.Parse the JSON version configuration file content.
GeneralTracer.Debug("2.Parse the JSON version configuration file content.");
var versions = StorageManager.GetJson<List<VersionOSS>>(jsonPath, VersionOSSJsonContext.Default.ListVersionOSS);
if (versions == null)
throw new NullReferenceException(nameof(versions));
versions = versions.OrderBy(v => v.PubTime).ToList();
//3.Download version by version according to the version of the configuration file.
GeneralTracer.Debug("3.Download version by version according to the version of the configuration file.");
await DownloadVersions(versions);
Decompress(versions);
//4.Launch the main application.
GeneralTracer.Debug("4.Launch the main application.");
LaunchApp();
}
catch (Exception ex)
{
GeneralTracer.Error("The ExecuteAsync method in the OSSStrategy class throws an exception.", ex);
}
finally
{
Process.GetCurrentProcess().Kill();
}
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Download all updated versions version by version.
/// </summary>
/// <param name="versions">The collection of version information to be updated as described in the configuration file.</param>
private async Task DownloadVersions(List<VersionOSS> versions)
{
var manager = new DownloadManager(_appPath, Format.ZIP, TimeOut);
foreach (var versionInfo in versions)
{
var version = new VersionInfo
{
Name = versionInfo.PacketName,
Version = versionInfo.Version,
Url = versionInfo.Url,
Format = Format.ZIP,
Hash = versionInfo.Hash
};
manager.Add(new DownloadTask(manager, version));
}
await manager.LaunchTasksAsync();
}
/// <summary>
/// Start the main application when the update is complete.
/// </summary>
/// <exception cref="FileNotFoundException"></exception>
private void LaunchApp()
{
var appPath = Path.Combine(_appPath, _parameter.AppName);
if (!File.Exists(appPath)) throw new FileNotFoundException($"{nameof(appPath)} , The application is not accessible !");
Process.Start(appPath);
GeneralTracer.Debug("5.Upgrade complete.");
GeneralTracer.Dispose();
}
private void Decompress(List<VersionOSS> versions)
{
var encoding = Encoding.GetEncoding(_parameter.Encoding);
foreach (var version in versions)
{
var zipFilePath = Path.Combine(_appPath, $"{version.PacketName}{Format.ZIP}");
CompressProvider.Decompress(Format.ZIP, zipFilePath, _appPath, encoding);
if (!File.Exists(zipFilePath)) continue;
File.SetAttributes(zipFilePath, FileAttributes.Normal);
File.Delete(zipFilePath);
}
}
#endregion Private Methods
}
}