From 99cf6981e7528b8e53f73ba91a7d6a0cf01f3a1f Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 6 Mar 2020 22:28:32 +0800 Subject: Init development of post image feature. --- Timeline/Services/UserAvatarService.cs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'Timeline/Services/UserAvatarService.cs') diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index 52d079a3..27922bab 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -53,7 +53,7 @@ namespace Timeline.Services /// Validate a avatar's format and size info. /// /// The avatar to validate. - /// Thrown when validation failed. + /// Thrown when validation failed. Task Validate(Avatar avatar); } @@ -79,7 +79,7 @@ namespace Timeline.Services /// The id of the user to set avatar for. /// The avatar. Can be null to delete the saved avatar. /// Thrown if any field in is null when is not null. - /// Thrown if avatar is of bad format. + /// Thrown if avatar is of bad format. Task SetAvatar(long id, Avatar? avatar); } @@ -134,23 +134,13 @@ namespace Timeline.Services public class UserAvatarValidator : IUserAvatarValidator { + private readonly ImageValidator _innerValidator = new ImageValidator(true); + public Task Validate(Avatar avatar) { - return Task.Run(() => - { - try - { - using var image = Image.Load(avatar.Data, out IImageFormat format); - if (!format.MimeTypes.Contains(avatar.Type)) - throw new AvatarFormatException(avatar, AvatarFormatException.ErrorReason.UnmatchedFormat); - if (image.Width != image.Height) - throw new AvatarFormatException(avatar, AvatarFormatException.ErrorReason.BadSize); - } - catch (UnknownImageFormatException e) - { - throw new AvatarFormatException(avatar, AvatarFormatException.ErrorReason.CantDecode, e); - } - }); + if (avatar == null) + throw new ArgumentNullException(nameof(avatar)); + return _innerValidator.Validate(avatar.Data, avatar.Type); } } -- cgit v1.2.3