protected virtual BrowscapXmlHelper GetBrowscapXmlHelper() { if (Singleton <BrowscapXmlHelper> .Instance != null) { return(Singleton <BrowscapXmlHelper> .Instance); } //no database created if (string.IsNullOrEmpty(_config.UserAgentFilePath)) { return(null); } //prevent multi loading data lock (_locker) { //data can be loaded while we waited if (Singleton <BrowscapXmlHelper> .Instance != null) { return(Singleton <BrowscapXmlHelper> .Instance); } var userAgentStringsPath = _fileProvider.MapContentPath(_config.UserAgentFilePath); var crawlerOnlyUserAgentStringsPath = !string.IsNullOrEmpty(_config.CrawlerOnlyUserAgentFilePath) ? _fileProvider.MapContentPath(_config.CrawlerOnlyUserAgentFilePath) : string.Empty; var browscapXmlHelper = new BrowscapXmlHelper(userAgentStringsPath, crawlerOnlyUserAgentStringsPath); Singleton <BrowscapXmlHelper> .Instance = browscapXmlHelper; return(Singleton <BrowscapXmlHelper> .Instance); } }
/// <summary> /// Save picture binary to file system /// </summary> /// <param name="pictureId"></param> /// <param name="pictureBinary"></param> /// <param name="mimeType"></param> protected virtual void SavePictureToFile(int pictureId, byte[] pictureBinary, string mimeType) { //ensure content/images directory exists var picturesDirectoryPath = _fileProvider.MapContentPath(PictureFolderPath); if (!Directory.Exists(picturesDirectoryPath)) { Directory.CreateDirectory(picturesDirectoryPath); } string fileName = string.Format(PictureFileNameFormat, pictureId, MimeTypeMap.GetExtension(mimeType)); var filePath = GetPictureLocalPath(fileName); File.WriteAllBytes(filePath, pictureBinary); }
public virtual void Log(LogLevel logLevel, string shortMessage, string fullMessage = null, string ipAddress = null, string pageUrl = null, string referrerUrl = null) { if (string.IsNullOrEmpty(shortMessage)) { return; } string logMessage = string.Format("{0} [{1}] {2}", DateTime.UtcNow.ToString("dd.MM.yyyy HH:mm:ss.fff zzz"), logLevel.ToString(), shortMessage); _fileProvider.CreateDirectory(_fileProvider.MapContentPath("logs")); using (StreamWriter sw = File.AppendText(_fileProvider.MapContentPath(string.Format("logs/{0:dd.MM.yyyy}.txt", DateTime.UtcNow)))) { sw.WriteLine(logMessage); } }
protected virtual async Task<string> FindLocation(string ipAddress) { try { var databasePath = _fileProvider.MapContentPath("~/Country.mmdb"); var reader = new DatabaseReader(databasePath); var response = reader.Country(ipAddress); if (response != null && response.Country != null && string.IsNullOrEmpty(response.Country.IsoCode)) throw new GeoIP2Exception("Country not found"); return await Task.FromResult(response.Country?.IsoCode); } catch (Exception ex) { _logger.Error("An error occurred while finding country.", ex, ipAddress); return null; } }