Beispiel #1
0
        public async Task AutoMaticallyPublishMarkdownPostsAsync(GitlabPostsNavInput input)
        {
            // var userId=  AbpSession.UserId;
            var sercertCode = _appConfiguration.GetMarkdownPostSercerCode();

            if (sercertCode.IsNullOrEmpty())
            {
                throw new UserFriendlyException("key丢失,请配置后,再提交。");
            }
            if (input.SercertCode != sercertCode)
            {
                throw new UserFriendlyException("密钥不正确,请重新提交新密钥。");
            }
            var fileContent = await _gitlabClientAppService.GetGitlabFileInfo(input);

            if (fileContent == null)
            {
                throw new UserFriendlyException($"{input.FilePath}路径下的{input.FileName}文件在Gitlab中不存在,请重试。");
            }
            var dto = JsonConvert.DeserializeObject <RepoPostsDto>(fileContent.ContentDecoded);

            if (dto.items != null)
            {
                var items = dto.items.Where(a => a.Enabled).ToList();

                if (items.Count > 0)
                {
                    //判断第一篇文章发布的博客名称
                    var blogshortName = items[0].blogShortName;

                    var blog = await _blogManager.GetByShortNameAsync(blogshortName);

                    foreach (var item in items)
                    {
                        //如果后续的文章名称和博客名称不一致则发布到新博客文章中去
                        if (item.blogShortName != blogshortName)
                        {
                            blog = await _blogManager.GetByShortNameAsync(blogshortName);
                        }

                        if (item.title.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        input.FileName = item.path;
                        //获取文章内容
                        var itemContent = await _gitlabClientAppService.GetGitlabFileInfo(input);

                        if (itemContent == null)
                        {
                            continue;
                        }

                        var name = itemContent.Filename;

                        if (item.URL.IsNullOrEmpty())
                        {
                            item.URL = Path.GetFileNameWithoutExtension(name);
                        }

                        //转换为html内容
                        var htmlContent = _markdownConverter.ConvertToHtml(itemContent.ContentDecoded);
                        //上传图片到图床
                        htmlContent = await UploadpicturesToPictureBedAsync(htmlContent, input, item);

                        //todo find a way to make it on client in prismJS configuration (eg: map C# => csharp)
                        htmlContent = HtmlNormalizer.ReplaceCodeBlocksLanguage(
                            htmlContent,
                            "language-C#",
                            "language-csharp"
                            );
                        htmlContent = HtmlNormalizer.ReplaceCodeLinkUrl(htmlContent);
                        var tags = item.tags.Split(',');

                        var post = new CreatePostDto
                        {
                            BlogId     = blog.Id,
                            Title      = item.title,
                            Content    = htmlContent,
                            Url        = item.URL,
                            NewTags    = tags.ToList(),
                            CoverImage = item.ConverImage,
                            PostType   = PostType.Original
                        };

                        await _postAppService.CreatePostByMakrdown(post);
                    }
                }
            }
        }