using Spectre.Console; using Serilog; namespace EdgeInstaller; public static partial class EdgeInstall { private static async Task GetRelease() { string releaseURL = "dists/stable/Release"; using HttpResponseMessage response = await _client.GetAsync(releaseURL); await Task.Delay(5000); Log.Information("Getting release from {url}", response.Headers.Location); response.EnsureSuccessStatusCode(); //WARN: this can throw an exception, Please wrap in try/catch or rewrite function var body = await response.Content.ReadAsStringAsync(); Log.Debug("{body}", body); return body; } static async Task downloadFile(string URL, string DestinationDirectory) { Uri file = new Uri(URL); string fileLocation = Path.Combine(DestinationDirectory, file.Segments.Last()); using HttpResponseMessage response = await _client.GetAsync(URL); response.EnsureSuccessStatusCode(); await Task.Delay(2000); var fileData = response.Content.ReadAsStreamAsync(); using var fileStream = new FileStream(fileLocation, FileMode.OpenOrCreate); await response.Content.CopyToAsync(fileStream); return fileLocation; } // This overload of downloadFile allows you to get the progess static async Task downloadFile(string URL, string DestinationDirectory, ProgressTask task) { Uri file = new Uri(URL, UriKind.RelativeOrAbsolute); // allow this to work regardless of relativity if (!file.IsAbsoluteUri && _client.BaseAddress is not null) { // Ego te absolvo file = new Uri(_client.BaseAddress, URL); } string fileLocation = Path.Combine(DestinationDirectory, file.Segments.Last()); using (HttpResponseMessage response = await _client.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); task.MaxValue = response.Content.Headers.ContentLength ?? 0; task.StartTask(); await Task.Delay(2000); AnsiConsole.MarkupLineInterpolated($"Starting download of [u]{URL}[/] ({task.MaxValue} Bytes)"); using (var contentStream = await response.Content.ReadAsStreamAsync()) using (var fileStream = File.Open(fileLocation, FileMode.OpenOrCreate)) { var buffer = new byte[downloadBufferBYTES]; while (true) { var read = await contentStream.ReadAsync(buffer, 0, buffer.Length); await Task.Delay(downloadDelayMS); if (read == 0) { AnsiConsole.MarkupLineInterpolated($"Download of [u]{URL}[/] [green b]Complete![/]"); break; } task.Increment(read); await fileStream.WriteAsync(buffer, 0, read); } } } return fileLocation; } // This overload of downloadFile allows you to get the progess static async Task downloadStream(string URL, ProgressTask task) { MemoryStream outputStream = new(); using (HttpResponseMessage response = await _client.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); task.MaxValue = response.Content.Headers.ContentLength ?? 0; task.StartTask(); await Task.Delay(2000); AnsiConsole.MarkupLineInterpolated($"Starting download of [u]{URL}[/] to memory ({task.MaxValue} Bytes)"); int bufferSize = (int)Math.Round(task.MaxValue / 100); using (var contentStream = await response.Content.ReadAsStreamAsync()) { var buffer = new byte[bufferSize]; while (true) { var read = await contentStream.ReadAsync(buffer, 0, buffer.Length); await Task.Delay(downloadDelayMS); if (read == 0) { AnsiConsole.MarkupLineInterpolated($"Download of [u]{URL}[/] [green b]Complete![/]"); break; } task.Increment(read); await outputStream.WriteAsync(buffer, 0, read); } } } return outputStream; } }