Updated download system

This commit is contained in:
2022-05-05 20:39:31 +03:00
parent d787457753
commit d3d687ea2f
4 changed files with 90 additions and 76 deletions

View File

@@ -18,7 +18,8 @@ namespace PluginManager.Online.Helpers
/// <param name="progress">The <see cref="IProgress{T}"/> that is used to track the download progress</param>
/// <param name="cancellation">The cancellation token</param>
/// <returns></returns>
internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, IProgress<float> progress = null, CancellationToken cancellation = default)
internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination,
IProgress<float> progress = null, IProgress<long> downloadedBytes = null, CancellationToken cancellation = default)
{
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
@@ -36,7 +37,11 @@ namespace PluginManager.Online.Helpers
}
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
var relativeProgress = new Progress<long>(totalBytes => progress.Report((float)totalBytes / contentLength.Value * 100));
var relativeProgress = new Progress<long>(totalBytes =>
{
progress.Report((float)totalBytes / contentLength.Value * 100);
downloadedBytes.Report(totalBytes);
});
// Use extension method to report progress while downloading
await download.CopyToOtherStreamAsync(destination, 81920, relativeProgress, cancellation);
progress.Report(1);

View File

@@ -33,7 +33,7 @@ namespace PluginManager.Online
/// <param name="location">The location where to store the downloaded data</param>
/// <param name="progress">The <see cref="IProgress{T}"/> to track the download</param>
/// <returns></returns>
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> progress)
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> progress, IProgress<long> downloadedBytes)
{
using (var client = new System.Net.Http.HttpClient())
{
@@ -41,9 +41,76 @@ namespace PluginManager.Online
using (var file = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.None))
{
await client.DownloadFileAsync(URL, file, progress);
await client.DownloadFileAsync(URL, file, progress, downloadedBytes);
}
}
}
/// <summary>
/// Download file from url
/// </summary>
/// <param name="URL">The url to the file</param>
/// <param name="location">The location where to store the downloaded data</param>
/// <returns></returns>
public static async Task DownloadFileAsync(string URL, string location)
{
bool isDownloading = true;
int c_progress = 0;
long secondsPast = 0;
long m_dwBytes = 0;
double c_downloadSpeed = 0f;
string c_downloadUnit = "MB";
Others.Console_Utilities.ProgressBar pbar = new Others.Console_Utilities.ProgressBar(100, "");
IProgress<float> progress = new Progress<float>(percent =>
{
c_progress = (int)percent;
});
IProgress<long> progress_downloaded = new Progress<long>(downloadedBytes =>
{
m_dwBytes = downloadedBytes;
});
Task updateProgressBarTask = new Task(async () =>
{
while (isDownloading)
{
//pbar.Update(c_progress, c_downloadSpeed, c_downloadUnit);
pbar.Update(c_progress);
if (c_progress == 100)
break;
System.Threading.Thread.Sleep(500);
}
});
Task calculateDownloadSpeed = new Task(async () =>
{
while (isDownloading)
{
secondsPast++;
c_downloadSpeed = m_dwBytes / secondsPast;
c_downloadSpeed /= 1024; // in KB
c_downloadSpeed /= 1024; // in MB
c_downloadSpeed = Math.Round(c_downloadSpeed, 2, MidpointRounding.AwayFromZero);
System.Threading.Thread.Sleep(1000);
}
});
// new System.Threading.Thread(calculateDownloadSpeed.Start).Start();
new System.Threading.Thread(updateProgressBarTask.Start).Start();
await DownloadFileAsync(URL, location, progress, progress_downloaded);
isDownloading = false;
c_progress = 100;
pbar.Update(100);
}
}
}

View File

@@ -24,7 +24,7 @@ namespace PluginManager.Others
Message = message;
}
public async void Update(int progress, bool r = false)
public async void Update(int progress, double speed = -1, string unit = null)
{
//progress bar
@@ -53,10 +53,17 @@ namespace PluginManager.Others
Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(progress.ToString() + " of " + Max.ToString() + " ");
if (speed == -1 || unit == null)
{
if (progress == Max)
Console.Write(progress.ToString() + " % ✓");
else Console.Write(progress.ToString() + " % ");
}
else
Console.Write(progress.ToString() + $"{speed} {unit}/s ");
if (r == false)
Update(progress, true);
//if (r == false)
//Update(progress, true);
}