From 7a54c3fea708f25303c27630ae2bc8101b9390c3 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Apr 2021 18:52:57 +0800 Subject: refactor: ... --- BackEnd/Timeline/Services/Imaging/IImageService.cs | 32 +++++++++++++ .../Timeline/Services/Imaging/ImageException.cs | 7 +-- BackEnd/Timeline/Services/Imaging/ImageService.cs | 54 ++++++++++++++++++++++ .../ImageServicesServiceCollectionExtensions.cs | 14 ++++++ .../Timeline/Services/Imaging/ImageValidator.cs | 53 --------------------- 5 files changed, 102 insertions(+), 58 deletions(-) create mode 100644 BackEnd/Timeline/Services/Imaging/IImageService.cs create mode 100644 BackEnd/Timeline/Services/Imaging/ImageService.cs create mode 100644 BackEnd/Timeline/Services/Imaging/ImageServicesServiceCollectionExtensions.cs delete mode 100644 BackEnd/Timeline/Services/Imaging/ImageValidator.cs (limited to 'BackEnd/Timeline/Services/Imaging') diff --git a/BackEnd/Timeline/Services/Imaging/IImageService.cs b/BackEnd/Timeline/Services/Imaging/IImageService.cs new file mode 100644 index 00000000..9e595ff8 --- /dev/null +++ b/BackEnd/Timeline/Services/Imaging/IImageService.cs @@ -0,0 +1,32 @@ +using SixLabors.ImageSharp.Formats; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Timeline.Services.Imaging +{ + public interface IImageService + { + /// + /// Detect the format of a image. + /// + /// + /// + /// The image format. + /// Thrown when is null. + /// Thrown when image data can't be detected. + Task DetectFormatAsync(byte[] data, CancellationToken cancellationToken = default); + + /// + /// 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. + /// Cancellation token. + /// 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. + Task ValidateAsync(byte[] data, string? requestType = null, bool square = false, CancellationToken cancellationToken = default); + } +} diff --git a/BackEnd/Timeline/Services/Imaging/ImageException.cs b/BackEnd/Timeline/Services/Imaging/ImageException.cs index 926ecc0a..12aefa0a 100644 --- a/BackEnd/Timeline/Services/Imaging/ImageException.cs +++ b/BackEnd/Timeline/Services/Imaging/ImageException.cs @@ -19,7 +19,7 @@ namespace Timeline.Services.Imaging /// /// Image is not of required size. /// - NotSquare, + BadSize, /// /// Other unknown errer. /// @@ -42,16 +42,13 @@ namespace Timeline.Services.Imaging { ErrorReason.CantDecode => Resource.ExceptionImageReasonCantDecode, ErrorReason.UnmatchedFormat => Resource.ExceptionImageReasonUnmatchedFormat, - ErrorReason.NotSquare => Resource.ExceptionImageReasonBadSize, + ErrorReason.BadSize => Resource.ExceptionImageReasonBadSize, _ => Resource.ExceptionImageReasonUnknownError }); public ErrorReason Error { get; } -#pragma warning disable CA1819 // Properties should not return arrays public byte[]? ImageData { get; } -#pragma warning restore CA1819 // Properties should not return arrays public string? RequestType { get; } - // This field will be null if decoding failed. public string? RealType { get; } } diff --git a/BackEnd/Timeline/Services/Imaging/ImageService.cs b/BackEnd/Timeline/Services/Imaging/ImageService.cs new file mode 100644 index 00000000..9fefe3e9 --- /dev/null +++ b/BackEnd/Timeline/Services/Imaging/ImageService.cs @@ -0,0 +1,54 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Timeline.Services.Imaging +{ + + public class ImageService : IImageService + { + public async Task DetectFormatAsync(byte[] data, CancellationToken cancellationToken = default) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + var format = await Task.Run(() => + { + var format = Image.DetectFormat(data); + if (format is null) + { + throw new ImageException(ImageException.ErrorReason.CantDecode, data, null, null, null); + } + return format; + }, cancellationToken); + return format; + } + + public async Task ValidateAsync(byte[] data, string? requestType = null, bool square = false, CancellationToken cancellationToken = default) + { + 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.BadSize, data, requestType, format.DefaultMimeType); + return format; + } + catch (UnknownImageFormatException e) + { + throw new ImageException(ImageException.ErrorReason.CantDecode, data, requestType, null, null, e); + } + }, cancellationToken); + return format; + } + } +} diff --git a/BackEnd/Timeline/Services/Imaging/ImageServicesServiceCollectionExtensions.cs b/BackEnd/Timeline/Services/Imaging/ImageServicesServiceCollectionExtensions.cs new file mode 100644 index 00000000..7906b35e --- /dev/null +++ b/BackEnd/Timeline/Services/Imaging/ImageServicesServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Timeline.Services.Imaging +{ + public static class ImageServicesServiceCollectionExtensions + { + public static IServiceCollection AddImageServices(this IServiceCollection services) + { + services.TryAddTransient(); + return services; + } + } +} 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 - { - /// - /// 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. - 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)); - - 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; - } - } -} -- cgit v1.2.3