@page "/" @using System.Xml.Linq @using Microsoft.AspNetCore.WebUtilities @using Microsoft.Extensions.Primitives @inject HttpClient Http @inject IJSRuntime JsRuntime @inject NavigationManager NavManager

UpTool2 WebHax

Welcome to the WebHax UpTool UI
Select a repository and click load to view contained packages Load @if (_uriSelected) { @foreach (UTApp app in _apps) { }
# App Description Version Hash ID
@(app.Index + 1) @app.Name @app.Description @app.Version @app.Hash @app.Id
} @foreach (string entry in pLog) {
@entry }
@code { [Parameter] public string Link { get; set; } = "https://gitlab.com/JFronny/UpTool2/snippets/1988600/raw"; private string CurrentPageLink => _baseLinkCurrent + "?link=" + Uri.EscapeDataString(Link); private string _baseLinkCurrent = ""; List _apps = new List(); bool _uriSelected; const string CorsProxy = "https://cors-anywhere.herokuapp.com/"; private string _btnState = "disabled"; private List pLog = new List(); private async Task> FetchRepo() { List repos = new List(); List apps = new List(); repos.Add(Link); int i = 0; while (i < repos.Count) { try { await Log(CorsProxy + repos[i]); string resp = await Req(repos[i]); XDocument doc = XDocument.Parse(resp); repos.AddRange(doc.Element("repo").Elements("repolink").Select(s => s.Value) .Where(s => !repos.Contains(s))); XElement[] tmpApparray = doc.Element("repo").Elements("app").Where(app => !apps.Any(a => a.Element("ID").Value == app.Element("ID").Value) || !apps .Where(a => a.Element("ID").Value == app.Element("ID").Value).Any(a => GetVer(a.Element("Version")) >= GetVer(app.Element("Version")))).ToArray(); foreach (Task task in doc.Element("repo").Elements("applink") .Select(async s => { await Log($"- Loading {s.Value}"); return await Req(s.Value); //XDocument.Parse(req(s.Value).Result).Element("app"); })) tmpApparray = tmpApparray.Concat(new []{XDocument.Parse(await task).Element("app")}).ToArray(); foreach (XElement app in tmpApparray) { //"Sanity check" Version.Parse(app.Element("Version").Value); Guid.Parse(app.Element("ID").Value); //Create XElement apps.Add(new XElement("App", new XElement("Name", app.Element("Name").Value), new XElement("Description", app.Element("Description").Value), new XElement("Version", app.Element("Version").Value), new XElement("ID", app.Element("ID").Value), new XElement("File", app.Element("File").Value), new XElement("Hash", app.Element("Hash").Value) )); //Check for duplicates if (apps.Count(a => a.Element("ID").Value == app.Element("ID").Value) > 1) apps.Where(a => a.Element("ID").Value == app.Element("ID").Value).Reverse() .Skip(1) .ToList().ForEach(a => apps.Remove(a)); } await Log("Fetched metadata"); } catch (Exception e) { await Log($"ERROR: {e}"); } i++; } return apps; } private async Task ProcessApps(IEnumerable apps) { try { int i = 0; foreach (XElement app in apps) { _apps.Add(new UTApp( app.Element("Name").Value, app.Element("Description").Value, Version.Parse(app.Element("Version").Value), app.Element("File").Value, app.Element("Hash").Value, Guid.Parse(app.Element("ID").Value), i)); i++; } await Log("App objects created"); } catch (Exception e) { await Log($"ERROR: {e}"); } } protected override async Task OnInitializedAsync() { await Log("Initialized JFronny log engine"); Uri uri = NavManager.ToAbsoluteUri(NavManager.Uri); _baseLinkCurrent = uri.ToString(); if (_baseLinkCurrent.Contains('?')) _baseLinkCurrent = _baseLinkCurrent.Substring(0, _baseLinkCurrent.IndexOf('?')); await UpdateApps(); } private async Task UpdateApps() { NavManager.NavigateTo(CurrentPageLink); Uri uri = new Uri(CurrentPageLink); if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("link", out StringValues linkInit)) { Link = linkInit; _apps.Clear(); _btnState = "disabled"; StateHasChanged(); _uriSelected = true; await ProcessApps(await FetchRepo()); _btnState = ""; } pLog.Clear(); } private static Version GetVer(XElement el) => int.TryParse(el.Value, out int i) ? new Version(0, 0, 0, i) : Version.Parse(el.Value); private async Task Log(string text) { pLog.Add(text); StateHasChanged(); await JsRuntime.InvokeAsync("console.log", text); } private Task Req(string link) => Http.GetStringAsync(CorsProxy + link); }