mirror of
https://github.com/JuniorDark/RustyHearts-Launcher.git
synced 2026-05-07 05:21:44 -04:00
Version 1.2.0
This commit is contained in:
parent
9b3a0e00a2
commit
e74d93fab9
83 changed files with 110087 additions and 47507 deletions
69
RHLauncher.Http/DownloadHelper.cs
Normal file
69
RHLauncher.Http/DownloadHelper.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using RHLauncher.RHLauncher.Helper;
|
||||
using System.Net;
|
||||
|
||||
namespace RHLauncher.RHLauncher.Http
|
||||
{
|
||||
public class DownloadHelper
|
||||
{
|
||||
public static async Task<long> GetFileSizeAsync(HttpClient client, string fileUrl, CancellationToken cancellationToken)
|
||||
{
|
||||
using HttpRequestMessage request = new(HttpMethod.Get, fileUrl);
|
||||
|
||||
try
|
||||
{
|
||||
using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return response.Content.Headers.ContentLength ?? throw new Exception("Content-Length header not found in response headers.");
|
||||
}
|
||||
else if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
Logger.WriteLog($"File not found: {fileUrl}");
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.WriteLog($"Error getting file size for {fileUrl}: {response.StatusCode}");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
Logger.WriteLog($"File not found: {fileUrl}");
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLog($"Error getting file size for {fileUrl}: {ex.Message}");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static string FormatDownloadSpeed(double totalDownloadSpeed)
|
||||
{
|
||||
string[] sizes = { "B/s", "KB/s", "MB/s", "GB/s" };
|
||||
int order = 0;
|
||||
while (totalDownloadSpeed >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
totalDownloadSpeed /= 1024;
|
||||
}
|
||||
|
||||
return $"{totalDownloadSpeed:0.#} {sizes[order]}";
|
||||
}
|
||||
|
||||
public static string FormatFileSize(long bytes)
|
||||
{
|
||||
string[] sizes = { "B", "KB", "MB", "GB" };
|
||||
int order = 0;
|
||||
while (bytes >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
bytes /= 1024;
|
||||
}
|
||||
|
||||
return $"{bytes:0.#} {sizes[order]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue