コード例 #1
0
ファイル: SyncCore.cs プロジェクト: ciker/WPYoudaonNote
        /// <summary>
        /// 将笔记中图片的本地地址替换成远程地址。
        /// </summary>
        /// <param name="noteContent"></param>
        /// <returns></returns>
        public string ConvertImageLocalPathToRemoteUrl(string noteContent)
        {
            var dom = new HtmlDocument();

            dom.LoadHtml(noteContent);

            var root = dom.DocumentNode;

            var imgNodes = root.Descendants("img");

            foreach (var img in imgNodes)
            {
                // Invalid imgNode tag.
                if (!img.Attributes.Contains("src") || string.IsNullOrEmpty(img.Attributes["src"].Value))
                {
                    continue;
                }

                var entity = ImageDao.GetImageByLocalSavePath(img.Attributes["src"].Value);
                if (entity != null)
                {
                    // Replace the image's src attribute to local path.
                    img.Attributes["src"].Value = entity.ImgRemoteUrl;
                }
                else // 在本地数据库没有找到相关图片记录说明是本地新添加的图片,则上传到服务器
                {
                    var imgUrl = _api.UploadImage(Path.Combine("Images", Path.GetFileName(img.Attributes["src"].Value)));
                    img.Attributes["src"].Value = imgUrl;
                }
            }
            return(root.InnerHtml);
        }