diff options
Diffstat (limited to 'Timeline/Services/ImageValidator.cs')
-rw-r--r-- | Timeline/Services/ImageValidator.cs | 23 |
1 files changed, 13 insertions, 10 deletions
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;
- }
-
/// <summary>
/// Validate a image data.
/// </summary>
/// <param name="data">The data of the image. Can't be null.</param>
/// <param name="requestType">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.</param>
+ /// <param name="square">If true, image must be square.</param>
/// <returns>The format.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="data"/> is null.</exception>
/// <exception cref="ImageException">Thrown when image data can't be decoded or real type does not match request type or image is not square when required.</exception>
- public async Task<IImageFormat> Validate(byte[] data, string? requestType = null)
+ Task<IImageFormat> Validate(byte[] data, string? requestType = null, bool square = false);
+ }
+
+ public class ImageValidator : IImageValidator
+ {
+ public ImageValidator()
+ {
+ }
+
+ public async Task<IImageFormat> 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;
}
|