Пример #1
0
        public async Task <ActionResult> Publish(PostCommand post, [Required(ErrorMessage = "验证码不能为空")] string code, CancellationToken cancellationToken)
        {
            if (await RedisHelper.GetAsync("code:" + post.Email) != code)
            {
                return(ResultData(null, false, "验证码错误!"));
            }

            if (PostService.Any(p => p.Status == Status.Forbidden && p.Email == post.Email))
            {
                return(ResultData(null, false, "由于您曾经恶意投稿,该邮箱已经被标记为黑名单,无法进行投稿,如有疑问,请联系网站管理员进行处理。"));
            }

            var match = Regex.Match(post.Title + post.Author + post.Content, CommonHelper.BanRegex);

            if (match.Success)
            {
                LogManager.Info($"提交内容:{post.Title}/{post.Author}/{post.Content},敏感词:{match.Value}");
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            if (!CategoryService.Any(c => c.Id == post.CategoryId))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            post.Label   = string.IsNullOrEmpty(post.Label?.Trim()) ? null : post.Label.Replace(",", ",");
            post.Status  = Status.Pending;
            post.Content = await ImagebedClient.ReplaceImgSrc(await post.Content.HtmlSantinizerStandard().ClearImgAttributes(), cancellationToken);

            Post p = post.Mapper <Post>();

            p.IP            = ClientIP;
            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.DisableCopy   = true;
            p.Rss           = true;
            p = PostService.AddEntitySaved(p);
            if (p == null)
            {
                return(ResultData(null, false, "文章发表失败!"));
            }

            await RedisHelper.ExpireAsync("code:" + p.Email, 1);

            var content = new Template(await new FileInfo(HostEnvironment.WebRootPath + "/template/publish.html").ShareReadWrite().ReadAllTextAsync(Encoding.UTF8))
                          .Set("link", Url.Action("Details", "Post", new { id = p.Id }, Request.Scheme))
                          .Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                          .Set("title", p.Title).Render();

            BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "有访客投稿:", content, CommonHelper.SystemSettings["ReceiveEmail"], ClientIP));
            return(ResultData(p.Mapper <PostDto>(), message: "文章发表成功,待站长审核通过以后将显示到列表中!"));
        }