private static string GetBrandKey(string brandName) { return(PartNumberCleaner.GetCleanedBrandName(brandName)?.ToLower()); }
private async Task <AzureFileInfo> FindAndUploadPartNumberThumbnailAsync( string brandName, string partNumber, string imageKey) { try { var cleanedBrandName = PartNumberCleaner.GetCleanedBrandName(brandName); var cleanedPartNumber = PartNumberCleaner.GetCleanedPartNumber(partNumber); var articles = await _tecDocWsClient.SearchByArticleNumberAsync(cleanedPartNumber, null, ArticleNumberTypeEnum.ArticleNumber); var article = articles .Where(x => PartNumberCleaner.GetCleanedBrandName(x.BrandName)?.Equals(cleanedBrandName, StringComparison.OrdinalIgnoreCase) == true) .FirstOrDefault(); if (article == null) { return(null); } var articleExtendedInfos = await _tecDocWsClient.GetArticlesExtendedInfoAsync(new long[] { article.ArticleId }); var articleExtendedInfo = articleExtendedInfos.FirstOrDefault(); if (articleExtendedInfo?.ArticleDocuments == null || articleExtendedInfo.ArticleDocuments.Length == 0) { return(null); } var imageDocument = articleExtendedInfo.ArticleDocuments .Where(x => x.DocFileName != null) .Where(x => x.DocFileName.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) || x.DocFileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || x.DocFileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) || x.DocFileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); if (imageDocument == null) { return(null); } var fileName = $"{imageKey}{Path.GetExtension(imageDocument.DocFileName)}"; var fileInfo = await _azureStorageClient.GetBlobFileIfExistsAsync( EkBlobContainerNames.Images, fileName); if (fileInfo != null) { // if uploaded earlier return(fileInfo); } var imageContent = await _tecDocWsClient.GetDocumentAsync(imageDocument.DocId, imageDocument.DocTypeId); if (imageContent.Content == null) { return(null); } const string ImageBodyStartMarker = "base64,"; var imageBodyStartIndex = imageContent.Content.IndexOf(ImageBodyStartMarker, StringComparison.Ordinal); if (imageBodyStartIndex == -1) { return(null); } imageBodyStartIndex += ImageBodyStartMarker.Length; var imageBodyBase64 = imageContent.Content.Substring(imageBodyStartIndex); var imageBody = Convert.FromBase64String(imageBodyBase64); fileInfo = await _azureStorageClient.UploadBlobFileAsync( EkBlobContainerNames.Images, fileName, imageBody); return(fileInfo); } catch (Exception ex) { _logger.LogError(LoggingEvents.UnhandledException, nameof(FindAndUploadPartNumberThumbnailAsync), ex); return(null); } }