public IEnumerable<string> GetAssetSources(AssetType type, PageModel pageModel, UrlHelper helper) { //load items not in any group or delivered by CDN foreach (Asset asset in GetNonGroupedAssets(pageModel.Assets, type)) { if (Settings.Default.UseCDN && GetCdnUrl(asset.Name) != null) yield return GetCdnUrl(asset.Name); else yield return helper.AssetPath(asset.AssetType, asset.Name); } var groups = pageModel.Assets.Select(a => a.Group).Distinct().ToArray(); var grouped = GetGroupedAssets(pageModel.Assets, groups, type); if (AssetGroupMode == AssetGroupMode.Disabled) { foreach (string s in grouped.Select(a => helper.AssetPath(a.AssetType, a.Name))) yield return s; } else //load local via combination { if (grouped.Count > 0) { foreach (string group in groups) { //the following method will load and cache the combined script for later var version = GetGroupCombined(type, group, helper).Md5; //add versioning querystring for future updates overridding expires yield return helper.RouteUrl("AssetGroup" + type.ToString(), new { group = group, v = version }); } } } }
public GroupCombinedAsset GetGroupCombined(AssetType type, string group, UrlHelper helper) { string key = string.Format("{0} - {1}", type, group); //first try to get from cache GroupCombinedAsset combined = helper.RequestContext.HttpContext.Cache[key] as GroupCombinedAsset; if (combined != null) return combined; if (type != AssetType.Js && type != AssetType.Css) throw new ArgumentException("Can't group for the given type.", "type"); AggregateCacheDependency cacheDep = new AggregateCacheDependency(); IContainer container = (IContainer)helper.RequestContext.HttpContext.Application["Container"]; var assets = GetGroupedAssets( //global Assets //pages .Concat(container.GetAllInstances<IPage>().SelectMany(p => p.Assets)) //widgets .Concat(container.GetAllInstances<IWidget>().SelectMany(w => w.Assets)).ToList(), new[] { group }, type); StringBuilder sb = new StringBuilder(); foreach (Asset a in assets) { //add helpful debugging info when not going to minify string path = helper.AssetPath(a.AssetType, a.Name); if (AssetGroupMode != AssetGroupMode.CombineAndMinify) { sb.Append(Environment.NewLine); sb.Append(Environment.NewLine); sb.AppendFormat("/* asset: {0} */", path); sb.Append(Environment.NewLine); } path = helper.RequestContext.HttpContext.Server.MapPath(path); try { sb.Append(File.ReadAllText(path)); cacheDep.Add(new CacheDependency(path)); sb.Append(Environment.NewLine); } catch (FileNotFoundException ex) { LogService.Warn(ex.Message); } } string content = string.Empty; if (sb.Length > 0) { if (type == AssetType.Css) content = AssetGroupMode == AssetGroupMode.CombineAndMinify ? Minify(type, sb.ToString()) : sb.ToString(); else content = AssetGroupMode == AssetGroupMode.CombineAndMinify ? Minify(type, sb.ToString()) : sb.ToString(); } combined = new GroupCombinedAsset() { Content = content, Group = group, Md5 = AtomSite.Utils.SecurityHelper.HashIt(content, "MD5"), LastModified = cacheDep.UtcLastModified.ToLocalTime() }; helper.RequestContext.HttpContext.Cache.Add(key, combined, cacheDep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null); return combined; }