Commit 50280bc4 authored by Liang Ding's avatar Liang Ding

#12670

parent 0135ecbd
......@@ -17,7 +17,10 @@
*/
package org.b3log.solo.model;
import org.apache.commons.lang.StringUtils;
import org.b3log.solo.util.Images;
import org.b3log.solo.util.Markdowns;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
......@@ -25,7 +28,7 @@ import org.jsoup.safety.Whitelist;
* This class defines all article model relevant keys.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.0.1, Feb 6, 2019
* @version 1.3.0.0, Feb 25, 2019
* @since 0.3.1
*/
public final class Article {
......@@ -155,6 +158,11 @@ public final class Article {
*/
public static final String ARTICLE_VIEW_PWD = "articleViewPwd";
/**
* Key of article image1 URL. https://github.com/b3log/solo/issues/12670
*/
public static final String ARTICLE_IMG1_URL = "articleImg1URL";
//// constants
/**
......@@ -162,12 +170,53 @@ public final class Article {
*/
private static final int ARTICLE_ABSTRACT_LENGTH = 500;
/**
* Width of article first image.
*/
public static final int ARTICLE_THUMB_IMG_WIDTH = 960;
/**
* Height of article first image.
*/
public static final int ARTICLE_THUMB_IMG_HEIGHT = 540;
/**
* Private constructor.
*/
private Article() {
}
/**
* Gets the first image URL of the specified article.
*
* @param article the specified article
* @return the first image URL, returns {@code ""} if not found
*/
public static String getArticleImg1URL(final JSONObject article) {
final String content = article.optString(Article.ARTICLE_CONTENT);
final String html = Markdowns.toHTML(content);
final String[] imgs = StringUtils.substringsBetween(html, "<img", ">");
if (null == imgs || 0 == imgs.length) {
return Images.imageSize(Images.randImage(), ARTICLE_THUMB_IMG_WIDTH, ARTICLE_THUMB_IMG_HEIGHT);
}
String ret = null;
for (final String img : imgs) {
ret = StringUtils.substringBetween(img, "src=\"", "\"");
if (!StringUtils.containsIgnoreCase(ret, ".ico")) {
break;
}
}
if (StringUtils.isBlank(ret)) {
return Images.imageSize(Images.randImage(), ARTICLE_THUMB_IMG_WIDTH, ARTICLE_THUMB_IMG_HEIGHT);
}
ret = Images.imageSize(ret, ARTICLE_THUMB_IMG_WIDTH, ARTICLE_THUMB_IMG_HEIGHT);
return ret;
}
/**
* Gets the abstract of the specified content.
*
......
......@@ -134,13 +134,13 @@ public class ArticleConsole {
// original: 1920*1080
final String wStr = context.param("w");
int w = 960;
int w = Article.ARTICLE_THUMB_IMG_WIDTH;
if (Strings.isNumeric(wStr)) {
w = Integer.valueOf(wStr);
}
final int width = w;
final String hStr = context.param("h");
int h = 540;
int h = Article.ARTICLE_THUMB_IMG_HEIGHT;
if (Strings.isNumeric(hStr)) {
h = Integer.valueOf(hStr);
}
......
......@@ -448,6 +448,9 @@ public class ArticleMgmtService {
article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
final String articleImg1URL = getArticleImg1URL(article);
article.put(ARTICLE_IMG1_URL, articleImg1URL);
final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
article.remove(Common.POST_TO_COMMUNITY); // Do not persist this property
......
......@@ -257,7 +257,7 @@ public class InitService {
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_TITLE, langPropsService.get("helloWorld.title"));
final String content = "![](" + Images.imageSize(Images.randImage(), 960, 540) + ") \n\n" +
final String content = "![](" + Images.imageSize(Images.randImage(), Article.ARTICLE_THUMB_IMG_WIDTH, Article.ARTICLE_THUMB_IMG_HEIGHT) + ") \n\n" +
langPropsService.get("helloWorld.content");
article.put(Article.ARTICLE_ABSTRACT, content);
......@@ -278,6 +278,8 @@ public class InitService {
article.put(Article.ARTICLE_AUTHOR_ID, admin.optString(Keys.OBJECT_ID));
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
final String articleImg1URL = Article.getArticleImg1URL(article);
article.put(Article.ARTICLE_IMG1_URL, articleImg1URL);
final String articleId = addHelloWorldArticle(article);
......
......@@ -529,6 +529,12 @@
"description": "文章浏览密码,留空为不设置访问密码",
"type": "String",
"legnth": 255
},
{
"name": "articleImg1URL",
"type": "String",
"length": 255,
"description": "文章首图地址"
}
]
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment