private static IEnumerable <IExtensionModel> GetSuggestedExtensions(SuggestionFileModel fileModel, string filePath, out IEnumerable <string> hits)
        {
            List <IExtensionModel> list    = new List <IExtensionModel>();
            List <string>          matches = new List <string>();
            string fileType = Path.GetFileName(filePath);

            foreach (string key in fileModel.Extensions.Keys)
            {
                foreach (SuggestionModel model in fileModel.Extensions[key])
                {
                    string match = model?.FileTypes?.FirstOrDefault(ft => fileType.EndsWith(ft, StringComparison.OrdinalIgnoreCase));

                    if (!string.IsNullOrEmpty(match) || model.Category == SuggestionFileModel.GENERAL)
                    {
                        if (!string.IsNullOrEmpty(model.TextMatch) && File.Exists(filePath))
                        {
                            string content = File.ReadAllText(filePath);
                            if (!Regex.IsMatch(content, model.TextMatch))
                            {
                                continue;
                            }
                        }

                        matches.Add(match);
                        list.Add(model);
                    }
                }
            }

            hits = matches;
            return(list);
        }
Beispiel #2
0
        public static SuggestionFileModel FromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return(null);
            }

            string fileContent = File.ReadAllText(fileName);

            var obj = JObject.Parse(fileContent);
            SuggestionFileModel fileModel = new SuggestionFileModel
            {
                Extensions = new Dictionary <string, IEnumerable <IExtensionModel> >()
            };

            var fileBased = new List <IExtensionModel>();

            foreach (var ext in obj["fileBased"])
            {
                SuggestionModel model = new SuggestionModel
                {
                    Name        = ((JProperty)ext).Name,
                    ProductId   = ext.FirstOrDefault()?["productId"].ToString(),
                    Description = ext.FirstOrDefault()?["description"].ToString(),
                    Link        = ext.FirstOrDefault()?["link"].ToString(),
                    FileTypes   = ext.FirstOrDefault()?["fileTypes"].Values <string>().ToArray(),
                    TextMatch   = ext.FirstOrDefault()?["textMatch"]?.ToString(),
                    Category    = FILE_BASED
                };

                fileBased.Add(model);
            }

            fileModel.Extensions.Add(FILE_BASED, fileBased);

            var general = new List <IExtensionModel>();

            foreach (var ext in obj["general"])
            {
                SuggestionModel model = new SuggestionModel
                {
                    Name        = ((JProperty)ext).Name,
                    ProductId   = ext.FirstOrDefault()?["productId"].ToString(),
                    Link        = ext.FirstOrDefault()?["link"].ToString(),
                    Description = ext.FirstOrDefault()?["description"].ToString(),
                    Category    = GENERAL
                };

                general.Add(model);
            }

            fileModel.Extensions.Add(GENERAL, general);

            return(fileModel);
        }
        public static SuggestionFileModel FromFile(string fileName)
        {
            if (!File.Exists(fileName))
                return null;

            string fileContent = File.ReadAllText(fileName);

            var obj = JObject.Parse(fileContent);
            SuggestionFileModel fileModel = new SuggestionFileModel
            {
                Extensions = new Dictionary<string, IEnumerable<IExtensionModel>>()
            };

            var fileBased = new List<IExtensionModel>();

            foreach (var ext in obj["fileBased"])
            {
                SuggestionModel model = new SuggestionModel
                {
                    Name = ((JProperty)ext).Name,
                    ProductId = ext.FirstOrDefault()?["productId"].ToString(),
                    Description = ext.FirstOrDefault()?["description"].ToString(),
                    Link = ext.FirstOrDefault()?["link"].ToString(),
                    FileTypes = ext.FirstOrDefault()?["fileTypes"].Values<string>().ToArray(),
                    TextMatch = ext.FirstOrDefault()?["textMatch"]?.ToString(),
                    Category = FILE_BASED
                };

                fileBased.Add(model);
            }

            fileModel.Extensions.Add(FILE_BASED, fileBased);

            var general = new List<IExtensionModel>();

            foreach (var ext in obj["general"])
            {
                SuggestionModel model = new SuggestionModel
                {
                    Name = ((JProperty)ext).Name,
                    ProductId = ext.FirstOrDefault()?["productId"].ToString(),
                    Link = ext.FirstOrDefault()?["link"].ToString(),
                    Description = ext.FirstOrDefault()?["description"].ToString(),
                    Category = GENERAL
                };

                general.Add(model);
            }

            fileModel.Extensions.Add(GENERAL, general);

            return fileModel;
        }
        public SuggestionFileModel GetCurrentFileModel()
        {
            if (_model == null)
            {
                string assembly = Assembly.GetExecutingAssembly().Location;
                string folder   = Path.GetDirectoryName(assembly);
                string fileName = Path.Combine(folder, "JSON\\Schema\\", Constants.SUGGESTIONS_FILENAME);

                _model = SuggestionFileModel.FromFile(fileName);
            }

            return(_model);
        }
        public SuggestionFileModel GetCurrentFileModel()
        {
            if (_model == null)
            {
                string assembly = Assembly.GetExecutingAssembly().Location;
                string folder = Path.GetDirectoryName(assembly);
                string fileName = Path.Combine(folder, "JSON\\Schema\\", Constants.SUGGESTIONS_FILENAME);

                _model = SuggestionFileModel.FromFile(fileName);
            }

            return _model;
        }
        public static IEnumerable<IExtensionModel> GetSuggestedExtensions(SuggestionFileModel fileModel, string fileType, out IEnumerable<string> hits)
        {
            List<IExtensionModel> list = new List<IExtensionModel>();
            List<string> matches = new List<string>();

            foreach (string key in fileModel.Extensions.Keys)
                foreach (SuggestionModel model in fileModel.Extensions[key])
                {
                    string match = model?.FileTypes?.FirstOrDefault(ft => fileType.EndsWith(ft, StringComparison.Ordinal));

                    if (!string.IsNullOrEmpty(match) || model.Category == SuggestionFileModel.GENERAL)
                    {
                        matches.Add(match);
                        list.Add(model);
                    }
                }

            hits = matches;
            return list;
        }
Beispiel #7
0
        public static IEnumerable <IExtensionModel> GetSuggestedExtensions(SuggestionFileModel fileModel, string fileType, out IEnumerable <string> hits)
        {
            List <IExtensionModel> list    = new List <IExtensionModel>();
            List <string>          matches = new List <string>();

            foreach (string key in fileModel.Extensions.Keys)
            {
                foreach (SuggestionModel model in fileModel.Extensions[key])
                {
                    string match = model?.FileTypes?.FirstOrDefault(ft => fileType.EndsWith(ft, StringComparison.Ordinal));

                    if (!string.IsNullOrEmpty(match) || model.Category == SuggestionFileModel.GENERAL)
                    {
                        matches.Add(match);
                        list.Add(model);
                    }
                }
            }

            hits = matches;
            return(list);
        }
        private static IEnumerable<IExtensionModel> GetSuggestedExtensions(SuggestionFileModel fileModel, string filePath, out IEnumerable<string> hits)
        {
            List<IExtensionModel> list = new List<IExtensionModel>();
            List<string> matches = new List<string>();
            string fileType = Path.GetFileName(filePath);

            foreach (string key in fileModel.Extensions.Keys)
                foreach (SuggestionModel model in fileModel.Extensions[key])
                {
                    string match = model?.FileTypes?.FirstOrDefault(ft => fileType.EndsWith(ft, StringComparison.OrdinalIgnoreCase));

                    if (!string.IsNullOrEmpty(match) || model.Category == SuggestionFileModel.GENERAL)
                    {
                        if (!string.IsNullOrEmpty(model.TextMatch) && File.Exists(filePath))
                        {
                            string content = File.ReadAllText(filePath);
                            if (!Regex.IsMatch(content, model.TextMatch))
                                continue;
                        }

                        matches.Add(match);
                        list.Add(model);
                    }
                }

            hits = matches;
            return list;
        }