Move mediaproxy URL rewriting to ProxyService
This shows full size avatars in API / cards. Also, rewrite URLs currently stored with media.discordapp.net "back" to cdn.discordapp.com before sending them to users.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Extensions;
|
||||
@@ -11,12 +10,6 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public static class ContextAvatarExt
|
||||
{
|
||||
// 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
|
||||
// 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|webp)(\?.*)?$");
|
||||
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)
|
||||
{
|
||||
// If we have a user @mention/ID, use their avatar
|
||||
@@ -41,13 +34,13 @@ namespace PluralKit.Bot
|
||||
throw Errors.InvalidUrl(arg);
|
||||
|
||||
// ToString URL-decodes, which breaks URLs to spaces; AbsoluteUri doesn't
|
||||
return new ParsedImage {Url = TryRewriteCdnUrl(uri.AbsoluteUri), Source = AvatarSource.Url};
|
||||
return new ParsedImage {Url = uri.AbsoluteUri, Source = AvatarSource.Url};
|
||||
}
|
||||
|
||||
// If we have an attachment, use that
|
||||
if (ctx.Message.Attachments.FirstOrDefault() is {} attachment)
|
||||
{
|
||||
var url = TryRewriteCdnUrl(attachment.ProxyUrl);
|
||||
var url = attachment.ProxyUrl;
|
||||
return new ParsedImage {Url = url, Source = AvatarSource.Attachment};
|
||||
}
|
||||
|
||||
@@ -55,9 +48,6 @@ namespace PluralKit.Bot
|
||||
// and if there are no attachments (which would have been caught just before)
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string TryRewriteCdnUrl(string url) =>
|
||||
DiscordCdnUrl.Replace(url, DiscordMediaUrlReplacement);
|
||||
}
|
||||
|
||||
public struct ParsedImage
|
||||
|
@@ -227,7 +227,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
var eb = new EmbedBuilder()
|
||||
.Title("Group icon")
|
||||
.Image(new(target.Icon));
|
||||
.Image(new(target.Icon.TryGetCleanCdnUrl()));
|
||||
|
||||
if (target.System == ctx.System?.Id)
|
||||
{
|
||||
|
@@ -154,7 +154,7 @@ namespace PluralKit.Bot
|
||||
profile.Append($"\n**Created on:** {created.FormatZoned(zone)}");
|
||||
|
||||
if (opts.IncludeAvatar && m.AvatarFor(lookupCtx) is {} avatar)
|
||||
profile.Append($"\n**Avatar URL:** {avatar}");
|
||||
profile.Append($"\n**Avatar URL:** {avatar.TryGetCleanCdnUrl()}");
|
||||
|
||||
if (m.DescriptionFor(lookupCtx) is {} desc)
|
||||
profile.Append($"\n\n{desc}");
|
||||
|
@@ -60,7 +60,7 @@ namespace PluralKit.Bot
|
||||
|
||||
var eb = new EmbedBuilder()
|
||||
.Title($"{target.NameFor(ctx)}'s {field}")
|
||||
.Image(new(currentValue));
|
||||
.Image(new(currentValue?.TryGetCleanCdnUrl()));
|
||||
if (target.System == ctx.System?.Id)
|
||||
eb.Description($"To clear, use `pk;member {target.Reference()} {cmd} clear`.");
|
||||
await ctx.Reply(embed: eb.Build());
|
||||
|
@@ -203,7 +203,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
var eb = new EmbedBuilder()
|
||||
.Title("System icon")
|
||||
.Image(new(ctx.System.AvatarUrl))
|
||||
.Image(new(ctx.System.AvatarUrl.TryGetCleanCdnUrl()))
|
||||
.Description("To clear, use `pk;system icon clear`.");
|
||||
await ctx.Reply(embed: eb.Build());
|
||||
}
|
||||
|
Reference in New Issue
Block a user