コード例 #1
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: "文章修改成功!"));
        }