aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/ImageValidator.cs
blob: 59424a7ca73609ad2acbe5f8aac39fb2e04e34fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using System;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Services.Exceptions;

namespace Timeline.Services
{
    public interface IImageValidator
    {
        /// <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>
        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));

            var format = await Task.Run(() =>
            {
                try
                {
                    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 (square && image.Width != image.Height)
                        throw new ImageException(ImageException.ErrorReason.NotSquare, data, requestType, format.DefaultMimeType);
                    return format;
                }
                catch (UnknownImageFormatException e)
                {
                    throw new ImageException(ImageException.ErrorReason.CantDecode, data, requestType, null, null, e);
                }
            });
            return format;
        }
    }
}