Beispiel #1
0
        protected Dictionary <string, ImageInfo> GenerateContentIdsForImageUrlsFromHtmlContent()
        {
            var result = new Dictionary <string, ImageInfo>();

            // cid:{0}
            if (HtmlContent != null)
            {
                var originalMessageContent = HtmlContent; // TODO: save this somewhere
                // not sure if needed
                string html = string.Format("<html><head></head><body>{0}</body></html>", HtmlContent);

                List <Tuple <HtmlNode, string> > nodesToReplaceWithTextSmiley = new List <Tuple <HtmlNode, string> >();

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                var imageNodes = doc.DocumentNode.SelectNodes("//img");
                if (imageNodes != null)
                {
                    foreach (var imageNode in imageNodes)
                    {
                        var imgUrl     = imageNode.GetAttributeValue("src", "");
                        var textSmiley = MessageUtils.GetTextReplacementForImageUrl(imgUrl);
                        if (!string.IsNullOrWhiteSpace(textSmiley))
                        {
                            nodesToReplaceWithTextSmiley.Add(Tuple.Create(imageNode, textSmiley));
                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(imgUrl))
                        {
                            continue;
                        }

                        var cid = MimeUtils.GenerateMessageId();
                        imageNode.SetAttributeValue("src", $"cid:{cid}");

                        var imageInfo = new ImageInfo(imgUrl, imgUrl, ImageType.Public);

                        // Handles URLs like this: src =\"https://eu-api.asm.skype.com/v1/objects/0-weu-d11-00000000000000000000000000000000/views/imgo\"
                        var teamsImageUrlPattern = @"http.*?/objects/([0-9a-zA-Z-]+?)/views/imgo";
                        var match = Regex.Match(HtmlContent, teamsImageUrlPattern);
                        if (match.Success)
                        {
                            imageInfo.CacheKey  = match.Value;
                            imageInfo.ImageType = ImageType.TeamsWithAuthentication;
                        }

                        result.Add(cid, imageInfo);
                    }

                    foreach (var textReplacement in nodesToReplaceWithTextSmiley)
                    {
                        try
                        {
                            logger.Debug("Replacing image node with smiley {0} (HTML was: {1})", textReplacement.Item2, textReplacement.Item1.OuterHtml);
                            textReplacement.Item1.ParentNode.ReplaceChild(HtmlNode.CreateNode($"<span>{textReplacement.Item2}</span>"), textReplacement.Item1);
                        }
                        catch (Exception e)
                        {
                            logger.Error(e, "Exception while replacing image node with smiley");
                            continue;
                        }
                    }
                }
                var bodyElement = doc.DocumentNode.SelectSingleNode("//body");
                HtmlContent = bodyElement.InnerHtml;
            }
            return(result);
        }