-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ISimpleDownloadProgress to Redpoint.ProgressMonitor (#66)
- Loading branch information
Showing
8 changed files
with
141 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
UET/Redpoint.ProgressMonitor/Utils/DefaultSimpleDownloadProgress.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#if NETCOREAPP | ||
|
||
namespace Redpoint.ProgressMonitor.Utils | ||
{ | ||
using Redpoint.ProgressMonitor; | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
internal class DefaultSimpleDownloadProgress : ISimpleDownloadProgress | ||
{ | ||
private readonly IProgressFactory _progressFactory; | ||
private readonly IMonitorFactory _monitorFactory; | ||
|
||
public DefaultSimpleDownloadProgress( | ||
IProgressFactory progressFactory, | ||
IMonitorFactory monitorFactory) | ||
{ | ||
_progressFactory = progressFactory; | ||
_monitorFactory = monitorFactory; | ||
} | ||
|
||
private class DidOutput | ||
{ | ||
public bool Did; | ||
} | ||
|
||
public async Task DownloadAndCopyToStreamAsync( | ||
HttpClient client, | ||
Uri downloadUrl, | ||
Func<Stream, Task> copier, | ||
CancellationToken cancellationToken) | ||
{ | ||
var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); | ||
|
||
using var stream = new PositionAwareStream( | ||
await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), | ||
response.Content.Headers.ContentLength!.Value); | ||
|
||
using var cts = new CancellationTokenSource(); | ||
var progress = _progressFactory.CreateProgressForStream(stream); | ||
var outputTrack = new DidOutput(); | ||
var monitorTask = Task.Run(async () => | ||
{ | ||
var consoleWidth = 0; | ||
try | ||
{ | ||
consoleWidth = Console.BufferWidth; | ||
} | ||
catch { } | ||
|
||
var monitor = _monitorFactory.CreateByteBasedMonitor(); | ||
await monitor.MonitorAsync( | ||
progress, | ||
null, | ||
(message, count) => | ||
{ | ||
if (consoleWidth != 0) | ||
{ | ||
Console.Write($"\r {message}".PadRight(consoleWidth)); | ||
outputTrack.Did = true; | ||
} | ||
else if (count % 50 == 0) | ||
{ | ||
Console.WriteLine($" {message}"); | ||
outputTrack.Did = true; | ||
} | ||
}, | ||
cts.Token).ConfigureAwait(false); | ||
}, cts.Token); | ||
|
||
await copier(stream).ConfigureAwait(false); | ||
|
||
cts.Cancel(); | ||
try | ||
{ | ||
await monitorTask.ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
if (outputTrack.Did) | ||
{ | ||
var consoleWidth = 0; | ||
try | ||
{ | ||
consoleWidth = Console.BufferWidth; | ||
} | ||
catch { } | ||
if (consoleWidth != 0) | ||
{ | ||
Console.WriteLine(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
32 changes: 32 additions & 0 deletions
32
UET/Redpoint.ProgressMonitor/Utils/ISimpleDownloadProgress.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#if NETCOREAPP | ||
|
||
namespace Redpoint.ProgressMonitor.Utils | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// A utility interface which downloads a URL to a stream and emits progress information to the console. | ||
/// </summary> | ||
public interface ISimpleDownloadProgress | ||
{ | ||
/// <summary> | ||
/// Download the target URL using the specified <see cref="HttpClient"/>, passing the download stream to <paramref name="copier"/> (which is typically a lambda that calls <see cref="Stream.CopyToAsync(Stream, CancellationToken)"/>). | ||
/// </summary> | ||
/// <param name="client">The <see cref="HttpClient"/> to use for the download.</param> | ||
/// <param name="downloadUrl">The URL to download.</param> | ||
/// <param name="copier">The asynchronous function that the download stream will be passed to. When the returned task completes, the stream should have been fully written or consumed.</param> | ||
/// <param name="cancellationToken">A token which can be used to cancel the download.</param> | ||
/// <returns>An awaitable task.</returns> | ||
Task DownloadAndCopyToStreamAsync( | ||
HttpClient client, | ||
Uri downloadUrl, | ||
Func<Stream, Task> copier, | ||
CancellationToken cancellationToken); | ||
} | ||
} | ||
|
||
#endif |
15 changes: 0 additions & 15 deletions
15
UET/Redpoint.Uet.SdkManagement/DownloadProgress/ISimpleDownloadProgress.cs
This file was deleted.
Oops, something went wrong.
95 changes: 0 additions & 95 deletions
95
UET/Redpoint.Uet.SdkManagement/DownloadProgress/SimpleDownloadProgress.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters