protected virtual int ProcessAvatars( ImportExecuteContext context, IEnumerable <ImportRow <Customer> > batch) { foreach (var row in batch) { var urlOrPath = row.GetDataValue <string>("AvatarPictureUrl"); if (urlOrPath.IsEmpty()) { continue; } var image = CreateDownloadImage(context, urlOrPath, 1); if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); using (var stream = File.OpenRead(image.Path)) { if ((stream?.Length ?? 0) > 0) { var currentFiles = new List <MediaFileInfo>(); var fileId = row.Entity.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId); var file = _mediaService.GetFileById(fileId, MediaLoadFlags.AsNoTracking); if (file != null) { currentFiles.Add(file); } if (!_mediaService.FindEqualFile(stream, currentFiles.Select(x => x.File), true, out var _)) { // Don't manage avatar files. Just overwrite existing file. var path = _mediaService.CombinePaths(SystemAlbumProvider.Customers, image.FileName.ToValidFileName()); var newFile = _mediaService.SaveFile(path, stream, false, DuplicateFileHandling.Overwrite); if ((newFile?.Id ?? 0) != 0) { SaveAttribute(row, SystemCustomerAttributeNames.AvatarPictureId, newFile.Id); } } else { context.Result.AddInfo("Found equal image in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl"); } } } } else { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl"); } } return(_services.DbContext.SaveChanges()); }
public ActionResult Navbar() { var currentCustomer = _services.WorkContext.CurrentCustomer; ViewBag.UserName = _services.Settings.LoadSetting <CustomerSettings>().UsernamesEnabled ? currentCustomer.Username : currentCustomer.Email; ViewBag.Stores = _services.StoreService.GetAllStores(); ViewBag.CheckUpdateResult = AsyncRunner.RunSync(() => CheckUpdateAsync(false)); return(PartialView()); }
public ActionResult Navbar() { var currentCustomer = _services.WorkContext.CurrentCustomer; ViewBag.UserName = _services.Settings.LoadSetting <CustomerSettings>().UsernamesEnabled ? currentCustomer.Username : currentCustomer.Email; ViewBag.Stores = _services.StoreService.GetAllStores(); if (_services.Permissions.Authorize(StandardPermissionProvider.ManageMaintenance)) { ViewBag.CheckUpdateResult = AsyncRunner.RunSync(() => CheckUpdateAsync(false)); } return(PartialView()); }
public ActionResult Navbar() { var currentCustomer = _services.WorkContext.CurrentCustomer; ViewBag.UserName = _services.Settings.LoadSetting <CustomerSettings>().CustomerLoginType != CustomerLoginType.Email ? currentCustomer.Username : currentCustomer.Email; ViewBag.Stores = _services.StoreService.GetAllStores(); if (_services.Permissions.Authorize(Permissions.System.Maintenance.Read)) { ViewBag.CheckUpdateResult = AsyncRunner.RunSync(() => CheckUpdateInternalAsync(false)); } return(PartialView()); }
protected virtual int ProcessPictures( ImportExecuteContext context, IEnumerable <ImportRow <Category> > batch) { foreach (var row in batch) { try { var srcId = row.GetDataValue <int>("Id"); var imageUrl = row.GetDataValue <string>("ImageUrl"); if (imageUrl.IsEmpty()) { continue; } var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName); var image = CreateDownloadImage(context, imageUrl, seoName, 1); if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); var pictureBinary = File.ReadAllBytes(image.Path); if (pictureBinary != null && pictureBinary.Length > 0) { var currentPictures = new List <MediaFile>(); var pictureId = row.Entity.MediaFileId ?? 0; if (pictureId != 0) { var picture = _pictureRepository.TableUntracked.Expand(x => x.MediaStorage).FirstOrDefault(x => x.Id == pictureId); if (picture != null) { currentPictures.Add(picture); } } pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out var equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { var picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, false, false, "category"); if (picture != null) { row.Entity.MediaFileId = picture.Id; _categoryRepository.Update(row.Entity); } } else { context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "ImageUrls"); } } } else if (image.Url.HasValue()) { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "ImageUrls"); } } catch (Exception ex) { context.Result.AddWarning(ex.ToAllMessages(), row.GetRowInfo(), "ImageUrls"); } } var num = _categoryRepository.Context.SaveChanges(); return(num); }
protected virtual int ProcessPictures( ImportExecuteContext context, IEnumerable <ImportRow <Category> > batch, Dictionary <int, ImportCategoryMapping> srcToDestId) { Picture picture = null; var equalPictureId = 0; foreach (var row in batch) { try { var srcId = row.GetDataValue <int>("Id"); var urlOrPath = row.GetDataValue <string>("ImageUrl"); if (srcId != 0 && srcToDestId.ContainsKey(srcId) && urlOrPath.HasValue()) { var currentPictures = new List <Picture>(); var category = _categoryRepository.GetById(srcToDestId[srcId].DestinationId); var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName); var image = CreateDownloadImage(urlOrPath, seoName, 1); if (category != null && image != null) { if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); var pictureBinary = File.ReadAllBytes(image.Path); if (pictureBinary != null && pictureBinary.Length > 0) { if (category.PictureId.HasValue && (picture = _pictureRepository.GetById(category.PictureId.Value)) != null) { currentPictures.Add(picture); } var size = Size.Empty; pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size); pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { if ((picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, size.Width, size.Height, false)) != null) { category.PictureId = picture.Id; _categoryRepository.Update(category); } } else { context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "ImageUrls"); } } } else if (image.Url.HasValue()) { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "ImageUrls"); } } } } catch (Exception exception) { context.Result.AddWarning(exception.ToAllMessages(), row.GetRowInfo(), "ImageUrls"); } } var num = _categoryRepository.Context.SaveChanges(); return(num); }
protected virtual int ProcessPictures(ImportExecuteContext context, IEnumerable <ImportRow <Category> > batch) { var allFileIds = batch .Where(row => row.HasDataValue("ImageUrl") && row.Entity.MediaFileId > 0) .Select(row => row.Entity.MediaFileId.Value) .Distinct() .ToArray(); var allFiles = _mediaService.GetFilesByIds(allFileIds).ToDictionary(x => x.Id, x => x.File); var catalogAlbumId = _folderService.GetNodeByPath(SystemAlbumProvider.Catalog).Value.Id; foreach (var row in batch) { try { var imageUrl = row.GetDataValue <string>("ImageUrl"); if (imageUrl.IsEmpty()) { continue; } var image = CreateDownloadImage(context, imageUrl, 1); if (image == null) { continue; } if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); using (var stream = File.OpenRead(image.Path)) { if ((stream?.Length ?? 0) > 0) { var assignedFile = allFiles.Get(row.Entity.MediaFileId ?? 0); MediaFile sourceFile = null; if (assignedFile != null && _mediaService.FindEqualFile(stream, new[] { assignedFile }, true, out var _)) { context.Result.AddInfo($"Found equal image in data store for '{image.FileName}'. Skipping file.", row.GetRowInfo(), "ImageUrl"); } else if (_mediaService.FindEqualFile(stream, image.FileName, catalogAlbumId, true, out sourceFile)) { context.Result.AddInfo($"Found equal image in catalog album for '{image.FileName}'. Assigning existing file instead.", row.GetRowInfo(), "ImageUrl"); } else { var path = _mediaService.CombinePaths(SystemAlbumProvider.Catalog, image.FileName); sourceFile = _mediaService.SaveFile(path, stream, false, DuplicateFileHandling.Rename)?.File; } if (sourceFile?.Id > 0) { row.Entity.MediaFileId = sourceFile.Id; _categoryRepository.Update(row.Entity); } } } } else if (image.Url.HasValue()) { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "ImageUrls"); } } catch (Exception ex) { context.Result.AddWarning(ex.ToAllMessages(), row.GetRowInfo(), "ImageUrls"); } } var num = _categoryRepository.Context.SaveChanges(); return(num); }
protected virtual void ProcessProductPictures(ImportExecuteContext context, IEnumerable <ImportRow <Product> > batch) { // true, cause pictures must be saved and assigned an id prior adding a mapping. _productPictureRepository.AutoCommitEnabled = true; ProductPicture lastInserted = null; var equalPictureId = 0; var numberOfPictures = (context.ExtraData.NumberOfPictures ?? int.MaxValue); foreach (var row in batch) { var imageUrls = row.GetDataValue <List <string> >("ImageUrls"); if (imageUrls.IsNullOrEmpty()) { continue; } var imageNumber = 0; var displayOrder = -1; var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName); var imageFiles = new List <FileDownloadManagerItem>(); // collect required image file infos foreach (var urlOrPath in imageUrls) { var image = CreateDownloadImage(urlOrPath, seoName, ++imageNumber); if (image != null) { imageFiles.Add(image); } if (imageFiles.Count >= numberOfPictures) { break; } } // download images if (imageFiles.Any(x => x.Url.HasValue())) { // async downloading in batch processing is inefficient cause only the image processing benefits from async, // not the record processing itself. a per record processing may speed up the import. AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, imageFiles.Where(x => x.Url.HasValue() && !x.Success.HasValue))); } // import images foreach (var image in imageFiles.OrderBy(x => x.DisplayOrder)) { try { if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); var pictureBinary = File.ReadAllBytes(image.Path); if (pictureBinary != null && pictureBinary.Length > 0) { var currentProductPictures = _productPictureRepository.TableUntracked.Expand(x => x.Picture) .Where(x => x.ProductId == row.Entity.Id) .ToList(); var currentPictures = currentProductPictures .Select(x => x.Picture) .ToList(); if (displayOrder == -1) { displayOrder = (currentProductPictures.Any() ? currentProductPictures.Select(x => x.DisplayOrder).Max() : 0); } var size = Size.Empty; pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size); pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { // no equal picture found in sequence var newPicture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, size.Width, size.Height, false); if (newPicture != null) { var mapping = new ProductPicture { ProductId = row.Entity.Id, PictureId = newPicture.Id, DisplayOrder = ++displayOrder }; _productPictureRepository.Insert(mapping); lastInserted = mapping; } } else { context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "ImageUrls" + image.DisplayOrder.ToString()); } } } else if (image.Url.HasValue()) { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "ImageUrls" + image.DisplayOrder.ToString()); } } catch (Exception exception) { context.Result.AddWarning(exception.ToAllMessages(), row.GetRowInfo(), "ImageUrls" + image.DisplayOrder.ToString()); } } } // Perf: notify only about LAST insertion and update if (lastInserted != null) { _services.EventPublisher.EntityInserted(lastInserted); } }
protected virtual int ProcessAvatars( ImportExecuteContext context, IEnumerable <ImportRow <Customer> > batch) { foreach (var row in batch) { var urlOrPath = row.GetDataValue <string>("AvatarPictureUrl"); if (urlOrPath.IsEmpty()) { continue; } var equalPictureId = 0; var currentPictures = new List <Picture>(); var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName); var image = CreateDownloadImage(urlOrPath, seoName, 1); if (image == null) { continue; } if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); var pictureBinary = File.ReadAllBytes(image.Path); if (pictureBinary != null && pictureBinary.Length > 0) { var pictureId = row.Entity.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId); if (pictureId != 0) { var picture = _pictureRepository.TableUntracked.Expand(x => x.MediaStorage).FirstOrDefault(x => x.Id == pictureId); if (picture != null) { currentPictures.Add(picture); } } var size = Size.Empty; pictureBinary = _pictureService.ValidatePicture(pictureBinary, out size); pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { var picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, size.Width, size.Height, false); if (picture != null) { SaveAttribute(row, SystemCustomerAttributeNames.AvatarPictureId, picture.Id); } } else { context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl"); } } } else { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl"); } } return(_services.DbContext.SaveChanges()); }
protected virtual int ProcessPictures( ImportExecuteContext context, IEnumerable <ImportRow <Category> > batch) { foreach (var row in batch) { try { var srcId = row.GetDataValue <int>("Id"); var imageUrl = row.GetDataValue <string>("ImageUrl"); if (imageUrl.IsEmpty()) { continue; } var image = CreateDownloadImage(context, imageUrl, 1); if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); using (var stream = File.OpenRead(image.Path)) { if ((stream?.Length ?? 0) > 0) { var currentFiles = new List <MediaFileInfo>(); var file = _mediaService.GetFileById(row.Entity.MediaFileId ?? 0, MediaLoadFlags.AsNoTracking); if (file != null) { currentFiles.Add(file); } if (!_mediaService.FindEqualFile(stream, currentFiles.Select(x => x.File), true, out var _)) { var path = _mediaService.CombinePaths(SystemAlbumProvider.Catalog, image.FileName.ToValidFileName()); var newFile = _mediaService.SaveFile(path, stream, false, DuplicateFileHandling.Rename); if ((newFile?.Id ?? 0) != 0) { row.Entity.MediaFileId = newFile.Id; _categoryRepository.Update(row.Entity); } } else { context.Result.AddInfo("Found equal image in data store. Skipping field.", row.GetRowInfo(), "ImageUrls"); } } } } else if (image.Url.HasValue()) { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "ImageUrls"); } } catch (Exception ex) { context.Result.AddWarning(ex.ToAllMessages(), row.GetRowInfo(), "ImageUrls"); } } var num = _categoryRepository.Context.SaveChanges(); return(num); }
protected virtual int ProcessAvatars( ImportExecuteContext context, IEnumerable <ImportRow <Customer> > batch) { foreach (var row in batch) { var urlOrPath = row.GetDataValue <string>("AvatarPictureUrl"); if (urlOrPath.IsEmpty()) { continue; } var image = CreateDownloadImage(context, urlOrPath, 1); if (image == null) { continue; } if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); using (var stream = File.OpenRead(image.Path)) { if (stream?.Length > 0) { MediaFile sourceFile = null; var currentFiles = new List <MediaFileInfo>(); var fileId = row.Entity.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId); var file = _mediaService.GetFileById(fileId, MediaLoadFlags.AsNoTracking); if (file != null) { currentFiles.Add(file); } if (_mediaService.FindEqualFile(stream, currentFiles.Select(x => x.File), true, out var _)) { context.Result.AddInfo($"Found equal image in customer data for {image.FileName}. Skipping file.", row.GetRowInfo(), "AvatarPictureUrl"); } else { // An avatar may not be assigned to several customers. A customer could otherwise delete the avatar of another. // Overwriting is probably too dangerous here, because we could overwrite the avatar of another customer, so better rename. var path = _mediaService.CombinePaths(SystemAlbumProvider.Customers, image.FileName); sourceFile = _mediaService.SaveFile(path, stream, false, DuplicateFileHandling.Rename)?.File; } if (sourceFile?.Id > 0) { SaveAttribute(row, SystemCustomerAttributeNames.AvatarPictureId, sourceFile.Id); } } } } else { context.Result.AddInfo("Download of avatar failed.", row.GetRowInfo(), "AvatarPictureUrl"); } } return(_services.DbContext.SaveChanges()); }
public static ImportResult ImportProductsFromExcel(this IImportManager importManager, Stream stream) { //Func<Task<ImportResult>> fn = () => importManager.ImportProductsFromExcelAsync(stream); //return fn.RunSync(); return(AsyncRunner.RunSync(() => importManager.ImportProductsFromExcelAsync(stream))); }
private void ImportAvatar(IImportExecuteContext context, ImportRow <Customer> row) { var urlOrPath = row.GetDataValue <string>("AvatarPictureUrl"); if (urlOrPath.IsEmpty()) { return; } Picture picture = null; var equalPictureId = 0; var currentPictures = new List <Picture>(); var seoName = _pictureService.GetPictureSeName(row.EntityDisplayName); var image = CreateDownloadImage(urlOrPath, seoName, 1); if (image == null) { return; } if (image.Url.HasValue() && !image.Success.HasValue) { AsyncRunner.RunSync(() => _fileDownloadManager.DownloadAsync(DownloaderContext, new FileDownloadManagerItem[] { image })); } if ((image.Success ?? false) && File.Exists(image.Path)) { Succeeded(image); var pictureBinary = File.ReadAllBytes(image.Path); if (pictureBinary != null && pictureBinary.Length > 0) { var currentPictureId = row.Entity.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId); if (currentPictureId != 0 && (picture = _pictureRepository.GetById(currentPictureId)) != null) { currentPictures.Add(picture); } pictureBinary = _pictureService.ValidatePicture(pictureBinary); pictureBinary = _pictureService.FindEqualPicture(pictureBinary, currentPictures, out equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { if ((picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, false, false)) != null) { _pictureRepository.Context.SaveChanges(); _genericAttributeService.SaveAttribute(row.Entity.Id, SystemCustomerAttributeNames.AvatarPictureId, _attributeKeyGroup, picture.Id.ToString()); } } else { context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl"); } } } else { context.Result.AddInfo("Download of an image failed.", row.GetRowInfo(), "AvatarPictureUrl"); } }