Skip to content

Commit

Permalink
修复了镜像源、分片下载未设置的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Feb 7, 2025
1 parent 22518e2 commit 6162b10
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -231,15 +238,13 @@ private async Task DownloadFileInGroupAsync(DownloadRequest request, GroupDownlo

public async Task<GroupDownloadResult> DownloadFilesAsync(GroupDownloadRequest request, CancellationToken cancellationToken = default)
{
List<(DownloadRequest, DownloadResult)> failed = new();
List<Task> downloadTasks = new();
List<(DownloadRequest, DownloadResult)> failed = [];
List<Task> 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));
}
Expand Down Expand Up @@ -297,5 +302,6 @@ private record class DownloaderConfig(
long ChunkSize,
int WorkersPerDownloadTask,
int ConcurrentDownloadTasks,
IDownloadMirror? Mirror);
IDownloadMirror? Mirror,
bool EnableMultiPartDownload = true);
}

0 comments on commit 6162b10

Please sign in to comment.