private static string CopyAttachments(string content) { var doc = new HtmlDocument(); doc.LoadHtml(content); //process all nodes var nodes = doc.DocumentNode.Descendants().Where(d => d.Attributes.Where(a => a.Value.Contains("https://netping.atlassian.net/wiki/download/")).Count() > 0); foreach (var node in nodes) { var attributesWithFullUri = node.Attributes.Where(a => a.Value.Contains("https://netping.atlassian.net/wiki/download/")); foreach (var attribute in attributesWithFullUri) { if (attribute.Value.Contains("https://netping.atlassian.net/wiki/download/")) { string oldFileName; var newFileName = SaveFileFromUrlToLocal(attribute.Value, UrlBuilder.LocalPath_blogTempFiles, out oldFileName); var oldUrl = attribute.Value.Remove(attribute.Value.IndexOf('?')); attribute.Value = attribute.Value.Replace(oldUrl, UrlBuilder.GetblogFilesUrl() + newFileName); ChangeLinksToDownloadedFileFromNode(node, oldFileName, newFileName, oldUrl); } } if (node.Attributes["data-base-url"] != null && node.Attributes["data-base-url"].Value == "https://netping.atlassian.net/wiki") { node.Attributes["data-base-url"].Value = UrlBuilder.GetblogFilesUrl().ToString(); } } //change srcset attribute (workaround) nodes = doc.DocumentNode.Descendants().Where(d => d.Attributes.Any(a => a.Name == "srcset") && d.Attributes.Any(a => a.Name == "src") && d.Attributes.Any(a => a.Name == "data-image-src")); foreach (var node in nodes) { if (node.Attributes["src"] != null && node.Attributes["src"].Value.StartsWith(UrlBuilder.GetblogFilesUrl().ToString()) && node.Attributes["data-image-src"] != null && node.Attributes["data-image-src"].Value.StartsWith(UrlBuilder.GetblogFilesUrl().ToString())) { var oldUrl = node.Attributes["src"].Value.Remove(node.Attributes["src"].Value.IndexOf('?')); var newUrl = node.Attributes["data-image-src"].Value.Remove(node.Attributes["data-image-src"].Value.IndexOf('?')); if (Uri.IsWellFormedUriString(oldUrl, UriKind.Absolute) && Uri.IsWellFormedUriString(newUrl, UriKind.Absolute)) { node.Attributes["srcset"].Value = node.Attributes["srcset"].Value.Replace(oldUrl, newUrl); } } } return(doc.DocumentNode.OuterHtml); }
private static void ChangeLinksToDownloadedFileFromNode(HtmlNode node, string oldFileName, string newFileName, string oldUrl) { //rewrite the atrributes which have wiki/download/ to https://netping.atlassian.net/wiki/download/ //solve srcset issue //var wikiDownloadsAttributes = node.Attributes.Where(a => a.Name == "srcset" && a.Value.Contains("/wiki/download/") && a.Value.Contains(oldFileName)); var wikiDownloadsAttributes = node.Attributes.Where(a => a.Name == "srcset" && a.Value.Contains("/wiki/download/") && !a.Value.Contains("https://netping.atlassian.net/wiki/download/") && a.Value.Contains(oldFileName)); foreach (var attribute in wikiDownloadsAttributes) { attribute.Value = attribute.Value.Replace("/wiki/download/", "https://netping.atlassian.net/wiki/download/"); } var theSameFileNameAttributes = node.Attributes.Where(a => a.Value.Contains("https://netping.atlassian.net/wiki/download/") && a.Value.Contains(oldFileName)); //rewrite the atrributes with the same file name foreach (var attribute in theSameFileNameAttributes) { if (attribute.Value.Contains(oldUrl)) { attribute.Value = attribute.Value.Replace(oldUrl, UrlBuilder.GetblogFilesUrl() + newFileName); } } }