Skip to content

Commit de324ff

Browse files
committed
Resolves smartstore#955 Import: CustomerImporter > import avatar in a separate batch
1 parent 6a5f0fe commit de324ff

1 file changed

Lines changed: 75 additions & 59 deletions

File tree

src/Libraries/SmartStore.Services/Customers/Importer/CustomerImporter.cs

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Linq.Expressions;
65
using SmartStore.Core.Async;
76
using SmartStore.Core.Data;
87
using SmartStore.Core.Domain.Common;
@@ -140,62 +139,6 @@ private void UpsertRole(ImportRow<Customer> row, CustomerRole role, bool value)
140139
}
141140
}
142141

143-
protected virtual void ProcessAvatar(IImportExecuteContext context, ImportRow<Customer> row)
144-
{
145-
var urlOrPath = row.GetDataValue<string>("AvatarPictureUrl");
146-
if (urlOrPath.IsEmpty())
147-
return;
148-
149-
Picture picture = null;
150-
var equalPictureId = 0;
151-
var currentPictures = new List<Picture>();
152-
var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName);
153-
var image = CreateDownloadImage(urlOrPath, seoName, 1);
154-
155-
if (image == null)
156-
return;
157-
158-
if (image.Url.HasValue() && !image.Success.HasValue)
159-
{
160-
AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image }));
161-
}
162-
163-
if ((image.Success ?? false) && File.Exists(image.Path))
164-
{
165-
Succeeded(image);
166-
var pictureBinary = File.ReadAllBytes(image.Path);
167-
168-
if (pictureBinary != null && pictureBinary.Length > 0)
169-
{
170-
var currentPictureId = row.Entity.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId);
171-
if (currentPictureId != 0 && (picture = _pictureRepository.GetById(currentPictureId)) != null)
172-
{
173-
currentPictures.Add(picture);
174-
}
175-
176-
pictureBinary = _pictureService.ValidatePicture(pictureBinary);
177-
pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId);
178-
179-
if (pictureBinary != null && pictureBinary.Length > 0)
180-
{
181-
if ((picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, false, false)) != null)
182-
{
183-
_pictureRepository.Context.SaveChanges();
184-
SaveAttribute(row, SystemCustomerAttributeNames.AvatarPictureId, picture.Id);
185-
}
186-
}
187-
else
188-
{
189-
context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl");
190-
}
191-
}
192-
}
193-
else
194-
{
195-
context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl");
196-
}
197-
}
198-
199142
protected virtual int ProcessAddresses(
200143
IImportExecuteContext context,
201144
IEnumerable<ImportRow<Customer>> batch,
@@ -348,10 +291,68 @@ protected virtual int ProcessGenericAttributes(
348291
if (!customerNumber.IsEmpty())
349292
allCustomerNumbers.Add(customerNumber);
350293
}
294+
}
295+
296+
return _services.DbContext.SaveChanges();
297+
}
298+
299+
protected virtual int ProcessAvatars(
300+
IImportExecuteContext context,
301+
IEnumerable<ImportRow<Customer>> batch)
302+
{
303+
foreach (var row in batch)
304+
{
305+
var urlOrPath = row.GetDataValue<string>("AvatarPictureUrl");
306+
if (urlOrPath.IsEmpty())
307+
continue;
308+
309+
Picture picture = null;
310+
var equalPictureId = 0;
311+
var currentPictures = new List<Picture>();
312+
var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName);
313+
314+
var image = CreateDownloadImage(urlOrPath, seoName, 1);
315+
if (image == null)
316+
continue;
317+
318+
if (image.Url.HasValue() && !image.Success.HasValue)
319+
{
320+
AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image }));
321+
}
351322

352-
if (_customerSettings.AllowCustomersToUploadAvatars)
323+
if ((image.Success ?? false) && File.Exists(image.Path))
324+
{
325+
Succeeded(image);
326+
var pictureBinary = File.ReadAllBytes(image.Path);
327+
328+
if (pictureBinary != null && pictureBinary.Length > 0)
329+
{
330+
var currentPictureId = row.Entity.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId);
331+
if (currentPictureId != 0 && (picture = _pictureRepository.GetById(currentPictureId)) != null)
332+
{
333+
currentPictures.Add(picture);
334+
}
335+
336+
pictureBinary = _pictureService.ValidatePicture(pictureBinary);
337+
pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId);
338+
339+
if (pictureBinary != null && pictureBinary.Length > 0)
340+
{
341+
if ((picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, false, false)) != null)
342+
{
343+
_pictureRepository.Context.SaveChanges();
344+
SaveAttribute(row, SystemCustomerAttributeNames.AvatarPictureId, picture.Id);
345+
}
346+
}
347+
else
348+
{
349+
context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl");
350+
}
351+
}
352+
}
353+
else
353354
{
354-
ProcessAvatar(context, row);
355+
context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl");
355356
}
356357
}
357358

@@ -584,6 +585,21 @@ protected override void Import(IImportExecuteContext context)
584585
context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessGenericAttributes");
585586
}
586587

588+
// ===========================================================================
589+
// Process avatars
590+
// ===========================================================================
591+
if (_customerSettings.AllowCustomersToUploadAvatars)
592+
{
593+
try
594+
{
595+
ProcessAvatars(context, batch);
596+
}
597+
catch (Exception exception)
598+
{
599+
context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessAvatars");
600+
}
601+
}
602+
587603
// ===========================================================================
588604
// Process addresses
589605
// ===========================================================================

0 commit comments

Comments
 (0)