Skip to content

Commit a0e9adf

Browse files
committed
更新下载器对接逻辑和辅助方法
1 parent a2dc0e0 commit a0e9adf

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/dotnetCampus.FileDownloader.Tool/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ static async Task Main(string[] args)
2626
"https://download.jetbrains.8686c.com/resharper/ReSharperUltimate.2020.1.3/JetBrains.ReSharperUltimate.2020.1.3.exe";
2727
//var md5 = "7d6bbeb6617a7c0b7e615098fca1b167";// resharper
2828

29-
url = "http://localhost:5000";
29+
//url = "http://localhost:5000";
30+
url =
31+
"https://dscache.tencent-cloud.cn/upload//ES_686_194-4f155229efeef75bb9c9a3995060c766dc0eac28.png";
3032

3133
var file = new FileInfo(@"File.txt");
3234

3335
var progress = new Progress<DownloadProgress>();
3436

35-
var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress);
36-
await segmentFileDownloader.DownloadFileAsync();
37+
await FileDownloaderHelper.DownloadFileAsync(url, file, progress:progress);
3738
#endif
3839
await Task.Delay(100);
3940
});
@@ -76,7 +77,7 @@ private static async Task DownloadFileAsync(DownloadOption option)
7677

7778
var file = new FileInfo(output);
7879
var url = option.Url;
79-
var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress);
80+
using var segmentFileDownloader = new SegmentFileDownloaderByHttpClient(url, file, httpClient:null, logger, progress);
8081

8182
await segmentFileDownloader.DownloadFileAsync();
8283

src/dotnetCampus.FileDownloader/FileDownloaderHelper.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
using System.Security.AccessControl;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
1516
using dotnetCampus.FileDownloader.Utils;
17+
1618
using Microsoft.Extensions.Logging;
1719

1820
namespace dotnetCampus.FileDownloader
@@ -34,14 +36,20 @@ public static class FileDownloaderHelper
3436
/// <param name="bufferLength">缓存的数组长度,默认是 65535 的长度</param>
3537
/// <param name="stepTimeOut">每一步 每一分段下载超时时间 默认 10 秒</param>
3638
/// <returns></returns>
37-
public static Task DownloadFileAsync(string url, FileInfo file,
39+
public static async Task DownloadFileAsync(string url, FileInfo file,
3840
ILogger<SegmentFileDownloader>? logger = null,
3941
IProgress<DownloadProgress>? progress = null, ISharedArrayPool? sharedArrayPool = null,
4042
int bufferLength = ushort.MaxValue, TimeSpan? stepTimeOut = null)
4143
{
42-
var segmentFileDownloader = new SegmentFileDownloader(url, file, logger, progress, sharedArrayPool, bufferLength, stepTimeOut);
44+
#if NETCOREAPP3_1_OR_GREATER
45+
using var segmentFileDownloaderByHttpClient = new SegmentFileDownloaderByHttpClient(url, file, httpClient: null, logger, progress, sharedArrayPool, bufferLength, stepTimeOut);
46+
await segmentFileDownloaderByHttpClient.DownloadFileAsync();
47+
#else
48+
var segmentFileDownloader =
49+
new SegmentFileDownloader(url, file, logger, progress, sharedArrayPool, bufferLength, stepTimeOut);
4350

44-
return segmentFileDownloader.DownloadFileAsync();
51+
await segmentFileDownloader.DownloadFileAsync();
52+
#endif
4553
}
4654

4755
/// <summary>
@@ -79,10 +87,10 @@ public static async Task<FileInfo> DownloadFileToFolderAsync(string url, Directo
7987
var downloadFile = new FileInfo(Path.Combine(tempFolder.FullName, fileName));
8088

8189
#if NETCOREAPP3_1_OR_GREATER
82-
using var segmentFileDownloader = new InnerSegmentFileDownloaderByHttpClient(url, downloadFile, httpClient: null,
83-
logger, progress, sharedArrayPool, bufferLength, stepTimeOut);
90+
using var segmentFileDownloader = new InnerSegmentFileDownloaderByHttpClient(url, downloadFile, httpClient: null,
91+
logger, progress, sharedArrayPool, bufferLength, stepTimeOut);
8492

85-
await segmentFileDownloader.DownloadFileAsync();
93+
await segmentFileDownloader.DownloadFileAsync();
8694

8795
// 下载完成了之后,尝试移动文件夹
8896
// 优先使用服务器返回的文件名

0 commit comments

Comments
 (0)