public ActionResult Publish(PostInputDto post) { UserInfoOutputDto user = Session.GetByRedis <UserInfoOutputDto>(SessionKey.UserInfo); if (!CategoryBll.Any(c => c.Id == post.CategoryId && c.Status == Status.Available)) { return(ResultData(null, message: "请选择一个分类")); } if (string.IsNullOrEmpty(post.Label?.Trim())) { post.Label = null; } else if (post.Label.Trim().Length > 50) { post.Label = post.Label.Replace(",", ","); post.Label = post.Label.Trim().Substring(0, 50); } else { post.Label = post.Label.Replace(",", ","); } post.Status = Status.Pending; post.PostDate = DateTime.Now; post.ModifyDate = DateTime.Now; if (user != null && user.IsAdmin) { post.Status = Status.Pended; } else { post.Content = ReplaceImgSrc(Regex.Replace(post.Content.HtmlSantinizerStandard(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/"); } ViewBag.CategoryId = new SelectList(CategoryBll.LoadEntitiesNoTracking(c => c.Status == Status.Available), "Id", "Name", post.CategoryId); Post p = post.Mapper <Post>(); p.PostAccessRecord.Add(new PostAccessRecord() { AccessTime = DateTime.Today, ClickCount = 0 }); p = PostBll.AddEntitySaved(p); if (p != null) { if (p.Status == Status.Pending) { var email = GetSettings("ReceiveEmail"); string link = Url.Action("Details", "Post", new { id = p.Id }, Request.Url?.Scheme ?? "http"); string content = System.IO.File.ReadAllText(Request.MapPath("/template/publish.html")).Replace("{{link}}", link).Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{title}}", p.Title); BackgroundJob.Enqueue(() => SendMail(GetSettings("Title") + "有访客投稿:", content, email)); return(ResultData(p.Mapper <PostOutputDto>(), message: "文章发表成功,待站长审核通过以后将显示到列表中!")); } return(ResultData(p.Mapper <PostOutputDto>(), message: "文章发表成功!")); } return(ResultData(null, false, "文章发表失败!")); }
public ActionResult Write(PostInputDto post, string Seminars, DateTime?timespan, bool schedule = false) { post.Content = ReplaceImgSrc(Regex.Replace(post.Content.Trim(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/");//提取img标签,提取src属性并重新创建个只包含src属性的img标签 if (!CategoryBll.Any(c => c.Id == post.CategoryId && c.Status == Status.Available)) { return(ResultData(null, message: "请选择一个分类")); } if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null")) { post.Label = null; } else if (post.Label.Trim().Length > 50) { post.Label = post.Label.Replace(",", ","); post.Label = post.Label.Trim().Substring(0, 50); } else { post.Label = post.Label.Replace(",", ","); } if (!post.IsWordDocument) { post.ResourceName = null; } if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase)) { post.ProtectContent = null; } post.Status = Status.Pended; post.PostDate = DateTime.Now; post.ModifyDate = DateTime.Now; Post p = post.Mapper <Post>(); if (!string.IsNullOrEmpty(Seminars)) { var tmp = Seminars.Split(',').Distinct(); tmp.ForEach(s => { var id = s.ToInt32(); p.Seminar.Add(SeminarBll.GetById(id)); }); } p.PostAccessRecord.Add(new PostAccessRecord() { AccessTime = DateTime.Today, ClickCount = 0 }); if (schedule) { if (timespan.HasValue && timespan.Value > DateTime.Now) { p.Status = Status.Schedule; HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p); return(ResultData(p.Mapper <PostOutputDto>(), message: schedule ? $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!" : "文章发表成功!")); } return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!")); } p = PostBll.AddEntitySaved(p); if (p != null) { var cast = BroadcastBll.LoadEntities(c => c.Status == Status.Subscribed).ToList(); string link = Request.Url?.Scheme + "://" + Request.Url?.Authority + "/" + p.Id; cast.ForEach(c => { var ts = DateTime.Now.GetTotalMilliseconds(); string content = System.IO.File.ReadAllText(Request.MapPath("/template/broadcast.html")).Replace("{{link}}", link + "?email=" + c.Email).Replace("{{time}}", post.PostDate.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{title}}", post.Title).Replace("{{author}}", post.Author).Replace("{{content}}", post.Content.RemoveHtmlTag(150)).Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new { c.Email, act = "cancel", validate = c.ValidateCode, timespan = ts, hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(ConfigurationManager.AppSettings["BaiduAK"]) }, Request.Url.Scheme)); BackgroundJob.Schedule(() => SendMail(GetSettings("Title") + "博客有新文章发布了", content, c.Email), (p.PostDate - DateTime.Now)); }); HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.UpdateLucene)); return(ResultData(null, true, "文章发表成功!")); } return(ResultData(null, false, "文章发表失败!")); }
public ActionResult Edit(PostInputDto post, string Seminars, bool notify = true) { post.Content = ReplaceImgSrc(Regex.Replace(post.Content.Trim(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/"); if (!CategoryBll.Any(c => c.Id == post.CategoryId && c.Status == Status.Available)) { return(ResultData(null, message: "请选择一个分类")); } if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null")) { post.Label = null; } else if (post.Label.Trim().Length > 50) { post.Label = post.Label.Replace(",", ","); post.Label = post.Label.Trim().Substring(0, 50); } else { post.Label = post.Label.Replace(",", ","); } if (!post.IsWordDocument) { post.ResourceName = null; } if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase)) { post.ProtectContent = null; } post.ModifyDate = DateTime.Now; Post p = PostBll.GetById(post.Id); var history = p.Mapper <PostHistoryVersion>(); p.PostHistoryVersion.Add(history); Mapper.Map(post, p); if (!string.IsNullOrEmpty(Seminars)) { var tmp = Seminars.Split(',').Distinct(); p.Seminar.Clear(); tmp.ForEach(s => { p.Seminar.Add(SeminarBll.GetFirstEntity(e => e.Title.Equals(s))); }); } bool b = PostBll.UpdateEntitySaved(p); if (b) { #if !DEBUG if (notify) { var cast = BroadcastBll.LoadEntities(c => c.Status == Status.Subscribed).ToList(); string link = Request.Url?.Scheme + "://" + Request.Url?.Authority + "/" + p.Id; cast.ForEach(c => { var ts = DateTime.Now.GetTotalMilliseconds(); string content = System.IO.File.ReadAllText(Request.MapPath("/template/broadcast.html")).Replace("{{link}}", link + "?email=" + c.Email).Replace("{{time}}", post.PostDate.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{title}}", post.Title).Replace("{{author}}", post.Author).Replace("{{content}}", post.Content.RemoveHtmlTag(150)).Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new { c.Email, act = "cancel", validate = c.ValidateCode, timespan = ts, hash = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(ConfigurationManager.AppSettings["BaiduAK"]) }, Request.Url.Scheme)); BackgroundJob.Schedule(() => SendMail(GetSettings("Title") + "博客有新文章发布了", content, c.Email), (p.PostDate - DateTime.Now)); }); } #endif HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.UpdateLucene)); return(ResultData(p.Mapper <PostOutputDto>(), message: "文章修改成功!")); } return(ResultData(null, false, "文章修改失败!")); }