This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
WebHax/WebHax/Pages/Index.razor

198 lines
6.7 KiB
Plaintext

@page "/"
@using System.Xml.Linq
@using Microsoft.AspNetCore.WebUtilities
@using Microsoft.Extensions.Primitives
@inject HttpClient Http
@inject IJSRuntime JsRuntime
@inject NavigationManager NavManager
<h1>UpTool2 WebHax</h1>
Welcome to the WebHax UpTool UI<br>
Select a repository and click load to view contained packages
<input class="form-control" id="repoLink" @bind="Link">
<a class="btn btn-primary @_btnState" href="@CurrentPageLink" @onclick="UpdateApps">Load</a>
@if (_uriSelected)
{
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">App</th>
<th scope="col">Description</th>
<th scope="col">Version</th>
<th scope="col">Hash</th>
<th scope="col">ID</th>
</tr>
</thead>
<tbody>
@foreach (UTApp app in _apps)
{
<tr>
<th scope="row">@(app.Index + 1)</th>
<td><a class="btn btn-secondary" href="@app.File">@app.Name</a></td>
<td>@app.Description</td>
<td>@app.Version</td>
<td>@app.Hash</td>
<td>@app.Id</td>
</tr>
}
</tbody>
</table>
}
<small>
@foreach (string entry in pLog)
{
<br>
@entry
}
</small>
@code {
[Parameter]
public string Link { get; set; } = "";
private string CurrentPageLink => _baseLinkCurrent + (_baseLinkCurrent.EndsWith('/') ? "" : "/") + "?link=" + Uri.EscapeDataString(Link);
private string _baseLinkCurrent = "";
List<UTApp> _apps = new List<UTApp>();
bool _uriSelected;
const string CorsProxy = "https://cors-anywhere.herokuapp.com/";
private string _btnState = "disabled";
private List<string> pLog = new List<string>();
private async Task<IEnumerable<XElement>> FetchRepo()
{
List<string> repos = new List<string>();
List<XElement> apps = new List<XElement>();
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<string> 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<XElement> 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()
{
Uri uri;
if (string.IsNullOrWhiteSpace(Link))
uri = NavManager.ToAbsoluteUri(NavManager.Uri);
else
uri = new Uri(CurrentPageLink);
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("link", out StringValues linkInit))
{
Link = linkInit;
_apps.Clear();
_btnState = "disabled";
NavManager.NavigateTo(CurrentPageLink);
StateHasChanged();
_uriSelected = true;
await ProcessApps(await FetchRepo());
}
else
{
Link = "https://gitlab.com/uptool/UpTool2/-/snippets/1988600/raw/master/Repo.xml";
UpdateApps();
}
_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<string>("console.log", text);
}
private Task<string> Req(string link) => Http.GetStringAsync(CorsProxy + link);
}