fix downloader

This commit is contained in:
2025-12-10 13:38:45 +01:00
parent 22ded9029c
commit 37984f0631

View File

@@ -1,9 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO.Compression; using System.IO.Compression;
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json;
using Windows.Media.Protection.PlayReady;
namespace BlueberryUpdater namespace BlueberryUpdater
{ {
@@ -74,14 +70,12 @@ namespace BlueberryUpdater
string releaseUrl = "https://git.technopunk.space/api/v1/repos/tomi/Blueberry/releases/latest"; string releaseUrl = "https://git.technopunk.space/api/v1/repos/tomi/Blueberry/releases/latest";
// 1. Fetch JSON // 1. Fetch JSON
var root = client.GetFromJsonAsync<JsonElement>(releaseUrl).ConfigureAwait(false).GetAwaiter().GetResult(); var json = client.GetStringAsync(releaseUrl).ConfigureAwait(false).GetAwaiter().GetResult();
json = json.Split("\"name\":\"payload.zip\",")[1];
// 2. Find URL for "payload.zip" // 2. Find URL for "payload.zip"
string downloadUrl = root.GetProperty("assets") string downloadUrl = json.Split("\"browser_download_url\":\"")[1].Split("\"")[0];
.EnumerateArray()
.First(x => x.GetProperty("name").GetString() == "payload.zip")
.GetProperty("browser_download_url")
.GetString() ?? throw new NullReferenceException();
Console.WriteLine("Downloading Blueberry..."); Console.WriteLine("Downloading Blueberry...");
var stream = DownloadFileWithProgressAsync(client, downloadUrl).GetAwaiter().GetResult(); var stream = DownloadFileWithProgressAsync(client, downloadUrl).GetAwaiter().GetResult();
@@ -146,6 +140,8 @@ namespace BlueberryUpdater
static void DrawProgressBar(int percent, string filename) static void DrawProgressBar(int percent, string filename)
{ {
percent = Math.Clamp(percent, 0, 100);
// Move cursor to start of line // Move cursor to start of line
Console.CursorLeft = 0; Console.CursorLeft = 0;
@@ -153,7 +149,7 @@ namespace BlueberryUpdater
string shortName = filename.Length > 20 ? filename.Substring(0, 17) + "..." : filename.PadRight(20); string shortName = filename.Length > 20 ? filename.Substring(0, 17) + "..." : filename.PadRight(20);
Console.Write("["); Console.Write("[");
int width = Console.WindowWidth - 21; // Width of the bar int width = Console.WindowWidth - 31; // Width of the bar
int progress = (int)((percent / 100.0) * width); int progress = (int)((percent / 100.0) * width);
// Draw filled part // Draw filled part
@@ -161,7 +157,7 @@ namespace BlueberryUpdater
// Draw empty part // Draw empty part
Console.Write(new string('-', width - progress)); Console.Write(new string('-', width - progress));
Console.Write($"] {percent}% {shortName}"); Console.Write($"] {percent}% {shortName} ");
} }
static async Task<MemoryStream> DownloadFileWithProgressAsync(HttpClient client, string url) static async Task<MemoryStream> DownloadFileWithProgressAsync(HttpClient client, string url)
@@ -174,7 +170,7 @@ namespace BlueberryUpdater
var canReportProgress = totalBytes != -1; var canReportProgress = totalBytes != -1;
using var contentStream = await response.Content.ReadAsStreamAsync(); using var contentStream = await response.Content.ReadAsStreamAsync();
using var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var buffer = new byte[8192]; var buffer = new byte[8192];
long totalRead = 0; long totalRead = 0;
@@ -189,11 +185,13 @@ namespace BlueberryUpdater
DrawProgressBar((int)((double)totalRead / totalBytes * 100), "Downloading..."); DrawProgressBar((int)((double)totalRead / totalBytes * 100), "Downloading...");
} }
DrawProgressBar(100, "Done"); DrawProgressBar(100, "Done");
Console.WriteLine();
return memoryStream; return memoryStream;
} }
static void ExtractWithProgress(MemoryStream stream) static void ExtractWithProgress(MemoryStream stream)
{ {
using (ZipArchive archive = new(stream)) stream.Position = 0;
using (ZipArchive archive = new(stream, ZipArchiveMode.Read))
{ {
int totalEntries = archive.Entries.Count; int totalEntries = archive.Entries.Count;
int currentEntry = 0; int currentEntry = 0;
@@ -229,6 +227,20 @@ namespace BlueberryUpdater
} }
} }
DrawProgressBar(100, "Done"); DrawProgressBar(100, "Done");
Console.WriteLine();
}
}
public class Dto
{
public class Item
{
public Assets[] assets { get; set; }
}
public class Assets
{
public string name { get; set; }
public string browser_download_url { get; set; }
} }
} }
} }