新功能点:所有配置项支持从环境变量里读取,方便Docker镜像部署

This commit is contained in:
陈精华
2019-10-25 13:59:29 +08:00
committed by kl
parent 8fdf462c6c
commit fa7241bd4e
6 changed files with 75 additions and 48 deletions

View File

@@ -17,7 +17,6 @@ public class ConfigConstants {
private static Boolean cacheEnabled;
private static String[] simText = {};
private static String[] media = {};
private static String convertedFileCharset;
private static String officePreviewType;
private static String ftpUsername;
private static String ftpPassword;
@@ -25,6 +24,8 @@ public class ConfigConstants {
private static String fileDir = OfficeUtils.getHomePath() + File.separator + "file" + File.separator;
private static String baseUrl;
public static final String DEFAULT_FILE_DIR_VALUE = "default";
public static Boolean isCacheEnabled() {
return cacheEnabled;
}
@@ -49,14 +50,6 @@ public class ConfigConstants {
ConfigConstants.media = media;
}
public static String getConvertedFileCharset() {
return convertedFileCharset;
}
public static void setConvertedFileCharset(String convertedFileCharset) {
ConfigConstants.convertedFileCharset = convertedFileCharset;
}
public static String getOfficePreviewType() {
return officePreviewType;
}
@@ -98,13 +91,12 @@ public class ConfigConstants {
}
public static void setBaseUrl(String baseUrl) {
// 不以'/'结尾的,加上'/'
ConfigConstants.baseUrl = baseUrl.concat("/");
ConfigConstants.baseUrl = baseUrl;
}
@Value("${file.dir:default}")
public void setFileDir(String fileDir) {
if (!"default".equals(fileDir)) {
if (!DEFAULT_FILE_DIR_VALUE.equals(fileDir.toLowerCase())) {
if (!fileDir.endsWith(File.separator)) {
fileDir = fileDir + File.separator;
}

View File

@@ -5,7 +5,6 @@ import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
@@ -24,14 +23,13 @@ public class ConfigRefreshComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRefreshComponent.class);
public static final String DEFAULT_CACHE_ENABLED = "true";
public static final String DEFAULT_TXT_TYPE = "txt,html,xml,properties,md,java,py,c,cpp,sql";
public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd";
public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv";
public static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
public static final String DEFAULT_FTP_USERNAME = null;
public static final String DEFAULT_FTP_PASSWORD = null;
public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8";
public static final String BASE_URL = null;
public static final String DEFAULT_BASE_URL = "default";
@PostConstruct
void refresh() {
@@ -49,7 +47,6 @@ public class ConfigRefreshComponent {
Boolean cacheEnabled;
String[] textArray;
String[] mediaArray;
String convertedFileCharset;
String officePreviewType;
String ftpUsername;
String ftpPassword;
@@ -60,28 +57,25 @@ public class ConfigRefreshComponent {
FileReader fileReader = new FileReader(configFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
properties.load(bufferedReader);
OfficeUtils.restorePropertiesFromEnvFormat(properties);
cacheEnabled = new Boolean(properties.getProperty("cache.enabled", DEFAULT_CACHE_ENABLED));
text = properties.getProperty("simText", DEFAULT_TXT_TYPE);
media = properties.getProperty("media", DEFAULT_MEDIA_TYPE);
convertedFileCharset = properties.getProperty("converted.file.charset", DEFAULT_CONVERTER_CHARSET);
officePreviewType = properties.getProperty("office.preview.type", OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE);
ftpUsername = properties.getProperty("ftp.username", DEFAULT_FTP_USERNAME);
ftpPassword = properties.getProperty("ftp.password", DEFAULT_FTP_PASSWORD);
ftpControlEncoding = properties.getProperty("ftp.control.encoding", DEFAULT_FTP_CONTROL_ENCODING);
textArray = text.split(",");
mediaArray = media.split(",");
baseUlr = properties.getProperty("base.url", null);
baseUlr = properties.getProperty("base.url", DEFAULT_BASE_URL);
ConfigConstants.setCacheEnabled(cacheEnabled);
ConfigConstants.setSimText(textArray);
ConfigConstants.setMedia(mediaArray);
ConfigConstants.setConvertedFileCharset(convertedFileCharset);
ConfigConstants.setOfficePreviewType(officePreviewType);
ConfigConstants.setFtpUsername(ftpUsername);
ConfigConstants.setFtpPassword(ftpPassword);
ConfigConstants.setFtpControlEncoding(ftpControlEncoding);
if (baseUlr != null && !StringUtils.isEmpty(baseUlr)) {
ConfigConstants.setBaseUrl(baseUlr);
}
ConfigConstants.setBaseUrl(baseUlr);
bufferedReader.close();
fileReader.close();
Thread.sleep(1000L);

View File

@@ -1,7 +1,7 @@
package cn.keking.filters;
import cn.keking.config.ConfigConstants;
import org.springframework.util.StringUtils;
import cn.keking.config.ConfigRefreshComponent;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
@@ -23,8 +23,12 @@ public class ChinesePathFilter implements Filter {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String baseUrl;
if (ConfigConstants.getBaseUrl() != null) {
baseUrl = ConfigConstants.getBaseUrl();
String baseUrlTmp = ConfigConstants.getBaseUrl();
if (baseUrlTmp != null && !ConfigRefreshComponent.DEFAULT_BASE_URL.equals(baseUrlTmp.toLowerCase())) {
if (!baseUrlTmp.endsWith("/")) {
baseUrlTmp = baseUrlTmp.concat("/");
}
baseUrl = baseUrlTmp;
} else {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")

View File

@@ -25,6 +25,9 @@ import java.util.Map;
*/
@Component
public class FileUtils {
public static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
Logger log= LoggerFactory.getLogger(getClass());
@Autowired
@@ -233,9 +236,8 @@ public class FileUtils {
*/
public void doActionConvertedFile(String outFilePath) {
StringBuffer sb = new StringBuffer();
String charset = ConfigConstants.getConvertedFileCharset();
try (InputStream inputStream = new FileInputStream(outFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))){
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))){
String line;
while(null != (line = reader.readLine())){
if (line.contains("charset=gb2312")) {