public async Task <ActionResult> Create(BlogEdit blog, [FromServices] HtmlSanitizerService sanitizerService) { ViewBag.CategoryList = _catUtil.GetCategoryDropdown(); ViewBag.UserHanGroup = GetUserHanGroup(); string content = blog.Content; bool isLocalimg = false; Blog newblog; List <string> imglist = null; try { if (!ModelState.IsValid) { throw new BlogException(); } if (content == null || string.IsNullOrWhiteSpace(BlogHelper.removeAllTags(content))) { ModelState.AddModelError("", "内容不能为空或纯图片"); throw new BlogException(); } if (NolinkCategories == null || !NolinkCategories.Contains(blog.CategoryID)) { if (blog.BlogLinks == null) { ModelState.AddModelError("", "链接地址不能为空"); throw new BlogException(); } else { blog.BlogLinks = blog.BlogLinks.Where(b => !string.IsNullOrWhiteSpace(b.url)).ToArray(); if (!BlogHelper.checkBlogLinks(blog.BlogLinks)) { ModelState.AddModelError("", "链接地址不能为空,且不得包含javascript"); throw new BlogException(); } } } if (!_blogUtil.CheckAdmin()) { content = sanitizerService.Sanitize(content); } if (blog.HanGroupID.HasValue && !_db.HanGroupMembers.Any(h => h.Username == User.Identity.Name && h.HanGroupID == blog.HanGroupID)) { ModelState.AddModelError("", "汉化组ID无效,请刷新重试。"); throw new BlogException(); } List <IFormFile> BlogImages = new List <IFormFile>(); if (Request.Form.Files.Count > 0) { for (int i = 0; i < Request.Form.Files.Count; i++) { var file = Request.Form.Files[i]; if (file.Length > 0) { if (!file.ContentType.Contains("image")) { ModelState.AddModelError("", "不接受的文件类型"); throw new BlogException(); } else if (file.Length > 1048576 * 4) { ModelState.AddModelError("", "文件不得超过4MB"); throw new BlogException(); } isLocalimg = true; BlogImages.Add(file); } else { content = BlogHelper.removeImgPlaceholder(content, i); } } } if (!isLocalimg) { var imgname = BlogHelper.getFirstImg(content); if (imgname == null || imgname.Length < 5) { ModelState.AddModelError("", "请添加预览图!(上传或在文中外链图片)"); throw new BlogException(); } imglist = new List <string>() { imgname }; } else { try { imglist = await _uploadUtil.SaveImagesAsync(BlogImages); } catch (Exception e) { ModelState.AddModelError("", "保存图片时发生异常:(" + e.Message + ")。如多次出错,请汇报给管理员。"); throw new BlogException(e.Message, e); } if (imglist.Count < 1) { ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。"); throw new BlogException(); } } string imgpath = string.Empty; if (imglist != null) { imgpath = string.Join(";", imglist); } bool approve = User.IsInRole("Administrator") || User.IsInRole("Writers") || User.IsInRole("Moderator"); // Replace 【】() with []() blog.BlogTitle = blog.BlogTitle.ToSingleByteCharacterString(); blog.ImagePath = imgpath; content = BlogHelper.RemoveComments(content); newblog = _blogUtil.AddBlog(blog.BlogTitle, content, blog.CategoryID, imgpath, User.Identity.Name, approve, isLocalimg, blog.BlogLinks); var taglist = new List <Tag>(); if (!string.IsNullOrEmpty(blog.BlogTags)) { string[] tags = TagUtil.SplitTags(blog.BlogTags); taglist = _tagUtil.AddTagsForBlog(newblog.BlogID, tags, newblog.Author); } var save = false; if (BlogHelper.BlogIsHarmony(_db, newblog, HarmonySettings)) { newblog.isHarmony = true; save = true; } if (blog.HanGroupID.HasValue) { _db.HanGroupBlogs.Add(new HanGroupBlog { BlogID = newblog.BlogID, HanGroupID = blog.HanGroupID.Value }); save = true; } if (blog.Option != null && !blog.Option.IsDefault()) { newblog.option = blog.Option.OverrideOption(_blogUtil); if (newblog.option.NoApprove) { newblog.isApproved = false; } save = true; } if (save) { _db.SaveChanges(); } TriggerNewBlog(newblog, taglist); } catch (BlogException e) { if (Request.IsAjaxRequest()) { return(Json(new { err = e.Message + string.Join(";", ModelState.Values.SelectMany(m => m.Errors) .Select(err => err.ErrorMessage) .ToList()) })); } return(View(blog)); } catch { if (isLocalimg && imglist != null) { await _uploadUtil.DeleteFilesAsync(imglist.Concat(new[] { blog.ImagePath.Split(';')[0].Replace("/upload/", "/thumbs/") })); } throw; } if (Request.IsAjaxRequest()) { return(Json(new { id = newblog.BlogID, src = BlogHelper.firstImgPath(newblog, true) })); } return(RedirectToAction("Details", new { id = newblog.BlogID })); }
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)); }