Fix URL handling when the filename contains multiple known extensions (#196)

This commit is contained in:
kittens 2020-07-18 06:51:31 -04:00 committed by GitHub
parent e7f95c9ba6
commit 80e6d5b18e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,7 @@ namespace PluralKit.Bot
// Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files // Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files
// This lets us add resizing parameters to "borrow" their media proxy server to downsize the image // 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! // which in turn makes it more likely to be underneath the size limit!
private static readonly Regex DiscordCdnUrl = new Regex(@"^https?://(?:cdn\.discordapp\.com|media\.discordapp\.net)/attachments/(\d{17,19})/(\d{17,19})/([^\.?/]+)\.(png|jpg|jpeg).*"); private static readonly Regex DiscordCdnUrl = new Regex(@"^https?://(?:cdn\.discordapp\.com|media\.discordapp\.net)/attachments/(\d{17,19})/(\d{17,19})/([^/\\&\?]+)\.(png|jpg|jpeg)(\?.*)?$");
private static readonly string DiscordMediaUrlReplacement = "https://media.discordapp.net/attachments/$1/$2/$3.$4?width=256&height=256"; private static readonly string DiscordMediaUrlReplacement = "https://media.discordapp.net/attachments/$1/$2/$3.$4?width=256&height=256";
public static async Task<ParsedImage?> MatchImage(this Context ctx) public static async Task<ParsedImage?> MatchImage(this Context ctx)
@ -39,7 +39,6 @@ namespace PluralKit.Bot
if (uri.Scheme != "http" && uri.Scheme != "https") if (uri.Scheme != "http" && uri.Scheme != "https")
throw Errors.InvalidUrl(arg); throw Errors.InvalidUrl(arg);
return new ParsedImage {Url = TryRewriteCdnUrl(uri.ToString()), Source = AvatarSource.Url}; return new ParsedImage {Url = TryRewriteCdnUrl(uri.ToString()), Source = AvatarSource.Url};
} }