Download progress is now shown in MBit/s

This commit is contained in:
HorizonCode 2021-05-26 08:09:30 +02:00
parent 69ca2f91b9
commit 6dc54f175e
1 changed files with 45 additions and 5 deletions

View File

@ -12,7 +12,7 @@ namespace EZPPClient_Installer
public class DownloadUtil
{
public static List<string> FilesToDownload(string pathToEZPP)
public static List<string> FilesToDownload(string pathToEZPP, bool reinstall)
{
bool directoryExists = Directory.Exists(pathToEZPP);
@ -49,7 +49,7 @@ namespace EZPPClient_Installer
}.ShowAsync();
}
if (!directoryExists)
if (!directoryExists || reinstall)
{
#if DEBUG
MessageBox.Show("Folder does not exist: " + pathToEZPP);
@ -93,7 +93,7 @@ namespace EZPPClient_Installer
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
foreach(string file in files)
foreach (string file in files)
if (File.Exists(path + @"\" + file))
File.Delete(path + @"\" + file);
@ -104,17 +104,57 @@ namespace EZPPClient_Installer
}
}
private static async Task DownloadFile(string filename, string path)
{
long last = -1;
long lastTransferRate = -1;
List<long> byteTransfer = new List<long>();
try
{
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "EZPPClientInstaller");
wc.DownloadProgressChanged += (sender, value) => MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((value.BytesReceived / 1024000D), 2) + "MB/" + Math.Round((value.TotalBytesToReceive / 1024000D), 2) + "MB", value.ProgressPercentage);
wc.DownloadProgressChanged += (sender, value) =>
{
var bytesReceived = value.BytesReceived;
var totalBytes = value.TotalBytesToReceive;
var currentMillis = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var diffMillis = Math.Abs(currentMillis - last);
if (diffMillis >= 1000 || last == -1)
{
if (lastTransferRate != -1)
{
long transfer = Math.Abs(lastTransferRate - bytesReceived);
byteTransfer.Add(transfer);
if (byteTransfer.Count > 5)
byteTransfer.RemoveAt(0);
}
lastTransferRate = bytesReceived;
last = DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
long averageTransferRate = 0;
if (byteTransfer.Count > 0)
{
foreach (long tr in byteTransfer)
{
averageTransferRate += tr;
}
averageTransferRate /= byteTransfer.Count;
}
MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((averageTransferRate / 1024000D) * 1.8, 2) + "MBit/s", value.ProgressPercentage);
};
await wc.DownloadFileTaskAsync(@"https://new.ez-pp.farm/ezppclient?file=" + filename, path + @"\" + filename);
}
}catch(WebException)
}
catch (WebException)
{
}
}