2021-08-01 16:51:54 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2021-04-21 21:57:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public static class MiscUtils
|
2021-04-21 21:57:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
// discord mediaproxy URLs used to be stored directly in the database, so now we cleanup image urls before using them outside of proxying
|
|
|
|
private static readonly Regex MediaProxyUrl =
|
|
|
|
new(
|
|
|
|
@"^https?://media.discordapp.net/attachments/(\d{17,19})/(\d{17,19})/([^/\\&\?]+)\.(png|jpg|jpeg|webp)(\?.*)?$");
|
|
|
|
|
|
|
|
private static readonly string DiscordCdnReplacement = "https://cdn.discordapp.com/attachments/$1/$2/$3.$4";
|
|
|
|
|
|
|
|
public static bool TryMatchUri(string input, out Uri uri)
|
2021-04-21 21:57:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2021-04-21 21:57:19 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
uri = new Uri(input);
|
|
|
|
if (!uri.IsAbsoluteUri || uri.Scheme != "http" && uri.Scheme != "https")
|
2021-04-21 21:57:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (UriFormatException)
|
2021-08-02 10:22:28 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
uri = null;
|
|
|
|
return false;
|
2021-08-02 10:22:28 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return true;
|
2021-04-21 21:57:19 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
public static string? TryGetCleanCdnUrl(this string? url) =>
|
|
|
|
url == null ? null : MediaProxyUrl.Replace(url, DiscordCdnReplacement);
|
2021-04-21 21:57:19 +00:00
|
|
|
}
|