コード例 #1
0
        public async Task <ActionResult> Write(PostCommand post, DateTime?timespan, bool schedule = false)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!ValidatePost(post, out var resultData))
            {
                return(resultData);
            }

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

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = ClientIP;
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                foreach (var s in tmp)
                {
                    var     id      = s.ToInt32();
                    Seminar seminar = await SeminarService.GetByIdAsync(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)
                {
                    return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
                }

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

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

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

            return(ResultData(null, true, "文章发表成功!"));
        }
コード例 #2
0
ファイル: PostController.cs プロジェクト: ldqk/Masuit.MyBlogs
        public async Task <ActionResult> Write(PostCommand post, DateTime?timespan, bool schedule = false, CancellationToken cancellationToken = default)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(await post.Content.Trim().ClearImgAttributes(), cancellationToken);

            if (!ValidatePost(post, out var resultData))
            {
                return(resultData);
            }

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

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = ClientIP;
            p.Rss           = p.LimitMode is null or RegionLimitMode.All;
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                foreach (var s in tmp)
                {
                    var     id      = s.ToInt32();
                    Seminar seminar = await SeminarService.GetByIdAsync(id);

                    p.Seminar.Add(seminar);
                }
            }

            if (schedule)
            {
                if (!timespan.HasValue || timespan.Value <= DateTime.Now)
                {
                    return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
                }

                p.Status     = Status.Schedule;
                p.PostDate   = timespan.Value.ToUniversalTime();
                p.ModifyDate = timespan.Value.ToUniversalTime();
                BackgroundJob.Enqueue <IHangfireBackJob>(job => job.PublishPost(p));
                return(ResultData(p.Mapper <PostDto>(), message: $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!"));
            }

            PostService.AddEntity(p);
            var js = new JiebaSegmenter();

            (p.Keyword + "," + p.Label).Split(',', StringSplitOptions.RemoveEmptyEntries).ForEach(s => js.AddWord(s));
            bool b = await SearchEngine.SaveChangesAsync() > 0;

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

            return(ResultData(null, true, "文章发表成功!"));
        }
コード例 #3
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, "文章发表成功!"));
        }
コード例 #4
0
        public async Task <ActionResult> Write(PostCommand 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 (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

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

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = ClientIP;
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                foreach (var s in tmp)
                {
                    var     id      = s.ToInt32();
                    Seminar seminar = await SeminarService.GetByIdAsync(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)
                {
                    return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
                }

                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 <PostDto>(), message: $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!"));
            }

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

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

            return(ResultData(null, true, "文章发表成功!"));
        }