Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Minor improvements picture service

KrzysztofPajak1c27c32b2ed2
diff --git a/src/Business/Grand.Business.Storage/Services/PictureService.cs b/src/Business/Grand.Business.Storage/Services/PictureService.cs
index 279bd0354..0ab41e74f 100644
--- a/src/Business/Grand.Business.Storage/Services/PictureService.cs
+++ b/src/Business/Grand.Business.Storage/Services/PictureService.cs
@@ -1,24 +1,24 @@
-using Grand.Business.Storage.Interfaces;
-using Grand.Infrastructure;
-using Grand.Infrastructure.Caching;
-using Grand.Infrastructure.Caching.Constants;
+using Grand.Business.Common.Extensions;
+using Grand.Business.Common.Interfaces.Logging;
+using Grand.Business.Storage.Interfaces;
using Grand.Domain;
+using Grand.Domain.Common;
using Grand.Domain.Data;
using Grand.Domain.Media;
+using Grand.Infrastructure;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Caching.Constants;
+using Grand.Infrastructure.Extensions;
+using Grand.SharedKernel.Extensions;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using SkiaSharp;
using System;
using System.IO;
using System.Linq;
+using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
-using Grand.Business.Common.Interfaces.Logging;
-using Grand.Infrastructure.Extensions;
-using Grand.Business.Common.Extensions;
-using Grand.SharedKernel.Extensions;
-using Grand.Domain.Common;
-using System.Linq.Expressions;
namespace Grand.Business.Storage.Services
{
@@ -360,26 +360,15 @@ namespace Grand.Business.Storage.Services
bool showDefaultPicture = true,
string storeLocation = null)
{
-
if (picture == null)
- {
return showDefaultPicture ? await GetDefaultPictureUrl(targetSize, storeLocation) : string.Empty;
- }
-
- byte[] pictureBinary = null;
if (picture.IsNew)
{
- if ((picture.PictureBinary?.Length ?? 0) == 0)
- pictureBinary = await LoadPictureBinary(picture);
- else
- pictureBinary = picture.PictureBinary;
-
await DeletePictureThumbs(picture);
- //we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown
picture = await UpdatePicture(picture.Id,
- pictureBinary,
+ null,
picture.MimeType,
picture.SeoFilename,
picture.AltAttribute,
@@ -388,8 +377,8 @@ namespace Grand.Business.Storage.Services
false);
}
- string seoFileName = picture.SeoFilename;
- string lastPart = GetFileExtensionFromMimeType(picture.MimeType);
+ var seoFileName = picture.SeoFilename;
+ var lastPart = GetFileExtensionFromMimeType(picture.MimeType);
string thumbFileName;
if (targetSize == 0)
@@ -403,14 +392,12 @@ namespace Grand.Business.Storage.Services
if (!string.IsNullOrEmpty(thumbFilePath))
return GetThumbUrl(thumbFileName, storeLocation);
- pictureBinary ??= await LoadPictureBinary(picture);
+ var pictureBinary = await LoadPictureBinary(picture);
using (var mutex = new Mutex(false, thumbFileName))
{
mutex.WaitOne();
-
await SaveThumb(thumbFileName, pictureBinary);
-
mutex.ReleaseMutex();
}
}
@@ -425,7 +412,7 @@ namespace Grand.Business.Storage.Services
if (!string.IsNullOrEmpty(thumbFilePath))
return GetThumbUrl(thumbFileName, storeLocation);
- pictureBinary ??= await LoadPictureBinary(picture);
+ var pictureBinary = await LoadPictureBinary(picture);
using (var mutex = new Mutex(false, thumbFileName))
{
@@ -434,10 +421,8 @@ namespace Grand.Business.Storage.Services
{
try
{
- using (var image = SKBitmap.Decode(pictureBinary))
- {
- pictureBinary = ApplyResize(image, EncodedImageFormat(picture.MimeType), targetSize);
- }
+ using var image = SKBitmap.Decode(pictureBinary);
+ pictureBinary = ApplyResize(image, EncodedImageFormat(picture.MimeType), targetSize);
}
catch { }
}
@@ -482,8 +467,7 @@ namespace Grand.Business.Storage.Services
var query = _pictureRepository.Table
.Where(p => p.Id == pictureId)
.Select(p =>
- new Picture
- {
+ new Picture {
Id = p.Id,
AltAttribute = p.AltAttribute,
IsNew = p.IsNew,
@@ -602,8 +586,7 @@ namespace Grand.Business.Storage.Services
if (validateBinary)
pictureBinary = ValidatePicture(pictureBinary, mimeType);
- var picture = new Picture
- {
+ var picture = new Picture {
PictureBinary = _mediaSettings.StoreInDb ? pictureBinary : new byte[0],
MimeType = mimeType,
SeoFilename = seoFilename,
@@ -645,7 +628,7 @@ namespace Grand.Business.Storage.Services
seoFilename = CommonHelper.EnsureMaximumLength(seoFilename, 100);
- if (validateBinary)
+ if (validateBinary && pictureBinary != null)
pictureBinary = ValidatePicture(pictureBinary, mimeType);
var picture = await GetPictureById(pictureId);
@@ -653,19 +636,31 @@ namespace Grand.Business.Storage.Services
return null;
//delete old thumbs if a picture has been changed
- if (seoFilename != picture.SeoFilename)
+ if (seoFilename != picture.SeoFilename || pictureBinary != null)
await DeletePictureThumbs(picture);
- picture.PictureBinary = _mediaSettings.StoreInDb ? pictureBinary : new byte[0];
+ if (pictureBinary != null)
+ {
+ picture.PictureBinary = _mediaSettings.StoreInDb ? pictureBinary : Array.Empty<byte>();
+ await _pictureRepository.UpdateField(picture.Id, x => x.PictureBinary, picture.PictureBinary);
+ }
+
picture.MimeType = mimeType;
+ await _pictureRepository.UpdateField(picture.Id, x => x.MimeType, picture.MimeType);
+
picture.SeoFilename = seoFilename;
+ await _pictureRepository.UpdateField(picture.Id, x => x.SeoFilename, picture.SeoFilename);
+
picture.AltAttribute = altAttribute;
+ await _pictureRepository.UpdateField(picture.Id, x => x.AltAttribute, picture.AltAttribute);
+
picture.TitleAttribute = titleAttribute;
- picture.IsNew = isNew;
+ await _pictureRepository.UpdateField(picture.Id, x => x.TitleAttribute, picture.TitleAttribute);
- await _pictureRepository.UpdateAsync(picture);
+ picture.IsNew = isNew;
+ await _pictureRepository.UpdateField(picture.Id, x => x.IsNew, picture.IsNew);
- if (!_mediaSettings.StoreInDb)
+ if (!_mediaSettings.StoreInDb && pictureBinary != null)
await SavePictureInFile(picture.Id, pictureBinary, mimeType);
//event notification
@@ -717,7 +712,7 @@ namespace Grand.Business.Storage.Services
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PICTURE_BY_ID, picture.Id));
}
-
+
/// <summary>
/// Save picture on file system
/// </summary>
@@ -756,7 +751,7 @@ namespace Grand.Business.Storage.Services
{
//update SeoFilename picture
picture.SeoFilename = seoFilename;
- await UpdatField(picture, p=> p.SeoFilename, seoFilename);
+ await UpdatField(picture, p => p.SeoFilename, seoFilename);
//event notification
await _mediator.EntityUpdated(picture);