Ejemplo n.º 1
0
        public ActionResult Pass(int id)
        {
            Post post = PostService.GetById(id);

            post.Status     = Status.Pended;
            post.ModifyDate = DateTime.Now;
            post.PostDate   = DateTime.Now;
            bool b = PostService.SaveChanges() > 0;

            if (!b)
            {
                SearchEngine.LuceneIndexer.Add(post);
                return(ResultData(null, false, "审核失败!"));
            }

            if ("true" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
            {
                return(ResultData(null, true, "审核通过!"));
            }

            var cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
            var link = Request.Scheme + "://" + Request.Host + "/" + id;

            cast.ForEach(c =>
            {
                var ts      = DateTime.Now.GetTotalMilliseconds();
                var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/broadcast.html")
                              .Replace("{{link}}", link + "?email=" + c.Email)
                              .Replace("{{time}}", post.ModifyDate.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(AppConfig.BaiduAK)
                }, Request.Scheme));
                BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email));
            });

            return(ResultData(null, true, "审核通过!"));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Write(PostInputDto post, DateTime?timespan, bool schedule = false)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.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>();

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                tmp.ForEach(s =>
                {
                    var id          = s.ToInt32();
                    Seminar seminar = SeminarService.GetById(id);
                    p.Seminar.Add(new SeminarPost()
                    {
                        Post      = p,
                        PostId    = p.Id,
                        Seminar   = seminar,
                        SeminarId = seminar.Id
                    });
                });
            }

            if (schedule)
            {
                if (timespan.HasValue && timespan.Value > DateTime.Now)
                {
                    p.Status     = Status.Schedule;
                    p.PostDate   = timespan.Value;
                    p.ModifyDate = timespan.Value;
                    HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p);
                    return(ResultData(p.Mapper <PostOutputDto>(), message: $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!"));
                }

                return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
            }

            PostService.AddEntity(p);
            bool b = SearchEngine.SaveChanges() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章发表失败!"));
            }

            if ("true" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
            {
                return(ResultData(null, true, "文章发表成功!"));
            }
            var    cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
            string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;

            cast.ForEach(c =>
            {
                var ts         = DateTime.Now.GetTotalMilliseconds();
                string content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/broadcast.html")
                                 .Replace("{{link}}", link + "?email=" + c.Email)
                                 .Replace("{{time}}", post.ModifyDate.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(AppConfig.BaiduAK)
                }, Request.Scheme));
                BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
            });
            return(ResultData(null, true, "文章发表成功!"));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Edit(PostInputDto post, bool notify = true, bool reserve = true)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.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 p = PostService.GetById(post.Id);

            if (reserve)
            {
                var history = p.Mapper <PostHistoryVersion>();
                p.PostHistoryVersion.Add(history);
                post.ModifyDate = DateTime.Now;
                var user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);
                p.Modifier      = user.NickName;
                p.ModifierEmail = user.Email;
            }

            p.IP = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            Mapper.Map(post, p);
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                p.Seminar.Clear();
                tmp.ForEach(s =>
                {
                    var seminar = SeminarService.Get(e => e.Title.Equals(s));
                    if (seminar != null)
                    {
                        p.Seminar.Add(new SeminarPost()
                        {
                            Post      = p,
                            Seminar   = seminar,
                            PostId    = p.Id,
                            SeminarId = seminar.Id
                        });
                    }
                });
            }

            bool b = SearchEngine.SaveChanges() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章修改失败!"));
            }

#if !DEBUG
            if (notify && "false" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
            {
                var    cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
                string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;
                cast.ForEach(c =>
                {
                    var ts         = DateTime.Now.GetTotalMilliseconds();
                    string content = System.IO.File.ReadAllText(Path.Combine(HostEnvironment.WebRootPath, "template", "broadcast.html"))
                                     .Replace("{{link}}", link + "?email=" + c.Email)
                                     .Replace("{{time}}", post.ModifyDate.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(AppConfig.BaiduAK)
                    }, Request.Scheme));
                    BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
                });
            }
#endif
            return(ResultData(p.Mapper <PostOutputDto>(), message: "文章修改成功!"));
        }