2024-12-28 08:29:01 +03:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
2025-01-05 17:05:23 +03:00
|
|
|
namespace Nebula.Shared.Utils;
|
2024-12-28 08:29:01 +03:00
|
|
|
|
|
|
|
|
public static class Helper
|
|
|
|
|
{
|
|
|
|
|
public static readonly JsonSerializerOptions JsonWebOptions = new(JsonSerializerDefaults.Web);
|
2025-01-27 15:55:30 +03:00
|
|
|
public static void SafeOpenBrowser(string uri)
|
2024-12-28 08:29:01 +03:00
|
|
|
{
|
2025-01-27 15:55:30 +03:00
|
|
|
if (!Uri.TryCreate(uri, UriKind.Absolute, out var parsedUri))
|
2024-12-28 08:29:01 +03:00
|
|
|
{
|
2025-01-27 15:55:30 +03:00
|
|
|
Console.WriteLine("Unable to parse URI in server-provided link: {Link}", uri);
|
|
|
|
|
return;
|
2024-12-28 08:29:01 +03:00
|
|
|
}
|
2025-01-27 15:55:30 +03:00
|
|
|
|
|
|
|
|
if (parsedUri.Scheme is not ("http" or "https"))
|
2024-12-28 08:29:01 +03:00
|
|
|
{
|
2025-01-27 15:55:30 +03:00
|
|
|
Console.WriteLine("Refusing to open server-provided link {Link}, only http/https are allowed", parsedUri);
|
|
|
|
|
return;
|
2024-12-28 08:29:01 +03:00
|
|
|
}
|
2025-01-27 15:55:30 +03:00
|
|
|
|
|
|
|
|
OpenBrowser(parsedUri.ToString());
|
|
|
|
|
}
|
|
|
|
|
public static void OpenBrowser(string url)
|
|
|
|
|
{
|
|
|
|
|
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
|
2024-12-28 08:29:01 +03:00
|
|
|
}
|
2025-01-14 22:10:16 +03:00
|
|
|
|
2024-12-28 08:29:01 +03:00
|
|
|
public static async Task<T> AsJson<T>(this HttpContent content) where T : notnull
|
|
|
|
|
{
|
|
|
|
|
var str = await content.ReadAsStringAsync();
|
|
|
|
|
return JsonSerializer.Deserialize<T>(str, JsonWebOptions) ??
|
|
|
|
|
throw new JsonException("AsJson: did not expect null response");
|
|
|
|
|
}
|
|
|
|
|
}
|