private void CreateImagesJsonFiles(List <Feature> features)
        {
            _logger.LogInformation("Staring Image file creation: " + features.Count + " features");
            var items = new ConcurrentBag <ImageItem>();
            var size  = 200;

            Parallel.ForEach(features, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, (feature) =>
            {
                var urls = feature.Attributes.GetNames()
                           .Where(n => n.StartsWith(FeatureAttributes.IMAGE_URL)).Select(n => feature.Attributes[n].ToString())
                           .Where(u => !string.IsNullOrWhiteSpace(u));
                foreach (var url in urls)
                {
                    var needResize = true;
                    var updatedUrl = url;
                    var pattern    = @"(http.*\/\/upload\.wikimedia\.org\/wikipedia\/(commons|he|en)\/)(.*\/)(.*)";
                    if (Regex.Match(url, pattern).Success)
                    {
                        updatedUrl = Regex.Replace(url, pattern, $"$1thumb/$3$4/{size}px-$4");
                        updatedUrl = url.EndsWith(".svg") ? updatedUrl + ".png" : updatedUrl;
                        needResize = false;
                    }
                    try
                    {
                        var content = _remoteFileFetcherGateway.GetFileContent(updatedUrl).Result.Content;
                        if (content.Length == 0)
                        {
                            _logger.LogWarning("The following image does not exist: " + url + " feature: " + feature.GetId());
                            continue;
                        }
                        var image  = Image.FromStream(new MemoryStream(content));
                        var format = image.RawFormat.ToString().ToLowerInvariant();
                        if (!needResize)
                        {
                            items.Add(new ImageItem {
                                ImageUrl = url, Data = $"data:image/{format};base64," + Convert.ToBase64String(content)
                            });
                        }
                        else
                        {
                            content = ResizeImage(image, size);
                            items.Add(new ImageItem {
                                ImageUrl = url, Data = $"data:image/jpeg;base64," + Convert.ToBase64String(content)
                            });
                        }
                    }
                    catch (Exception)
                    {
                        _logger.LogWarning("The following image is not an image: " + url + " feature: " + feature.GetId());
                    }
                }
            });

            var list  = items.ToList();
            var index = 0;

            while (list.Count > 0)
            {
                var imageItemsString = JsonConvert.SerializeObject(list.Take(1000).ToList(), new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                _fileSystemHelper.WriteAllText($"images{index.ToString("000")}.json", imageItemsString);
                list = list.Skip(1000).ToList();
                index++;
            }
            _logger.LogInformation("Finished Image file creation: " + items.Count());
        }
Exemplo n.º 2
0
        public override ValidationResult Validate(RepositoryInfo repositoryInfo, Action <ValidationResult> notify = null)
        {
            var result    = new ValidationResult(this);
            var fileNames = _fileSystemHelper.GetFiles(repositoryInfo.RepositoryRootPath, _fileNamePattern, _sourceFileFilters).ToList();

            string message;

            var backupFileName = string.Empty;

            if (_isBackupEnabled)
            {
                try
                {
                    backupFileName = Backup(repositoryInfo.RepositoryRootPath, fileNames);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_rule_can_not_create_backup_file, TransformMessage.ToLower());
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    return(result);
                }
            }

            foreach (var fileName in fileNames)
            {
                string code;
                try
                {
                    code = _fileSystemHelper.ReadAllText(fileName);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_Can_not_read_file, TransformingMessage.ToLower(), fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }

                if (_isBackupEnabled)
                {
                    if (!BackupExists(backupFileName, fileName, code))
                    {
                        message = string.Format(Resources.TransformRule_Validate_Not_file_because_can_not_verify_backup, TransformingMessage.ToLower(), fileName);
                        result.AddResult(ResultLevel.Error, message, notify);
                        Logger.Error(message);
                        continue;
                    }
                }

                string transformedCode;
                try
                {
                    transformedCode = Transform(code, fileName, notify);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_error_in_file, TransformerMessage, e.Message, fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }
                try
                {
                    _fileSystemHelper.WriteAllText(fileName, transformedCode, Encoding.UTF8);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_Code_error_in_file, TransformingMessage.ToLower(), fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }

                message = string.Format(Resources.TransformRule_Validate_File_successfully, TransformedMessage.ToLower(), fileName);
                result.AddResult(ResultLevel.Passed, message, notify);
            }
            return(result);
        }