Пример #1
0
        internal string GetContentFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(controllerContext.RequestContext);

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Stylesheets.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var stylesheets = ses.ToList();

                var key = "scripthelper.rendercss." + string.Join("|", stylesheets.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder stylo = MothScriptHelper.GetFileContent(stylesheets, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(stylo.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    string url = urlHelper.Content("~/resources/css/");
                    return(string.Format("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{2}?keys={0}&amp;{1}\" />", string.Join("|", stylesheets.ToArray()), hashcode, url));
                }, Provider.CacheDurations.ExternalScript));
            }

            return(sb.ToString());
        }
Пример #2
0
        internal string GetScriptsFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(new RequestContext(httpContext, controllerContext.RouteData));

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Scripts.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var scripts = ses.ToList();

                //var scripts = MothScriptHelper.Scripts[cat].ToList();

                var key = "scripthelper.renderscripts." + string.Join("|", scripts.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder script = MothScriptHelper.GetFileContent(scripts, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(script.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    var keySb = new StringBuilder();
                    if (Provider.Enable.ScriptMinification)
                    {
                        string url = urlHelper.Content("~/resources/javascript/");
                        keySb.AppendLine(string.Format("<script src=\"{2}?keys={0}&amp;{1}\"></script>", string.Join("|", scripts.ToArray()), hashcode, url));
                    }
                    else
                    {
                        foreach (var s in scripts)
                        {
                            string url = urlHelper.Content(s);
                            keySb.AppendLine(string.Format("<script src=\"{0}?{1}\"></script>", url, hashcode));
                        }
                    }
                    return(keySb.ToString());
                }, Provider.CacheDurations.ExternalScript));
            }

            sb.AppendLine(RenderDataUriFallback(httpContext, controllerContext));

            if (MothScriptHelper.InlineScripts.Any())
            {
                var isb = new StringBuilder("<script>");
                foreach (var s in MothScriptHelper.InlineScripts)
                {
                    isb.AppendLine(s);
                }
                isb.Append("</script>");

                sb.Append(isb.ToString());
            }

            return(sb.ToString());
        }
Пример #3
0
        internal static DataUriImage GetDataUriImageFromCache(string image, HttpContextBase httpContext)
        {
            return(Provider.GetFromCache("datauri." + image, () =>
            {
                var img = new DataUriImage();
                img.ImageUrl = image;

                byte[] buffer;

                Uri uri;
                if (Uri.TryCreate(image, UriKind.RelativeOrAbsolute, out uri) && uri.IsAbsoluteUri)
                {
                    // download the file
                    using (var wc = new WebClient())
                    {
                        buffer = wc.DownloadData(uri);
                    }
                }
                else
                {
                    buffer = File.ReadAllBytes(httpContext.Server.MapPath("~/" + image.TrimStart('~').TrimStart('/')));
                }

                using (var imageStream = new MemoryStream(buffer))
                {
                    var bitmap = Image.FromStream(imageStream);

                    img.Width = bitmap.Width;
                    img.Height = bitmap.Height;

                    img.Type = GetStringFromImageType(bitmap.RawFormat);
                }

                img.Base64 = Convert.ToBase64String(buffer);

                img.Id = "";
                while (img.Id.Length < 10)
                {
                    img.Id += (char)('a' + Random.Next(26));
                }

                return img;
            }, Provider.CacheDurations.DataUri));
        }
Пример #4
0
        public void Dispose()
        {
            string content;


            if (_originalTw == null)
            {
                if (_htmlHelper.ViewContext.Writer is StringWriter)
                {
                    // MVC 3
                    var stringBuilder = ((StringWriter)_htmlHelper.ViewContext.Writer).GetStringBuilder();

                    char[] inlineScript = new char[stringBuilder.Length - _startIndexOf];
                    stringBuilder.CopyTo(_startIndexOf, inlineScript, 0, inlineScript.Length);

                    content = new string(inlineScript);

                    stringBuilder.Remove(_startIndexOf, inlineScript.Length);
                }
                else
                {
                    // fak joe
                    return;
                }
            }
            else
            {
                _tw.Flush();

                _ms.Seek(0, SeekOrigin.Begin);

                using (StreamReader sr = new StreamReader(_ms))
                {
                    content = sr.ReadToEnd();
                }
            }

            var key = "inputhelper.scripts." + new MurmurHash2UInt32Hack().Hash(Encoding.UTF8.GetBytes(content));

            if (Provider.Enable.ScriptMinification)
            {
                var minified = Provider.GetFromCache(key,
                                                     () => JavaScriptCompressor.Compress(content, false, true, true, false, Int32.MaxValue, Encoding.UTF8, new CultureInfo("en-US")),
                                                     Provider.CacheDurations.InlineScript);

                if (!content.Contains("<script") && minified.Contains("<script"))
                {
                    // YUI compressor makes some mistakes when doing inline thingy's. Like making '<scr' + 'ipt>' into <script> which breaks browser
                    // so we won't compress this part. sorry :-)
                }
                else
                {
                    content = minified;
                }
            }

            if (_position == ScriptPositionEnum.Current)
            {
                if (_originalTw == null)
                {
                    // MVC 3?
                    _htmlHelper.ViewContext.Writer.Write(content);
                }
                else
                {
                    _originalTw.Write(content);
                    _tw.Dispose();
                }
            }
            else
            {
                MothScriptHelper.RegisterInlineScript(content);
            }
            // Set the writer back to the original, whether we are outputting here or at the bottom
            if (_originalTw != null)
            {
                ((HtmlTextWriter)_htmlHelper.ViewContext.Writer).InnerWriter = _originalTw;
            }
        }