Skip to content

Commit fd47783

Browse files
committed
🧩 ✔️ Refactor, Test: Updated download method to using HttpClient, tested.
1 parent 463c218 commit fd47783

2 files changed

Lines changed: 36 additions & 65 deletions

File tree

Common.Algorithm.Interop.Test/HashTest.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ namespace Common.Algorithm.Interop.Test;
66
public class 哈希测试
77
{
88
[TestMethod]
9-
public async void 环境安装测试()
9+
public async Task 环境安装测试()
1010
{
11-
Console.WriteLine(Environment.CheckEnvironment());
11+
Console.WriteLine(Environment.Check());
1212

13-
Directory.Delete(Path.GetFullPath($"{Environment.DllPath}"), true);
14-
Console.WriteLine(Path.GetFullPath($"{Environment.DllPath}"));
15-
if (!Environment.CheckEnvironment())
16-
Environment.InstallEnvironment();
17-
Assert.IsTrue(Environment.CheckEnvironment());
13+
var dir = Path.GetFullPath($"{Environment.DllPath}");
14+
15+
Console.WriteLine(dir);
16+
17+
if (Directory.Exists(dir))
18+
Directory.Delete(Path.GetFullPath($"{Environment.DllPath}"), true);
19+
20+
if (!Environment.Check())
21+
await Environment.InstallAsync();
1822

19-
Directory.Delete(Path.GetFullPath($"{Environment.DllPath}"), true);
20-
if (!Environment.CheckEnvironment())
21-
await Environment.InstallEnvironmentAsync();
2223
Thread.Sleep(3000);
23-
Assert.IsTrue(Environment.CheckEnvironment());
24+
25+
Assert.IsTrue(Environment.Check());
2426
}
2527

2628
[TestMethod]
Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System.Net;
2-
3-
#pragma warning disable SYSLIB0014 // 类型或成员已过时
4-
5-
namespace Common.Algorithm.Interop;
1+
namespace Common.Algorithm.Interop;
62

73
public static class Environment
84
{
@@ -28,7 +24,7 @@ public static class Environment
2824
private static readonly List<string> CoreFiles = new()
2925
{
3026
"Math.dll",
31-
"Hash.dll"
27+
"Hash.dll",
3228
};
3329

3430
/// <summary>
@@ -47,7 +43,7 @@ public static class Environment
4743
/// 检查环境是否就绪, 核心文件是否存在
4844
/// </summary>
4945
/// <returns>环境是否就绪</returns>
50-
public static bool CheckEnvironment()
46+
public static bool Check()
5147
{
5248
foreach (string fn in CoreFiles)
5349
{
@@ -61,52 +57,35 @@ public static bool CheckEnvironment()
6157
/// 安装环境
6258
/// <paramref name="im">安装方式</paramref>
6359
/// </summary>
64-
public static void InstallEnvironment(InstallMethod im = InstallMethod.WebClient)
60+
public static async Task InstallAsync(InstallMethod im = InstallMethod.HttpClient)
6561
{
6662
if (!Directory.Exists(DllPath))
6763
Directory.CreateDirectory(DllPath);
68-
switch (im)
69-
{
70-
case InstallMethod.WebClient:
71-
WebClient wc = new();
72-
foreach (string fn in CoreFiles)
73-
{
74-
if (!File.Exists($"{DllPath}{fn}"))
75-
{
76-
wc.DownloadFile(
77-
new Uri($"{_cloudUrl}{_version}/{fn}", UriKind.Absolute),
78-
Path.GetFullPath($"{DllPath}{fn}"));
79-
}
80-
}
81-
wc.Dispose();
82-
break;
83-
}
84-
}
8564

86-
/// <summary>
87-
/// 异步安装环境
88-
/// <paramref name="im">异步安装方式</paramref>
89-
/// </summary>
90-
public static async Task InstallEnvironmentAsync(InstallMethodAsync im = InstallMethodAsync.WebClientAsync)
91-
{
92-
if (!Directory.Exists(DllPath))
93-
Directory.CreateDirectory(DllPath);
9465
switch (im)
9566
{
96-
case InstallMethodAsync.WebClientAsync:
97-
var wc = new WebClient();
98-
foreach (string fn in CoreFiles)
67+
case InstallMethod.HttpClient:
68+
9969
{
100-
if (!File.Exists($"{DllPath}{fn}"))
70+
using var client = new HttpClient();
71+
72+
foreach (var fn in CoreFiles)
10173
{
102-
await wc.DownloadFileTaskAsync(
103-
new Uri($"{_cloudUrl}{_version}/{fn}", UriKind.Absolute),
104-
Path.GetFullPath($"{DllPath}{fn}"));
74+
var downloadUrl = $"{_cloudUrl}{_version}/{fn}";
75+
var savePath = Path.GetFullPath($"{DllPath}{fn}");
76+
77+
if (!File.Exists(savePath))
78+
{
79+
using var stream = await client.GetStreamAsync(downloadUrl);
80+
81+
using var fileStream = File.Create(savePath);
82+
83+
await stream.CopyToAsync(fileStream);
84+
85+
await fileStream.FlushAsync();
86+
}
10587
}
10688
}
107-
wc?.Dispose();
108-
break;
109-
case InstallMethodAsync.Http:
11089

11190
break;
11291
}
@@ -117,16 +96,6 @@ await wc.DownloadFileTaskAsync(
11796
/// </summary>
11897
public enum InstallMethod
11998
{
120-
WebClient
121-
}
122-
123-
/// <summary>
124-
/// 安装环境下载方式
125-
/// </summary>
126-
public enum InstallMethodAsync
127-
{
128-
WebClientAsync, Http
99+
HttpClient = 1,
129100
}
130101
}
131-
132-
#pragma warning restore SYSLIB0014 // 类型或成员已过时

0 commit comments

Comments
 (0)