-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ProgressBar for Download and Upload
- Loading branch information
1 parent
af3f25e
commit ca4e6eb
Showing
3 changed files
with
259 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace KekUploadCLIClient; | ||
|
||
public class HttpClientDownloadWithProgress : IDisposable | ||
{ | ||
private readonly string _downloadUrl; | ||
private readonly string _destinationFilePath; | ||
|
||
private HttpClient _httpClient; | ||
|
||
public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage); | ||
|
||
public event ProgressChangedHandler ProgressChanged; | ||
|
||
public HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath) | ||
{ | ||
_downloadUrl = downloadUrl; | ||
_destinationFilePath = destinationFilePath; | ||
} | ||
|
||
public async Task StartDownload() | ||
{ | ||
_httpClient = new HttpClient { Timeout = TimeSpan.FromMinutes(5) }; | ||
|
||
using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead)) | ||
await DownloadFileFromHttpResponseMessage(response); | ||
} | ||
|
||
private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var totalBytes = response.Content.Headers.ContentLength; | ||
|
||
using (var contentStream = await response.Content.ReadAsStreamAsync()) | ||
await ProcessContentStream(totalBytes, contentStream); | ||
} | ||
|
||
private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream) | ||
{ | ||
var totalBytesRead = 0L; | ||
var readCount = 0L; | ||
var buffer = new byte[8192]; | ||
var isMoreToRead = true; | ||
|
||
using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) | ||
{ | ||
do | ||
{ | ||
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length); | ||
if (bytesRead == 0) | ||
{ | ||
isMoreToRead = false; | ||
TriggerProgressChanged(totalDownloadSize, totalBytesRead); | ||
continue; | ||
} | ||
|
||
await fileStream.WriteAsync(buffer, 0, bytesRead); | ||
|
||
totalBytesRead += bytesRead; | ||
readCount += 1; | ||
|
||
if (readCount % 100 == 0) | ||
TriggerProgressChanged(totalDownloadSize, totalBytesRead); | ||
} | ||
while (isMoreToRead); | ||
} | ||
} | ||
|
||
private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead) | ||
{ | ||
if (ProgressChanged == null) | ||
return; | ||
|
||
double? progressPercentage = null; | ||
if (totalDownloadSize.HasValue) | ||
progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2); | ||
|
||
ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_httpClient?.Dispose(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace KekUploadCLIClient | ||
{ | ||
/// <summary> | ||
/// Console progressbar | ||
/// </summary> | ||
public class ProgressBar : IDisposable { | ||
|
||
public float CurrentProgress => writer.CurrentProgress; | ||
|
||
private TextWriter OriginalWriter; | ||
private ProgressWriter writer; | ||
|
||
public ProgressBar() { | ||
OriginalWriter = Console.Out; | ||
writer = new ProgressWriter(OriginalWriter); | ||
Console.SetOut(writer); | ||
} | ||
|
||
public void Dispose() { | ||
Console.SetOut(OriginalWriter); | ||
writer.ClearProgressBar(); | ||
} | ||
|
||
public void SetProgress(float f) { | ||
writer.CurrentProgress = f; | ||
writer.RedrawProgress(); | ||
} | ||
public void SetProgress(int i) { | ||
SetProgress((float)i); | ||
} | ||
|
||
public void Increment(float f) { | ||
writer.CurrentProgress += f; | ||
writer.RedrawProgress(); | ||
} | ||
|
||
public void Increment(int i) { | ||
Increment((float)i); | ||
} | ||
|
||
private class ProgressWriter : TextWriter { | ||
|
||
public override Encoding Encoding => Encoding.UTF8; | ||
public float CurrentProgress { | ||
get { return _currentProgress; } | ||
set { | ||
_currentProgress = value; | ||
if(_currentProgress > 100) { | ||
_currentProgress = 100; | ||
}else if(CurrentProgress < 0) { | ||
_currentProgress = 0; | ||
} | ||
} | ||
} | ||
|
||
private float _currentProgress = 0; | ||
private TextWriter consoleOut; | ||
private const string ProgressTemplate = "[{0}] {1:n2}%"; | ||
private const int AllocatedTemplateSpace = 11; | ||
private object SyncLock = new object(); | ||
public ProgressWriter(TextWriter _consoleOut) { | ||
consoleOut = _consoleOut; | ||
RedrawProgress(); | ||
} | ||
|
||
private void DrawProgressBar() { | ||
lock (SyncLock) { | ||
int avalibleSpace = Console.BufferWidth - AllocatedTemplateSpace; | ||
int percentAmmount = (int)((float)avalibleSpace * (CurrentProgress / 100)); | ||
var col = Console.ForegroundColor; | ||
Console.ForegroundColor = ConsoleColor.DarkMagenta; | ||
string progressBar = string.Concat(new string('=', percentAmmount), new string(' ', avalibleSpace - percentAmmount)); | ||
consoleOut.Write(string.Format(ProgressTemplate, progressBar, CurrentProgress)); | ||
Console.ForegroundColor = col; | ||
} | ||
} | ||
|
||
public void RedrawProgress() { | ||
lock (SyncLock) { | ||
int LastLineWidth = Console.CursorLeft; | ||
var consoleH = Console.WindowTop + Console.WindowHeight - 1; | ||
Console.SetCursorPosition(0, consoleH); | ||
DrawProgressBar(); | ||
Console.SetCursorPosition(LastLineWidth, consoleH - 1); | ||
} | ||
} | ||
|
||
private void ClearLineEnd() { | ||
lock (SyncLock) { | ||
int lineEndClear = Console.BufferWidth - Console.CursorLeft - 1; | ||
consoleOut.Write(new string(' ', lineEndClear)); | ||
} | ||
} | ||
|
||
public void ClearProgressBar() { | ||
lock (SyncLock) { | ||
int LastLineWidth = Console.CursorLeft; | ||
var consoleH = Console.WindowTop + Console.WindowHeight - 1; | ||
Console.SetCursorPosition(0, consoleH); | ||
ClearLineEnd(); | ||
Console.SetCursorPosition(LastLineWidth, consoleH - 1); | ||
} | ||
} | ||
|
||
public override void Write(char value) { | ||
lock (SyncLock) { | ||
consoleOut.Write(value); | ||
} | ||
} | ||
|
||
public override void Write(string? value) { | ||
lock (SyncLock) { | ||
consoleOut.Write(value); | ||
} | ||
} | ||
|
||
public override void WriteLine(string? value) { | ||
lock (SyncLock) { | ||
consoleOut.Write(value); | ||
consoleOut.Write(Environment.NewLine); | ||
ClearLineEnd(); | ||
consoleOut.Write(Environment.NewLine); | ||
RedrawProgress(); | ||
} | ||
} | ||
|
||
public override void WriteLine(string format, params object[] arg) { | ||
WriteLine(string.Format(format, arg)); | ||
} | ||
|
||
public override void WriteLine(int i) { | ||
WriteLine(i.ToString()); | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
|