コード例 #1
0
        /// <summary>
        /// 替换图片的源路径
        /// </summary>
        /// <param name="content"></param>
        /// <param name="documentRawRootUrl"></param>
        /// <param name="localDirectory"></param>
        /// <returns></returns>
        public static string ReplaceImageSources(string content, string documentRawRootUrl, string localDirectory)
        {
            if (content == null)
            {
                return(null);
            }

            content = Regex.Replace(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", delegate(Match match)
            {
                if (WebUrlHelper.IsExternalLink(match.Groups[2].Value))
                {
                    return(match.Value);
                }

                //本地的图片,需要上传到图床中去

                var newImageSource = documentRawRootUrl.EnsureEndsWith('/') +
                                     (localDirectory.IsNullOrEmpty() ? "" : localDirectory.TrimStart('/').EnsureEndsWith('/')) +
                                     match.Groups[2].Value.TrimStart('/');

                var url = match.Groups[1] + " src=\"" + newImageSource + "\" " + match.Groups[3];
                return(url);
            }, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);

            return(content);
        }
コード例 #2
0
        /// <summary>
        /// Gets the scraping helper.
        /// </summary>
        /// <param name="scrapType">Type of the scrap.</param>
        /// <param name="document">The document.</param>
        /// <returns>IScrapingHelper</returns>
        public static IScrapingHelper GetScrapingHelper(ScrapingType scrapType, string document)
        {
            IScrapingHelper result;

            switch (scrapType)
            {
            case ScrapingType.HTMLValue:
                result = new HTMLValueHelper(document);
                break;

            case ScrapingType.HTMLDoc:
                result = new HTMLDocHelper(document);
                break;

            case ScrapingType.JsonDoc:
                result = new JsonDocHelper(document);
                break;

            case ScrapingType.ApiUrl:
                result = new ApiUrlHelper(document);
                break;

            case ScrapingType.WebUrl:
                result = new WebUrlHelper(document);
                break;

            case ScrapingType.Text:
                result = new TextHelper(document);
                break;

            case ScrapingType.HTMLAttribute:
                result = new HTMLAttributeHelper(document);
                break;

            case ScrapingType.JsonVlue:
                result = new JsonValueHelper(document);
                break;

            //case ScrapingType.WebDriver:
            // //   result = new WebDriverHelper(document);
            //    break;
            //case ScrapingType.Header:
            //   // result = new HeaderHelper(document);
            //    break;
            default:
                result = new ApiUrlHelper(document);
                break;
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// 对URL路径进行替换和重组
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>

        protected virtual string NormalizeLinks(
            string content


            )
        {
            var normalized = Regex.Replace(content, MarkdownLinkRegExp, delegate(Match match)
            {
                var link = match.Groups[2].Value;
                if (WebUrlHelper.IsExternalLink(link))
                {
                    return(match.Value);
                }

                var displayText = match.Groups[1].Value;

                var documentName = RemoveFileExtension(link);



                var result = string.Format(MdLinkFormat,
                                           displayText,
                                           documentName
                                           );


                return(result);
            });

            normalized = Regex.Replace(normalized, AnchorLinkRegExp, delegate(Match match)
            {
                var link = match.Groups[1].Value;
                if (WebUrlHelper.IsExternalLink(link))
                {
                    return(match.Value);
                }

                var displayText  = match.Groups[2].Value;
                var documentName = RemoveFileExtension(link);


                return(string.Format(
                           MdLinkFormat,
                           displayText,
                           documentName
                           ));
            });

            return(normalized);
        }
コード例 #4
0
        public List <FileExplorerItem> GetFileExplorerItems(string currentPath)
        {
            if (string.IsNullOrEmpty(currentPath))
            {
                return(null);
            }
            List <FileExplorerItem> items = new List <FileExplorerItem>();
            DirectoryInfo           root  = new DirectoryInfo(currentPath);

            DirectoryInfo[] dirs  = root.GetDirectories();
            FileInfo[]      files = root.GetFiles();
            if (dirs.Length <= 0 && files.Length <= 0)
            {
                return(null);
            }
            else
            {
                foreach (DirectoryInfo dir in dirs)
                {
                    if (!FileHelper.IsHiddenFile(dir.Attributes))
                    {
                        items.Add(new FileExplorerItem()
                        {
                            IsFile       = false,
                            FullFileName = dir.FullName,
                            FileName     = dir.Name
                        });
                    }
                }
                foreach (FileInfo file in files)
                {
                    if (!FileHelper.IsHiddenFile(file.Attributes))
                    {
                        items.Add(new FileExplorerItem()
                        {
                            IsFile        = true,
                            FullFileName  = file.FullName,
                            FileName      = file.Name,
                            FileExtension = file.Extension,
                            FileLength    = file.Length,
                            FileUrl       = WebUrlHelper.ToVirtual(file.FullName)
                        });
                    }
                }
                return(items);
            }
        }
コード例 #5
0
        /// <summary>
        /// 上传图片到图床地址中
        /// </summary>
        /// <param name="content"></param>
        /// <param name="input"></param>
        /// <param name="postConfig"></param>
        /// <returns></returns>
        private async Task <string> UploadpicturesToPictureBedAsync(string content, GitlabPostsNavInput input, PostItems postConfig)
        {
            if (content == null)
            {
                return(null);
            }

            //获取上传图片列表
            var toDoImgBedList      = Regex.Matches(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
            var newSourceImagesList = new List <string>();

            foreach (Match itemPic in toDoImgBedList)
            {
                //检查是否为外联,如果是外联按不处理
                if (WebUrlHelper.IsExternalLink(itemPic.Groups[2].Value))
                {
                    newSourceImagesList.Add(itemPic.Groups[2].Value);
                    //检查图片是否为外部链接
                }
                else
                {
                    input.FileName = itemPic.Groups[2].Value;
                    var file = await _gitlabClientAppService.GetGitlabFileInfo(input);

                    if (file == null)
                    {
                        continue;
                    }
                    //获取当前图片的base64,然后上传到图床中。
                    var tags          = postConfig.tags.Split(',');
                    var directoryPath = $"{postConfig.blogShortName}/{tags[0]}";
                    //存放的图床仓库和地址
                    var pictureToBed = new UploadPictureToBed
                    {
                        PathWithNamespace = "52abp/picturebed",
                        file          = file,
                        DirectoryPath = directoryPath
                    };
                    //上传图片到指定图床
                    var imgRawUrl = await _gitlabClientAppService.UploadPictureToImgBed(pictureToBed);

                    newSourceImagesList.Add(imgRawUrl);
                    //install-ubuntu-1.png
                    //http://code.52abp.com/52abp/picturebed/raw/master/2020/04/28/install-ubuntu-1.png
                }
            }

            //将本地图源,替换为远程图床的图源
            content = Regex.Replace(content, @"(<img\s+[^>]*)src=""([^""]*)""([^>]*>)", (Match match) =>
            {
                var oldImageSource = match.Groups[2].Value;

                if (WebUrlHelper.IsExternalLink(oldImageSource))
                {
                    //检查图片是否为外部链接

                    var newurl = match.Groups[1] + " class=\"img-fluid\" src =\"" + oldImageSource + "\" " + match.Groups[3];

                    return(newurl);
                    // return match.Value;
                }
                oldImageSource = Path.GetFileName(oldImageSource);

                var newImageSource = newSourceImagesList
                                     .FirstOrDefault(a => a.EndsWith(oldImageSource));

                var url = match.Groups[1] + " class=\"img-fluid\" src =\"" + newImageSource + "\" " + match.Groups[3];

                return(url);
            }, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);

            return(content);
        }