Skip to content

Commit

Permalink
Update DownloadHelper.Multipart.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Feb 23, 2025
1 parent 21eb3c2 commit 44e9283
Showing 1 changed file with 1 addition and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,48 +348,12 @@ public static async Task MultiPartDownloadTaskAsync(
{
// If we end up to here, which means either the chunk is too large or the download speed is too slow
// So we need to further split the chunk into smaller parts
var bytesTransmitted = fileToWriteTo.Length;

if (bytesTransmitted == 0)
{
// If we haven't received any data, we need to retry
await fileToWriteTo.DisposeAsync();
throw;
}

// Remove the current range from the download queue
ArgumentOutOfRangeException.ThrowIfEqual(downloadFile.Ranges.TryRemove(chunkInfo.Range, out _), false);

var chunkLength = chunkInfo.Range.End - chunkInfo.Range.Start;
var remaining = chunkLength - bytesTransmitted;
var finishedRange = chunkInfo.Range with { End = chunkInfo.Range.End - remaining };

// Add the finished parts
ArgumentOutOfRangeException.ThrowIfEqual(downloadFile.Ranges.TryAdd(finishedRange, null), false);
ArgumentOutOfRangeException.ThrowIfEqual(downloadFile.FinishedRangeStreams.TryAdd(finishedRange, fileToWriteTo), false);

if (remaining < 1024)
{
// File is too small to be split, just retry
var remainingRange = new DownloadRange
{
Start = finishedRange.End,
End = chunkInfo.Range.End,
TempFileName = GetTempFilePath()
};

if (remainingRange.Start >= remainingRange.End)
{
throw new ArgumentOutOfRangeException(
$"[{chunkLength}][{remaining}][{bytesTransmitted}][{chunkInfo.Range.End - remaining}]{remainingRange}[{e}]");
}

ArgumentOutOfRangeException.ThrowIfEqual(downloadFile.Ranges.TryAdd(remainingRange, null), false);

throw;
}

var subChunkRanges = CalculateDownloadRanges(remaining, finishedRange.End, downloadSettings);
var subChunkRanges = CalculateDownloadRanges(chunkLength, chunkInfo.Range.Start, downloadSettings);

// We add the sub chunks to the download queue
foreach (var range in subChunkRanges)
Expand Down

0 comments on commit 44e9283

Please sign in to comment.