fix(bot): avoid 403 status codes on image upload (#611)

This commit is contained in:
Jake Fulmine 2024-02-09 04:38:17 +01:00 committed by GitHub
parent 823db4cd24
commit 8befb1c857
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 18 deletions

View file

@ -63,11 +63,21 @@ public static class AvatarUtils
// This lets us add resizing parameters to "borrow" their media proxy server to downsize the image
// which in turn makes it more likely to be underneath the size limit!
private static readonly Regex DiscordCdnUrl =
new(@"^https?://(?:cdn\.discordapp\.com|media\.discordapp\.net)/attachments/(\d{17,19})/(\d{17,19})/([^/\\&\?]+)\.(png|jpg|jpeg|webp)(\?.*)?$");
new(@"^https?://(?:cdn\.discordapp\.com|media\.discordapp\.net)/attachments/(\d{17,19})/(\d{17,19})/([^/\\&\?]+)\.(png|jpg|jpeg|webp)(?:\?(?<query>.*))?$");
private static readonly string DiscordMediaUrlReplacement =
"https://media.discordapp.net/attachments/$1/$2/$3.$4?width=256&height=256";
public static string? TryRewriteCdnUrl(string? url) =>
url == null ? null : DiscordCdnUrl.Replace(url, DiscordMediaUrlReplacement);
public static string? TryRewriteCdnUrl(string? url)
{
if (url == null)
return null;
var match = DiscordCdnUrl.Match(url);
var newUrl = DiscordCdnUrl.Replace(url, DiscordMediaUrlReplacement);
if (match.Groups["query"].Success)
newUrl += "&" + match.Groups["query"].Value;
return newUrl;
}
}