protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int ret = TagUtil.CheckBlogTag(value.ToString()); if (ret != 0) { var err = ret > 0 ? string.Format(this.ErrorMessage, MaxCount) : "标签不得超过" + MaxLength + "个字符"; return(new ValidationResult(err)); } } return(null); }
public async Task <ActionResult> Edit(int id, TopicEdit etopic, [FromServices] HtmlSanitizerService sanitizerService) { etopic.LoadBlog(_db); ViewBag.CategoryID = new SelectList(_catUtil.GetCategoryList(), "CategoryID", "CategoryName", etopic.CategoryID); int ret = TagUtil.CheckBlogTag(etopic.TagName, 1); if (ret != 0) { ModelState.AddModelError("", ret > 0 ? "专题标签只能有1个" : "标签不得超过20个字符"); } else if (!_blogUtil.CheckAdmin()) { etopic.Content = sanitizerService.Sanitize(etopic.Content); } else if (ModelState.IsValid) { var topic = _db.Topics.Find(id); bool uploadsaved = false; bool bannersaved = false; var blogcurrent = _db.BlogsInTopics.Where(bi => bi.TopicID == id).ToList(); foreach (var blog in blogcurrent) { _db.BlogsInTopics.Remove(blog); } int i = 0; foreach (var bid in etopic.BlogIDs.Distinct()) { var b = etopic.Blogs.SingleOrDefault(bb => bb.BlogID == bid); if (b == null) { ModelState.AddModelError("", "未找到ID编号为" + bid + "的资源"); return(View(topic)); } var blogintopic = new BlogsInTopic { blog = b, topic = topic, BlogOrder = i++ }; _db.BlogsInTopics.Add(blogintopic); } if (topic.tag.TagName != etopic.TagName) { var tag = _db.Tags.SingleOrDefault(t => t.TagName == etopic.TagName); if (tag == null) { tag = new Tag { TagName = etopic.TagName }; } topic.tag = tag; } try { var originalImage = topic.ImagePath; var originalBanner = topic.BannerPath; bool shouldDeleteOldImage = false; bool shouldDeleteOldBanner = string.IsNullOrWhiteSpace(etopic.BannerPath); if (etopic.TopicImage != null) { shouldDeleteOldImage = topic.isLocalImg; topic.isLocalImg = true; var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { etopic.TopicImage }, true); if (imglist.Count < 1) { ModelState.AddModelError("", "保存图片时发生异常。请尝试转换图片格式后再次上传。如多次出错,请汇报给管理员。"); return(View(etopic)); } topic.ImagePath = imglist[0]; uploadsaved = true; } else if (!topic.isLocalImg || (topic.isLocalImg && !etopic.IsLocalImg)) { string imgname = BlogHelper.getFirstImg(etopic.Content); if (imgname == null || imgname.Length < 5) { ModelState.AddModelError("", "请添加预览图!(上传或在文中外链图片)"); return(View(etopic)); } shouldDeleteOldImage = !etopic.IsLocalImg; topic.isLocalImg = false; topic.ImagePath = imgname; } if (etopic.TopicBanner != null) { var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { etopic.TopicBanner }, false); if (imglist.Count < 1) { ModelState.AddModelError("", "图片服务器上传出错,请尝试转换图片格式后再次上传。如多次出错,请汇报给管理员。"); return(View(topic)); } shouldDeleteOldBanner = true; bannersaved = true; topic.BannerPath = imglist[0]; } else { topic.BannerPath = etopic.BannerPath; } if (shouldDeleteOldBanner && !string.IsNullOrWhiteSpace(originalBanner)) { await _uploadUtil.DeleteFileAsync(originalBanner); } if (shouldDeleteOldImage && !string.IsNullOrWhiteSpace(originalImage)) { await _uploadUtil.DeleteFilesAsync(new[] { originalImage, originalImage.Replace("/upload/", "/thumbs/") }); } topic.UpdateDate = DateTime.Now; topic.TopicTitle = etopic.TopicTitle; topic.CategoryID = etopic.CategoryID; var mention = new MentionHandler(_udb); topic.Content = mention.ParseMentions(BlogHelper.RemoveComments(etopic.Content)); mention.SendMentionMsg(_msgUtil, User.Identity.Name, etopic.TopicTitle, Url.Action("Details", new { id = topic.TopicID })); _db.Entry(topic).State = EntityState.Modified; _db.SaveChanges(); TriggerEditTopic(topic); if (User.Identity.Name != topic.Author) { _adminUtil.log(User.Identity.Name, "edittopic", topic.TopicID.ToString()); } } catch { if (uploadsaved) { await _uploadUtil.DeleteFilesAsync(new[] { topic.ImagePath, topic.ImagePath.Replace("/upload/", "/thumbs/") }); } if (bannersaved) { await _uploadUtil.DeleteFileAsync(topic.BannerPath); } throw; } return(RedirectToAction("Details", new { id })); } return(View(etopic)); }