public async Task <ActionResult> Edit([FromServices] HtmlSanitizerService sanitizerService, int id, BlogEdit blog, IFormFile[] files, bool setmain = false) { ViewBag.CategoryList = _catUtil.GetCategoryDropdown(blog.CategoryID); ViewBag.id = id; if (NolinkCategories == null || !NolinkCategories.Contains(blog.CategoryID)) { if (blog.BlogLinks == null) { ModelState.AddModelError("", "链接地址不能为空"); return(View(blog)); } else { blog.BlogLinks = blog.BlogLinks.Where(b => !string.IsNullOrWhiteSpace(b.url)).ToArray(); if (!BlogHelper.checkBlogLinks(blog.BlogLinks)) { ModelState.AddModelError("", "链接地址不能为空,且不得包含javascript"); return(View(blog)); } } } if (blog.Content == null || string.IsNullOrWhiteSpace(BlogHelper.removeAllTags(blog.Content))) { ModelState.AddModelError("", "内容不能为空或纯图片"); return(View(blog)); } if (!_blogUtil.CheckAdmin()) { blog.Content = sanitizerService.Sanitize(blog.Content); } if (ModelState.IsValid) { Blog originalblog = _db.Blogs.Find(id); bool hasupload = false; List <string> dellist = null; List <string> newlist = null; string thumb = null; string newthumb = null; string[] originalImglist = originalblog.ImagePath?.Split(';') ?? new string[] { }; string[] currentImglist = blog.ImagePath?.Split(';') ?? new string[] { }; int imgcount = currentImglist.Length; bool[] uploadpos = null; if (originalblog.option != null) { originalblog.option.MergeWith(_blogUtil, blog.Option); } else if (!blog.Option.OverrideOption(_blogUtil).IsDefault()) { originalblog.option = blog.Option; } if (originalblog.IsLocalImg) { dellist = originalImglist.ToList(); thumb = dellist.First(); } if (currentImglist.Length != 0) //item has local image & might or might not changed { // foreach name in current imglist, if orignal imglist does not contain the name,it is not valid if (!currentImglist.All(n => originalImglist.Contains(n))) { ModelState.AddModelError("", "内部参数错误,请刷新重试"); return(View(blog)); } // foreach name in orignial imglist, if current imglist does not contain the name,delete it dellist = originalImglist.Except(currentImglist).ToList(); originalblog.ImagePath = blog.ImagePath; } if (files != null) { uploadpos = new bool[files.Length]; imgcount += files.Where(f => f != null).Count(); for (int i = 0; i < files.Length; i++) { var data = files[i]; if (data != null) { if (data.Length > 1048576 * 4 || !data.ContentType.Contains("image")) { ModelState.AddModelError("", "单个文件不得超过4MB,且必须是图片"); return(View(blog)); } hasupload = true; uploadpos[i] = true; } else { uploadpos[i] = false; } } } if (hasupload) // has upload file { try { newlist = await _uploadUtil.SaveImagesAsync(files, false); } catch (Exception e) { ModelState.AddModelError("", "保存图片时发生异常:(" + e.Message + ")。如多次出错,请汇报给管理员。"); return(View(blog)); } if (newlist.Count < 1) { ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。"); return(View(blog)); } int i = 0; if (originalblog.IsLocalImg && setmain && files[0] != null && currentImglist.Length > 0) { List <string> updatedImgList = new List <string>(currentImglist); updatedImgList.Insert(0, newlist[0]); updatedImgList.AddRange(newlist.Skip(1)); originalblog.ImagePath = string.Join(";", updatedImgList); blog.Content = BlogHelper.InsertImgPlaceholder(blog.Content); i++; } else if (!originalblog.IsLocalImg) { originalblog.ImagePath = string.Join(";", newlist); } else { originalblog.ImagePath = string.Join(";", currentImglist.Concat(newlist)); } blog.Content = BlogHelper.ReplaceNewImgPlaceholder(blog.Content, i, currentImglist.Length, uploadpos); originalblog.IsLocalImg = true; } if (!originalblog.IsLocalImg || imgcount == 0) //no img no upload { string imgname = BlogHelper.getFirstImg(blog.Content); if (imgname == null || imgname.Length < 5) { ModelState.AddModelError("", "请添加预览图!(上传或外链图片)"); blog.ImagePath = originalblog.ImagePath; return(View(blog)); } originalblog.ImagePath = imgname; originalblog.IsLocalImg = false; } else { newthumb = originalblog.ImagePath.Split(';').ToList().First(); } var mention = new MentionHandler(_udb); blog.Content = mention.ParseMentions(blog.Content); mention.SendMentionMsg(_msgUtil, originalblog.Author, originalblog.BlogTitle, Url.Action("Details", new { id = originalblog.BlogID })); if (blog.Option != null && (originalblog.option != null || !blog.Option.IsDefault())) { originalblog.option = blog.Option; } // else image uploaded before and did not changed string[] tags = TagUtil.SplitTags(blog.BlogTags); List <Tag> updatedTags = null; try { // Replace 【】() with []() originalblog.BlogTitle = blog.BlogTitle.ToSingleByteCharacterString(); originalblog.Content = BlogHelper.RemoveComments(blog.Content); originalblog.CategoryID = blog.CategoryID; originalblog.Links = Newtonsoft.Json.JsonConvert.SerializeObject(blog.BlogLinks); if (blog.Option.NoApprove) { originalblog.isApproved = false; } else if (originalblog.isApproved == false) { originalblog.isApproved = null; } else if (originalblog.isApproved == null) { // Remove pending votes since the blog has changed. var audits = _db.BlogAudits.Where(b => b.BlogID == originalblog.BlogID).ToList(); var lastDecision = audits.Where(ba => ba.AuditAction == BlogAudit.Action.Approve || ba.AuditAction == BlogAudit.Action.Deny).OrderByDescending(ba => ba.BlogVersion).FirstOrDefault(); int lastVersion = lastDecision == null ? 0 : lastDecision.BlogVersion; _db.BlogAudits.RemoveRange(audits.Where(ba => ba.BlogVersion > lastVersion && (ba.AuditAction == BlogAudit.Action.VoteApprove || ba.AuditAction == BlogAudit.Action.VoteDeny))); } updatedTags = _tagUtil.SetTagsForBlog(originalblog.BlogID, tags, originalblog.Author); originalblog.isHarmony = BlogHelper.BlogIsHarmony(_db, originalblog, HarmonySettings); _db.SaveChanges(); } catch { if (originalblog.IsLocalImg && newlist != null) { await _uploadUtil.DeleteFilesAsync(newlist); } throw; } if (dellist != null && dellist.Count > 0) { await _uploadUtil.DeleteFilesAsync(dellist); } if (thumb != null && thumb != newthumb && blog.IsLocalImg) { await _uploadUtil.DeleteFileAsync(thumb.Replace("/upload/", "/thumbs/")); } if (originalblog.IsLocalImg && thumb != newthumb) { await _uploadUtil.SaveThumbAsync(originalblog.ImagePath); } if (originalblog.Author != User.Identity.Name) { _adminUtil.log(User.Identity.Name, "editblog", originalblog.BlogID.ToString()); } TriggerEditBlog(originalblog, updatedTags); return(RedirectToAction("Details", new { id })); } return(View(blog)); }