/// <summary> /// Retrieves the web asset by specified id. /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public WebAsset Retrieve(string id) { MergedAsset mergedAsset = CreateMergedAssetFromConfiguration(id) ?? CreateMergedAssetFromUrl(id); WebAssetHolder assetHolder = EnsureAsset(mergedAsset, id); return(new WebAsset(assetHolder.Asset.ContentType, assetHolder.Asset.Version, assetHolder.Asset.Compress, assetHolder.Asset.CacheDurationInDays, assetHolder.Content)); }
/// <summary> /// /// </summary> /// <param name="asset"></param> /// <param name="id"></param> /// <returns></returns> private WebAssetHolder EnsureAsset(MergedAsset asset, string id) { string key = "{0}:{1}".FormatWith(GetType().AssemblyQualifiedName, id); WebAssetHolder assetHolder; using (syncLock.ReadAndWrite()) { assetHolder = GetWebAssetHolder(key); if (assetHolder == null) { using (syncLock.Write()) { assetHolder = GetWebAssetHolder(key); if (assetHolder == null) { List <string> physicalPaths = new List <string>(); StringBuilder contentBuilder = new StringBuilder(); var files = asset.Directories .SelectMany(d => d.Files.Select(f => new { Directory = d, File = f })) .OrderBy(f => f.File.Order); foreach (var pair in files) { string path = "~/" + pair.Directory.Path + "/" + pair.File.Name; string virtualPath = assetLocator.Locate(path, asset.Version); string fileContent = virtualPathProvider.ReadAllText(virtualPath); if (string.Compare(asset.ContentType, "text/css", StringComparison.OrdinalIgnoreCase) == 0) { string baseDiretory = virtualPathProvider.GetDirectory(virtualPath); fileContent = ReplaceImagePath(baseDiretory, asset.Version, fileContent); } contentBuilder.AppendLine(fileContent); physicalPaths.Add(pathResolver.Resolve(virtualPath)); } assetHolder = new WebAssetHolder { Asset = asset, Content = contentBuilder.ToString() }; cacheManager.Insert(key, assetHolder, null, physicalPaths.ToArray()); } } } } return(assetHolder); }
private WebAssetHolder EnsureAsset(MergedAsset asset, string id) { string key = CreateKey(id); WebAssetHolder assetHolder; using (syncLock.ReadAndWrite()) { assetHolder = cacheManager.GetItem(key) as WebAssetHolder; if (assetHolder == null) { using (syncLock.Write()) { assetHolder = cacheManager.GetItem(key) as WebAssetHolder; if (assetHolder == null) { IEnumerable<string> items = asset.Directories .SelectMany(d => d.Files.Select(f => new { f.Order, path = "~/" + d.Path + "/" + f.Name })) .OrderBy(f => f.Order) .Select(f => f.path); List<string> physicalPaths = new List<string>(); StringBuilder contentBuilder = new StringBuilder(); foreach (string item in items) { string virtualPath = assetLocator.Locate(item); string physicalPath = pathResolver.Resolve(virtualPath); string fileContent = fileSystem.ReadAllText(physicalPath); contentBuilder.AppendLine(fileContent); physicalPaths.Add(physicalPath); } assetHolder = new WebAssetHolder { Asset = asset, Content = contentBuilder.ToString() }; cacheManager.Insert(key, assetHolder, null, physicalPaths.ToArray()); } } } } return assetHolder; }
private WebAssetHolder EnsureAsset(MergedAsset asset, string id) { string key = "{0}:{1}".FormatWith(GetType().AssemblyQualifiedName, id); WebAssetHolder assetHolder; using (syncLock.ReadAndWrite()) { assetHolder = GetWebAssetHolder(key); if (assetHolder == null) { using (syncLock.Write()) { assetHolder = GetWebAssetHolder(key); if (assetHolder == null) { List<string> physicalPaths = new List<string>(); StringBuilder contentBuilder = new StringBuilder(); var files = asset.Directories .SelectMany(d => d.Files.Select(f => new { Directory = d, File = f })) .OrderBy(f => f.File.Order); foreach (var pair in files) { string path = "~/" + pair.Directory.Path + "/" + pair.File.Name; string virtualPath = assetLocator.Locate(path, asset.Version); string fileContent = virtualPathProvider.ReadAllText(virtualPath); if (string.Compare(asset.ContentType, "text/css", StringComparison.OrdinalIgnoreCase) == 0) { string baseDiretory = virtualPathProvider.GetDirectory(virtualPath); fileContent = ReplaceImagePath(baseDiretory, fileContent); } contentBuilder.AppendLine(fileContent); physicalPaths.Add(pathResolver.Resolve(virtualPath)); } assetHolder = new WebAssetHolder { Asset = asset, Content = contentBuilder.ToString() }; cacheManager.Insert(key, assetHolder, null, physicalPaths.ToArray()); } } } } return assetHolder; }