Files
redirect/Redirects.cs
2026-05-01 07:36:46 -05:00

125 lines
4.7 KiB
C#

using System.Text.Json.Serialization;
class Redirect
{
private static Dictionary<string,IRedirect> redirects = new Dictionary<string, IRedirect>
{
// CrossLang Shell
{"crosslang-shell",Gitea("https://git.tesses.org/","tesses50/crosslangextras","ShellPackage.crvm",true)},
// TessesFramework
{"tessesframework",Std("https://git.tesses.org/tesses50/tessesframework")},
{"tf-tools-aarch64-linux",TF("tessesframework-tools-aarch64-linux-musl.tar.gz")},
{"tf-tools-aarch64-macos",TF("tessesframework-tools-aarch64-macos-none.tar.gz")},
{"tf-tools-aarch64-windows",TF("tessesframework-tools-aarch64-windows-gnu.zip")},
{"tf-tools-arm-linux",TF("tessesframework-tools-arm-linux-musleabi.tar.gz")},
{"tf-tools-powerpc-linux",TF("tessesframework-tools-powerpc-linux-musleabihf.tar.gz")},
{"tf-tools-riscv64-linux",TF("tessesframework-tools-riscv64-linux-musl.tar.gz")},
{"tf-tools-x86-linux",TF("tessesframework-tools-x86-linux-musl.tar.gz")},
{"tf-tools-x86-windows",TF("tessesframework-tools-x86-windows-gnu.zip")},
{"tf-tools-x86_64-linux",TF("tessesframework-tools-x86_64-linux-musl.tar.gz")},
{"tf-tools-x86_64-macos",TF("tessesframework-tools-x86_64-macos-none.tar.gz")},
{"tf-tools-x86_64-windows",TF("tessesframework-tools-x86_64-windows-gnu.zip")},
{"cl-slim-aarch64-linux-musl", CL("crosslang-slim-aarch64-linux-musl")},
{"cl-slim-aarch64-macos-none", CL("crosslang-slim-aarch64-macos-none")},
{"cl-slim-aarch64-windows-gnu", CL("crosslang-slim-aarch64-windows-gnu.exe")},
{"cl-slim-arm-linux-musleabi", CL("crosslang-slim-arm-linux-musleabi")},
{"cl-slim-powerpc-linux-musleabihf", CL("crosslang-slim-powerpc-linux-musleabihf")},
{"cl-slim-riscv64-linux-musl", CL("crosslang-slim-riscv64-linux-musl")},
{"cl-slim-x86-linux-musl", CL("crosslang-slim-x86-linux-musl")},
{"cl-slim-x86-windows-gnu", CL("crosslang-slim-x86-windows-gnu.exe")},
{"cl-slim-x86_64-linux-musl", CL("crosslang-slim-x86_64-linux-musl")},
{"cl-slim-x86_64-macos-none", CL("crosslang-slim-x86_64-macos-none")},
{"cl-slim-x86_64-windows-gnu", CL("crosslang-slim-x86_64-windows-gnu.exe")},
};
private static IRedirect Std(string url)
{
return new StdRedirect(url);
}
private static IRedirect Gitea(string giteaServer, string giteaProject, string file, bool prerelease = false)
{
return new GiteaReleaseRedirect(giteaServer,giteaProject,file,prerelease);
}
private static IRedirect TF(string filename)
{
return Gitea(
"https://git.tesses.org/",
"tesses50/tessesframework",
filename,
true
);
}
private static IRedirect CL(string filename)
{
return Gitea(
"https://git.tesses.org/",
"tesses50/crosslang",
filename,
true
);
}
public static async Task<string?> HandleRedirectAsync(string redir)
{
if(redirects.ContainsKey(redir))
return await redirects[redir].RedirectAsync();
return null;
}
}
interface IRedirect
{
public Task<string?> RedirectAsync();
}
public class StdRedirect(string url) : IRedirect
{
public async Task<string?> RedirectAsync()
{
return await Task.FromResult(url);
}
}
public class GiteaReleaseRedirect(string giteaServer, string giteaProject, string file, bool prerelease = false) : IRedirect
{
private static HttpClient client =new HttpClient();
public async Task<string?> RedirectAsync()
{
// https://git.tesseslanguage.com/api/v1/repos/tesses50/crosslang/releases?pre-release=true&limit=1
var resp = await client.GetFromJsonAsync<List<_release>>($"{giteaServer.TrimEnd('/')}/api/v1/repos/{System.Web.HttpUtility.UrlPathEncode(giteaProject)}/releases?pre-release={(prerelease ? "true":"false")}&limit=1");
if(resp is not null)
{
var resp2 = resp.FirstOrDefault();
if(resp2 is not null)
{
foreach(var item in resp2.Assets)
{
if(item.Name == file)
return item.Url;
}
}
}
return null;
}
private class _release
{
[JsonPropertyName("assets")]
public List<_asset> Assets {get;set;}=new List<_asset>();
public class _asset
{
[JsonPropertyName("name")]
public string Name {get;set;}="";
[JsonPropertyName("browser_download_url")]
public string Url {get;set;}="";
}
}
}