Beispiel #1
0
        public static void ResourceGroupRender(FrontContext context, string id)
        {
            var db    = context.SiteDb;
            var group = db.ResourceGroups.GetByNameOrId(id, ConstObjectType.Style);

            if (group == null)
            {
                group = db.ResourceGroups.GetByNameOrId(id, ConstObjectType.Script);
            }
            if (group == null)
            {
                group = db.ResourceGroups.TableScan.Where(o => Lib.Helper.StringHelper.IsSameValue(o.Name, id)).FirstOrDefault();
            }
            if (group == null)
            {
                return;
            }
            IRepository repo    = null;
            string      spliter = "\r\n";

            switch (group.Type)
            {
            case ConstObjectType.Style:
                context.RenderContext.Response.ContentType = "text/css;charset=utf-8";
                repo = context.SiteDb.Styles as IRepository;
                break;

            case ConstObjectType.Script:
                context.RenderContext.Response.ContentType = "text/javascript;charset=utf-8";
                repo    = context.SiteDb.Scripts as IRepository;
                spliter = ";\r\n";    //need split with newline ,otherwise two different combinations of comments will report an error
                break;

            default:
                break;
            }

            StringBuilder sb = new StringBuilder();

            foreach (var item in group.Children.OrderBy(o => o.Value))
            {
                var route = context.SiteDb.Routes.Get(item.Key);
                if (route != null)
                {
                    var siteobject = repo.Get(route.objectId);
                    if (siteobject != null && siteobject is ITextObject)
                    {
                        var text = siteobject as ITextObject;
                        sb.Append(text.Body);
                        sb.Append(spliter);
                    }
                }
            }

            string result = sb.ToString();

            TextBodyRender.SetBody(context, result);
            // context.RenderContext.Response.Body = DataConstants.DefaultEncoding.GetBytes(sb.ToString());
        }
Beispiel #2
0
        public static void ResourceGroupRender(FrontContext context, string id)
        {
            var db    = context.SiteDb;
            var group = db.ResourceGroups.GetByNameOrId(id, ConstObjectType.Style);

            if (group == null)
            {
                group = db.ResourceGroups.GetByNameOrId(id, ConstObjectType.Script);
            }
            if (group == null)
            {
                group = db.ResourceGroups.TableScan.Where(o => Lib.Helper.StringHelper.IsSameValue(o.Name, id)).FirstOrDefault();
            }
            if (group == null)
            {
                return;
            }
            IRepository repo    = null;
            string      spliter = "\r\n";

            switch (group.Type)
            {
            case ConstObjectType.Style:
                context.RenderContext.Response.ContentType = "text/css;charset=utf-8";
                repo = context.SiteDb.Styles as IRepository;
                break;

            case ConstObjectType.Script:
                context.RenderContext.Response.ContentType = "text/javascript;charset=utf-8";
                repo    = context.SiteDb.Scripts as IRepository;
                spliter = ";\r\n";    //need split with newline ,otherwise two different combinations of comments will report an error
                break;

            default:
                break;
            }

            StringBuilder sb = new StringBuilder();

            long totalversion = 0;

            foreach (var item in group.Children.OrderBy(o => o.Value))
            {
                var route = context.SiteDb.Routes.Get(item.Key);
                if (route != null)
                {
                    var siteobject = repo.Get(route.objectId);
                    if (siteobject != null)
                    {
                        if (siteobject is ITextObject)
                        {
                            var text = siteobject as ITextObject;
                            sb.Append(text.Body);
                            sb.Append(spliter);
                        }

                        if (siteobject is ICoreObject)
                        {
                            var core = siteobject as ICoreObject;
                            totalversion += core.Version;
                        }
                    }
                }
            }

            string result = sb.ToString();

            if (context.RenderContext.WebSite != null && context.RenderContext.WebSite.EnableJsCssCompress)
            {
                if (!string.IsNullOrWhiteSpace(result))
                {
                    if (group.Type == ConstObjectType.Style)
                    {
                        result = CompressCache.Get(group.Id, totalversion, result, CompressType.css);
                    }
                    else if (group.Type == ConstObjectType.Script)
                    {
                        result = CompressCache.Get(group.Id, totalversion, result, CompressType.js);
                    }
                }
            }


            TextBodyRender.SetBody(context, result);

            var version = context.RenderContext.Request.GetValue("version");

            if (!string.IsNullOrWhiteSpace(version))
            {
                context.RenderContext.Response.Headers["Expires"] = DateTime.UtcNow.AddYears(1).ToString("r");
            }
        }