From 6488df7d9e7b1c3cc9fbceb0fa25a9770988a7ca Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 7 Mar 2020 22:02:31 +0800 Subject: ... --- Timeline/Services/ImageValidator.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'Timeline/Services/ImageValidator.cs') diff --git a/Timeline/Services/ImageValidator.cs b/Timeline/Services/ImageValidator.cs index 897a37b8..c331d912 100644 --- a/Timeline/Services/ImageValidator.cs +++ b/Timeline/Services/ImageValidator.cs @@ -6,24 +6,27 @@ using System.Threading.Tasks; namespace Timeline.Services { - public class ImageValidator + public interface IImageValidator { - private readonly bool _requireSquare; - - public ImageValidator(bool requireSquare = false) - { - _requireSquare = requireSquare; - } - /// /// Validate a image data. /// /// The data of the image. Can't be null. /// If not null, the real image format will be check against the requested format and throw if not match. If null, then do not check. + /// If true, image must be square. /// The format. /// Thrown when is null. /// Thrown when image data can't be decoded or real type does not match request type or image is not square when required. - public async Task Validate(byte[] data, string? requestType = null) + Task Validate(byte[] data, string? requestType = null, bool square = false); + } + + public class ImageValidator : IImageValidator + { + public ImageValidator() + { + } + + public async Task Validate(byte[] data, string? requestType = null, bool square = false) { if (data == null) throw new ArgumentNullException(nameof(data)); @@ -35,7 +38,7 @@ namespace Timeline.Services using var image = Image.Load(data, out IImageFormat format); if (requestType != null && !format.MimeTypes.Contains(requestType)) throw new ImageException(ImageException.ErrorReason.UnmatchedFormat, data, requestType, format.DefaultMimeType); - if (_requireSquare && image.Width != image.Height) + if (square && image.Width != image.Height) throw new ImageException(ImageException.ErrorReason.NotSquare, data, requestType, format.DefaultMimeType); return format; } -- cgit v1.2.3