From f53371ab193dc051ee719a48c8162d3938ad4df2 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 15 Apr 2024 13:04:49 +0200 Subject: [PATCH] Helpers: Collapse extensions into one single file --- Elwig/Helpers/Extensions.cs | 38 ++++++++++++++++++++++++++- Elwig/Helpers/HttpClientExtensions.cs | 24 ----------------- Elwig/Helpers/StreamExtensions.cs | 25 ------------------ 3 files changed, 37 insertions(+), 50 deletions(-) delete mode 100644 Elwig/Helpers/HttpClientExtensions.cs delete mode 100644 Elwig/Helpers/StreamExtensions.cs diff --git a/Elwig/Helpers/Extensions.cs b/Elwig/Helpers/Extensions.cs index 0d17c29..2ff39bd 100644 --- a/Elwig/Helpers/Extensions.cs +++ b/Elwig/Helpers/Extensions.cs @@ -1,5 +1,8 @@ -using System.IO; +using System; +using System.IO; +using System.Net.Http; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; namespace Elwig.Helpers { @@ -59,5 +62,38 @@ namespace Elwig.Helpers { } return null; } + + public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress? progress = null, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(source); + if (!source.CanRead) throw new ArgumentException("Has to be readable", nameof(source)); + ArgumentNullException.ThrowIfNull(destination); + if (!destination.CanWrite) throw new ArgumentException("Has to be writable", nameof(destination)); + ArgumentOutOfRangeException.ThrowIfNegative(bufferSize); + + var buffer = new byte[bufferSize]; + long totalBytesRead = 0; + int bytesRead; + while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) { + await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false); + totalBytesRead += bytesRead; + progress?.Report(totalBytesRead); + } + } + + public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress? progress = null, CancellationToken cancellationToken = default) { + using var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + response.EnsureSuccessStatusCode(); + var contentLength = response.Content.Headers.ContentLength; + + using var download = await response.Content.ReadAsStreamAsync(cancellationToken); + if (progress == null || !contentLength.HasValue) { + await download.CopyToAsync(destination, cancellationToken); + return; + } + + var relativeProgress = new Progress(totalBytes => progress.Report((double)totalBytes / contentLength.Value)); + await download.CopyToAsync(destination, 81920, relativeProgress, cancellationToken); + progress.Report(100.0); + } } } diff --git a/Elwig/Helpers/HttpClientExtensions.cs b/Elwig/Helpers/HttpClientExtensions.cs deleted file mode 100644 index f1793bd..0000000 --- a/Elwig/Helpers/HttpClientExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.IO; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace Elwig.Helpers { - public static class HttpClientExtensions { - public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress? progress = null, CancellationToken cancellationToken = default) { - using var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken); - var contentLength = response.Content.Headers.ContentLength; - - using var download = await response.Content.ReadAsStreamAsync(cancellationToken); - if (progress == null || !contentLength.HasValue) { - await download.CopyToAsync(destination, cancellationToken); - return; - } - - var relativeProgress = new Progress(totalBytes => progress.Report((double)totalBytes / contentLength.Value)); - await download.CopyToAsync(destination, 81920, relativeProgress, cancellationToken); - progress.Report(100.0); - } - } -} diff --git a/Elwig/Helpers/StreamExtensions.cs b/Elwig/Helpers/StreamExtensions.cs deleted file mode 100644 index dac0fd4..0000000 --- a/Elwig/Helpers/StreamExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace Elwig.Helpers { - public static class StreamExtensions { - public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress? progress = null, CancellationToken cancellationToken = default) { - ArgumentNullException.ThrowIfNull(source); - if (!source.CanRead) throw new ArgumentException("Has to be readable", nameof(source)); - ArgumentNullException.ThrowIfNull(destination); - if (!destination.CanWrite) throw new ArgumentException("Has to be writable", nameof(destination)); - ArgumentOutOfRangeException.ThrowIfNegative(bufferSize); - - var buffer = new byte[bufferSize]; - long totalBytesRead = 0; - int bytesRead; - while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) { - await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false); - totalBytesRead += bytesRead; - progress?.Report(totalBytesRead); - } - } - } -}