Ejemplo n.º 1
0
 protected void Load()
 {
     if (_fileWrapper.Exists(_settingsPath))
     {
         var text = _fileWrapper.ReadAllText(_settingsPath);
         _settings = JsonConvert.DeserializeObject <T>(text);
     }
 }
Ejemplo n.º 2
0
 //Text
 void Text(string path, bool isJS)
 {
     try
     {
         var    server      = HttpContext.Server;
         string decodedPath = server.UrlDecode(path);
         if (String.IsNullOrEmpty(decodedPath))
         {
             return;
         }
         List <string> urls  = new List <string>();
         List <string> paths = new List <string>();
         Regex         r     = new Regex(@"://(?<host>([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]+[a-z])");
         FileInfo      fi;
         string[]      array = decodedPath.Split('|');
         if (array == null || array.Length == 0)
         {
             return;
         }
         foreach (string s in array)
         {
             if (!r.IsMatch(s))
             {
                 fi = new FileInfo(server.MapPath(server.UrlDecode(s)));
                 if (!fi.Exists || ((fi.Extension != ".js" && isJS) || (fi.Extension != ".css" && !isJS)))
                 {
                     continue;
                 }
                 paths.Add(fi.FullName);
             }
             else
             {
                 if (PermittedUrls.IndexOf(r.Match(s).Result("${host}")) <= 0)
                 {
                     continue;
                 }
                 urls.Add(s);
             }
         }
         string encodingHeader = Request.Headers["Accept-Encoding"];
         bool   gzip           = !String.IsNullOrEmpty(encodingHeader) && (encodingHeader.Contains("gzip") || encodingHeader.Contains("deflate"));
         string encoding       = gzip ? "gzip" : "utf-8";
         string key            = MD5Fingerprint(decodedPath + encoding);
         byte[] bytes          = GetBytesFromCache(key);
         if (bytes == null)
         {
             using (var stream = new MemoryStream())
                 using (var wstream = gzip ? (Stream) new GZipStream(stream, CompressionMode.Compress) : stream)
                 {
                     var allFileText = new StringBuilder();
                     foreach (string filePath in paths)
                     {
                         allFileText.Append(file.ReadAllText(filePath));
                         allFileText.Append(Environment.NewLine);
                     }
                     foreach (string url in urls)
                     {
                         allFileText.Append(file.ReadAllTextFromUrl(url));
                         allFileText.Append(Environment.NewLine);
                     }
                     byte[] utf8Bytes = Encoding.UTF8.GetBytes(allFileText.ToString());
                     wstream.Write(utf8Bytes, 0, utf8Bytes.Length);
                     wstream.Close();
                     bytes = stream.ToArray();
                     HttpContext.Cache.Insert(key, bytes, null, /*or: new CacheDependency(paths), */ Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(60));
                 }
         }
         Response.AddFileDependencies(paths.ToArray());
         Response.ContentType = isJS ? "application/x-javascript" : "text/css";
         Response.AppendHeader("Content-Encoding", encoding);
         Response.Cache.SetCacheability(HttpCacheability.Public);
         Response.Cache.SetExpires(Cache.NoAbsoluteExpiration);
         Response.Cache.SetLastModifiedFromFileDependencies();
         Response.OutputStream.Write(bytes, 0, bytes.Length);
         Response.Flush();
     }
     catch (HttpException)
     {
     }
     catch (Exception ex)
     {
         Logger.LogException(path, ex);
     }
 }