aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Imaging/ImageValidator.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-04-27 18:52:57 +0800
committercrupest <crupest@outlook.com>2021-04-27 18:52:57 +0800
commit7a54c3fea708f25303c27630ae2bc8101b9390c3 (patch)
tree8a9f0cb6d6b6bd33d5ce4f3e987e339e433f396a /BackEnd/Timeline/Services/Imaging/ImageValidator.cs
parent2cbcd8b63bcd7e3d45cd92baa5bacd828527aea8 (diff)
downloadtimeline-7a54c3fea708f25303c27630ae2bc8101b9390c3.tar.gz
timeline-7a54c3fea708f25303c27630ae2bc8101b9390c3.tar.bz2
timeline-7a54c3fea708f25303c27630ae2bc8101b9390c3.zip
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Services/Imaging/ImageValidator.cs')
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageValidator.cs53
1 files changed, 0 insertions, 53 deletions
diff --git a/BackEnd/Timeline/Services/Imaging/ImageValidator.cs b/BackEnd/Timeline/Services/Imaging/ImageValidator.cs
deleted file mode 100644
index b4ae68dc..00000000
--- a/BackEnd/Timeline/Services/Imaging/ImageValidator.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using SixLabors.ImageSharp;
-using SixLabors.ImageSharp.Formats;
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-
-namespace Timeline.Services.Imaging
-{
- 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;
- }
- }
-}