public ReturnResult<long> PublishArticle(ArticleContentDataContract article) { if (null == article) { return new ReturnResult<long>(101, 0, "数据错误"); } if (string.IsNullOrWhiteSpace(article.Title)) { return new ReturnResult<long>(104, 0, "帖子标题不允许为空"); } if (string.IsNullOrWhiteSpace(article.ArticleContent)) { return new ReturnResult<long>(102, 0, "帖子内容不允许为空"); } var parentCategoryList = articleCategoryService.GetParentCategoryList(article.CategoryId); if (!parentCategoryList.Any()) { return new ReturnResult<long>(103, 0, "文章分类ID错误,未找到指定的ID"); } int rootCategoryId = parentCategoryList.First(); rootCategoryId = rootCategoryId == 0 ? article.CategoryId : rootCategoryId; parentCategoryList.Add(article.CategoryId); article.CategoryIdList = string.Join("|", parentCategoryList); article.PostTime = DateTime.Now; article.UpdateTime = DateTime.Now; article.HitTime = DateTime.Now; article.Description = article.ArticleContent.RemoveHtmlTag().CutString(480); if (article.CategoryId == 390) { article.Title = "[视频]"; } else if (article.CategoryId == 388) { article.Title = "[图片]"; } else { article.Title = string.IsNullOrEmpty(article.Description) ? article.Author : article.Description.CutString(20); } article.Title = article.Title ?? ""; if (ArticleContentRepository.Instance.Insert(rootCategoryId, article)) { return new ReturnResult<long>(article.ArticleID); } else { return new ReturnResult<long>(102, 0, "发布失败."); } }
public JsonResult Publish(OauthToken token, int categoryId, string title, string content) { try { content = Server.UrlDecode(content); if (string.IsNullOrWhiteSpace(content)) { return Json(new ReturnResult<string>(101, null, "参数content不能为空")); } #region 图片解析与上传 try { Regex re = new Regex("<img( ||.*?)src=('|\"|)([^\"|^\']+)('|\"|>| )", RegexOptions.IgnoreCase); MatchCollection matches = re.Matches(content); foreach (Match mh in matches) { string base64Str = mh.Groups[3].Value;//src里面的路径 if (!string.IsNullOrWhiteSpace(base64Str) && base64Str.StartsWith("data:image/")) { string imageData = base64Str.Split(',')[1]; var fileName = string.Format("{0}.jpg", Guid.NewGuid().ToString()); var filePath = string.Format("Upload/images/{0}", Guid.NewGuid().ToString("N").CutString(2).ToLower()); var serverPath = Server.MapPath("~/" + filePath); byte[] imageBytes = Convert.FromBase64String(imageData); if (!System.IO.Directory.Exists(serverPath)) { System.IO.Directory.CreateDirectory(serverPath); } using (MemoryStream ms = new MemoryStream(imageBytes)) { Bitmap bmp = new Bitmap(ms); bmp.Save(serverPath + "/" + fileName); } content = content.Replace(base64Str, string.Format("http://{0}/{1}/{2}", Request.Url.Authority, filePath, fileName)); } } } catch (Exception e) { return Json(new ReturnResult<string>(102, null, "数据解析错误," + e.ToString())); } #endregion ArticleContentDataContract aritcle = new ArticleContentDataContract(); aritcle.CategoryId = categoryId; aritcle.Title = title; aritcle.UserKeyId = token.UserKeyId; aritcle.Author = token.User.NickName ?? token.User.UserName; aritcle.ArticleContent = content; return Json(articleContentService.PublishArticle(aritcle)); } catch (Exception e) { return Json(new ReturnResult<string>(103, null, "数据解析错误," + e.ToString())); } }