Exemplo n.º 1
0
        public void ImportGeoIPCountryData(string path, bool ignoreInvalidRows)
        {
            _cancelImport = false;
            FileStream fs = null;

            IsRunning = true;
            try
            {
                var files = Directory.EnumerateFiles(path, "GeoIPCountryWhois.csv").ToList();
                if (!files.Any())
                {
                    IsRunning = false;
                    if (OnComplete != null)
                    {
                        OnComplete.Invoke(this, new EventArgs());
                    }
                    _progressText.Invoke("could not locate file 'GeoIPCountryWhois.csv', aborting.");
                    return;
                }

                List <IBulkCopyItem> geopIpCountries = new List <IBulkCopyItem>();
                string fileName = files.First();
                fs = File.OpenRead(fileName);
                StreamReader sr = new StreamReader(fs);
                _progressCallback.Invoke(0);
                long bytesRead = 0;
                while (!sr.EndOfStream)
                {
                    if (_cancelImport)
                    {
                        return;
                    }

                    string lineData = sr.ReadLine();

                    if (lineData == null)
                    {
                        break;
                    }

                    if (lineData.Length > 0 && lineData[0] == '#')
                    {
                        continue;
                    }

                    bytesRead += lineData.Length;
                    _progressCallback.Invoke((double)fs.Length / bytesRead);
                    MatchCollection mc = Regex.Matches(lineData, "\".*?\"");

                    try
                    {
                        if (mc.Count == 6)
                        {
                            var geoIpCountry = new GeoIPCountryBo()
                            {
                                IPAddressFrom = mc[0].Value.Replace("\"", ""),
                                IPAddressTo   = mc[1].Value.Replace("\"", ""),
                                IPFrom        = long.Parse(mc[2].Value.Replace("\"", "")),
                                IPTo          = long.Parse(mc[3].Value.Replace("\"", "")),
                                CountryCode   = mc[4].Value.Replace("\"", ""),
                                CountryName   = mc[5].Value.Replace("\"", ""),
                            };
                            geopIpCountries.Add(new BulkCopyItemBo(geoIpCountry));
                        }
                    }
                    catch (Exception ex)
                    {
                        _progressText.Invoke("Exception while parsing GeoIPCountry row: " + ex.Message);
                        if (!ignoreInvalidRows)
                        {
                            return;
                        }
                    }
                }
                fs.Close();
                fs = null;

                _progressText.Invoke(string.Format("Finished parsing {0} rows from import file. Starting database import", geopIpCountries.Count));

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                _geoIpRepository = new GeoIPRepository();

                // GeoIPCountryBo column definition
                List <ColumnDefinition> geoIpCountryDefinitions = new List <ColumnDefinition>();
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 0, ColumnName = "IPFrom", ColumnDataType = typeof(long)
                });
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 1, ColumnName = "IPTo", ColumnDataType = typeof(long)
                });
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 2, ColumnName = "IPAddressFrom", ColumnDataType = typeof(string)
                });
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 3, ColumnName = "IPAddressTo", ColumnDataType = typeof(string)
                });
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 4, ColumnName = "CountryCode", ColumnDataType = typeof(string)
                });
                geoIpCountryDefinitions.Add(new ColumnDefinition()
                {
                    Index = 5, ColumnName = "CountryName", ColumnDataType = typeof(string)
                });

                _geoIpRepository.ImportGeoIPCountryList(geopIpCountries, geoIpCountryDefinitions, d => _progressCallback(d));
                stopwatch.Stop();
                _progressText.Invoke("Database import completed after: " + stopwatch.Elapsed);

                if (OnComplete != null)
                {
                    OnComplete.Invoke(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                _errorStringBuilder.AppendLine(ex.Message);
                if (OnError != null)
                {
                    OnError.Invoke(this, new ErrorEventArgs(ex));
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                IsRunning = false;
            }
        }
Exemplo n.º 2
0
 public GeoIPCountryDto ConvertToGeoIPCountryDto(GeoIPCountryBo geoIPCountryBo)
 {
     return(ObjectMapper.Map <GeoIPCountryBo, GeoIPCountryDto>(geoIPCountryBo));
 }