migration

This commit is contained in:
2026-02-13 21:24:55 +01:00
Unverified
parent 84286c2161
commit 3ae545ea8b
10 changed files with 574 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleToolkit.Extensions
{
public static class HttpClientExtensions
{
public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, CancellationToken cancellationToken = default, IProgress<double> progress = null)
{
using (HttpResponseMessage response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead))
{
long? contentLength = response.Content.Headers.ContentLength;
using (Stream download = await response.Content.ReadAsStreamAsync(cancellationToken))
{
if (progress == null || !contentLength.HasValue)
{
await download.CopyToAsync(destination);
return;
}
var relativeProgress = new Progress<long>(totalBytes => progress.Report((double)totalBytes * 100 / contentLength.Value));
await download.CopyToAsync(destination, 81920, relativeProgress, cancellationToken);
progress.Report(100);
}
}
}
}
}