コード例 #1
0
ファイル: ResourceExtensions.cs プロジェクト: tuanvt/EyePatch
 private static string BuildJs(ResourcePath resourcePath)
 {
     var scriptTag = new TagBuilder("script");
     scriptTag.Attributes["type"] = resourcePath.ContentType;
     scriptTag.Attributes["src"] = resourcePath.Url;
     return scriptTag.ToString(TagRenderMode.Normal);
 }
コード例 #2
0
ファイル: ResourceExtensions.cs プロジェクト: tuanvt/EyePatch
 private static string BuildCss(ResourcePath resourcePath)
 {
     var scriptTag = new TagBuilder("link");
     scriptTag.Attributes["type"] = resourcePath.ContentType;
     scriptTag.Attributes["href"] = resourcePath.Url;
     scriptTag.Attributes["rel"] = "stylesheet";
     return scriptTag.ToString(TagRenderMode.SelfClosing);
 }
コード例 #3
0
ファイル: ResourceService.cs プロジェクト: tuanvt/EyePatch
        public ResourcePath GetContentsFor(string identifer)
        {
            var cachedVersion = cacheProvider.Get<ResourcePath>(identifer);
            if (cachedVersion != null)
                return cachedVersion;

            // look for the physical file
            // backup step only
            var physicalFilePath = GetFilePath(identifer);
            if (File.Exists(physicalFilePath))
            {
                var result = new ResourcePath();
                result.Contents = File.ReadAllText(physicalFilePath);
                result.FileName = physicalFilePath;
                result.Url = Url.Action("fetch", "resource", new {id = identifer});
            }
            return null;
        }
コード例 #4
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //get request and response
            var request = filterContext.HttpContext.Request;
            var response = filterContext.HttpContext.Response;

            //get encoding
            var encoding = request.Headers["Accept-Encoding"];
            if (string.IsNullOrWhiteSpace(encoding))
                return;

            encoding = encoding.ToUpperInvariant();

            if (encoding.Contains("DEFLATE") || encoding == "*")
            {
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
            }
            else if (encoding.Contains("GZIP"))
            {
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            }

            var urlHelper = new UrlHelper(filterContext.RequestContext);

            var page = filterContext.RouteData.Values["epPage"] as Page;

            if (page == null || string.IsNullOrWhiteSpace(page.AnalyticsKey))
                return;

            var analytics = new ResourcePath
            {
                ContentType = "text/javascript",
                Url = urlHelper.RouteUrl("Default",  new { action = "Analytics", controller = "Content", id = page.Id }).ToLowerInvariant()
            };

            var tags = new List<ResourcePath> { analytics }.ToTags();
            response.Filter = new RegexResponseFilter(response.Filter, adminPanelRegex,
                                                      string.Format("{0}</head>", tags));
        }
コード例 #5
0
ファイル: ResourceService.cs プロジェクト: tuanvt/EyePatch
        private ResourcePath ProcessResource(Resource resource, bool minify, bool cacheRefresh)
        {
            if (resource == null) throw new ArgumentNullException("resource");

            if (!(resource is EmbeddedResource) && (resource.IsExternal || !minify))
                return new ResourcePath {ContentType = resource.ContentType, Url = resource.Url};

            var cacheKey = resource.FileName.ToMinPath();

            var upgraded = false;
            try
            {
                cacheLock.EnterUpgradeableReadLock();

                var cached = cacheProvider.Get<ResourcePath>(cacheKey);
                if (cached != null && cacheRefresh)
                    return cached;

                cacheLock.EnterWriteLock();
                upgraded = true;

                var priorWrite = cacheProvider.Get<ResourcePath>(cacheKey);
                if (priorWrite != null && cacheRefresh)
                    return priorWrite;

                // regenerate
                var result = new ResourcePath();
                result.ContentType = resource.ContentType;
                result.Contents = resource.FileContents();
                result.Url = Url.Action("fetch", "resource", new {id = cacheKey});

                // minify

                if (minify)
                {
                    result.Contents = resource.ContentType == "text/javascript"
                                          ? JavaScriptCompressor.Compress(result.Contents)
                                          : CssCompressor.Compress(result.Contents);
                }

                // write backup file
                var physicalFilePath = GetFilePath(cacheKey, false);
                if (!Directory.Exists(Path.GetDirectoryName(physicalFilePath)))
                    Directory.CreateDirectory(Path.GetDirectoryName(physicalFilePath));

                if (File.Exists(physicalFilePath))
                    File.Delete(physicalFilePath);

                File.WriteAllText(physicalFilePath, result.Contents);
                result.FileName = physicalFilePath;

                cacheProvider.Add(cacheKey, result, resource.DependentFile);
                return result;
            }
            finally
            {
                if (upgraded)
                    cacheLock.ExitWriteLock();

                cacheLock.ExitUpgradeableReadLock();
            }
        }
コード例 #6
0
ファイル: ResourceService.cs プロジェクト: tuanvt/EyePatch
        private ResourcePath ProcessGroup(ResourceCollection set, bool minify, bool cacheRefresh)
        {
            if (!set.Any())
                throw new ApplicationException("Cannot process empty group");

            var firstElement = set.First();
            if (set.Count() == 1 && firstElement.IsExternal)
                return new ResourcePath {ContentType = firstElement.ContentType, Url = firstElement.Url};

            var cacheKey = minify ? set.UnqiueId.ToMinPath() : set.UnqiueId;

            var upgraded = false;
            try
            {
                cacheLock.EnterUpgradeableReadLock();

                var cached = cacheProvider.Get<ResourcePath>(cacheKey);
                if (cached != null && cacheRefresh)
                    return cached;

                cacheLock.EnterWriteLock();
                upgraded = true;

                var priorWrite = cacheProvider.Get<ResourcePath>(cacheKey);
                if (priorWrite != null && cacheRefresh)
                    return priorWrite;

                // regenerate
                var result = new ResourcePath();
                result.ContentType = firstElement.ContentType;
                result.Url = Url.Action("fetch", "resource", new {id = cacheKey});

                // mash
                result.Contents = set.Mash();

                // minify
                if (minify)
                {
                    result.Contents = firstElement.ContentType == "text/javascript"
                                          ? JavaScriptCompressor.Compress(result.Contents)
                                          : CssCompressor.Compress(result.Contents);
                }

                // write backup file
                var physicalFilePath = GetFilePath(set.UnqiueId, minify);

                if (!Directory.Exists(Path.GetDirectoryName(physicalFilePath)))
                    Directory.CreateDirectory(Path.GetDirectoryName(physicalFilePath));

                if (File.Exists(physicalFilePath))
                    File.Delete(physicalFilePath);

                File.WriteAllText(physicalFilePath, result.Contents);
                result.FileName = physicalFilePath;

                cacheProvider.Add(cacheKey, result, set.Files());
                return result;
            }
            finally
            {
                if (upgraded)
                    cacheLock.ExitWriteLock();

                cacheLock.ExitUpgradeableReadLock();
            }
        }