From 6162b105ca9c2b4c165ab23c52ddae79a299ae7a Mon Sep 17 00:00:00 2001 From: natsurainko Date: Fri, 7 Feb 2025 19:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=95=9C=E5=83=8F?= =?UTF-8?q?=E6=BA=90=E3=80=81=E5=88=86=E7=89=87=E4=B8=8B=E8=BD=BD=E6=9C=AA?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Downloader/MultipartDownloader.cs | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Natsurainko.FluentCore/GameManagement/Downloader/MultipartDownloader.cs b/Natsurainko.FluentCore/GameManagement/Downloader/MultipartDownloader.cs index f7b49b2..3239684 100644 --- a/Natsurainko.FluentCore/GameManagement/Downloader/MultipartDownloader.cs +++ b/Natsurainko.FluentCore/GameManagement/Downloader/MultipartDownloader.cs @@ -17,6 +17,7 @@ public class MultipartDownloader : IDownloader public int WorkersPerDownloadTask { get => _config.WorkersPerDownloadTask; } public int ConcurrentDownloadTasks { get => _config.ConcurrentDownloadTasks; } public IDownloadMirror? Mirror { get => _config.Mirror; } + public bool EnableMultiPartDownload { get => _config.EnableMultiPartDownload; } private HttpClient HttpClient { get => _config.HttpClient; } @@ -25,10 +26,16 @@ public class MultipartDownloader : IDownloader private readonly SemaphoreSlim _globalDownloadTasksSemaphore; - public MultipartDownloader(HttpClient? httpClient, long chunkSize = 1048576 /* 1MB */, int workersPerDownloadTask = 16, int concurrentDownloadTasks = 5, IDownloadMirror? mirror = null) + public MultipartDownloader( + HttpClient? httpClient, + long chunkSize = 1048576 /* 1MB */, + int workersPerDownloadTask = 16, + int concurrentDownloadTasks = 5, + IDownloadMirror? mirror = null, + bool enableMultiPartDownload = true) { httpClient ??= HttpUtils.HttpClient; - _config = new DownloaderConfig(httpClient, chunkSize, workersPerDownloadTask, concurrentDownloadTasks, mirror); + _config = new DownloaderConfig(httpClient, chunkSize, workersPerDownloadTask, concurrentDownloadTasks, mirror, enableMultiPartDownload); _globalDownloadTasksSemaphore = new SemaphoreSlim(concurrentDownloadTasks, concurrentDownloadTasks); } @@ -87,7 +94,7 @@ public async Task DownloadFileDriverAsync(DownloadRequest request, CancellationT // Use multi-part download if Content-Length is provided and the remote server supports range requests // Fall back to single part download if the remote server does not provide a Content-Length or does not support range requests bool useMultiPart = false; - if (response.Content.Headers.ContentLength is long contentLength) + if (EnableMultiPartDownload && response.Content.Headers.ContentLength is long contentLength) { states.TotalBytes = contentLength; // Commented: some servers return AcceptRange="bytes" while they return 404 for range requests @@ -231,15 +238,13 @@ private async Task DownloadFileInGroupAsync(DownloadRequest request, GroupDownlo public async Task DownloadFilesAsync(GroupDownloadRequest request, CancellationToken cancellationToken = default) { - List<(DownloadRequest, DownloadResult)> failed = new(); - List downloadTasks = new(); + List<(DownloadRequest, DownloadResult)> failed = []; + List downloadTasks = []; + foreach (var req in request.Files) { - string url = req.Url; - string localPath = req.LocalPath; - if (_config.Mirror is not null) - url = _config.Mirror.GetMirrorUrl(url); + req.Url = _config.Mirror.GetMirrorUrl(req.Url); downloadTasks.Add(DownloadFileInGroupAsync(req, request, failed, cancellationToken)); } @@ -297,5 +302,6 @@ private record class DownloaderConfig( long ChunkSize, int WorkersPerDownloadTask, int ConcurrentDownloadTasks, - IDownloadMirror? Mirror); + IDownloadMirror? Mirror, + bool EnableMultiPartDownload = true); }