public IHttpActionResult LoadExportManifest([FromUri] string fileUrl)
        {
            PlatformExportManifest retVal = null;

            using (var stream = _blobStorageProvider.OpenRead(fileUrl))
            {
                retVal = _platformExportManager.ReadExportManifest(stream);
            }
            return(Ok(retVal));
        }
        public void BackgroundImport(ImportRequest request, ImportNotification notification)
        {
            Action <ExportImportProgressInfo> progressCallback = c =>
            {
                notification.InjectFrom(c);
                _notifier.Upsert(notification);
            };

            using (var stream = _blobStorageProvider.OpenRead(request.FileUrl))
            {
                try
                {
                    _csvCouponImporter.DoImport(stream, request.Delimiter, request.PromotionId, request.ExpirationDate, progressCallback);
                }
                catch (Exception exception)
                {
                    notification.Description = "Import error";
                    notification.ErrorCount++;
                    notification.Errors.Add(exception.ToString());
                }
                finally
                {
                    notification.Finished    = DateTime.UtcNow;
                    notification.Description = "Import finished" + (notification.Errors.Any() ? " with errors" : " successfully");
                    _notifier.Upsert(notification);
                }
            }
        }
        /// <summary>
        /// Loads image binary data of IHasImages objects
        /// </summary>
        /// <param name="haveImagesObjects"></param>
        /// <param name="blobStorageProvider"></param>
        /// <returns>Returns list of loading errors, or empty one, if no errors occured.</returns>
        public static IEnumerable <string> LoadImages(this IHasImages[] haveImagesObjects, IBlobStorageProvider blobStorageProvider)
        {
            var result = new List <string>();

            var allImages = haveImagesObjects
                            .SelectMany(x => x.GetFlatObjectsListWithInterface <IHasImages>())
                            .SelectMany(x => x.Images)
                            .ToArray();

            foreach (var image in allImages)
            {
                try
                {
                    using (var stream = blobStorageProvider.OpenRead(image.Url))
                    {
                        image.BinaryData = stream.ReadFully();
                    }
                }
                catch (Exception ex)
                {
                    result.Add(ex.Message);
                }
            }

            return(result);
        }
        private void LoadImages(IHasImages[] haveImagesObjects, ExportImportProgressInfo progressInfo, bool handleBinaryData)
        {
            var allImages = haveImagesObjects.SelectMany(x => x.GetFlatObjectsListWithInterface <IHasImages>())
                            .SelectMany(x => x.Images).ToArray();

            foreach (var image in allImages)
            {
                image.Url = image.RelativeUrl;

                if (handleBinaryData && !image.HasExternalUrl)
                {
                    try
                    {
                        using (var stream = _blobStorageProvider.OpenRead(image.Url))
                        {
                            image.BinaryData = stream.ReadFully();
                        }
                    }
                    catch (Exception ex)
                    {
                        progressInfo.Errors.Add(ex.Message);
                    }
                }
            }
        }
        public async Task <License> GetLicenseAsync()
        {
            License license = null;

            var licenseUrl = _blobUrlResolver.GetAbsoluteUrl(_platformOptions.LicenseBlobPath);

            if (await LicenseExistsAsync(licenseUrl))
            {
                var rawLicense = string.Empty;
                using (var stream = _blobStorageProvider.OpenRead(licenseUrl))
                {
                    rawLicense = stream.ReadToString();
                }

                license = License.Parse(rawLicense, _platformOptions.LicensePublicKeyResourceName);

                if (license != null)
                {
                    license.RawLicense = null;
                }
            }

            // Fallback to the old file system implementation
            if (license == null)
            {
                license = GetLicenseFromFile();
            }

            return(license);
        }
        public IHttpActionResult GetMappingConfiguration([FromUri] string fileUrl, [FromUri] string delimiter = ";")
        {
            var retVal = CsvProductMappingConfiguration.GetDefaultConfiguration();

            retVal.Delimiter = delimiter;

            //Read csv headers and try to auto map fields by name
            using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenRead(fileUrl))))
            {
                reader.Configuration.Delimiter = delimiter;
                if (reader.Read())
                {
                    retVal.AutoMap(reader.FieldHeaders);
                }
            }

            return(Ok(retVal));
        }
Beispiel #7
0
        public async Task <ActionResult <CsvProductMappingConfiguration> > GetMappingConfiguration([FromQuery] string fileUrl, [FromQuery] string delimiter = ";")
        {
            var result           = CsvProductMappingConfiguration.GetDefaultConfiguration();
            var decodedDelimiter = HttpUtility.UrlDecode(delimiter);

            result.Delimiter = decodedDelimiter;

            //Read csv headers and try to auto map fields by name

            using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenRead(fileUrl))))
            {
                reader.Configuration.Delimiter = decodedDelimiter;

                if (await reader.ReadAsync() && reader.ReadHeader())
                {
                    result.AutoMap(reader.Context.HeaderRecord);
                }
            }

            return(Ok(result));
        }
        private void LoadImages(IHasImages[] haveImagesObjects)
        {
            var allImages = haveImagesObjects.SelectMany(x => x.GetFlatObjectsListWithInterface <IHasImages>())
                            .SelectMany(x => x.Images).ToArray();

            foreach (var image in allImages)
            {
                using (var stream = _blobStorageProvider.OpenRead(image.Url))
                {
                    image.BinaryData = stream.ReadFully();
                }
            }
        }
        public CustomerImportPagedDataSource(string filePath, IBlobStorageProvider blobStorageProvider, int pageSize,
                                             Configuration configuration)
        {
            var stream = blobStorageProvider.OpenRead(filePath);

            _stream       = stream;
            _streamReader = new StreamReader(stream);

            _configuration = configuration;
            _csvReader     = new CsvReader(_streamReader, configuration);

            PageSize = pageSize;
        }
Beispiel #10
0
        public IHttpActionResult GetMappingConfiguration([FromUri] string fileUrl, [FromUri] string delimiter = ";")
        {
            var retVal = Data.Model.CsvProductMappingConfiguration.GetDefaultConfiguration();

            string decodedDelimiter = HttpUtility.UrlDecode(delimiter);

            retVal.Delimiter = decodedDelimiter;

            //Read csv headers and try to auto map fields by name
            using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenRead(fileUrl))))
            {
                reader.Configuration.Delimiter = decodedDelimiter;
                if (reader.Read())
                {
                    if (reader.ReadHeader())
                    {
                        retVal.AutoMap(reader.Context.HeaderRecord);
                    }
                }
            }

            return(Ok(retVal));
        }
        public async Task <ImportDataValidationResult> ValidateAsync(string filePath)
        {
            var errorsList = new List <ImportDataValidationError>();

            var fileMaxSize = _settingsManager.GetValue(ModuleConstants.Settings.General.ImportFileMaxSize.Name,
                                                        (int)ModuleConstants.Settings.General.ImportFileMaxSize.DefaultValue) * ModuleConstants.MByte;

            var blobInfo = await _blobStorageProvider.GetBlobInfoAsync(filePath);

            if (blobInfo == null)
            {
                var error = new ImportDataValidationError()
                {
                    ErrorCode = ModuleConstants.ValidationErrors.FileNotExisted
                };
                errorsList.Add(error);
            }
            else if (blobInfo.Size > fileMaxSize)
            {
                var error = new ImportDataValidationError()
                {
                    ErrorCode = ModuleConstants.ValidationErrors.ExceedingFileMaxSize
                };
                error.Properties.Add(nameof(fileMaxSize), fileMaxSize.ToString());
                error.Properties.Add(nameof(blobInfo.Size), blobInfo.Size.ToString());
                errorsList.Add(error);
            }
            else
            {
                var stream           = _blobStorageProvider.OpenRead(filePath);
                var csvConfiguration = _importConfigurationFactory.Create();
                csvConfiguration.BadDataFound             = null;
                csvConfiguration.ReadingExceptionOccurred = null;

                await ValidateDelimiterAndDataExists(stream, csvConfiguration, errorsList);

                ValidateRequiredColumns(stream, csvConfiguration, errorsList);

                ValidateLineLimit(stream, csvConfiguration, errorsList);

                await stream.DisposeAsync();
            }

            var result = new ImportDataValidationResult {
                Errors = errorsList.ToArray()
            };

            return(result);
        }
Beispiel #12
0
 /// <summary>
 /// Load to Image from blob.
 /// </summary>
 /// <param name="imageUrl">image url.</param>
 /// <param name="format">image format.</param>
 /// <returns>Image object.</returns>
 public virtual Task <Image <Rgba32> > LoadImageAsync(string imageUrl, out IImageFormat format)
 {
     try
     {
         using (var blobStream = _storageProvider.OpenRead(imageUrl))
         {
             var result = Image.Load <Rgba32>(blobStream, out format);
             return(Task.FromResult(result));
         }
     }
     catch (Exception)
     {
         format = null;
         return(Task.FromResult <Image <Rgba32> >(null));
     }
 }
Beispiel #13
0
        /// <summary>
        /// Load to Image from blob.
        /// </summary>
        /// <param name="imageUrl">image url.</param>
        /// <returns>Image object.</returns>
        public virtual async Task <Image> LoadImageAsync(string imageUrl)
        {
            try
            {
                using (var blobStream = _storageProvider.OpenRead(imageUrl))
                    using (var stream = new MemoryStream())
                    {
                        await blobStream.CopyToAsync(stream);

                        var result = Image.FromStream(stream);
                        return(result);
                    }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #14
0
        private void LoadImages(IHasImages[] haveImagesObjects)
        {
            var hasImagesObjects = haveImagesObjects.SelectMany(x => x.GetFlatObjectsListWithInterface <IHasImages>());

            hasImagesObjects = hasImagesObjects.Where(x => !(x is Category)); // Exclude images for upper categories
            var allImages = hasImagesObjects.SelectMany(x => x.Images).ToArray();

            foreach (var image in allImages)
            {
                if (!image.HasExternalUrl) // Skip external links.
                {
                    using (var stream = _blobStorageProvider.OpenRead(image.Url))
                    {
                        image.BinaryData = stream.ReadFully();
                    }
                }
            }
        }
        public CsvPagedPriceDataSource(
            string filePath,
            IProductSearchService productSearchService,
            IBlobStorageProvider blobStorageProvider,
            int pageSize,
            CsvConfiguration configuration,
            Func <TextReader, CsvConfiguration, IReader> csvReaderFactory)
        {
            _productSearchService = productSearchService;

            var stream = blobStorageProvider.OpenRead(filePath);

            _stream       = stream;
            _streamReader = new StreamReader(stream);

            _configuration = configuration;
            _csvReader     = csvReaderFactory(_streamReader, _configuration);

            PageSize = pageSize;
        }
Beispiel #16
0
        private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback, bool loadBinaryData)
        {
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);


            const SearchResponseGroup responseGroup = SearchResponseGroup.WithCatalogs | SearchResponseGroup.WithCategories | SearchResponseGroup.WithProducts;
            var searchResponse = _catalogSearchService.Search(new SearchCriteria {
                Take = int.MaxValue, Skip = 0, ResponseGroup = responseGroup
            });

            var retVal = new BackupObject();

            progressInfo.Description = String.Format("{0} catalogs loading", searchResponse.Catalogs.Count());
            progressCallback(progressInfo);

            //Catalogs
            retVal.Catalogs = searchResponse.Catalogs.Select(x => _catalogService.GetById(x.Id)).ToList();

            progressInfo.Description = String.Format("{0} categories loading", searchResponse.Categories.Count());
            progressCallback(progressInfo);

            //Categories
            retVal.Categories = _categoryService.GetByIds(searchResponse.Categories.Select(x => x.Id).ToArray(), CategoryResponseGroup.Full);

            //Products
            for (int i = 0; i < searchResponse.Products.Count(); i += 50)
            {
                var products = _itemService.GetByIds(searchResponse.Products.Skip(i).Take(50).Select(x => x.Id).ToArray(), ItemResponseGroup.ItemLarge);
                retVal.Products.AddRange(products);

                progressInfo.Description = String.Format("{0} of {1} products loaded", Math.Min(searchResponse.ProductsTotalCount, i), searchResponse.ProductsTotalCount);
                progressCallback(progressInfo);
            }
            //Binary data
            if (loadBinaryData)
            {
                var allImages = retVal.Products.SelectMany(x => x.Images);
                allImages = allImages.Concat(retVal.Categories.SelectMany(x => x.Images));
                allImages = allImages.Concat(retVal.Products.SelectMany(x => x.Variations).SelectMany(x => x.Images));

                var index            = 0;
                var progressTemplate = "{0} of " + allImages.Count() + " images downloading";
                foreach (var image in allImages)
                {
                    progressInfo.Description = String.Format(progressTemplate, index);
                    progressCallback(progressInfo);
                    try
                    {
                        using (var stream = _blobStorageProvider.OpenRead(image.Url))
                        {
                            image.BinaryData = stream.ReadFully();
                        }
                    }
                    catch (Exception ex)
                    {
                        progressInfo.Errors.Add(ex.ToString());
                        progressCallback(progressInfo);
                    }
                    index++;
                }
            }

            //Properties
            progressInfo.Description = String.Format("Properties loading");
            progressCallback(progressInfo);

            retVal.Properties = _propertyService.GetAllProperties();

            //Reset some props to descrease resulting json size
            foreach (var catalog in retVal.Catalogs)
            {
                catalog.Properties = null;
            }

            foreach (var category in retVal.Categories)
            {
                category.Catalog    = null;
                category.Properties = null;
                category.Children   = null;
                category.Parents    = null;
                foreach (var propvalue in category.PropertyValues)
                {
                    propvalue.Property = null;
                }
            }
            foreach (var product in retVal.Products.Concat(retVal.Products.SelectMany(x => x.Variations)))
            {
                product.Catalog     = null;
                product.Category    = null;
                product.Properties  = null;
                product.MainProduct = null;
                foreach (var propvalue in product.PropertyValues)
                {
                    propvalue.Property = null;
                }
            }
            return(retVal);
        }