diff --git a/src/BackOffice.BFF.Application/BackOffice.BFF.Application.csproj b/src/BackOffice.BFF.Application/BackOffice.BFF.Application.csproj
index 8d57410..b294a18 100644
--- a/src/BackOffice.BFF.Application/BackOffice.BFF.Application.csproj
+++ b/src/BackOffice.BFF.Application/BackOffice.BFF.Application.csproj
@@ -12,6 +12,7 @@
+
diff --git a/src/BackOffice.BFF.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs b/src/BackOffice.BFF.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs
index fa08430..9bbee0d 100644
--- a/src/BackOffice.BFF.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs
+++ b/src/BackOffice.BFF.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs
@@ -1,6 +1,10 @@
+using System.IO;
using BackOffice.BFF.Application.Common.Interfaces;
using CMSMicroservice.Protobuf.Protos.Products;
using Google.Protobuf;
+using SixLabors.ImageSharp;
+using SixLabors.ImageSharp.Formats.Jpeg;
+using SixLabors.ImageSharp.Processing;
namespace BackOffice.BFF.Application.ProductsCQ.Commands.CreateNewProducts;
public class CreateNewProductsCommandHandler : IRequestHandler
@@ -16,30 +20,66 @@ public class CreateNewProductsCommandHandler : IRequestHandler();
+ if (string.IsNullOrWhiteSpace(createRequest.Description))
+ {
+ if (!string.IsNullOrWhiteSpace(createRequest.ShortInfomation))
+ {
+ createRequest.Description = createRequest.ShortInfomation;
+ }
+ else if (!string.IsNullOrWhiteSpace(createRequest.FullInformation))
+ {
+ createRequest.Description = createRequest.FullInformation;
+ }
+ else if (!string.IsNullOrWhiteSpace(createRequest.Title))
+ {
+ createRequest.Description = createRequest.Title;
+ }
+ else
+ {
+ createRequest.Description = "-";
+ }
+ }
+
+ if (string.IsNullOrWhiteSpace(createRequest.ShortInfomation))
+ {
+ createRequest.ShortInfomation = createRequest.Description;
+ }
+
+ if (string.IsNullOrWhiteSpace(createRequest.FullInformation))
+ {
+ createRequest.FullInformation = createRequest.Description;
+ }
+
+ createRequest.Rate = 0;
+
if (request.ImageFile != null && request.ImageFile.File != null && request.ImageFile.File.Length > 0)
{
+ var optimizedMainImage = OptimizeImage(request.ImageFile.File, 1200, 1200, 80);
+
var fileInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
{
Directory = "Images/Products",
IsBase64 = false,
MIME = request.ImageFile.Mime,
FileName = request.ImageFile.FileName,
- File = ByteString.CopyFrom(request.ImageFile.File)
+ File = ByteString.CopyFrom(optimizedMainImage)
}, cancellationToken: cancellationToken);
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
createRequest.ImagePath = fileInfo.File;
}
- if (request.ThumbnailFile != null && request.ThumbnailFile.File != null && request.ThumbnailFile.File.Length > 0)
+ if (request.ImageFile != null && request.ImageFile.File != null && request.ImageFile.File.Length > 0)
{
+ var thumbnailImage = OptimizeImage(request.ImageFile.File, 300, 300, 75);
+
var thumbInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
{
Directory = "Images/Products/Thumbnail",
IsBase64 = false,
- MIME = request.ThumbnailFile.Mime,
- FileName = request.ThumbnailFile.FileName,
- File = ByteString.CopyFrom(request.ThumbnailFile.File)
+ MIME = request.ImageFile.Mime,
+ FileName = request.ImageFile.FileName,
+ File = ByteString.CopyFrom(thumbnailImage)
}, cancellationToken: cancellationToken);
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
@@ -50,5 +90,21 @@ public class CreateNewProductsCommandHandler : IRequestHandler();
}
-}
+ private static byte[] OptimizeImage(byte[] original, int maxWidth, int maxHeight, int quality)
+ {
+ using var image = Image.Load(original);
+
+ var ratio = Math.Min(maxWidth / (float)image.Width, maxHeight / (float)image.Height);
+ if (ratio < 1f)
+ {
+ var width = (int)(image.Width * ratio);
+ var height = (int)(image.Height * ratio);
+ image.Mutate(x => x.Resize(width, height));
+ }
+
+ using var ms = new MemoryStream();
+ image.Save(ms, new JpegEncoder { Quality = quality });
+ return ms.ToArray();
+ }
+}
diff --git a/src/BackOffice.BFF.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs b/src/BackOffice.BFF.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs
index 454d414..30da8d2 100644
--- a/src/BackOffice.BFF.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs
+++ b/src/BackOffice.BFF.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs
@@ -1,6 +1,10 @@
+using System.IO;
using BackOffice.BFF.Application.Common.Interfaces;
using CMSMicroservice.Protobuf.Protos.Products;
using Google.Protobuf;
+using SixLabors.ImageSharp;
+using SixLabors.ImageSharp.Formats.Jpeg;
+using SixLabors.ImageSharp.Processing;
namespace BackOffice.BFF.Application.ProductsCQ.Commands.UpdateProducts;
public class UpdateProductsCommandHandler : IRequestHandler
@@ -18,28 +22,32 @@ public class UpdateProductsCommandHandler : IRequestHandler 0)
{
+ var optimizedMainImage = OptimizeImage(request.ImageFile.File, 1200, 1200, 80);
+
var fileInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
{
Directory = "Images/Products",
IsBase64 = false,
MIME = request.ImageFile.Mime,
FileName = request.ImageFile.FileName,
- File = ByteString.CopyFrom(request.ImageFile.File)
+ File = ByteString.CopyFrom(optimizedMainImage)
}, cancellationToken: cancellationToken);
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
updateRequest.ImagePath = fileInfo.File;
}
- if (request.ThumbnailFile != null && request.ThumbnailFile.File != null && request.ThumbnailFile.File.Length > 0)
+ if (request.ImageFile != null && request.ImageFile.File != null && request.ImageFile.File.Length > 0)
{
+ var thumbnailImage = OptimizeImage(request.ImageFile.File, 300, 300, 75);
+
var thumbInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
{
Directory = "Images/Products/Thumbnail",
IsBase64 = false,
- MIME = request.ThumbnailFile.Mime,
- FileName = request.ThumbnailFile.FileName,
- File = ByteString.CopyFrom(request.ThumbnailFile.File)
+ MIME = request.ImageFile.Mime,
+ FileName = request.ImageFile.FileName,
+ File = ByteString.CopyFrom(thumbnailImage)
}, cancellationToken: cancellationToken);
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
@@ -50,5 +58,21 @@ public class UpdateProductsCommandHandler : IRequestHandler x.Resize(width, height));
+ }
+
+ using var ms = new MemoryStream();
+ image.Save(ms, new JpegEncoder { Quality = quality });
+ return ms.ToArray();
+ }
+}