From 05f1ee92ea9790217c2b86cef141ce5a71b17281 Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 22 Nov 2020 15:43:38 -0500 Subject: [PATCH] DRY-ify checking URL length for avatars --- PluralKit.Bot/Commands/Groups.cs | 2 -- PluralKit.Bot/Commands/MemberAvatar.cs | 9 +-------- PluralKit.Bot/Commands/SystemEdit.cs | 2 -- PluralKit.Bot/Utils/AvatarUtils.cs | 3 +++ 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 09ef467b..b0e45b9e 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -177,8 +177,6 @@ namespace PluralKit.Bot { ctx.CheckOwnGroup(target); - if (img.Url.Length > Limits.MaxUriLength) - throw Errors.InvalidUrl(img.Url); await AvatarUtils.VerifyAvatarOrThrow(img.Url); await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url})); diff --git a/PluralKit.Bot/Commands/MemberAvatar.cs b/PluralKit.Bot/Commands/MemberAvatar.cs index 6164b82c..786779b6 100644 --- a/PluralKit.Bot/Commands/MemberAvatar.cs +++ b/PluralKit.Bot/Commands/MemberAvatar.cs @@ -102,18 +102,11 @@ namespace PluralKit.Bot } ctx.CheckSystem().CheckOwnMember(target); - await ValidateUrl(avatarArg.Value.Url); + await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Value.Url); await UpdateAvatar(location, ctx, target, avatarArg.Value.Url); 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, MemberGuildSettings? targetGuildData) { diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 39676ebd..a4f641af 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -140,8 +140,6 @@ namespace PluralKit.Bot async Task SetIcon(ParsedImage img) { - if (img.Url.Length > Limits.MaxUriLength) - throw Errors.InvalidUrl(img.Url); await AvatarUtils.VerifyAvatarOrThrow(img.Url); await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url})); diff --git a/PluralKit.Bot/Utils/AvatarUtils.cs b/PluralKit.Bot/Utils/AvatarUtils.cs index 837fc2fc..df4881ab 100644 --- a/PluralKit.Bot/Utils/AvatarUtils.cs +++ b/PluralKit.Bot/Utils/AvatarUtils.cs @@ -11,6 +11,9 @@ namespace PluralKit.Bot { public static class AvatarUtils { public static async Task VerifyAvatarOrThrow(string url) { + if (url.Length > Limits.MaxUriLength) + throw Errors.UrlTooLong(url); + // List of MIME types we consider acceptable var acceptableMimeTypes = new[] {