aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Imaging
diff options
context:
space:
mode:
Diffstat (limited to 'BackEnd/Timeline/Services/Imaging')
-rw-r--r--BackEnd/Timeline/Services/Imaging/IImageService.cs32
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageException.cs7
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageService.cs54
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageServicesServiceCollectionExtensions.cs14
-rw-r--r--BackEnd/Timeline/Services/Imaging/ImageValidator.cs53
5 files changed, 102 insertions, 58 deletions
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
+ {
+ /// <summary>
+ /// Detect the format of a image.
+ /// </summary>
+ /// <param name="data"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns>The image format.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="data"/> is null.</exception>
+ /// <exception cref="ImageException">Thrown when image data can't be detected.</exception>
+ Task<IImageFormat> DetectFormatAsync(byte[] data, CancellationToken cancellationToken = default);
+
+ /// <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>
+ /// <param name="cancellationToken">Cancellation token.</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> 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
/// <summary>
/// Image is not of required size.
/// </summary>
- NotSquare,
+ BadSize,
/// <summary>
/// Other unknown errer.
/// </summary>
@@ -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<IImageFormat> 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<IImageFormat> 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<IImageService, ImageService>();
+ 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
- {
- /// <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;
- }
- }
-}