update 1
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
<PackageReference Include="Mapster.DependencyInjection" Version="1.0.0" />
|
<PackageReference Include="Mapster.DependencyInjection" Version="1.0.0" />
|
||||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
|
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
|
||||||
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
|
using System.IO;
|
||||||
using BackOffice.BFF.Application.Common.Interfaces;
|
using BackOffice.BFF.Application.Common.Interfaces;
|
||||||
using CMSMicroservice.Protobuf.Protos.Products;
|
using CMSMicroservice.Protobuf.Protos.Products;
|
||||||
using Google.Protobuf;
|
using Google.Protobuf;
|
||||||
|
using SixLabors.ImageSharp;
|
||||||
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||||
|
using SixLabors.ImageSharp.Processing;
|
||||||
|
|
||||||
namespace BackOffice.BFF.Application.ProductsCQ.Commands.CreateNewProducts;
|
namespace BackOffice.BFF.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||||
public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProductsCommand, CreateNewProductsResponseDto>
|
public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProductsCommand, CreateNewProductsResponseDto>
|
||||||
@@ -16,30 +20,66 @@ public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProducts
|
|||||||
{
|
{
|
||||||
var createRequest = request.Adapt<CreateNewProductsRequest>();
|
var createRequest = request.Adapt<CreateNewProductsRequest>();
|
||||||
|
|
||||||
|
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)
|
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()
|
var fileInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
|
||||||
{
|
{
|
||||||
Directory = "Images/Products",
|
Directory = "Images/Products",
|
||||||
IsBase64 = false,
|
IsBase64 = false,
|
||||||
MIME = request.ImageFile.Mime,
|
MIME = request.ImageFile.Mime,
|
||||||
FileName = request.ImageFile.FileName,
|
FileName = request.ImageFile.FileName,
|
||||||
File = ByteString.CopyFrom(request.ImageFile.File)
|
File = ByteString.CopyFrom(optimizedMainImage)
|
||||||
}, cancellationToken: cancellationToken);
|
}, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
|
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
|
||||||
createRequest.ImagePath = 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()
|
var thumbInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
|
||||||
{
|
{
|
||||||
Directory = "Images/Products/Thumbnail",
|
Directory = "Images/Products/Thumbnail",
|
||||||
IsBase64 = false,
|
IsBase64 = false,
|
||||||
MIME = request.ThumbnailFile.Mime,
|
MIME = request.ImageFile.Mime,
|
||||||
FileName = request.ThumbnailFile.FileName,
|
FileName = request.ImageFile.FileName,
|
||||||
File = ByteString.CopyFrom(request.ThumbnailFile.File)
|
File = ByteString.CopyFrom(thumbnailImage)
|
||||||
}, cancellationToken: cancellationToken);
|
}, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
|
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
|
||||||
@@ -50,5 +90,21 @@ public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProducts
|
|||||||
|
|
||||||
return response.Adapt<CreateNewProductsResponseDto>();
|
return response.Adapt<CreateNewProductsResponseDto>();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
|
using System.IO;
|
||||||
using BackOffice.BFF.Application.Common.Interfaces;
|
using BackOffice.BFF.Application.Common.Interfaces;
|
||||||
using CMSMicroservice.Protobuf.Protos.Products;
|
using CMSMicroservice.Protobuf.Protos.Products;
|
||||||
using Google.Protobuf;
|
using Google.Protobuf;
|
||||||
|
using SixLabors.ImageSharp;
|
||||||
|
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||||
|
using SixLabors.ImageSharp.Processing;
|
||||||
|
|
||||||
namespace BackOffice.BFF.Application.ProductsCQ.Commands.UpdateProducts;
|
namespace BackOffice.BFF.Application.ProductsCQ.Commands.UpdateProducts;
|
||||||
public class UpdateProductsCommandHandler : IRequestHandler<UpdateProductsCommand, Unit>
|
public class UpdateProductsCommandHandler : IRequestHandler<UpdateProductsCommand, Unit>
|
||||||
@@ -18,28 +22,32 @@ public class UpdateProductsCommandHandler : IRequestHandler<UpdateProductsComman
|
|||||||
|
|
||||||
if (request.ImageFile != null && request.ImageFile.File != null && request.ImageFile.File.Length > 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()
|
var fileInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
|
||||||
{
|
{
|
||||||
Directory = "Images/Products",
|
Directory = "Images/Products",
|
||||||
IsBase64 = false,
|
IsBase64 = false,
|
||||||
MIME = request.ImageFile.Mime,
|
MIME = request.ImageFile.Mime,
|
||||||
FileName = request.ImageFile.FileName,
|
FileName = request.ImageFile.FileName,
|
||||||
File = ByteString.CopyFrom(request.ImageFile.File)
|
File = ByteString.CopyFrom(optimizedMainImage)
|
||||||
}, cancellationToken: cancellationToken);
|
}, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
|
if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.File))
|
||||||
updateRequest.ImagePath = 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()
|
var thumbInfo = await _context.FileInfos.CreateNewFileInfoAsync(new()
|
||||||
{
|
{
|
||||||
Directory = "Images/Products/Thumbnail",
|
Directory = "Images/Products/Thumbnail",
|
||||||
IsBase64 = false,
|
IsBase64 = false,
|
||||||
MIME = request.ThumbnailFile.Mime,
|
MIME = request.ImageFile.Mime,
|
||||||
FileName = request.ThumbnailFile.FileName,
|
FileName = request.ImageFile.FileName,
|
||||||
File = ByteString.CopyFrom(request.ThumbnailFile.File)
|
File = ByteString.CopyFrom(thumbnailImage)
|
||||||
}, cancellationToken: cancellationToken);
|
}, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
|
if (thumbInfo != null && !string.IsNullOrWhiteSpace(thumbInfo.File))
|
||||||
@@ -50,5 +58,21 @@ public class UpdateProductsCommandHandler : IRequestHandler<UpdateProductsComman
|
|||||||
|
|
||||||
return Unit.Value;
|
return Unit.Value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user