Files
NebulaLauncher/Nebula.UpdateResolver/Rest/Helper.cs

32 lines
1.1 KiB
C#

using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace Nebula.UpdateResolver.Rest;
public static class Helper
{
public static readonly JsonSerializerOptions JsonWebOptions = new(JsonSerializerDefaults.Web);
public static async Task<T> AsJson<T>(this HttpContent content) where T : notnull
{
var str = await content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(str, JsonWebOptions) ??
throw new JsonException("AsJson: did not expect null response");
}
public static void CopyTo(this Stream input, Stream output, string fileName, long totalLength)
{
const int bufferSize = 81920;
var buffer = new byte[bufferSize];
long totalRead = 0;
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
totalRead += bytesRead;
LogStandalone.Log($"Saving {fileName}", (int)(((float)totalLength / totalRead) * 100));
}
}
}