WIP: revise avatars service
Some checks failed
Build and push Docker image / .net docker build (push) Has been cancelled
.net checks / run .net tests (push) Has been cancelled
.net checks / dotnet-format (push) Has been cancelled
Build and push Rust service Docker images / rust docker build (push) Has been cancelled
rust checks / cargo fmt (push) Has been cancelled

This commit is contained in:
asleepyskye 2026-01-24 11:43:05 -05:00
parent 0a474c43eb
commit f69587ceaf
26 changed files with 912 additions and 202 deletions

View file

@ -290,8 +290,8 @@ public class Groups
{
ctx.CheckOwnGroup(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Avatar);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url);
await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { Icon = img.CleanUrl ?? img.Url });
@ -365,8 +365,8 @@ public class Groups
{
ctx.CheckOwnGroup(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url, true);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Banner);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url, true);
await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -81,9 +81,9 @@ public class Member
uriBuilder.Query = "";
img.CleanUrl = uriBuilder.Uri.AbsoluteUri;
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Avatar);
await _avatarHosting.VerifyAvatarOrThrow(img.Url);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url);
await ctx.Repository.UpdateMember(member.Id, new MemberPatch { AvatarUrl = img.CleanUrl ?? img.Url }, conn);
dispatchData.Add("avatar_url", img.CleanUrl);

View file

@ -158,8 +158,8 @@ public class MemberAvatar
ctx.CheckSystem().CheckOwnMember(target);
avatarArg = await _avatarHosting.TryRehostImage(avatarArg.Value, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(avatarArg.Value.Url);
avatarArg = await _avatarHosting.TryRehostImage(ctx, avatarArg.Value, AvatarHostingService.RehostedImageType.Avatar);
await _avatarHosting.VerifyAvatarOrThrow(ctx, avatarArg.Value.Url);
await UpdateAvatar(location, ctx, target, avatarArg.Value.CleanUrl ?? avatarArg.Value.Url);
await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
}

View file

@ -230,8 +230,8 @@ public class MemberEdit
async Task SetBannerImage(ParsedImage img)
{
ctx.CheckOwnMember(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url, true);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Banner);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url, true);
await ctx.Repository.UpdateMember(target.Id, new MemberPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -573,8 +573,8 @@ public class SystemEdit
{
ctx.CheckOwnSystem(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Avatar);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url);
await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { AvatarUrl = img.CleanUrl ?? img.Url });
@ -660,8 +660,8 @@ public class SystemEdit
{
ctx.CheckOwnSystem(target);
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Avatar);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url);
await ctx.Repository.UpdateSystemGuild(target.Id, ctx.Guild.Id, new SystemGuildPatch { AvatarUrl = img.CleanUrl ?? img.Url });
@ -782,8 +782,8 @@ public class SystemEdit
else if (await ctx.MatchImage() is { } img)
{
img = await _avatarHosting.TryRehostImage(img, AvatarHostingService.RehostedImageType.Banner, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(img.Url, true);
img = await _avatarHosting.TryRehostImage(ctx, img, AvatarHostingService.RehostedImageType.Banner);
await _avatarHosting.VerifyAvatarOrThrow(ctx, img.Url, true);
await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { BannerImage = img.CleanUrl ?? img.Url });

View file

@ -1,4 +1,5 @@
using PluralKit.Core;
using Serilog;
using System.Net;
using System.Net.Http.Json;
@ -18,7 +19,7 @@ public class AvatarHostingService
};
}
public async Task VerifyAvatarOrThrow(string url, bool isBanner = false)
public async Task VerifyAvatarOrThrow(Context ctx, string url, bool isBanner = false)
{
if (url.Length > Limits.MaxUriLength)
throw Errors.UrlTooLong(url);
@ -35,6 +36,7 @@ public class AvatarHostingService
return;
var kind = isBanner ? "banner" : "avatar";
if (ctx.Premium) kind = "premium_" + kind;
try
{
@ -56,11 +58,11 @@ public class AvatarHostingService
}
}
public async Task<ParsedImage> TryRehostImage(ParsedImage input, RehostedImageType type, ulong userId, PKSystem? system)
public async Task<ParsedImage> TryRehostImage(Context ctx, ParsedImage input, RehostedImageType type)
{
try
{
var uploaded = await TryUploadAvatar(input.Url, type, userId, system);
var uploaded = await TryUploadAvatar(ctx, input.Url, type);
if (uploaded != null)
{
// todo: make new image type called Cdn?
@ -78,7 +80,7 @@ public class AvatarHostingService
}
}
public async Task<string?> TryUploadAvatar(string? avatarUrl, RehostedImageType type, ulong userId, PKSystem? system)
public async Task<string?> TryUploadAvatar(Context ctx, string? avatarUrl, RehostedImageType type)
{
if (!AvatarUtils.IsDiscordCdnUrl(avatarUrl))
return null;
@ -93,8 +95,10 @@ public class AvatarHostingService
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
if (ctx.Premium) kind = "premium_" + kind;
var response = await _client.PostAsJsonAsync(_config.AvatarServiceUrl + "/pull",
new { url = avatarUrl, kind, uploaded_by = userId, system_id = system?.Uuid.ToString() });
new { url = avatarUrl, kind, uploaded_by = ctx.Author.Id, system_id = ctx.System.Uuid.ToString() });
if (response.StatusCode != HttpStatusCode.OK)
{
var error = await response.Content.ReadFromJsonAsync<ErrorResponse>();

View file

@ -25,6 +25,9 @@ public static class AvatarUtils
if (match.Groups["query"].Success)
newUrl += "&" + match.Groups["query"].Value;
//if it's our cdn, add proxy=true to the end to allow for proxy image resizing
if (url.StartsWith("https://cdn.pluralkit.me")) newUrl += "?proxy=true";
return newUrl;
}