Skip to content

Commit

Permalink
fix download progress display
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Nov 25, 2024
1 parent 89dcaff commit 3075da5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
48 changes: 36 additions & 12 deletions ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static class DownloadHelper
private static HttpClient MultiPart => HttpClientHelper.MultiPartClient;

/// <summary>
/// Receive data from remote stream
/// Receive data from remote stream (only for partial download)
/// </summary>
/// <returns>Elapsed time in seconds</returns>
private static async Task<double> ReceiveFromRemoteStreamAsync(
Expand Down Expand Up @@ -87,26 +88,49 @@ public static async Task DownloadData(DownloadFile downloadFile, DownloadSetting

using var hashProvider = downloadSettings.GetCryptoTransform();

double averageSpeed;

var averageSpeed = 0d;
var outputStream = File.Create(filePath);
var cryptoStream = new CryptoStream(outputStream, hashProvider, CryptoStreamMode.Write, true);

await using (var stream = await res.Content.ReadAsStreamAsync(cts.Token))
await using (Stream destStream = hashCheckFile ? cryptoStream : outputStream)
{
const int defaultCopyBufferSize = 81920;

using var buffer = MemoryPool<byte>.Shared.Rent(defaultCopyBufferSize);

var bytesReadInTotal = 0L;

while (true)
{
var startTime = Stopwatch.GetTimestamp();
var bytesRead = await stream.ReadAsync(buffer.Memory, cts.Token);

if (bytesRead == 0) break;

bytesReadInTotal += bytesRead;

await destStream.WriteAsync(buffer.Memory[..bytesRead], cts.Token);

var duration = Stopwatch.GetElapsedTime(startTime);
var elapsedTime = duration.TotalSeconds == 0 ? 1 : duration.TotalSeconds;

averageSpeed = responseLength / elapsedTime;

if (downloadSettings.ShowDownloadProgress)
downloadFile.OnChanged(
averageSpeed,
ProgressValue.Create(bytesReadInTotal, responseLength),
responseLength,
responseLength);
}

/*
var elapsedTime = await ReceiveFromRemoteStreamAsync(
stream,
destStream,
cts.Token);

averageSpeed = responseLength / elapsedTime;

downloadFile.OnChanged(
averageSpeed,
ProgressValue.FromDisplay(100),
responseLength,
responseLength);
*/

if (hashCheckFile && destStream is CryptoStream cStream)
await cStream.FlushFinalBlockAsync(cts.Token);
Expand Down Expand Up @@ -480,7 +504,7 @@ public static async Task MultiPartDownloadTaskAsync(
var addedAggregatedSpeed = Interlocked.Add(ref aggregatedSpeed, (uint)speed);
var addedBytesReceived = Interlocked.Add(ref bytesReceived, correctedSpeed);

if (downloadSettings.ShowDownloadProgressForPartialDownload)
if (downloadSettings.ShowDownloadProgress)
downloadFile.OnChanged(
(double)addedAggregatedSpeed / addedAggregatedSpeedCount,
ProgressValue.Create(addedBytesReceived, urlInfo.FileLength),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DownloadSettings
public TimeSpan Timeout { get; init; }
public int DownloadParts { get; set; }
public HashType HashType { get; init; }
public bool ShowDownloadProgressForPartialDownload { get; init; }
public bool ShowDownloadProgress { get; init; }

/// <summary>
/// 认证
Expand Down

0 comments on commit 3075da5

Please sign in to comment.