Esempio n. 1
0
        private void WriteResponse(BundleResource bundleResource, AsyncBundlingRequestState asyncState)
        {
            string content = TransformContent(bundleResource, asyncState.Context);

            asyncState.ResourceData.Append(content).Append(Environment.NewLine);

            asyncState.Context.Response.Write(content);
            asyncState.Context.Response.Write(Environment.NewLine);
        }
Esempio n. 2
0
        private string TransformContent(BundleResource bundleResource, HttpContext context)
        {
            var bundleType = bundleResource.Type;
            var content    = bundleResource.Content;
            var serverpath = bundleResource.ServerPath;

            try
            {
                if (bundleType == BundleResourceType.EmbeddedScript || bundleType == BundleResourceType.ScriptFile)
                {
                    var compressor = new JavaScriptCompressor
                    {
                        CompressionType     = CompressionType.Standard,
                        Encoding            = Encoding.UTF8,
                        ObfuscateJavascript = bundleResource.ObfuscateJs
                    };


                    //Minimize
                    var contentOut = compressor.Compress(content.Trim()) + ";";

                    //Return deffered execution
                    if (ClientSettings.IsJavascriptDefferingEnabled)
                    {
                        return(string.Format(ClientSettings.JavascriptDefferingScript,
                                             JsonConvert.SerializeObject(contentOut)));
                    }
                    return(contentOut);
                }
                if (!string.IsNullOrEmpty(serverpath))
                {
                    string directoryName = Path.GetDirectoryName(serverpath);
                    if (directoryName != null)
                    {
                        serverpath = directoryName.Replace('\\', '/');
                    }
                }
                if (bundleType == BundleResourceType.EmbeddedStyle || bundleType == BundleResourceType.StyleFile)
                {
                    var compressor = new CssCompressor
                    {
                        CompressionType = CompressionType.Standard,
                        RemoveComments  = true
                    };

                    return(ReplaceUrls(compressor.Compress(content), serverpath, context));
                }
                if (bundleType == BundleResourceType.LessFile)
                {
                    DotlessConfiguration cfg = DotlessConfiguration.GetDefaultWeb();
                    cfg.Web           = true;
                    cfg.MinifyOutput  = true;
                    cfg.MapPathsToWeb = true;
                    cfg.CacheEnabled  = false;

                    //Prefilter
                    content = ReplaceImportRegex.Replace(content, match => ReplaceImports(match, serverpath));
                    string processed = ReplaceUrls(LessWeb.Parse(content, cfg), serverpath, context);
                    return(processed);
                }
            }
            catch (EcmaScript.NET.EcmaScriptException e)
            {
                _log.ErrorFormat("EcmaScriptException: {0} in {1} at {2} ({3}, {4}) at ", e.Message, serverpath, e.LineSource, e.LineNumber, e.ColumnNumber, e.ScriptStackTrace);
            }
            catch (Exception e)
            {
                _log.Error(e);
            }
            return(content);
        }
Esempio n. 3
0
        private void ProcessItem(BundleResource resource, AsyncBundlingRequestState asyncState)
        {
            if (resource == BundleResource.Empty)
            {
                return;
            }

            if (resource.Type == BundleResourceType.EmbeddedScript || resource.Type == BundleResourceType.EmbeddedStyle)
            {
                resource.ServerPath = null;
                WriteResponse(resource, asyncState);

                ProcessItem(GetNextResource(asyncState), asyncState);
            }
            else if (resource.Type == BundleResourceType.ClientScript)
            {
                try
                {
                    var handler = ClientScriptBundle.GetHttpHandler(resource.ServerPath.Split('/').Last().Split('.').First());
                    var content = handler.GetData(asyncState.Context);

                    asyncState.ResourceData.Append(content).Append(Environment.NewLine);
                    asyncState.Context.Response.Write(content);
                    asyncState.Context.Response.Write(Environment.NewLine);

                    ProcessItem(GetNextResource(asyncState), asyncState);
                }
                catch (Exception e)
                {
                    _log.Error("ClientScriptError", e);
                }
            }
            else
            {
                //If it's file begin read and go to IOCP thread
                if (File.Exists(resource.Content))
                {
                    FileStream file   = File.Open(resource.Content, FileMode.Open, FileAccess.Read, FileShare.Read);
                    var        buffer = new byte[file.Length];
                    file.BeginRead(buffer, 0, (int)file.Length, result =>
                    {
                        try
                        {
                            int readed      = file.EndRead(result);
                            var asyncObject =
                                result.AsyncState as
                                AsyncBundlingRequestState;
                            HttpContext.Current = asyncState.Context;
                            resource.Content    =
                                Encoding.UTF8.GetString(buffer, 0,
                                                        readed);
                            WriteResponse(resource, asyncState);
                            ProcessItem(GetNextResource(asyncObject),
                                        asyncObject);
                        }
                        catch (Exception e)
                        {
                            _log.Error(
                                string.Format(
                                    "Error while processing file:{0}",
                                    resource.Content), e);
                        }
                        finally
                        {
                            //We got wht we need in buffer. close stream
                            file.Close();
                            file.Dispose();
                        }
                    }, asyncState);
                }
            }
        }