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 async Task <IHttpActionResult> UploadAsset([FromUri] string folderUrl, [FromUri] string url = null)
        {
            if (url == null && !Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            if (url != null)
            {
                using (var client = new WebClient())
                {
                    var uploadInfo = new UploadStreamInfo
                    {
                        FileByteStream = client.OpenRead(url),
                        FolderName     = folderUrl,
                        FileName       = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url))
                    };

                    var key    = _blobProvider.Upload(uploadInfo);
                    var retVal = new webModel.BlobInfo
                    {
                        Name        = uploadInfo.FileName,
                        RelativeUrl = key,
                        Url         = _urlResolver.GetAbsoluteUrl(key)
                    };
                    return(Ok(retVal));
                }
            }
            else
            {
                var blobMultipartProvider = new BlobStorageMultipartProvider(_blobProvider, _tempPath, folderUrl);
                await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

                var retVal = new List <webModel.BlobInfo>();

                foreach (var blobInfo in blobMultipartProvider.BlobInfos)
                {
                    retVal.Add(new webModel.BlobInfo
                    {
                        Name        = blobInfo.FileName,
                        Size        = blobInfo.Size.ToString(),
                        MimeType    = blobInfo.ContentType,
                        RelativeUrl = blobInfo.Key,
                        Url         = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                    });
                }

                return(Ok(retVal.ToArray()));
            }
        }
        //initial version of product converter to amazon product.
        //it should be adopted to the particular customer needs as many Amazon properties (like category) are unique and can't be mapped automatically.
        public static Product ToAmazonModel(this moduleModel.CatalogProduct product, IBlobUrlResolver assetUrlResolver, moduleModel.Property[] properties = null)
        {
            var amazonProduct = new Product();
            amazonProduct.InjectFrom(product);

            amazonProduct.DescriptionData = new ProductDescriptionData
            {
                Brand = "Brand",
                Description = "Product description",

            };

            amazonProduct.Condition = new ConditionInfo { ConditionType = ConditionType.New };
            if (product.Images != null && product.Images.Any())
                amazonProduct.ExternalProductUrl = assetUrlResolver.GetAbsoluteUrl(product.Images.First().Url.TrimStart('/'));
            amazonProduct.SKU = product.Code;
            amazonProduct.StandardProductID = new StandardProductID { Value = amazonProduct.SKU, Type = StandardProductIDType.ASIN };

            //var mainCat = new Home();
            //var subCat = new Kitchen();
            //mainCat.ProductType = new HomeProductType { Item = subCat };

            //amazonProduct.ProductData = new ProductProductData { Item = mainCat };

            return amazonProduct;
        }        
        //initial version of product converter to amazon product.
        //it should be adopted to the particular customer needs as many Amazon properties (like category) are unique and can't be mapped automatically.
        public static Product ToAmazonModel(this moduleModel.CatalogProduct product, IBlobUrlResolver assetUrlResolver, moduleModel.Property[] properties = null)
        {
            var amazonProduct = new Product();

            amazonProduct.InjectFrom(product);

            amazonProduct.DescriptionData = new ProductDescriptionData
            {
                Brand       = "Brand",
                Description = "Product description",
            };

            amazonProduct.Condition = new ConditionInfo {
                ConditionType = ConditionType.New
            };
            if (product.Images != null && product.Images.Any())
            {
                amazonProduct.ExternalProductUrl = assetUrlResolver.GetAbsoluteUrl(product.Images.First().Url.TrimStart('/'));
            }
            amazonProduct.SKU = product.Code;
            amazonProduct.StandardProductID = new StandardProductID {
                Value = amazonProduct.SKU, Type = StandardProductIDType.ASIN
            };

            //var mainCat = new Home();
            //var subCat = new Kitchen();
            //mainCat.ProductType = new HomeProductType { Item = subCat };

            //amazonProduct.ProductData = new ProductProductData { Item = mainCat };

            return(amazonProduct);
        }
        public virtual async Task LoadDependenciesAsync(IEnumerable <CatalogProduct> products)
        {
            //TODO: refactor to do this by one call and iteration
            var catalogsIds = new { products }.GetFlatObjectsListWithInterface <IHasCatalogId>().Select(x => x.CatalogId).Where(x => x != null).Distinct().ToArray();
            var catalogsByIdDict = (await _catalogService.GetByIdsAsync(catalogsIds)).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

            var categoriesIds = new { products }.GetFlatObjectsListWithInterface <IHasCategoryId>().Select(x => x.CategoryId).Where(x => x != null).Distinct().ToArray();
            var categoriesByIdDict = (await _categoryService.GetByIdsAsync(categoriesIds, CategoryResponseGroup.Full.ToString())).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

            var allImages = new { products }.GetFlatObjectsListWithInterface <IHasImages>().Where(x => x.Images != null).SelectMany(x => x.Images);

            foreach (var image in allImages.Where(x => !string.IsNullOrEmpty(x.Url)))
            {
                image.RelativeUrl = !string.IsNullOrEmpty(image.RelativeUrl) ? image.RelativeUrl : image.Url;
                image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
            }

            foreach (var product in products)
            {
                SetProductDependencies(product, catalogsByIdDict, categoriesByIdDict);

                if (product.MainProduct != null)
                {
                    SetProductDependencies(product.MainProduct, catalogsByIdDict, categoriesByIdDict);
                }
                if (!product.Variations.IsNullOrEmpty())
                {
                    foreach (var variation in product.Variations)
                    {
                        SetProductDependencies(variation, catalogsByIdDict, categoriesByIdDict);
                    }
                }
            }
        }
        public async Task <IHttpActionResult> UploadAsset(string folder)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var blobMultipartProvider = new BlobStorageMultipartProvider(_blobProvider, _tempPath, folder);
            await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

            var retVal = new List <webModel.BlobInfo>();

            foreach (var blobInfo in blobMultipartProvider.BlobInfos)
            {
                retVal.Add(new webModel.BlobInfo
                {
                    Name        = blobInfo.FileName,
                    Size        = blobInfo.Size.ToString(),
                    MimeType    = blobInfo.ContentType,
                    RelativeUrl = blobInfo.Key,
                    Url         = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                });
            }

            return(Ok(retVal.ToArray()));
        }
        public void PlatformExportBackground(PlatformImportExportRequest exportRequest, PlatformExportPushNotification pushNotification)
        {
            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                pushNotification.InjectFrom(x);
                pushNotification.Errors = x.Errors;
                _pushNotifier.Upsert(pushNotification);
            };

            try
            {
                var relativeUrl = "tmp/exported_data.zip";
                using (var stream = _blobStorageProvider.OpenWrite(relativeUrl))
                {
                    var manifest = exportRequest.ToManifest();
                    _platformExportManager.Export(stream, manifest, progressCallback);
                    //Get a download url
                    pushNotification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl);
                }
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Description = "Export finished";
                pushNotification.Finished    = DateTime.UtcNow;
                _pushNotifier.Upsert(pushNotification);
            }
        }
Beispiel #8
0
        public virtual AssetEntry ToModel(AssetEntry asset, IBlobUrlResolver blobUrlResolver)
        {
            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            asset.CreatedBy    = CreatedBy;
            asset.CreatedDate  = CreatedDate;
            asset.ModifiedBy   = ModifiedBy;
            asset.ModifiedDate = ModifiedDate;
            asset.LanguageCode = LanguageCode;
            asset.Group        = Group;
            asset.Id           = Id;

            if (asset.BlobInfo == null)
            {
                asset.BlobInfo = AbstractTypeFactory <BlobInfo> .TryCreateInstance();
            }
            asset.BlobInfo.Name        = Name;
            asset.BlobInfo.ContentType = MimeType;
            asset.BlobInfo.RelativeUrl = RelativeUrl;
            asset.BlobInfo.Url         = blobUrlResolver.GetAbsoluteUrl(RelativeUrl);
            asset.BlobInfo.Size        = Size;

            if (asset.Tenant == null)
            {
                asset.Tenant = AbstractTypeFactory <TenantIdentity> .TryCreateInstance();
            }
            asset.Tenant.Id   = TenantId;
            asset.Tenant.Type = TenantType;

            return(asset);
        }
        public static googleModel.Product ToGoogleModel(this moduleModel.CatalogProduct product, IBlobUrlResolver assetUrlResolver, moduleModel.Property[] properties = null)
        {
            var retVal = new googleModel.Product();
            retVal.InjectFrom(product);
            var langCode = product.Catalog.Languages.First().LanguageCode;

            retVal.Link = @"http://virtocommerce-test.azurewebsites.net/";

            retVal.OfferId = product.Id;
            retVal.Title = product.Name;
            retVal.Description = product.Reviews.Any() ? product.Reviews.First(x => x.LanguageCode == langCode).Content : product.Name;
            retVal.Link = @"http://virtocommerce-test.azurewebsites.net/";
            retVal.ImageLink = assetUrlResolver.GetAbsoluteUrl(product.Assets.First().Url).TrimStart('/');
            retVal.ContentLanguage = langCode;
            retVal.TargetCountry = "US";
            retVal.Channel = "online";
            retVal.Availability = "in stock";
            retVal.Condition = "new";
            retVal.GoogleProductCategory = "Media > Books";
            retVal.Gtin = "9780007350896";
            retVal.Taxes = new[] { new googleModel.ProductTax { Country = "US", Rate = 10, Region = "CA" } };
            retVal.Shipping = new[]
            {
                new googleModel.ProductShipping
                {
                    Country = "US",
                    Price = new googleModel.Price { Currency = "USD", Value = "5"}
                }
            };

            return retVal;
        }
        protected virtual void FillAdditionalProperties(Dictionary <Price, ExportablePrice> viewableMap)
        {
            var models       = viewableMap.Keys;
            var productIds   = models.Select(x => x.ProductId).Distinct().ToArray();
            var pricelistIds = models.Select(x => x.PricelistId).Distinct().ToArray();
            var products     = _itemService.GetByIds(productIds, ItemResponseGroup.ItemInfo);
            var pricelists   = _pricingService.GetPricelistsById(pricelistIds);

            foreach (var kvp in viewableMap)
            {
                var model          = kvp.Key;
                var viewableEntity = kvp.Value;
                var product        = products.FirstOrDefault(x => x.Id == model.ProductId);
                var pricelist      = pricelists.FirstOrDefault(x => x.Id == model.PricelistId);
                var imageUrl       = product?.Images?.FirstOrDefault()?.Url;

                viewableEntity.Code     = product?.Code;
                viewableEntity.ImageUrl = imageUrl != null?_blobUrlResolver.GetAbsoluteUrl(imageUrl) : null;

                viewableEntity.Name          = product?.Name;
                viewableEntity.ProductName   = product?.Name;
                viewableEntity.Parent        = pricelist?.Name;
                viewableEntity.PricelistName = pricelist?.Name;
            }
        }
Beispiel #11
0
        public async Task <IHttpActionResult> UploadAsset([FromUri] string folderUrl, [FromUri] string url = null, [FromUri] string name = null)
        {
            if (url == null && !Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var retVal = new List <webModel.BlobInfo>();

            if (url != null)
            {
                var fileName = name ?? HttpUtility.UrlDecode(Path.GetFileName(url));
                var fileUrl  = folderUrl + "/" + fileName;
                using (var client = new WebClient())
                    using (var blobStream = _blobProvider.OpenWrite(fileUrl))
                        using (var remoteStream = client.OpenRead(url))
                        {
                            remoteStream.CopyTo(blobStream);

                            retVal.Add(new webModel.BlobInfo
                            {
                                Name        = fileName,
                                RelativeUrl = fileUrl,
                                Url         = _urlResolver.GetAbsoluteUrl(fileUrl)
                            });
                        }
            }
            else
            {
                var blobMultipartProvider = new BlobStorageMultipartProvider(_blobProvider, _urlResolver, folderUrl);
                await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

                foreach (var blobInfo in blobMultipartProvider.BlobInfos)
                {
                    retVal.Add(new webModel.BlobInfo
                    {
                        Name        = blobInfo.FileName,
                        Size        = blobInfo.Size.ToString(),
                        MimeType    = blobInfo.ContentType,
                        RelativeUrl = blobInfo.Key,
                        Url         = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                    });
                }
            }

            return(Ok(retVal.ToArray()));
        }
Beispiel #12
0
        protected virtual async Task LoadDependenciesAsync(IEnumerable <Category> categories, IDictionary <string, Category> preloadedCategoriesMap)
        {
            var catalogsByIdDict = (await _catalogService.GetCatalogsListAsync()).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase).WithDefaultValue(null);

            foreach (var category in categories)
            {
                category.Catalog   = catalogsByIdDict.GetValueOrThrow(category.CatalogId, $"catalog with key {category.CatalogId} doesn't exist");
                category.IsVirtual = category.Catalog.IsVirtual;
                category.Parents   = Array.Empty <Category>();
                //Load all parent categories
                if (category.ParentId != null)
                {
                    category.Parents = TreeExtension.GetAncestors(category, x => x.ParentId != null ? preloadedCategoriesMap[x.ParentId] : null)
                                       .Reverse()
                                       .ToArray();
                    category.Parent = category.Parents.LastOrDefault();
                }
                category.Level = category.Parents?.Count() ?? 0;

                if (!category.Links.IsNullOrEmpty())
                {
                    foreach (var link in category.Links)
                    {
                        link.Catalog = catalogsByIdDict.GetValueOrThrow(link.CatalogId, $"link catalog with key {link.CatalogId} doesn't exist");
                        if (link.CategoryId != null)
                        {
                            if (preloadedCategoriesMap.ContainsKey(link.CategoryId))
                            {
                                link.Category = preloadedCategoriesMap[link.CategoryId];
                            }
                        }
                    }
                }

                if (!category.Properties.IsNullOrEmpty())
                {
                    foreach (var property in category.Properties)
                    {
                        property.Catalog = catalogsByIdDict.GetValueOrThrow(property.CatalogId, $"property catalog with key {property.CatalogId} doesn't exist");
                        if (property.CategoryId != null)
                        {
                            if (preloadedCategoriesMap.ContainsKey(property.CategoryId))
                            {
                                property.Category = preloadedCategoriesMap[property.CategoryId];
                            }
                        }
                    }
                }
                //Resolve relative urls for category assets
                if (category.Images != null)
                {
                    foreach (var image in category.Images.Where(x => !string.IsNullOrEmpty(x.Url)))
                    {
                        image.RelativeUrl = image.Url;
                        image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
                    }
                }
            }
        }
Beispiel #13
0
        public async Task <IHttpActionResult> UploadContent(string contentType, string storeId, [FromUri] string folderUrl, [FromUri] string url = null)
        {
            if (url == null && !Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var storageProvider = _contentStorageProviderFactory(GetContentBasePath(contentType, storeId));

            if (url != null)
            {
                var fileName = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url));
                var fileUrl  = folderUrl + "/" + fileName;
                using (var client = new WebClient())
                    using (var blobStream = storageProvider.OpenWrite(fileUrl))
                        using (var remoteStream = client.OpenRead(url))
                        {
                            remoteStream.CopyTo(blobStream);

                            var retVal = new ContentFile
                            {
                                Name = fileName,
                                Url  = _urlResolver.GetAbsoluteUrl(fileUrl)
                            };
                            return(Ok(retVal));
                        }
            }
            else
            {
                var blobMultipartProvider = new BlobStorageMultipartProvider(storageProvider, _urlResolver, folderUrl);
                await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

                var retVal = new List <ContentFile>();

                foreach (var blobInfo in blobMultipartProvider.BlobInfos)
                {
                    retVal.Add(new ContentFile
                    {
                        Name = blobInfo.FileName,
                        Url  = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                    });
                }

                return(Ok(retVal.ToArray()));
            }
        }
Beispiel #14
0
        protected virtual void LoadDependencies(IEnumerable <Category> categories, Dictionary <string, Category> preloadedCategoriesMap)
        {
            var catalogsMap = _catalogService.GetAllCatalogs().ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

            foreach (var category in categories)
            {
                category.Catalog   = catalogsMap.GetValueOrThrow(category.CatalogId, $"catalog with key {category.CatalogId} doesn't exist");
                category.IsVirtual = category.Catalog.IsVirtual;
                category.Parents   = Array.Empty <Category>();
                //Load all parent categories
                if (category.ParentId != null)
                {
                    category.Parents = TreeExtension.GetAncestors(category, x => x.ParentId != null && preloadedCategoriesMap.ContainsKey(x.ParentId) ? preloadedCategoriesMap[x.ParentId] : null)
                                       .Reverse()
                                       .ToArray();
                    category.Parent = category.Parents.Last();
                }
                category.Level = category.Parents?.Count() ?? 0;

                if (!category.Links.IsNullOrEmpty())
                {
                    foreach (var link in category.Links)
                    {
                        link.Catalog = catalogsMap.GetValueOrThrow(link.CatalogId, $"link catalog with key {link.CatalogId} doesn't exist");
                        if (link.CategoryId != null)
                        {
                            if (preloadedCategoriesMap.ContainsKey(link.CategoryId))
                            {
                                link.Category = preloadedCategoriesMap[link.CategoryId];
                            }
                        }
                    }
                }

                if (!category.Properties.IsNullOrEmpty())
                {
                    foreach (var property in category.Properties)
                    {
                        property.Catalog = catalogsMap.GetValueOrThrow(property.CatalogId, $"property catalog with key {property.CatalogId} doesn't exist");
                        if (property.CategoryId != null)
                        {
                            if (preloadedCategoriesMap.ContainsKey(property.CategoryId))
                            {
                                property.Category = preloadedCategoriesMap[property.CategoryId];
                            }
                        }
                    }
                }
                //Resolve relative urls for category assets
                if (category.AllAssets != null)
                {
                    foreach (var asset in category.AllAssets)
                    {
                        asset.Url = _blobUrlResolver.GetAbsoluteUrl(asset.RelativeUrl);
                    }
                }
            }
        }
Beispiel #15
0
        // Only public methods can be invoked in the background. (Hangfire)
        public async Task BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent)
        {
            var currencies = await _currencyService.GetAllCurrenciesAsync();

            var defaultCurrency = currencies.First(x => x.IsPrimary);

            exportInfo.Currency ??= defaultCurrency.Code;
            var catalog = await _catalogService.GetByIdsAsync(new[] { exportInfo.CatalogId });

            if (catalog == null)
            {
                throw new InvalidOperationException($"Cannot get catalog with id '{exportInfo.CatalogId}'");
            }

            Action <ExportImportProgressInfo> progressCallback = async x =>
            {
                notifyEvent.InjectFrom(x);
                await _notifier.SendAsync(notifyEvent);
            };

            using (var stream = new MemoryStream())
            {
                try
                {
                    exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration();
                    await _csvExporter.DoExportAsync(stream, exportInfo, progressCallback);

                    stream.Position = 0;
                    var fileNameTemplate = await _settingsManager.GetValueAsync(CsvModuleConstants.Settings.General.ExportFileNameTemplate.Name, CsvModuleConstants.Settings.General.ExportFileNameTemplate.DefaultValue.ToString());

                    var fileName = string.Format(fileNameTemplate, DateTime.UtcNow);
                    fileName = Path.ChangeExtension(fileName, ".csv");

                    var blobRelativeUrl = Path.Combine("temp", fileName);

                    //Upload result csv to blob storage
                    using (var blobStream = _blobStorageProvider.OpenWrite(blobRelativeUrl))
                    {
                        stream.CopyTo(blobStream);
                    }

                    //Get a download url
                    notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobRelativeUrl);
                    notifyEvent.Description = "Export finished";
                }
                catch (Exception ex)
                {
                    notifyEvent.Description = "Export failed";
                    notifyEvent.Errors.Add(ex.ExpandExceptionMessage());
                }
                finally
                {
                    notifyEvent.Finished = DateTime.UtcNow;
                    await _notifier.SendAsync(notifyEvent);
                }
            }
        }
Beispiel #16
0
        public static webModel.ItemImage ToImageWebModel(this moduleModel.ItemAsset asset, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.ItemImage();

            retVal.InjectFrom(asset);
            retVal.Src      = blobUrlResolver.GetAbsoluteUrl(asset.Url);
            retVal.ThumbSrc = retVal.Src;
            retVal.Name     = asset.Group;
            return(retVal);
        }
Beispiel #17
0
        // Only public methods can be invoked in the background. (Hangfire)
        public void BackgroundExport(Data.Model.CsvExportInfo exportInfo, ExportNotification notifyEvent)
        {
            var currencies      = _commerceService.GetAllCurrencies();
            var defaultCurrency = currencies.First(x => x.IsPrimary);

            exportInfo.Currency = exportInfo.Currency ?? defaultCurrency.Code;
            var catalog = _catalogService.GetById(exportInfo.CatalogId);

            if (catalog == null)
            {
                throw new NullReferenceException("catalog");
            }

            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                notifyEvent.InjectFrom(x);
                _notifier.Upsert(notifyEvent);
            };

            using (var stream = new MemoryStream())
            {
                try
                {
                    exportInfo.Configuration = Data.Model.CsvProductMappingConfiguration.GetDefaultConfiguration();
                    _csvExporter.DoExport(stream, exportInfo, progressCallback);

                    stream.Position = 0;
                    var fileNameTemplate = _settingsManager.GetValue("CsvCatalogImport.ExportFileNameTemplate", string.Empty);
                    var fileName         = string.Format(fileNameTemplate, DateTime.UtcNow);
                    fileName = Path.ChangeExtension(fileName, ".csv");

                    var blobRelativeUrl = Path.Combine("temp", fileName);

                    //Upload result csv to blob storage
                    using (var blobStream = _blobStorageProvider.OpenWrite(blobRelativeUrl))
                    {
                        stream.CopyTo(blobStream);
                    }
                    //Get a download url
                    notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobRelativeUrl);
                    notifyEvent.Description = "Export finished";
                }
                catch (Exception ex)
                {
                    notifyEvent.Description = "Export failed";
                    notifyEvent.Errors.Add(ex.ExpandExceptionMessage());
                }
                finally
                {
                    notifyEvent.Finished = DateTime.UtcNow;
                    _notifier.Upsert(notifyEvent);
                }
            }
        }
        public static webModel.Image ToWebModel(this coreModel.Image image, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.Image();

			retVal.InjectFrom(image);
			if (blobUrlResolver != null)
			{
				retVal.Url = blobUrlResolver.GetAbsoluteUrl(image.Url);
			}
			retVal.RelativeUrl = image.Url;
            return retVal;
        }
Beispiel #19
0
        public IHttpActionResult UploadAssetFromUrl(string folder, [FromUri] string url)
        {
            using (var client = new WebClient())
            {
                var uploadInfo = new UploadStreamInfo
                {
                    FileByteStream = client.OpenRead(url),
                    FolderName     = folder,
                    FileName       = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url))
                };

                var key    = _blobProvider.Upload(uploadInfo);
                var retVal = new webModel.BlobInfo
                {
                    Name        = uploadInfo.FileName,
                    RelativeUrl = key,
                    Url         = _urlResolver.GetAbsoluteUrl(key)
                };
                return(Ok(retVal));
            }
        }
Beispiel #20
0
        public static webModel.Image ToWebModel(this coreModel.Image image, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.Image();

            retVal.InjectFrom(image);
            if (blobUrlResolver != null)
            {
                retVal.Url = blobUrlResolver.GetAbsoluteUrl(image.Url);
            }
            retVal.RelativeUrl = image.Url;
            return(retVal);
        }
Beispiel #21
0
        protected virtual async Task InnerLoadDependenciesAsync(IEnumerable <CatalogProduct> products, bool processVariations = true)
        {
            var catalogsByIdDict = (await _catalogService.GetCatalogsListAsync()).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase)
                                   .WithDefaultValue(null);
            var productsCategoryIds = products.Select(x => x.CategoryId)
                                      .Where(x => x != null).Distinct()
                                      .ToArray();
            var productLinksCategoryIds = products.Any(p => !p.Links.IsNullOrEmpty()) ? products.SelectMany(p => p.Links)
                                          .Select(l => l.CategoryId).Distinct().ToArray() : new string[] { };

            var productCategoriesByIdDict      = (await _categoryService.GetByIdsAsync(productsCategoryIds, CategoryResponseGroup.Full.ToString())).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase).WithDefaultValue(null);
            var productLinksCategoriesByIdDict = (await _categoryService.GetByIdsAsync(productLinksCategoryIds, (CategoryResponseGroup.WithProperties | CategoryResponseGroup.WithParents).ToString())).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase).WithDefaultValue(null);

            foreach (var product in products)
            {
                if (string.IsNullOrEmpty(product.Code))
                {
                    product.Code = _skuGenerator.GenerateSku(product);
                }
                product.Catalog = catalogsByIdDict.GetValueOrThrow(product.CatalogId, $"catalog with key {product.CatalogId} doesn't exist");
                if (product.CategoryId != null)
                {
                    product.Category = productCategoriesByIdDict.GetValueOrThrow(product.CategoryId, $"category with key {product.CategoryId} doesn't exist");
                }

                if (product.Links != null)
                {
                    foreach (var link in product.Links)
                    {
                        link.Catalog  = catalogsByIdDict.GetValueOrThrow(link.CatalogId, $"link catalog with key {link.CatalogId} doesn't exist");
                        link.Category = productLinksCategoriesByIdDict.GetValueOrThrow(link.CategoryId, $"link category with key {link.CategoryId} doesn't exist");
                    }
                }

                if (product.MainProduct != null)
                {
                    await InnerLoadDependenciesAsync(new[] { product.MainProduct }, false);
                }
                if (processVariations && !product.Variations.IsNullOrEmpty())
                {
                    await InnerLoadDependenciesAsync(product.Variations.ToArray());
                }
                //Resolve relative urls for all product images
                if (!product.Images.IsNullOrEmpty())
                {
                    foreach (var image in product.Images.Where(x => !string.IsNullOrEmpty(x.Url)))
                    {
                        image.RelativeUrl = image.Url;
                        image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
                    }
                }
            }
        }
Beispiel #22
0
        public static webModel.Asset ToAssetWebModel(this moduleModel.ItemAsset asset, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.Asset();

            retVal.InjectFrom(asset);

            retVal.Name     = HttpUtility.UrlDecode(System.IO.Path.GetFileName(asset.Url));
            retVal.MimeType = MimeTypeResolver.ResolveContentType(retVal.Name);

            retVal.Url = blobUrlResolver.GetAbsoluteUrl(asset.Url);
            return(retVal);
        }
      	public static webModel.Image ToWebModel(this moduleModel.Image image, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.Image();
			retVal.InjectFrom(image);
			retVal.Src = blobUrlResolver.GetAbsoluteUrl(image.Url);
            retVal.ThumbSrc = retVal.Src;
			if(retVal.Name == null)
			{
				retVal.Name = retVal.Group;
			}
            return retVal;
        }
        public static webModel.Image ToWebModel(this moduleModel.Image image, IBlobUrlResolver blobUrlResolver)
        {
            var retVal = new webModel.Image();

            retVal.InjectFrom(image);
            retVal.Src      = blobUrlResolver.GetAbsoluteUrl(image.Url);
            retVal.ThumbSrc = retVal.Src;
            if (retVal.Name == null)
            {
                retVal.Name = retVal.Group;
            }
            return(retVal);
        }
        public void BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent)
        {
            var curencySetting  = _settingsManager.GetSettingByName("VirtoCommerce.Core.General.Currencies");
            var defaultCurrency = EnumUtility.SafeParse <CurrencyCodes>(curencySetting.DefaultValue, CurrencyCodes.USD);

            exportInfo.Currency = exportInfo.Currency ?? defaultCurrency;
            var catalog = _catalogService.GetById(exportInfo.CatalogId);

            if (catalog == null)
            {
                throw new NullReferenceException("catalog");
            }

            Action <ExportImportProgressInfo> progressCallback = (x) =>
            {
                notifyEvent.InjectFrom(x);
                _notifier.Upsert(notifyEvent);
            };

            using (var stream = new MemoryStream())
            {
                try
                {
                    exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration();
                    _csvExporter.DoExport(stream, exportInfo, progressCallback);

                    stream.Position = 0;
                    //Upload result csv to blob storage
                    var uploadInfo = new UploadStreamInfo
                    {
                        FileName       = "Catalog-" + catalog.Name + "-export.csv",
                        FileByteStream = stream,
                        FolderName     = "temp"
                    };
                    var blobKey = _blobStorageProvider.Upload(uploadInfo);
                    //Get a download url
                    notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobKey);
                    notifyEvent.Description = "Export finished";
                }
                catch (Exception ex)
                {
                    notifyEvent.Description = "Export failed";
                    notifyEvent.Errors.Add(ex.ExpandExceptionMessage());
                }
                finally
                {
                    notifyEvent.Finished = DateTime.UtcNow;
                    _notifier.Upsert(notifyEvent);
                }
            }
        }
        public async Task ExportBackgroundAsync(ExportDataRequest request, ExportPushNotification notification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportProgressInfo x)
            {
                notification.Patch(x);
                notification.JobId = context.BackgroundJob.Id;
                _pushNotificationManager.Send(notification);
            }

            try
            {
                if (string.IsNullOrEmpty(_platformOptions.DefaultExportFolder))
                {
                    throw new PlatformException($"{nameof(_platformOptions.DefaultExportFolder)} should be set.");
                }

                var fileName = string.Format(FileNameTemplate, DateTime.UtcNow);

                // Do not like provider creation here to get file extension, maybe need to pass created provider to Exporter.
                // Create stream inside Exporter is not good as it is not Exporter resposibility to decide where to write.
                var provider = _exportProviderFactory.CreateProvider(request);

                if (!string.IsNullOrEmpty(provider.ExportedFileExtension))
                {
                    fileName = Path.ChangeExtension(fileName, provider.ExportedFileExtension);
                }

                var url = UrlHelperExtensions.Combine(_platformOptions.DefaultExportFolder, fileName);
                using (var blobStream = _blobStorageProvider.OpenWrite(url))
                {
                    _dataExporter.Export(blobStream, request, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                }

                notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(url);
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                notification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                notification.Description = "Export finished";
                notification.Finished    = DateTime.UtcNow;
                await _pushNotificationManager.SendAsync(notification);
            }
        }
Beispiel #27
0
        // Only public methods can be invoked in the background. (Hangfire)
        public void BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent)
        {
            var currencies      = _commerceService.GetAllCurrencies();
            var defaultCurrency = currencies.First(x => x.IsPrimary);

            exportInfo.Currency = exportInfo.Currency ?? defaultCurrency.Code;
            var catalog = _catalogService.GetById(exportInfo.CatalogId);

            if (catalog == null)
            {
                throw new NullReferenceException("catalog");
            }

            Action <ExportImportProgressInfo> progressCallback = (x) =>
            {
                notifyEvent.InjectFrom(x);
                _notifier.Upsert(notifyEvent);
            };

            using (var stream = new MemoryStream())
            {
                try
                {
                    exportInfo.Configuration = CsvProductMappingConfiguration.GetDefaultConfiguration();
                    _csvExporter.DoExport(stream, exportInfo, progressCallback);

                    stream.Position = 0;
                    var blobRelativeUrl = "temp/Catalog-" + catalog.Name + "-export.csv";
                    //Upload result csv to blob storage
                    using (var blobStream = _blobStorageProvider.OpenWrite(blobRelativeUrl))
                    {
                        stream.CopyTo(blobStream);
                    }
                    //Get a download url
                    notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobRelativeUrl);
                    notifyEvent.Description = "Export finished";
                }
                catch (Exception ex)
                {
                    notifyEvent.Description = "Export failed";
                    notifyEvent.Errors.Add(ex.ExpandExceptionMessage());
                }
                finally
                {
                    notifyEvent.Finished = DateTime.UtcNow;
                    _notifier.Upsert(notifyEvent);
                }
            }
        }
Beispiel #28
0
        public void PlatformExportBackground(PlatformExportImportRequest exportRequest, string platformVersion, string author, ExportImportProgressNotificationEvent notifyEvent)
        {
            Action <ExportImportProgressInfo> progressCallback = (x) =>
            {
                notifyEvent.InjectFrom(x);
                _eventNotifier.Upsert(notifyEvent);
            };

            var now = DateTime.UtcNow;

            try
            {
                var exportedModules = InnerGetModulesWithInterface(typeof(ISupportExportModule)).Where(x => exportRequest.Modules.Contains(x.Id)).ToArray();
                using (var stream = new MemoryStream())
                {
                    var options = new PlatformExportImportOptions
                    {
                        Modules          = exportedModules,
                        PlatformSecurity = exportRequest.PlatformSecurity,
                        PlatformSettings = exportRequest.PlatformSettings,
                        PlatformVersion  = SemanticVersion.Parse(platformVersion),
                        Author           = author
                    };

                    _platformExportManager.Export(stream, options, progressCallback);
                    stream.Seek(0, SeekOrigin.Begin);
                    //Upload result  to blob storage
                    var uploadInfo = new UploadStreamInfo
                    {
                        FileName       = "Platform-" + now.ToString("yyMMddhh") + "-export.zip",
                        FileByteStream = stream,
                        FolderName     = "tmp"
                    };
                    var blobKey = _blobStorageProvider.Upload(uploadInfo);
                    //Get a download url
                    notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobKey);
                    notifyEvent.Description = "Export finished";
                }
            }
            catch (Exception ex)
            {
                notifyEvent.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                notifyEvent.Finished = DateTime.UtcNow;
                _eventNotifier.Upsert(notifyEvent);
            }
        }
Beispiel #29
0
        protected virtual IEnumerable <IExportable> ToExportable(IEnumerable <ICloneable> objects)
        {
            var models = objects.Cast <CatalogProduct>();

            var exportableProducts = models.Select(x =>
            {
                var exportableProduct = AbstractTypeFactory <ExportableProduct> .TryCreateInstance().FromModel(x);
                var imageUrl          = x?.Images?.FirstOrDefault()?.Url;

                exportableProduct.ImageUrl = imageUrl != null ? _blobUrlResolver.GetAbsoluteUrl(imageUrl) : null;
                return(exportableProduct);
            });

            return(exportableProducts);
        }
        public async Task PlatformExportBackgroundAsync(PlatformImportExportRequest exportRequest, PlatformExportPushNotification pushNotification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                pushNotification.Path(x);
                pushNotification.JobId = context.BackgroundJob.Id;
                _pushNotifier.SendAsync(pushNotification);
            };

            try
            {
                const string relativeUrl    = "tmp/exported_data.zip";
                var          localTmpFolder = _hostEnv.MapPath(Path.Combine(_platformOptions.LocalUploadFolderPath, "tmp"));
                var          localTmpPath   = Path.Combine(localTmpFolder, "exported_data.zip");
                if (!Directory.Exists(localTmpFolder))
                {
                    Directory.CreateDirectory(localTmpFolder);
                }
                //Import first to local tmp folder because Azure blob storage doesn't support some special file access mode
                using (var stream = System.IO.File.Open(localTmpPath, FileMode.OpenOrCreate))
                {
                    var manifest = exportRequest.ToManifest();
                    await _platformExportManager.ExportAsync(stream, manifest, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                }
                //Copy export data to blob provider for get public download url
                using (var localStream = System.IO.File.Open(localTmpPath, FileMode.Open))
                    using (var blobStream = _blobStorageProvider.OpenWrite(relativeUrl))
                    {
                        localStream.CopyTo(blobStream);
                        //Get a download url
                        pushNotification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl);
                    }
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Description = "Export finished";
                pushNotification.Finished    = DateTime.UtcNow;
                await _pushNotifier.SendAsync(pushNotification);
            }
        }
Beispiel #31
0
        protected virtual async Task LoadDependenciesAsync(IEnumerable <Category> categories, IDictionary <string, Category> preloadedCategoriesMap)
        {
            var catalogsIds = new { categories }.GetFlatObjectsListWithInterface <IHasCatalogId>().Select(x => x.CatalogId).Where(x => x != null).Distinct().ToArray();
            var catalogsByIdDict = (await _catalogService.GetByIdsAsync(catalogsIds)).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

            //Resolve relative urls for all category assets
            var allImages = new { categories }.GetFlatObjectsListWithInterface <IHasImages>().Where(x => x.Images != null).SelectMany(x => x.Images);

            foreach (var image in allImages.Where(x => !string.IsNullOrEmpty(x.Url)))
            {
                image.RelativeUrl = !string.IsNullOrEmpty(image.RelativeUrl) ? image.RelativeUrl : image.Url;
                image.Url         = _blobUrlResolver.GetAbsoluteUrl(image.Url);
            }

            foreach (var category in categories)
            {
                category.Catalog   = catalogsByIdDict.GetValueOrThrow(category.CatalogId, $"catalog with key {category.CatalogId} doesn't exist");
                category.IsVirtual = category.Catalog.IsVirtual;
                category.Parents   = Array.Empty <Category>();
                //Load all parent categories
                if (category.ParentId != null)
                {
                    category.Parents = TreeExtension.GetAncestors(category, x => x.ParentId != null ? preloadedCategoriesMap[x.ParentId] : null)
                                       .Reverse()
                                       .ToArray();
                    category.Parent = category.Parents.LastOrDefault();
                }
                category.Level = category.Parents?.Count() ?? 0;

                foreach (var link in category.Links ?? Array.Empty <CategoryLink>())
                {
                    link.Catalog = catalogsByIdDict.GetValueOrThrow(link.CatalogId, $"link catalog with key {link.CatalogId} doesn't exist");
                    if (link.CategoryId != null)
                    {
                        link.Category = preloadedCategoriesMap[link.CategoryId];
                    }
                }

                foreach (var property in category.Properties ?? Array.Empty <Property>())
                {
                    property.Catalog = property.CatalogId != null ? catalogsByIdDict[property.CatalogId] : null;
                    if (property.CategoryId != null)
                    {
                        property.Category = preloadedCategoriesMap[property.CategoryId];
                    }
                }
            }
        }
Beispiel #32
0
        public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
        {
            var fileName    = Path.GetFileName((headers.ContentDisposition.FileName ?? headers.ContentDisposition.Name).Replace("\"", string.Empty));
            var relativeUrl = _rootPath + "/" + fileName;
            var absoluteUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl);

            BlobInfos.Add(new BlobInfo
            {
                ContentType = MimeTypeResolver.ResolveContentType(fileName),
                FileName    = fileName,
                Key         = relativeUrl,
                Url         = absoluteUrl
            });

            return(_blobProvider.OpenWrite(_rootPath + "/" + fileName));
        }
		public static webModel.Asset ToWebModel(this moduleModel.Asset asset, IBlobUrlResolver blobUrlResolver)
		{
			var retVal = new webModel.Asset();
			retVal.InjectFrom(asset);
			if (asset.Name == null)
			{
				retVal.Name = HttpUtility.UrlDecode(System.IO.Path.GetFileName(asset.Url));
			}
			if (asset.MimeType == null)
			{
				retVal.MimeType = MimeTypeResolver.ResolveContentType(retVal.Name);
			}

			retVal.Url = blobUrlResolver.GetAbsoluteUrl(asset.Url);
			return retVal;
		}
Beispiel #34
0
        public static webModel.ProductAssetBase ToWebModel(this moduleModel.ItemAsset asset, IBlobUrlResolver blobUrlResolver)
        {
            webModel.ProductAssetBase retVal = new webModel.ProductImage();
            if (asset.Type == moduleModel.ItemAssetType.File)
            {
                var productAsset  = new webModel.ProductAsset();

				productAsset.Name = HttpUtility.UrlDecode(System.IO.Path.GetFileName(asset.Url));
				productAsset.MimeType = MimeTypeResolver.ResolveContentType(productAsset.Name);		

				retVal = productAsset;
            }
            retVal.InjectFrom(asset);
			retVal.Url = blobUrlResolver.GetAbsoluteUrl(asset.Url);
			retVal.RelativeUrl = asset.Url;
            return retVal;
        }
Beispiel #35
0
        public static webModel.ProductAssetBase ToWebModel(this moduleModel.ItemAsset asset, IBlobUrlResolver blobUrlResolver)
        {
            webModel.ProductAssetBase retVal = new webModel.ProductImage();
            if (asset.Type == moduleModel.ItemAssetType.File)
            {
                var productAsset = new webModel.ProductAsset();

                productAsset.Name     = HttpUtility.UrlDecode(System.IO.Path.GetFileName(asset.Url));
                productAsset.MimeType = MimeTypeResolver.ResolveContentType(productAsset.Name);

                retVal = productAsset;
            }
            retVal.InjectFrom(asset);
            retVal.Url         = blobUrlResolver.GetAbsoluteUrl(asset.Url);
            retVal.RelativeUrl = asset.Url;
            return(retVal);
        }
Beispiel #36
0
        protected virtual void InnerLoadDependencies(IEnumerable <CatalogProduct> products, bool processVariations = true)
        {
            var catalogsMap      = _catalogService.GetAllCatalogs().ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);
            var allCategoriesIds = products.Select(x => x.CategoryId).Where(x => x != null).Distinct().ToArray();
            var categoriesMap    = _categoryService.GetByIds(allCategoriesIds).ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);

            foreach (var product in products)
            {
                if (string.IsNullOrEmpty(product.Code))
                {
                    product.Code = _skuGenerator.GenerateSku(product);
                }
                product.Catalog = catalogsMap.GetValueOrThrow(product.CatalogId, $"catalog with key {product.CatalogId} doesn't exist");
                if (product.CategoryId != null)
                {
                    product.Category = categoriesMap.GetValueOrThrow(product.CategoryId, $"category with key {product.CategoryId} doesn't exist");
                }

                if (product.Links != null)
                {
                    foreach (var link in product.Links)
                    {
                        link.Catalog  = catalogsMap.GetValueOrThrow(link.CatalogId, $"link catalog with key {link.CatalogId} doesn't exist");
                        link.Category = _categoryService.GetByIds(new[] { link.CategoryId }, (CategoryResponseGroup.WithProperties | CategoryResponseGroup.WithParents).ToString()).FirstOrDefault();
                    }
                }

                if (product.MainProduct != null)
                {
                    InnerLoadDependencies(new[] { product.MainProduct }, false);
                }
                if (processVariations && !product.Variations.IsNullOrEmpty())
                {
                    InnerLoadDependencies(product.Variations.ToArray());
                }
                //Resolve relative urls for all product assets
                if (product.AllAssets != null)
                {
                    foreach (var asset in product.AllAssets)
                    {
                        asset.Url = _blobUrlResolver.GetAbsoluteUrl(asset.RelativeUrl);
                    }
                }
            }
        }