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:
spiral
2021-08-01 12:51:54 -04:00
parent 2bb8c084c9
commit dcc15dc847
15 changed files with 44 additions and 35 deletions

View File

@@ -29,6 +29,6 @@ namespace PluralKit.Core
group.DescriptionPrivacy.Get(ctx, group.Description);
public static string? IconFor(this PKGroup group, LookupContext ctx) =>
group.IconPrivacy.Get(ctx, group.Icon);
group.IconPrivacy.Get(ctx, group.Icon?.TryGetCleanCdnUrl());
}
}

View File

@@ -58,7 +58,7 @@ namespace PluralKit.Core {
member.NamePrivacy.Get(ctx, member.Name, member.DisplayName ?? member.Name);
public static string AvatarFor(this PKMember member, LookupContext ctx) =>
member.AvatarPrivacy.Get(ctx, member.AvatarUrl);
member.AvatarPrivacy.Get(ctx, member.AvatarUrl.TryGetCleanCdnUrl());
public static string DescriptionFor(this PKMember member, LookupContext ctx) =>
member.DescriptionPrivacy.Get(ctx, member.Description);

View File

@@ -42,7 +42,7 @@ namespace PluralKit.Core
Birthday = m.Birthday?.FormatExport(),
Pronouns = m.Pronouns,
Color = m.Color,
AvatarUrl = m.AvatarUrl,
AvatarUrl = m.AvatarUrl.TryGetCleanCdnUrl(),
ProxyTags = m.ProxyTags,
KeepProxy = m.KeepProxy,
Created = m.Created.FormatExport(),
@@ -127,7 +127,7 @@ namespace PluralKit.Core
var patch = new SystemPatch {Name = data.Name};
if (data.Description != null) patch.Description = data.Description;
if (data.Tag != null) patch.Tag = data.Tag;
if (data.AvatarUrl != null) patch.AvatarUrl = data.AvatarUrl;
if (data.AvatarUrl != null) patch.AvatarUrl = data.AvatarUrl.TryGetCleanCdnUrl();
if (data.TimeZone != null) patch.UiTz = data.TimeZone ?? "UTC";
await _repo.UpdateSystem(conn, system.Id, patch);

View File

@@ -101,7 +101,7 @@ namespace PluralKit.Core
Description = patch.Description.Value,
Pronouns = patch.Pronouns.Value,
Color = patch.Color.Value,
AvatarUrl = patch.AvatarUrl.Value,
AvatarUrl = patch.AvatarUrl.Value?.TryGetCleanCdnUrl(),
KeepProxy = patch.KeepProxy.Value,
ProxyTags = patch.ProxyTags.Value,
Birthday = patch.Birthday.Value,

View File

@@ -1,4 +1,5 @@
using System;
using System.Text.RegularExpressions;
namespace PluralKit.Core
{
@@ -20,5 +21,12 @@ namespace PluralKit.Core
return true;
}
// 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 Regex(@"^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 string TryGetCleanCdnUrl(this string url) =>
MediaProxyUrl.Replace(url, DiscordCdnReplacement);
}
}