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);
        }
예제 #2
0
        public SFile Convert(ListItem listItem, SharepointClient sp)
        {
            try
            {
                var userGuideFileName = "User guide";

                var fileName = listItem.Get <String>(SharepointFields.Title);

                var fileType = listItem.Get <TaxonomyFieldValue>(SharepointFields.ManualFileType).ToSPTerm(_fileTypeTerms);

                var title = listItem.Get <String>(SharepointFields.Title);

                var devices = listItem.Get <TaxonomyFieldValueCollection>(SharepointFields.Devices).ToSPTermList(_names);

                var created = listItem.Get <DateTime>(SharepointFields.Created);

                var fileUrl = listItem.Get <FieldUrlValue>(SharepointFields.UrlUpperCase).Url;

                Debug.WriteLine($"Start loading manual '{fileUrl}'");
                Log.Trace($"Start loading manual '{fileUrl}'");

                var sFile = new SFile
                {
                    Id        = listItem.Id,
                    Name      = fileName,
                    Title     = title,
                    Devices   = devices,
                    File_type = fileType,
                    Created   = created,
                    Url       = UrlBuilder.GetPublicFilesUrl(fileName).ToString()
                };
                if (fileType.OwnNameFromPath != userGuideFileName)
                {
                    sp.DownloadFileToLocal(fileUrl, UrlBuilder.LocalPath_pubfiles, fileName);
                }
                else
                {
                    sFile.Url = fileUrl;
                }

                /*
                 * if (fileType.OwnNameFromPath == userGuideFileName)
                 * {
                 * var contentId = _confluenceClient.GetContentIdFromUrl(fileUrl);
                 *
                 * if (contentId.HasValue)
                 * {
                 *  var content = _confluenceClient.GetUserManual(contentId.Value, listItem.Id);
                 *
                 *  _manualSaver(content);
                 *
                 *  var url = UrlBuilder.GetRelativeDeviceGuideUrl(PrepareUrlName(content)).ToString();
                 *
                 *  sFile.Url = url;
                 * }
                 * else
                 * {
                 *  Debug.WriteLine($"End loading manual '{fileUrl}'");
                 *  Log.Trace($"End loading manual '{fileUrl}'");
                 *
                 *  return null;
                 * }
                 *
                 *
                 * } else
                 * {
                 *
                 *  sp.DownloadFileToLocal(fileUrl, UrlBuilder.LocalPath_pubfiles, fileName);
                 * }
                 */
                Debug.WriteLine($"End loading manual '{fileUrl}'");
                Log.Trace($"End loading manual '{fileUrl}'");

                return(sFile);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }