mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-08 06:47:56 +00:00
feat(bot): use avater service for image verify
This commit is contained in:
parent
5f6c8c0d14
commit
d537f05b23
9 changed files with 94 additions and 63 deletions
|
|
@ -8,57 +8,6 @@ namespace PluralKit.Bot;
|
|||
|
||||
public static class AvatarUtils
|
||||
{
|
||||
public static async Task VerifyAvatarOrThrow(HttpClient client, string url, bool isFullSizeImage = false)
|
||||
{
|
||||
if (url.Length > Limits.MaxUriLength)
|
||||
throw Errors.UrlTooLong(url);
|
||||
|
||||
// List of MIME types we consider acceptable
|
||||
var acceptableMimeTypes = new[]
|
||||
{
|
||||
"image/jpeg", "image/gif", "image/png", "image/webp"
|
||||
};
|
||||
|
||||
if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri))
|
||||
throw Errors.InvalidUrl;
|
||||
|
||||
if (uri.Host.Contains("toyhou.se"))
|
||||
throw new PKError("Due to server issues, PluralKit is unable to read images hosted on toyhou.se.");
|
||||
|
||||
url = TryRewriteCdnUrl(url);
|
||||
|
||||
var response = await client.GetAsync(url);
|
||||
if (!response.IsSuccessStatusCode) // Check status code
|
||||
throw Errors.AvatarServerError(response.StatusCode);
|
||||
if (response.Content.Headers.ContentLength == null) // Check presence of content length
|
||||
throw Errors.AvatarNotAnImage(null);
|
||||
try
|
||||
{
|
||||
if (!acceptableMimeTypes.Contains(response.Content.Headers.ContentType.MediaType)) // Check MIME type
|
||||
throw Errors.AvatarNotAnImage(response.Content.Headers.ContentType.MediaType);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
throw new PKError("Could not verify avatar is an image. This can happen when the server sends a malformed response."
|
||||
+ "\nPlease join the support server for help: <https://discord.gg/PczBt78>");
|
||||
}
|
||||
|
||||
if (isFullSizeImage)
|
||||
// no need to do size checking on banners
|
||||
return;
|
||||
|
||||
if (response.Content.Headers.ContentLength > Limits.AvatarFileSizeLimit) // Check content length
|
||||
throw Errors.AvatarFileSizeLimit(response.Content.Headers.ContentLength.Value);
|
||||
|
||||
// Parse the image header in a worker
|
||||
var stream = await response.Content.ReadAsStreamAsync();
|
||||
var image = await Task.Run(() => Image.Identify(stream));
|
||||
if (image == null) throw Errors.AvatarInvalid;
|
||||
if (image.Width > Limits.AvatarDimensionLimit ||
|
||||
image.Height > Limits.AvatarDimensionLimit) // Check image size
|
||||
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
|
||||
}
|
||||
|
||||
// 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!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue