DRY-ify checking URL length for avatars

This commit is contained in:
spiral 2020-11-22 15:43:38 -05:00
parent 7f82a3e63a
commit 05f1ee92ea
No known key found for this signature in database
GPG Key ID: 00C26F208D3FCCAB
4 changed files with 4 additions and 12 deletions

View File

@ -177,8 +177,6 @@ namespace PluralKit.Bot
{ {
ctx.CheckOwnGroup(target); ctx.CheckOwnGroup(target);
if (img.Url.Length > Limits.MaxUriLength)
throw Errors.InvalidUrl(img.Url);
await AvatarUtils.VerifyAvatarOrThrow(img.Url); await AvatarUtils.VerifyAvatarOrThrow(img.Url);
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url})); await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));

View File

@ -102,18 +102,11 @@ namespace PluralKit.Bot
} }
ctx.CheckSystem().CheckOwnMember(target); ctx.CheckSystem().CheckOwnMember(target);
await ValidateUrl(avatarArg.Value.Url); await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Value.Url);
await UpdateAvatar(location, ctx, target, avatarArg.Value.Url); await UpdateAvatar(location, ctx, target, avatarArg.Value.Url);
await PrintResponse(location, ctx, target, avatarArg.Value, guildData); await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
} }
private static Task ValidateUrl(string url)
{
if (url.Length > Limits.MaxUriLength)
throw Errors.InvalidUrl(url);
return AvatarUtils.VerifyAvatarOrThrow(url);
}
private Task PrintResponse(AvatarLocation location, Context ctx, PKMember target, ParsedImage avatar, private Task PrintResponse(AvatarLocation location, Context ctx, PKMember target, ParsedImage avatar,
MemberGuildSettings? targetGuildData) MemberGuildSettings? targetGuildData)
{ {

View File

@ -140,8 +140,6 @@ namespace PluralKit.Bot
async Task SetIcon(ParsedImage img) async Task SetIcon(ParsedImage img)
{ {
if (img.Url.Length > Limits.MaxUriLength)
throw Errors.InvalidUrl(img.Url);
await AvatarUtils.VerifyAvatarOrThrow(img.Url); await AvatarUtils.VerifyAvatarOrThrow(img.Url);
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url})); await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url}));

View File

@ -11,6 +11,9 @@ namespace PluralKit.Bot {
public static class AvatarUtils { public static class AvatarUtils {
public static async Task VerifyAvatarOrThrow(string url) public static async Task VerifyAvatarOrThrow(string url)
{ {
if (url.Length > Limits.MaxUriLength)
throw Errors.UrlTooLong(url);
// List of MIME types we consider acceptable // List of MIME types we consider acceptable
var acceptableMimeTypes = new[] var acceptableMimeTypes = new[]
{ {