Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Jan 31, 2024
1 parent 0044ce5 commit 048aa3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions PatchSync.SDK/Client/IDownloadProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PatchSync.SDK.Client;

public interface IDownloadProgress
{
public long TotalSize { get; }
public long TotalDownloaded { get; }
public double Speed { get; }
}
27 changes: 21 additions & 6 deletions PatchSync.SDK/Client/PatchSyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public async Task<SignatureFile> GetSignatureFile(int originalFileSize, string r
return signatureFile;
}

public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(string relativeUri, IEnumerable<(int Start ,int End)> ranges, IProgress<(double Progress, double Speed)> progress = null)
public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(
string relativeUri,
IEnumerable<(long Start, long End)> ranges,
IProgress<IDownloadProgress> progress = null
)
{
long totalSize = 0;
using var httpClient = HttpHandler.CreateHttpClient();
Expand All @@ -65,7 +69,7 @@ public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(string relativ
httpClient.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue();
foreach (var range in ranges)
{
totalSize += range.End - range.Start + 1;;
totalSize += range.End - range.Start + 1;
httpClient.DefaultRequestHeaders.Range.Ranges.Add(
new System.Net.Http.Headers.RangeItemHeaderValue(range.Start, range.End)
);
Expand All @@ -76,7 +80,10 @@ public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(string relativ
// Throw if not successful
response.EnsureSuccessStatusCode();

var progressInfo = new ProgressInfo();
var progressInfo = new ProgressInfo
{
TotalSize = totalSize
};

if (response.Content.Headers.ContentType?.MediaType.Equals(
"multipart/byteranges", StringComparison.OrdinalIgnoreCase
Expand All @@ -88,7 +95,7 @@ public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(string relativ
{
foreach (var content in multipart)
{
await CopyToStreamAsync(content, memoryStream, progressInfo, totalSize, progress);
await CopyToStreamAsync(content, memoryStream, progressInfo, progress);
memoryStream.Seek(0, SeekOrigin.Begin); // Reset memory stream position for reading
yield return memoryStream;
memoryStream.SetLength(0); // Clear memory stream for next part
Expand All @@ -101,7 +108,7 @@ public async IAsyncEnumerable<Stream> DownloadDeltaPatchFileAsync(string relativ
}
}

private static async Task CopyToStreamAsync(HttpContent content, Stream destination, ProgressInfo progressInfo, long totalSize, IProgress<(double Progress, double Speed)> progress)
private static async Task CopyToStreamAsync(HttpContent content, Stream destination, ProgressInfo progressInfo, IProgress<IDownloadProgress> progress)
{
#if NET6_0_OR_GREATER
byte[] buffer = GC.AllocateUninitializedArray<byte>(81920);
Expand All @@ -123,7 +130,7 @@ private static async Task CopyToStreamAsync(HttpContent content, Stream destinat
speed = progressInfo.TotalRead / progressInfo.Stopwatch.Elapsed.TotalSeconds;
}

progress?.Report(((double)progressInfo.TotalRead / totalSize, speed));
progress?.Report(new DownloadProgress(progressInfo.TotalSize, progressInfo.TotalRead, speed));
}
}

Expand All @@ -132,7 +139,15 @@ public void ValidateFiles(string baseFolder, Func<ValidationResult> callback) =>

private class ProgressInfo
{
public long TotalSize { get; set; }
public long TotalRead { get; set; }
public Stopwatch Stopwatch { get; } = new();
}

private class DownloadProgress(long totalSize, long totalDownloaded, double speed) : IDownloadProgress
{
public long TotalSize { get; } = totalSize;
public long TotalDownloaded { get; } = totalDownloaded;
public double Speed { get; } = speed;
}
}

0 comments on commit 048aa3d

Please sign in to comment.