Пример #1
0
        public void Reload()
        {
            this.bytes = File.ReadAllBytes(path);

            bool shouldCompress;

            this.contentType = MimeUtils.GetContentType(path, out shouldCompress, out isDownload);

            if (!path.Contains(".min."))
            {
                var ext = Path.GetExtension(path);
                switch (ext)
                {
                case ".css":
                {
                    var temp = Encoding.UTF8.GetString(bytes);
                    temp       = CSSMinifier.Compress(temp);
                    this.bytes = Encoding.UTF8.GetBytes(temp);
                    break;
                }

                case ".js":
                {
                    var temp = Encoding.UTF8.GetString(bytes);
                    temp       = JSMinifier.Compress(temp);
                    this.bytes = Encoding.UTF8.GetBytes(temp);
                    break;
                }

                case ".html":
                {
                    var temp = Encoding.UTF8.GetString(bytes);
                    temp       = HTMLMinifier.Compress(temp);
                    this.bytes = Encoding.UTF8.GetBytes(temp);
                    break;
                }
                }
            }

            if (shouldCompress && (this.bytes.Length < 1400 || this.bytes.Length > 1024 * 512))
            {
                shouldCompress = false;
            }

            this.isCompressed = shouldCompress;
            this.hash         = StringUtils.MD5(this.bytes);

            if (shouldCompress)
            {
                this.bytes = this.bytes.GZIPCompress();
            }
        }
Пример #2
0
        private void _WriteConstants(HttpRequest request)
        {
            StringBuilder js = new StringBuilder();

            js.AppendLine("var DIRECTORY_SEPERATOR = '" + _EscapeCode(Path.DirectorySeparatorChar.ToString()) + "';");
            js.AppendLine("var HELP_CLASS= 'help';");
            js.AppendLine("var NPANXX_HELP='N = [2-9]<br/>Z = [1-9]<br/>X = [0-9]<br/>. = \\d+<br/>| = ignore on output, bracket in the validation';");
            js.AppendLine("var CURRENT_OS=" + JSON.JsonEncode(Utility.OperatingSystem) + ";");
            js.AppendLine("var PORT_RANGE_REGEX = '" + Constants.PORT_RANGE_REGEX + "';");
            js.AppendLine("var IS_SETUP = " + Utility.IsSiteSetup.ToString().ToLower() + ";");
            if (JavascriptConstants.Current.Count > 0)
            {
                foreach (string str in JavascriptConstants.Current.Keys)
                {
                    js.AppendLine("var " + str + " = '" + _EscapeCode(JavascriptConstants.Current[str]) + "';");
                }
            }
            js.AppendLine("Backbone=_.extend(Backbone,{HasConfigurationChangesToMake:" + ConfigurationController.HasChangesToMake.ToString().ToLower() + "});");
            request.ResponseWriter.WriteLine(JSMinifier.Minify(js.ToString()));
        }
Пример #3
0
 public AssetCache(LoggerCallback logger, string filePath) : base(logger, filePath)
 {
     BuildAssetCache("js", "application/javascript", x => JSMinifier.Compress(x));
     BuildAssetCache("css", "text/css", x => CSSMinifier.Compress(x));
 }
Пример #4
0
 public void Does_add_seperator_between_strings()
 {
     Assert.That(JSMinifier.MinifyJs("let a = `b`\nlet c = 1").Trim(), Is.EqualTo("let a=`b`\nlet c=1"));
     Assert.That(JSMinifier.MinifyJs("let a = 'b'\nlet c = 1").Trim(), Is.EqualTo("let a='b'\nlet c=1"));
     Assert.That(JSMinifier.MinifyJs("let a = \"b\"\nlet c = 1").Trim(), Is.EqualTo("let a=\"b\"\nlet c=1"));
 }
Пример #5
0
        public void ProcessRequest(HttpRequest request, Org.Reddragonit.EmbeddedWebServer.Interfaces.Site site)
        {
            request.ResponseHeaders["Cache-Control"] = "max-age = " + (60 * 60).ToString();
            List <string> paths = new List <string>();
            string        ext   = request.URL.AbsolutePath.Substring(request.URL.AbsolutePath.LastIndexOf("."));
            string        bPath = "scripts";

            if (ext == ".css")
            {
                bPath = "styles";
            }
            switch (request.URL.AbsolutePath)
            {
            case "/resources/scripts/core.js":
            case "/resources/styles/core.css":
                paths.AddRange(_CORE_PATHS);
                break;

            case "/resources/scripts/setup.js":
            case "/resources/styles/setup.css":
                paths.AddRange(_SETUP_PATHS);
                break;

            case "/resources/scripts/user.js":
                paths.AddRange(_USER_PATHS);
                foreach (MainMenuItem mmi in MainMenuItem.LoadAll())
                {
                    if (mmi.JavascriptURLs != null)
                    {
                        paths.AddRange(mmi.JavascriptURLs);
                    }
                    if (mmi.CombinedURLs != null)
                    {
                        paths.AddRange(mmi.CombinedURLs);
                    }
                    if (mmi.SubMenuItems != null)
                    {
                        foreach (SubMenuItem smi in mmi.SubMenuItems)
                        {
                            if (smi.JavascriptURLs != null)
                            {
                                paths.AddRange(smi.JavascriptURLs);
                            }
                            if (smi.CombinedURLs != null)
                            {
                                paths.AddRange(smi.CombinedURLs);
                            }
                        }
                    }
                    foreach (IHomePageComponent ihp in parts)
                    {
                        if (ihp.JSUrls != null)
                        {
                            paths.AddRange(ihp.JSUrls);
                        }
                    }
                }
                break;

            case "/resources/styles/user.css":
                paths.AddRange(_USER_PATHS);
                foreach (MainMenuItem mmi in MainMenuItem.LoadAll())
                {
                    if (mmi.CssURLs != null)
                    {
                        paths.AddRange(mmi.CssURLs);
                    }
                    if (mmi.CombinedURLs != null)
                    {
                        paths.AddRange(mmi.CombinedURLs);
                    }
                    if (mmi.SubMenuItems != null)
                    {
                        foreach (SubMenuItem smi in mmi.SubMenuItems)
                        {
                            if (smi.CssURLs != null)
                            {
                                paths.AddRange(smi.CssURLs);
                            }
                            if (smi.CombinedURLs != null)
                            {
                                paths.AddRange(smi.CombinedURLs);
                            }
                        }
                    }
                    foreach (IHomePageComponent ihp in parts)
                    {
                        if (ihp.CSSUrls != null)
                        {
                            paths.AddRange(ihp.CSSUrls);
                        }
                    }
                }
                break;
            }
            request.ResponseHeaders.ContentType = HttpUtility.GetContentTypeForExtension(request.URL.AbsolutePath.Substring(request.URL.AbsolutePath.LastIndexOf(".")));
            foreach (string str in paths)
            {
                if (str.StartsWith("TYPE=") || str.StartsWith("/EmbeddedJSGenerator.js?TYPE="))
                {
                    if (ext != ".css")
                    {
                        request.ResponseWriter.WriteLine("/* " + str + " */");
                        foreach (IRequestHandler irh in site.Handlers)
                        {
                            if (irh is EmbeddedServiceHandler)
                            {
                                request.ResponseWriter.WriteLine(((EmbeddedServiceHandler)irh).GenerateJSForServiceType(str.Substring(str.IndexOf("=") + 1)));
                            }
                        }
                    }
                }
                else
                {
                    List <string> tpaths = new List <string>();
                    if (str.StartsWith("/"))
                    {
                        if (str.EndsWith(".min" + ext))
                        {
                            tpaths.Add(str);
                            tpaths.Add(str.Substring(0, str.LastIndexOf(".min")) + ext);
                        }
                        else
                        {
                            tpaths.Add(str);
                            tpaths.Add(str.Substring(0, str.LastIndexOf(ext)) + ".min" + ext);
                        }
                    }
                    else
                    {
                        tpaths.AddRange(new string[] {
                            "/resources/" + bPath + "/" + str.Replace(".", "/") + ext,
                            "/resources/" + bPath + "/base/" + str.Replace(".", "/") + ext,
                            "/resources/" + bPath + "/" + (request.IsMobile ? "mobile" : "desktop") + "/" + str.Replace(".", "/") + ext,
                            "Org.Reddragonit.FreeSwitchConfig.Site." + bPath + ".base." + str + ext,
                            "Org.Reddragonit.FreeSwitchConfig.Site." + bPath + "." + (request.IsMobile ? "mobile" : "desktop") + "." + str + ext,
                            "/resources/" + bPath + "/" + str.Replace(".", "/") + ".min" + ext,
                            "/resources/" + bPath + "/base/" + str.Replace(".", "/") + ".min" + ext,
                            "/resources/" + bPath + "/" + (request.IsMobile ? "mobile" : "desktop") + "/" + str.Replace(".", "/") + ".min" + ext,
                            "Org.Reddragonit.FreeSwitchConfig.Site." + bPath + ".base." + str + ".min" + ext,
                            "Org.Reddragonit.FreeSwitchConfig.Site." + bPath + "." + (request.IsMobile ? "mobile" : "desktop") + "." + str + ".min" + ext
                        });
                    }
                    foreach (string path in tpaths)
                    {
                        if (path.StartsWith("/"))
                        {
                            request.ResponseWriter.WriteLine("/* " + path + " */");
                            VirtualMappedRequest vmp = new VirtualMappedRequest(new Uri("http://" + request.URL.Host + ":" + request.URL.Port.ToString() + path), request.Headers["Accept-Language"]);
                            Org.Reddragonit.BackBoneDotNet.RequestHandler.HandleRequest(vmp);
                            request.ResponseWriter.WriteLine(vmp.ToString());
                            if (site.EmbeddedFiles != null)
                            {
                                if (site.EmbeddedFiles.ContainsKey(path))
                                {
                                    if (ModelHandler.CompressJS)
                                    {
                                        request.ResponseWriter.WriteLine((request.URL.AbsolutePath.EndsWith(".js") ? JSMinifier.Minify(Utility.ReadEmbeddedResource(site.EmbeddedFiles[path].DLLPath)) : CSSMinifier.Minify(Utility.ReadEmbeddedResource(site.EmbeddedFiles[path].DLLPath))));
                                    }
                                    else
                                    {
                                        request.ResponseWriter.WriteLine(Utility.ReadEmbeddedResource(site.EmbeddedFiles[path].DLLPath));
                                    }
                                }
                            }
                            string tmpStr = Utility.ReadEmbeddedResource(_ReverseURL(path));
                            if (tmpStr != null)
                            {
                                if (ModelHandler.CompressJS)
                                {
                                    request.ResponseWriter.WriteLine((request.URL.AbsolutePath.EndsWith(".js") ? JSMinifier.Minify(tmpStr) : CSSMinifier.Minify(tmpStr)));
                                }
                                else
                                {
                                    request.ResponseWriter.WriteLine(tmpStr);
                                }
                            }
                        }
                        else
                        {
                            request.ResponseWriter.WriteLine("/* " + _ExtractURL(path) + " */");
                            request.ResponseWriter.WriteLine(Utility.ReadEmbeddedResource(path));
                        }
                    }
                }
            }
            if (request.URL.AbsolutePath == "/resources/scripts/core.js")
            {
                _WriteConstants(request);
            }
            else if (request.URL.AbsolutePath == "/resources/scripts/setup.js")
            {
            }
            else if (request.URL.AbsolutePath == "/resources/scripts/user.js")
            {
                Template st = new Template(Utility.ReadEmbeddedResource("Org.Reddragonit.FreeSwitchConfig.Site.Deployments.home.js"));
                st.SetAttribute("components", parts);
                request.ResponseWriter.WriteLine(st.ToString());
            }
        }