public static string GetStringAsset(string path, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { if (DetectBundle(path)) { using (var webClient = new WebClient()) { try { var style = webClient.DownloadString(webRoot + path); return(style); } catch (WebException wex) { // TODO: log web exception return(string.Empty); } } } var localpath = mapPathResolver.MapPath(pagePath, path); if (File.Exists(localpath)) { var style = File.ReadAllText(localpath); return(style); } else { return(string.Empty); } }
//MemoryStream ms; //ZipArchive zipArchive; public PackageBuilder(IMapPathResolver mapPathResolver, string webRoot) { this.mapPathResolver = mapPathResolver; this.webRoot = webRoot; AssetsContents = new List <AssetContent>(); //ms = new MemoryStream(); //zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true); }
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(); }
public static void AddBinaryAssetToArchive( this ZipArchive zipArchive, string serialAssetName, string serialAssetPath, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { var asset = GetBinaryAsset(serialAssetPath, mapPathResolver, webRoot, pagePath); if (asset.Length > 0) { var nentry = zipArchive.CreateEntry(serialAssetName, CompressionLevel.Fastest); using (var writer = new BinaryWriter(nentry.Open())) { writer.Write(asset); } } }
public static async Task <byte[]> GetBinaryAsset(string path, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { byte[] content = null; try { var localpath = mapPathResolver.MapPath(pagePath, path); if (File.Exists(localpath)) { content = File.ReadAllBytes(localpath); } } catch (Exception ex) { // TODO: trace somewhere } if (content == null) { using (var webClient = new HttpClient()) { try { if (path.StartsWith("http") || path.StartsWith("//")) { content = await webClient.GetByteArrayAsync(path); } else { content = await webClient.GetByteArrayAsync(webRoot + path); } } catch (Exception ex) { // TODO: trace somewhere } } } return(content); }
public static async Task <string> GetStringAsset(string path, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { var stringContent = string.Empty; try { var localpath = mapPathResolver.MapPath(pagePath, path); if (File.Exists(localpath)) { stringContent = File.ReadAllText(localpath); } } catch (Exception ex) { /* TODO: Trace something */ } if (stringContent == string.Empty) { using (var webClient = new HttpClient()) { try { if (path.StartsWith("http") || path.StartsWith("//")) { stringContent = await webClient.GetStringAsync(path); } else { stringContent = await webClient.GetStringAsync(webRoot + path); } } catch (WebException wex) { // TODO: log web exception } } } return(stringContent); }
public static byte[] GetBinaryAsset(string path, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { if (DetectBundle(path)) { using (var webClient = new WebClient()) { var asset = webClient.DownloadData(webRoot + path); return(asset); } } var localpath = mapPathResolver.MapPath(pagePath, path); if (File.Exists(localpath)) { var asset = File.ReadAllBytes(localpath); return(asset); } else { return(new byte[] { }); } }
public static async Task <byte[]> GetBinaryAsset(string path, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { if (DetectBundle(path)) { using (var webClient = new HttpClient()) { var asset = await webClient.GetByteArrayAsync(webRoot + path); return(asset); } } var localpath = mapPathResolver.MapPath(pagePath, path); if (File.Exists(localpath)) { var asset = File.ReadAllBytes(localpath); return(asset); } else { return(new byte[] { }); } }
public static byte[] ZipPage(string html, IMapPathResolver mapPathResolver, string webRoot, string pagePath) { var parser = new AngleSharp.Parser.Html.HtmlParser(); var doc = parser.Parse(html); 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")); var serialAssets = new Dictionary <string, string>(); serialAssets.AddSerializedAssets(images, "src"); serialAssets.AddSerializedAssets(scripts, "src"); var serialStyles = new Dictionary <string, string>(); serialStyles.AddSerializedAssets(styles, "href"); var newHtml = doc.ToHtml(new HtmlMarkupFormatter()); var doneAssets = new List <string>(); using (var ms = new MemoryStream()) { using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true)) { //foreach (var attachment in attachmentFiles) { var entry = zipArchive.CreateEntry("index.html", CompressionLevel.Fastest); using (StreamWriter writer = new StreamWriter(entry.Open())) { try { writer.Write(newHtml); } catch (Exception ex) { EventLog.WriteEntry("RotativaHQ", ex.Message); throw; } doneAssets.Add("index.html"); } } foreach (var serialStyle in serialStyles) { if (!doneAssets.Contains(serialStyle.Value)) { var style = GetStringAsset(serialStyle.Key, mapPathResolver, webRoot, pagePath); if (!string.IsNullOrEmpty(style)) { var urls = ExtaxtUrlsFromStyle(style); foreach (var url in urls) { var localPath = ReturnLocalPath(url); var suffix = localPath.Split('.').Last(); var newUrl = Guid.NewGuid().ToString().Replace("-", "") + "." + suffix; style = style.Replace(url, newUrl); if (!doneAssets.Contains(newUrl)) { zipArchive.AddBinaryAssetToArchive(newUrl, localPath, mapPathResolver, webRoot, serialStyle.Key); doneAssets.Add(newUrl); } } var sentry = zipArchive.CreateEntry(serialStyle.Value, CompressionLevel.Fastest); using (StreamWriter writer = new StreamWriter(sentry.Open())) { writer.Write(style); } doneAssets.Add(serialStyle.Value); } } } foreach (var serialAsset in serialAssets) { if (!doneAssets.Contains(serialAsset.Value)) { zipArchive.AddBinaryAssetToArchive( serialAsset.Value, serialAsset.Key, mapPathResolver, webRoot, pagePath); doneAssets.Add(serialAsset.Value); } } } return(ms.ToArray()); } }