public RhqPackageBuilder(IMapPathResolver mapPathResolver, string webRoot) { this.mapPathResolver = mapPathResolver; this.webRoot = webRoot; ms = new MemoryStream(); zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true); Assets = new AssetPathCollection(); StyleAssets = new AssetPathCollection(); }
private async Task <AssetPathCollection> ExtractAssets(string source, string suffix, string pagePath, AssetPathCollection assets) { if (suffix.ToLower().StartsWith("htm")) { var parser = new AngleSharp.Parser.Html.HtmlParser(); var doc = parser.Parse(source); var images = doc.Images .Where(x => x.HasAttribute("src")); var styles = doc.GetElementsByTagName("link") .Where(l => l.Attributes["rel"].Value.Trim().ToLower() == "stylesheet") .Where(c => c.HasAttribute("href")); var scripts = doc.GetElementsByTagName("script") .Where(x => x.HasAttribute("src")); assets.AddSerializedAssets(images, "src"); assets.AddSerializedAssets(scripts, "src"); assets.AddSerializedAssets(styles, "href"); foreach (var asset in assets.Assets.Where(a => a.AssetType == "css")) { var content = await Zipper.GetStringAsset(asset.OriginalPath, mapPathResolver, webRoot, pagePath); var binaryContent = Encoding.UTF8.GetBytes(content); asset.Content = binaryContent; await ExtractAssets(content, "css", asset.OriginalPath, assets); } foreach (var asset in assets.Assets.Where(a => a.AssetType != "css")) { var binaryContent = await Zipper.GetBinaryAsset(asset.OriginalPath, mapPathResolver, webRoot, pagePath); asset.Content = binaryContent; } } else if (suffix.ToLower() == "css") { var urls = Zipper.ExtaxtUrlsFromStyle(source); foreach (var url in urls) { var csslocalPath = await Zipper.ReturnLocalPath(url); var csssuffix = csslocalPath.Split('.').Last(); var newUrl = Guid.NewGuid().ToString().Replace("-", "") + "." + suffix; source = source.Replace(url, newUrl); //assets.AddSerializedAssets() //if (!doneAssets.Contains(newUrl)) //{ // zipArchive.AddBinaryAssetToArchive(newUrl, localPath, mapPathResolver, webRoot, serialStyle.Key); // doneAssets.Add(newUrl); //} } } return(assets); }