コード例 #1
0
        private void context_BeginRequest(HttpContext context)
        {
            var requestPath = context.Request.Path;
            if (!requestPath.EndsWith(_extension))
            {
                return;
            }

            var filePath = context.Server.MapPath(requestPath);
            if (!File.Exists(filePath))
            {
                return;
            }

            DateTime lastTimeUpdatedUtc = StylesHashing.GetLastStyleUpdateTimeUtc(_fileWatcherMask);

            var filePathCss = filePath.Substring(0, filePath.Length - _extension.Length) + ".min.css";

            bool cssFileExists = File.Exists(filePathCss);

            if (!cssFileExists || File.GetLastWriteTimeUtc(filePathCss) < lastTimeUpdatedUtc)
            {
                string fileKey = filePathCss.ToLowerInvariant();

                var errorInfo = _filesWithErrors[fileKey];
                // Not recompiling a file if previous compilations failed
                if (cssFileExists && errorInfo != null
                    && errorInfo.LastModificationDateUtc == lastTimeUpdatedUtc
                    && !(errorInfo.IsCssCompilationException && UserValidationFacade.IsLoggedIn()))
                {
                    context.Response.ContentType = "text/css";
                    context.Response.WriteFile(filePathCss);
                    context.ApplicationInstance.CompleteRequest();
                    return;
                }

                try
                {
                    _compilationLock.EnterWriteLock();

                    try
                    {
                        if (!File.Exists(filePathCss) || File.GetLastWriteTimeUtc(filePathCss) < lastTimeUpdatedUtc)
                        {
                            _compileAction(filePath, filePathCss, lastTimeUpdatedUtc);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (UserValidationFacade.IsLoggedIn() && ex is CssCompileException)
                        {
                            // Showing a friendly error message for logged in users
                            EmitCssMakrupForException(context.Response, requestPath, ex);
                            context.ApplicationInstance.CompleteRequest();
                            return;
                        }

                        if (!cssFileExists)
                        {
                            throw;
                        }

                        _filesWithErrors[fileKey] = new CssCompilationErrorInformation
                        {
                            IsCssCompilationException = ex is CssCompileException,
                            LastModificationDateUtc = lastTimeUpdatedUtc
                        };

                        Log.LogError(GetType().FullName, ex);

                        context.Response.ContentType = "text/css";
                        context.Response.WriteFile(filePathCss);
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                finally
                {
                    _compilationLock.ExitWriteLock();
                }
            }

            if ((DateTime.UtcNow - lastTimeUpdatedUtc).Days >= 1 || !UserValidationFacade.IsLoggedIn())
            {
                context.Response.Cache.SetExpires(DateTime.Now.AddDays(1.0));
            }
            else
            {
                context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(15));
            }

            context.Response.ContentType = "text/css";

            try
            {
                _compilationLock.EnterReadLock();

                context.Response.WriteFile(filePathCss);
            }
            finally
            {
                _compilationLock.ExitReadLock();
            }

            context.ApplicationInstance.CompleteRequest();
        }
コード例 #2
0
        private void context_BeginRequest(HttpContext context)
        {
            var requestPath = context.Request.Path;

            if (!requestPath.EndsWith(_extension))
            {
                return;
            }

            var filePath = context.Server.MapPath(requestPath);

            if (!File.Exists(filePath))
            {
                return;
            }

            DateTime lastTimeUpdatedUtc = StylesHashing.GetLastStyleUpdateTimeUtc(_fileWatcherMask);

            var filePathCss = filePath.Substring(0, filePath.Length - _extension.Length) + ".min.css";

            bool cssFileExists = File.Exists(filePathCss);

            if (!cssFileExists || File.GetLastWriteTimeUtc(filePathCss) < lastTimeUpdatedUtc)
            {
                string fileKey = filePathCss.ToLowerInvariant();

                var errorInfo = _filesWithErrors[fileKey];
                // Not recompiling a file if previous compilations failed
                if (cssFileExists && errorInfo != null &&
                    errorInfo.LastModificationDateUtc == lastTimeUpdatedUtc &&
                    !(errorInfo.IsCssCompilationException && UserValidationFacade.IsLoggedIn()))
                {
                    context.Response.ContentType = "text/css";
                    context.Response.WriteFile(filePathCss);
                    context.ApplicationInstance.CompleteRequest();
                    return;
                }

                try
                {
                    _compilationLock.EnterWriteLock();

                    try
                    {
                        if (!File.Exists(filePathCss) || File.GetLastWriteTimeUtc(filePathCss) < lastTimeUpdatedUtc)
                        {
                            _compileAction(filePath, filePathCss, lastTimeUpdatedUtc);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (UserValidationFacade.IsLoggedIn() && ex is CssCompileException)
                        {
                            // Showing a friendly error message for logged in users
                            EmitCssMakrupForException(context.Response, requestPath, ex);
                            context.ApplicationInstance.CompleteRequest();
                            return;
                        }

                        if (!cssFileExists)
                        {
                            throw;
                        }

                        _filesWithErrors[fileKey] = new CssCompilationErrorInformation
                        {
                            IsCssCompilationException = ex is CssCompileException,
                            LastModificationDateUtc   = lastTimeUpdatedUtc
                        };

                        Log.LogError(GetType().FullName, ex);

                        context.Response.ContentType = "text/css";
                        context.Response.WriteFile(filePathCss);
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                finally
                {
                    _compilationLock.ExitWriteLock();
                }
            }

            if ((DateTime.UtcNow - lastTimeUpdatedUtc).Days >= 1 || !UserValidationFacade.IsLoggedIn())
            {
                context.Response.Cache.SetExpires(DateTime.Now.AddDays(1.0));
            }
            else
            {
                context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(15));
            }


            context.Response.ContentType = "text/css";

            try
            {
                _compilationLock.EnterReadLock();

                context.Response.WriteFile(filePathCss);
            }
            finally
            {
                _compilationLock.ExitReadLock();
            }

            context.ApplicationInstance.CompleteRequest();
        }