|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
5 | | -using System.Linq.Expressions; |
6 | 5 | using SmartStore.Core.Async; |
7 | 6 | using SmartStore.Core.Data; |
8 | 7 | using SmartStore.Core.Domain.Common; |
@@ -140,62 +139,6 @@ private void UpsertRole(ImportRow<Customer> row, CustomerRole role, bool value) |
140 | 139 | } |
141 | 140 | } |
142 | 141 |
|
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 | | - |
199 | 142 | protected virtual int ProcessAddresses( |
200 | 143 | IImportExecuteContext context, |
201 | 144 | IEnumerable<ImportRow<Customer>> batch, |
@@ -348,10 +291,68 @@ protected virtual int ProcessGenericAttributes( |
348 | 291 | if (!customerNumber.IsEmpty()) |
349 | 292 | allCustomerNumbers.Add(customerNumber); |
350 | 293 | } |
| 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 | + } |
351 | 322 |
|
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 |
353 | 354 | { |
354 | | - ProcessAvatar(context, row); |
| 355 | + context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl"); |
355 | 356 | } |
356 | 357 | } |
357 | 358 |
|
@@ -584,6 +585,21 @@ protected override void Import(IImportExecuteContext context) |
584 | 585 | context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessGenericAttributes"); |
585 | 586 | } |
586 | 587 |
|
| 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 | + |
587 | 603 | // =========================================================================== |
588 | 604 | // Process addresses |
589 | 605 | // =========================================================================== |
|
0 commit comments