Prechádzať zdrojové kódy

修复文章草稿提交与正文图片丢失

恢复文章完整字段读取,保留 guest_access_flag 和 sort_number,避免草稿修改提交时写入 NULL。

文章新增、修改和变动统一生成新的编辑正文与运行正文文件,数据库失败或流程退回时不再覆盖旧文件;提交前拦截缺少有效地址或仍为 blob 地址的图片。

Validation:
- Java 8 发布服务编译通过
- 本地带图片草稿新增、二次修改、提交审核通过
- guest_access_flag 保持为 0,修改前后正文文件及图片 src 均完整
非繁 4 mesiacov pred
rodič
commit
1798e10576

+ 47 - 25
src/space/apps/moudules/website/services/ArticleService.java

@@ -211,6 +211,36 @@ public class ArticleService extends BaseTransaction {
     private void saveArticleDesc(Article article, String htmlContent) {
     }
 
+    private boolean verifyArticleImageSources(String htmlContent) throws Exception {
+        if (StringUtil.isNull(htmlContent)) {
+            return true;
+        }
+
+        Document doc = Jsoup.parseBodyFragment(htmlContent);
+        for (Element image : doc.select("img")) {
+            String src = image.attr("src");
+            String originalSrc = image.attr("data-original");
+            boolean missingSource = StringUtil.isNull(src) && StringUtil.isNull(originalSrc);
+            boolean temporarySource = (StringUtil.notNull(src) && src.startsWith("blob:"))
+                    || (StringUtil.notNull(originalSrc) && originalSrc.startsWith("blob:"));
+            if (missingSource || temporarySource) {
+                setResponsePackage(getResponseWarningMsgPackage("正文中存在未上传完成或失效的图片,请重新上传"));
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private void saveArticleContentFiles(Article article, String content) throws Exception {
+        String editPath = WebEditorUtil.createHtmlFile(content);
+        String runContent = converArticleContentHtmlLink(getWebRootPath() + editPath,
+                space.apps.moudules.website.common.ConstVars.domain);
+        String runPath = WebEditorUtil.createHtmlFile(runContent);
+
+        article.setEditContent(editPath);
+        article.setContent(runPath);
+    }
+
     /**
      * 文章录入
      *
@@ -242,15 +272,11 @@ public class ArticleService extends BaseTransaction {
         if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
             return DEFAULT_FORWARD;
         }
+        if (!verifyArticleImageSources(content)) {
+            return DEFAULT_FORWARD;
+        }
 
-        String editPath = WebEditorUtil.createHtmlFile(content);
-
-        String runContent = converArticleContentHtmlLink(getWebRootPath() + editPath,
-                space.apps.moudules.website.common.ConstVars.domain);
-        String runPath = WebEditorUtil.createHtmlFile(runContent);
-
-        article.setContent(runPath);// 运行文件
-        article.setEditContent(editPath);// 编辑文件
+        saveArticleContentFiles(article, content);
 
         article.setUnitid(appUser.getUnitId());
         article.setDcode(dataStatusCode.INSERT.getCode());
@@ -316,17 +342,13 @@ public class ArticleService extends BaseTransaction {
     }
 
     private Article getArticleById(GeneralDBAccess gdba, Long articleid) throws Exception {
-        String sql = " select articleid, dcode, websiteid, unitid, ascode, navigationid, title, ajtcode, `desc`,"
-                + " source, thumbnail, sticktop, sticktop_end_time, sticktop_schedulesid, `release`, release_time,"
-                + " release_task_schedulesid, content, edit_content, url, read_count, create_time, last_change_time,"
-                + " create_pid, last_change_pid from article where articleid = :articleid ";
-        SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
-        sqlCmd.setString("articleid", articleid);
-        List<Article> list = gdba.selectAsList(sqlCmd, Article.class);
-        if (list == null || list.isEmpty()) {
+        Article article = new Article();
+        article.setArticleid(articleid);
+        gdba.selectById(article);
+        if (article.getDcode() == null) {
             throw new Exception("文章不存在");
         }
-        return list.get(0);
+        return article;
     }
 
     /**
@@ -396,15 +418,15 @@ public class ArticleService extends BaseTransaction {
         if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
             return DEFAULT_FORWARD;
         }
+        if (!verifyArticleImageSources(content)) {
+            return DEFAULT_FORWARD;
+        }
 
         article.setSticktop("yes".equals(top) ? 1L : 0L);
 
         ChangeLogUtil.addChangeLog(gdba, appUser, "Article", a2.getArticleid(), GsonUtil.toJson(a2));
 
-        FileUtil.writeFile(content, getWebRootPath() + a2.getEditContent());// 编辑文件,获取页面上的正文内容
-        String runContent = converArticleContentHtmlLink(getWebRootPath() + a2.getEditContent(),
-                space.apps.moudules.website.common.ConstVars.domain);
-        FileUtil.writeFile(runContent, getWebRootPath() + a2.getContent());// 运行文件,从编辑文件里转换为运行文件
+        saveArticleContentFiles(article, content);
 
         article.setLastChangePid(appUser.getUserId());
         article.setLastChangeTime(nowTimestamp);
@@ -648,6 +670,9 @@ public class ArticleService extends BaseTransaction {
         if (!verifyArticleCopyNavigationList(gdba, navIdList, article)) {
             return TIPS_FORWARD;
         }
+        if (!verifyArticleImageSources(content)) {
+            return TIPS_FORWARD;
+        }
 
         article.setSticktop("yes".equals(top) ? 1L : 0L);
 
@@ -657,10 +682,7 @@ public class ArticleService extends BaseTransaction {
 
         verifyArticleUrl(article, a2, wn);
 
-        FileUtil.writeFile(content, getWebRootPath() + a2.getEditContent());// 编辑文件,获取页面上的正文内容
-        String runContent = converArticleContentHtmlLink(getWebRootPath() + a2.getEditContent(),
-                space.apps.moudules.website.common.ConstVars.domain);
-        FileUtil.writeFile(runContent, getWebRootPath() + a2.getContent());// 运行文件,从编辑文件里转换为运行文件
+        saveArticleContentFiles(article, content);
 
         article.setDcode(dataStatusCode.STAY_SUBMIT.getCode());
         article.setLastChangePid(appUser.getUserId());

+ 5 - 10
src/space/apps/moudules/website/services/flow/ArticleFlowService.java

@@ -76,18 +76,13 @@ public class ArticleFlowService extends BaseTransaction {
 	 * @log 2020年10月11日
 	 */
 	private Article getArticleById(GeneralDBAccess gdba, Long articleid) throws Exception {
-		// 显式列查询:生产 article 表含 guest_access_flag,但 apps.pojo.Article 未同步该字段
-		String sql = "SELECT articleid, dcode, title, `desc`, navigationid, unitid, websiteid, ascode,"
-				+ " create_pid, last_change_pid, create_time, last_change_time, content, edit_content,"
-				+ " source, thumbnail, sticktop, `release`, url, read_count, ajtcode"
-				+ " FROM article WHERE articleid = :articleid";
-		SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
-		sqlCmd.setString("articleid", articleid);
-		List<Article> list = gdba.selectAsList(sqlCmd, Article.class);
-		if (list == null || list.isEmpty()) {
+		Article article = new Article();
+		article.setArticleid(articleid);
+		gdba.selectById(article);
+		if (article.getDcode() == null) {
 			throw new Exception("文章不存在");
 		}
-		return list.get(0);
+		return article;
 	}
 	
 	/**