Exemple #1
0
        /// <summary>
        /// This method asynchronously reads the construct from the filesystem
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public async Task <PublicSuffixDatabase> ReadFromFileAsync(string filename)
        {
            // Instantiate our stream reader
            using StreamReader streamReader = new StreamReader(filename);
            // Read the file into memory
            string json = await streamReader.ReadToEndAsync();

            // Check the content
            if (!string.IsNullOrEmpty(json) && !string.IsNullOrWhiteSpace(json))
            {
                // Deserialize the file
                PublicSuffixDatabase fileInstance = JsonConvert.DeserializeObject <PublicSuffixDatabase>(json);
                // Set the custom top-level domains into the instance
                CustomTopLevelDomains = fileInstance.CustomTopLevelDomains;
                // Set the last refreshed timestamp into the instance
                LastRefresh = fileInstance.LastRefresh;
                // Set the next refresh timestamp into the instance
                NextRefresh = fileInstance.NextRefresh;
                // Set the top-level domains into the instance
                TopLevelDomains = fileInstance.TopLevelDomains;
            }
            // We're done with the file reader, close it
            streamReader.Close();
            // We're done, return the instance
            return(this);
        }
        /// <summary>
        /// This method asynchronously refreshes the PublicSuffix database
        /// </summary>
        /// <returns></returns>
        public async Task <PublicSuffix> RefreshAsync()
        {
            // Check the next refresh timestamp to see if we even need to refresh
            if (_nextRefresh.HasValue && (_nextRefresh.Value > DateTime.UtcNow))
            {
                return(this);
            }
            // Ensure we have a database path
            if (string.IsNullOrEmpty(PublicSuffixPath) || string.IsNullOrWhiteSpace(PublicSuffixPath))
            {
                PublicSuffixPath = Path.GetTempFileName();
            }
            // Read the database from the file
            PublicSuffixDatabase database = await PublicSuffixDatabase.OpenAsync(PublicSuffixPath);

            // Check the value of the next refresh
            if (database.LastRefresh.HasValue && (database.LastRefresh.Value > DateTime.UtcNow))
            {
                // Set the custom TLDs into the instance
                _customTopLevelDomains.AddRange(database.CustomTopLevelDomains);
                // Set the last refreshed timestamp into the instance
                _lastRefresh = database.LastRefresh;
                // Set the next refresh timestamp into the instance
                _nextRefresh = database.NextRefresh;
                // Set the top-level domains into the instance
                _topLevelDomains.AddRange(database.TopLevelDomains);
                // We're done, return the instance
                return(this);
            }
            // Set the last refresh timestamp into the instance
            _lastRefresh = DateTime.UtcNow;
            // Set the next refresh timestamp into the instance
            _nextRefresh = _lastRefresh.Value.AddHours(24);
            // Localize our WebClient
            using WebClient client = new WebClient();
            // Download the Public Suffix database
            string publicSuffixDatabase = await client.DownloadStringTaskAsync(new Uri(DatabaseUrl));

            // We're done, localize the data and return the instance
            Localize(publicSuffixDatabase, null);
            // Writeh the datbase to file
            await PublicSuffixDatabase.StoreAsync(PublicSuffixPath, this);

            // We're done, return the instance
            return(this);
        }