Пример #1
0
        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));
        }
Пример #2
0
        public async Task <ActionResult> Create(TopicEdit topic, [FromServices] HtmlSanitizerService sanitizerService)
        {
            ViewBag.CategoryID = sl;
            topic.LoadBlog(_db);
            if (ModelState.IsValid)
            {
                Topic ntopic = new Topic();
                if (!_blogUtil.CheckAdmin())
                {
                    topic.Content = sanitizerService.Sanitize(topic.Content);
                }
                int i = 0;
                foreach (var bid in topic.BlogIDs.Distinct())
                {
                    var b = topic.Blogs.SingleOrDefault(bb => bb.BlogID == bid);
                    if (b == null)
                    {
                        ModelState.AddModelError("", "未找到ID编号为" + bid + "的资源");
                        return(View(topic));
                    }
                    var blogintopic = new BlogsInTopic {
                        blog = b, topic = ntopic, BlogOrder = i++
                    };
                    _db.BlogsInTopics.Add(blogintopic);
                }

                ntopic.Author     = User.Identity.Name;
                ntopic.CategoryID = topic.CategoryID;
                ntopic.Content    = topic.Content;
                ntopic.CreateDate = DateTime.Now;
                ntopic.UpdateDate = DateTime.Now;
                ntopic.TopicVisit = 0;
                ntopic.TopicTitle = topic.TopicTitle;

                if (topic.TopicImage != null)
                {   //ValidateFileAttribute里已经检查过了
                    ntopic.isLocalImg = true;
                    var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { topic.TopicImage });

                    if (imglist.Count < 1)
                    {
                        ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。");
                        return(View(topic));
                    }
                    ntopic.ImagePath = imglist[0];
                }
                else
                {
                    string imgname = BlogHelper.getFirstImg(ntopic.Content);
                    if (imgname == null || imgname.Length < 5)
                    {
                        ModelState.AddModelError("", "请添加预览图!(上传或在文中外链图片)");
                        return(View(topic));
                    }
                    ntopic.ImagePath = imgname;
                }
                if (topic.TopicBanner != null)
                {
                    var imglist = await _uploadUtil.SaveImagesAsync(new IFormFile[] { topic.TopicBanner }, false);

                    if (imglist.Count < 1)
                    {
                        ModelState.AddModelError("", "图片服务器上传出错,请稍后再试。如多次出错,请汇报给管理员。");
                        return(View(topic));
                    }
                    ntopic.BannerPath = imglist[0];
                }

                var tag = _db.Tags.SingleOrDefault(t => t.TagName == topic.TagName);
                if (tag == null)
                {
                    tag = new Tag {
                        TagName = topic.TagName
                    };
                }
                ntopic.tag = tag;
                var mention = new MentionHandler(_udb);
                ntopic.Content = mention.ParseMentions(BlogHelper.RemoveComments(ntopic.Content));
                _db.Topics.Add(ntopic);
                _db.SaveChanges();
                mention.SendMentionMsg(_msgUtil, ntopic.Author, ntopic.TopicTitle, Url.Action("Details", new { id = ntopic.TopicID }));
                TriggerNewTopic(ntopic);
                return(RedirectToAction("Details", new { id = ntopic.TopicID }));
            }
            return(View(topic));
        }