コード例 #1
0
        /// <summary>
        /// Parses each of the images in the document and posts them to the server.
        /// Updates the HTML with the returned Image Urls
        /// </summary>
        /// <param name="html">HTML that contains images</param>
        /// <param name="basePath">image file name</param>
        /// <param name="wrapper">blog wrapper instance that sends</param>
        /// <param name="metaData">metadata containing post info</param>
        /// <param name="imgFileMapping">mapping of filename with hash to NewMediaObject</param>
        /// <returns>update HTML string for the document with updated images</returns>
        private string SendImages(string html, string basePath,
                                  MetaWeblogWrapper wrapper, Dictionary <string, MediaObjectInfo> imgFileMapping)
        {
            // base folder name for uploads - just the folder name of the image
            var baseName = Path.GetFileName(basePath);

            baseName = FileUtils.SafeFilename(baseName).Replace(" ", "-");

            var doc = new HtmlDocument();

            doc.LoadHtml(html);
            try
            {
                // send up normalized path images as separate media items
                var images = doc.DocumentNode.SelectNodes("//img");
                if (images != null)
                {
                    foreach (HtmlNode img in images)
                    {
                        string origImageLink = img.Attributes["src"]?.Value;
                        string imgFile       = StringUtils.UrlDecode(origImageLink);

                        if (imgFile == null)
                        {
                            continue;
                        }

                        if (!imgFile.StartsWith("http://") && !imgFile.StartsWith("https://"))
                        {
                            if (!imgFile.Contains(":\\"))
                            {
                                imgFile = Path.Combine(basePath, imgFile.Replace("/", "\\"));
                            }


                            if (System.IO.File.Exists(imgFile))
                            {
                                var uploadFilename = Path.GetFileName(imgFile);
                                var media          = new MediaObject()
                                {
                                    Type = ImageUtils.GetImageMediaTypeFromFilename(imgFile),
                                    Bits = System.IO.File.ReadAllBytes(imgFile),
                                    Name = baseName + "/" + uploadFilename
                                };

                                //use file name with hash to check if file is already uploaded
                                var             imgFileChk  = origImageLink + "#" + Convert.ToBase64String(MD5.Create().ComputeHash(media.Bits));
                                MediaObjectInfo mediaResult = null;
                                if (imgFileMapping != null && imgFileMapping.ContainsKey(imgFileChk))
                                {
                                    //Image has already uploaded
                                    mediaResult = imgFileMapping[imgFileChk];
                                }
                                else
                                {
                                    mediaResult = wrapper.NewMediaObject(media);
                                    if (imgFileMapping != null)
                                    {
                                        imgFileMapping.Add(imgFileChk, mediaResult);
                                    }
                                }

                                img.Attributes["src"].Value = mediaResult.URL;

                                // use first image as featured image
                                if (!DontInferFeaturedImage)
                                {
                                    if (string.IsNullOrEmpty(FeaturedImageUrl))
                                    {
                                        FeaturedImageUrl = mediaResult.URL;
                                    }
                                    if (string.IsNullOrEmpty(FeatureImageId))
                                    {
                                        FeatureImageId = mediaResult.Id;
                                    }
                                }

                                if (WeblogAddinConfiguration.Current.ReplacePostImagesWithOnlineUrls)
                                {
                                    mmApp.Model.ActiveDocument.CurrentText =
                                        mmApp.Model.ActiveDocument.CurrentText
                                        .Replace($"]({origImageLink})", $"]({mediaResult.URL})")
                                        .Replace($"=\"{origImageLink}\"", $"=\"{mediaResult.URL}\"");
                                }
                            }
                        }
                        WindowUtilities.DoEvents();
                    }

                    html = doc.DocumentNode.OuterHtml;
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error posting images to Weblog: " + ex.GetBaseException().Message;
                mmApp.Log("Failed to post image to Weblog", ex);
                return(null);
            }

            return(html);
        }
コード例 #2
0
        /// <summary>
        /// Parses each of the images in the document and posts them to the server.
        /// Updates the HTML with the returned Image Urls
        /// </summary>
        /// <param name="html">HTML that contains images</param>
        /// <param name="basePath">image file name</param>
        /// <param name="wrapper">blog wrapper instance that sends</param>
        /// <param name="metaData">metadata containing post info</param>
        /// <returns>update HTML string for the document with updated images</returns>
        private string SendImages(string html, string basePath,
                                  MetaWeblogWrapper wrapper)
        {
            // base folder name for uploads - just the folder name of the image
            var baseName = Path.GetFileName(basePath);

            baseName = FileUtils.SafeFilename(baseName).Replace(" ", "-");

            var doc = new HtmlDocument();

            doc.LoadHtml(html);
            try
            {
                // send up normalized path images as separate media items
                var images = doc.DocumentNode.SelectNodes("//img");
                if (images != null)
                {
                    foreach (HtmlNode img in images)
                    {
                        string imgFile = img.Attributes["src"]?.Value;
                        imgFile = StringUtils.UrlDecode(imgFile);

                        if (imgFile == null)
                        {
                            continue;
                        }

                        if (!imgFile.StartsWith("http://") && !imgFile.StartsWith("https://"))
                        {
                            if (!imgFile.Contains(":\\"))
                            {
                                imgFile = Path.Combine(basePath, imgFile.Replace("/", "\\"));
                            }

                            if (System.IO.File.Exists(imgFile))
                            {
                                var uploadFilename = Path.GetFileName(imgFile);
                                var media          = new MediaObject()
                                {
                                    Type = ImageUtils.GetImageMediaTypeFromFilename(imgFile),
                                    Bits = System.IO.File.ReadAllBytes(imgFile),
                                    Name = baseName + "/" + uploadFilename
                                };
                                var mediaResult = wrapper.NewMediaObject(media);
                                img.Attributes["src"].Value = mediaResult.URL;

                                // use first image as featured image
                                if (!DontInferFeaturedImage)
                                {
                                    if (string.IsNullOrEmpty(FeaturedImageUrl))
                                    {
                                        FeaturedImageUrl = mediaResult.URL;
                                    }
                                    if (string.IsNullOrEmpty(FeatureImageId))
                                    {
                                        FeatureImageId = mediaResult.Id;
                                    }
                                }
                            }
                        }
                        WindowUtilities.DoEvents();
                    }

                    html = doc.DocumentNode.OuterHtml;
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = "Error posting images to Weblog: " + ex.Message;
                return(null);
            }

            return(html);
        }