edge-install/Program.Downloaders.cs
Robert Morrison 60e06b2144
Another bad commit
Lots of changes here...

- Removed code using RecursivExtractor due to bad usage of /tmp
  Note: the code that used RecursiveExtractor may not have been in the
  previous commit
- Created _functioning_ implementation of DebUnpacker
- Restructured Project layout.

TODO:
- Possibly rewrite how the DebUnpacker works to have the file contents
  part of an entry.
- Further restructuring and refactoring to make the code a little
  neater.
- Add code for the final unpack steps Un-XZ -> untar -> write to disk
- Add code to install
- Add code for modified post-install pre-remove etc..
  This is kinda needed to ensure proper system integration of new
  packages.
2023-06-08 04:36:31 +01:00

127 lines
4.4 KiB
C#

using Spectre.Console;
using Serilog;
namespace EdgeInstaller;
public static partial class EdgeInstall
{
private static async Task<string> 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<string> 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<string> 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<MemoryStream> 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;
}
}