Beispiel #1
0
        public async Task <ActionResult <ImportDataPreview> > GetImportPreview([FromBody] ImportDataPreviewRequest request)
        {
            if (request.FilePath.IsNullOrEmpty())
            {
                return(BadRequest($"{nameof(request.FilePath)} can not be null"));
            }

            var blobInfo = await _blobStorageProvider.GetBlobInfoAsync(request.FilePath);

            if (blobInfo == null)
            {
                return(BadRequest("Blob with the such url does not exist."));
            }

            using var csvDataSource = _csvPagedPriceDataSourceFactory.Create(request.FilePath, 10);

            var result = new ImportDataPreview
            {
                TotalCount = csvDataSource.GetTotalCount()
            };

            await csvDataSource.FetchAsync();

            result.Results = csvDataSource.Items;

            return(Ok(result));
        }
        public async Task <ICsvCustomerImportReporter> CreateAsync(string reportFilePath, string delimiter)
        {
            var reportBlob = await _blobStorageProvider.GetBlobInfoAsync(reportFilePath);

            if (reportBlob != null)
            {
                await _blobStorageProvider.RemoveAsync(new[] { reportFilePath });
            }

            return(new CsvCustomerImportReporter(reportFilePath, _blobStorageProvider, delimiter));
        }
        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);
        }
        public async Task <ActionResult <ImportDataPreview> > GetImportPreview([FromBody] ImportDataPreviewRequest request)
        {
            if (request.FilePath.IsNullOrEmpty())
            {
                return(BadRequest($"{nameof(request.FilePath)} can not be null"));
            }

            var blobInfo = await _blobStorageProvider.GetBlobInfoAsync(request.FilePath);

            if (blobInfo == null)
            {
                return(BadRequest("Blob with the such url does not exist."));
            }

            var result = new ImportDataPreview();

            switch (request.DataType)
            {
            case nameof(Contact):
                using (var csvDataSource = await _customerImportPagedDataSourceFactory.CreateAsync <ImportableContact, Contact>(request.FilePath,
                                                                                                                                10, null))
                {
                    result.TotalCount = csvDataSource.GetTotalCount();
                    await csvDataSource.FetchAsync();

                    result.Results = csvDataSource.Items.Select(item => item.Record).ToArray();
                }
                break;

            case nameof(Organization):
                using (var csvDataSource = await _customerImportPagedDataSourceFactory.CreateAsync <ImportableOrganization, Organization>(request.FilePath,
                                                                                                                                          10, null))
                {
                    result.TotalCount = csvDataSource.GetTotalCount();
                    await csvDataSource.FetchAsync();

                    result.Results = csvDataSource.Items.Select(item => item.Record).ToArray();
                }
                break;
            }

            return(Ok(result));
        }
Beispiel #5
0
        /// <summary>
        /// Check if image is exist in blob storage by url.
        /// </summary>
        /// <param name="imageUrl">Image url.</param>
        /// <returns>
        /// EntryState if image exist.
        /// Null is image is empty
        /// </returns>
        protected virtual async Task <bool> ExistsAsync(string imageUrl)
        {
            var blobInfo = await _storageProvider.GetBlobInfoAsync(imageUrl);

            return(blobInfo != null);
        }
        private async Task <bool> LicenseExistsAsync(string licenseUrl)
        {
            var blobInfo = await _blobStorageProvider.GetBlobInfoAsync(licenseUrl);

            return(blobInfo != null);
        }
        public async Task ExportAsync(ExportDataRequest request, Action <ExportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var exportProgress = new ExportProgressInfo {
                ProcessedCount = 0, Description = "Export has started"
            };

            var dataSource = _customerExportPagedDataSourceFactory.Create(ModuleConstants.Settings.PageSize, request);

            exportProgress.TotalCount = await dataSource.GetTotalCountAsync();

            progressCallback(exportProgress);

            const string exportDescription = "{0} out of {1} have been exported.";

            exportProgress.Description = "Fetching...";
            progressCallback(exportProgress);

            var dynamicProperties = await _dynamicPropertySearchService.SearchDynamicPropertiesAsync(new DynamicPropertySearchCriteria()
            {
                ObjectTypes = new List <string> {
                    typeof(Contact).FullName, typeof(Organization).FullName
                },
                Skip = 0,
                Take = int.MaxValue
            });

            var contactsDynamicProperties =
                dynamicProperties.Results.Where(x => x.ObjectType == typeof(Contact).FullName).ToArray();

            var organizationsDynamicProperties =
                dynamicProperties.Results.Where(x => x.ObjectType == typeof(Organization).FullName).ToArray();

            var contactsFilePath    = GetExportFilePath("Contacts");
            var contactExportWriter = _exportWriterFactory.Create <ExportableContact>(contactsFilePath, new ExportConfiguration(), contactsDynamicProperties);

            var organizationFilePath     = GetExportFilePath("Organizations");
            var organizationExportWriter = _exportWriterFactory.Create <ExportableOrganization>(organizationFilePath, new ExportConfiguration(), organizationsDynamicProperties);

            try
            {
                while (await dataSource.FetchAsync())
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var contacts = dataSource.Items.OfType <ExportableContact>().ToArray();

                    if (!contacts.IsNullOrEmpty())
                    {
                        contactExportWriter.WriteRecords(contacts);
                    }

                    var organizations = dataSource.Items.OfType <ExportableOrganization>().ToArray();

                    if (!organizations.IsNullOrEmpty())
                    {
                        organizationExportWriter.WriteRecords(organizations);
                    }

                    exportProgress.ProcessedCount += dataSource.Items.Length;
                    exportProgress.Description     = string.Format(exportDescription, exportProgress.ProcessedCount,
                                                                   exportProgress.TotalCount);
                    progressCallback(exportProgress);
                }

                exportProgress.Description = "Export completed";
            }
            finally
            {
                contactExportWriter.Dispose();
                organizationExportWriter.Dispose();
            }

            try
            {
                var contactsFileInfo = await _blobStorageProvider.GetBlobInfoAsync(contactsFilePath);

                var organizationsFileInfo = await _blobStorageProvider.GetBlobInfoAsync(organizationFilePath);

                if (contactsFileInfo.Size > 0)
                {
                    exportProgress.ContactsFileUrl = _blobUrlResolver.GetAbsoluteUrl(contactsFilePath);
                }
                else
                {
                    await _blobStorageProvider.RemoveAsync(new string[] { contactsFilePath });
                }

                if (organizationsFileInfo.Size > 0)
                {
                    exportProgress.OrganizationsFileUrl = _blobUrlResolver.GetAbsoluteUrl(organizationFilePath);
                }
                else
                {
                    await _blobStorageProvider.RemoveAsync(new string[] { organizationFilePath });
                }
            }
            finally
            {
                progressCallback(exportProgress);
            }
        }