Error when given an invalid image file

This commit is contained in:
Ske 2019-11-03 13:56:05 +01:00
parent 8d003da091
commit 0cdd99d195
2 changed files with 2 additions and 0 deletions

View File

@ -34,6 +34,7 @@ namespace PluralKit.Bot {
public static PKError AvatarFileSizeLimit(long size) => new PKError($"File size too large ({size.Bytes().ToString("#.#")} > {Limits.AvatarFileSizeLimit.Bytes().ToString("#.#")}), try shrinking or compressing the image."); public static PKError AvatarFileSizeLimit(long size) => new PKError($"File size too large ({size.Bytes().ToString("#.#")} > {Limits.AvatarFileSizeLimit.Bytes().ToString("#.#")}), try shrinking or compressing the image.");
public static PKError AvatarNotAnImage(string mimeType) => new PKError($"The given link does not point to an image{(mimeType != null ? $" ({mimeType.SanitizeMentions()})" : "")}. Make sure you're using a direct link (ending in .jpg, .png, .gif)."); public static PKError AvatarNotAnImage(string mimeType) => new PKError($"The given link does not point to an image{(mimeType != null ? $" ({mimeType.SanitizeMentions()})" : "")}. Make sure you're using a direct link (ending in .jpg, .png, .gif).");
public static PKError AvatarDimensionsTooLarge(int width, int height) => new PKError($"Image too large ({width}x{height} > {Limits.AvatarDimensionLimit}x{Limits.AvatarDimensionLimit}), try resizing the image."); public static PKError AvatarDimensionsTooLarge(int width, int height) => new PKError($"Image too large ({width}x{height} > {Limits.AvatarDimensionLimit}x{Limits.AvatarDimensionLimit}), try resizing the image.");
public static PKError AvatarInvalid => new PKError($"Could not read image file - perhaps it's corrupted or the wrong format. Try a different image.");
public static PKError UserHasNoAvatar => new PKError("The given user has no avatar set."); public static PKError UserHasNoAvatar => new PKError("The given user has no avatar set.");
public static PKError InvalidUrl(string url) => new PKError($"The given URL is invalid."); public static PKError InvalidUrl(string url) => new PKError($"The given URL is invalid.");

View File

@ -62,6 +62,7 @@ namespace PluralKit.Bot
// Parse the image header in a worker // Parse the image header in a worker
var stream = await response.Content.ReadAsStreamAsync(); var stream = await response.Content.ReadAsStreamAsync();
var image = await Task.Run(() => Image.Identify(stream)); 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 if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height); throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
} }