| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package com.zd.ueditor;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- public class ConfigManager {
- private String rootPath;
- private String originalPath;
- private String contextPath;
- private String realPath;
- private static String configFileName = "config.json";
- private String parentPath;
- private JSONObject jsonConfig;
- private static String SCRAWL_FILE_NAME = "scrawl";
- private static String REMOTE_FILE_NAME = "remote";
- private ConfigManager(String rootPath, String contextPath, String uri,String realPath) throws FileNotFoundException, IOException {
- this.parentPath = null;
- this.jsonConfig = null;
- rootPath = rootPath.replace("\\", "/");
- this.rootPath = rootPath;
- this.realPath = realPath;
- this.contextPath = contextPath;
- if (contextPath.length() > 0) {
- this.originalPath = this.rootPath + uri.substring(contextPath.length());
- } else {
- this.originalPath = this.rootPath + uri;
- }
- this.initEnv();
- }
- public static ConfigManager getInstance(String rootPath, String contextPath, String uri,String realPath) {
- try {
- return new ConfigManager(rootPath, contextPath, uri,realPath);
- } catch (Exception e) {
- return null;
- }
- }
- public boolean valid() {
- return this.jsonConfig != null;
- }
- public JSONObject getAllConfig() {
- return this.jsonConfig;
- }
- public Map<String, Object> getConfig(int type) {
- Map<String, Object> conf = new HashMap<String, Object>();
- String savePath = null;
- switch (type) {
- case 4: {
- conf.put("isBase64", "false");
- conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
- conf.put("allowFiles", this.getArray("fileAllowFiles"));
- conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
- savePath = this.jsonConfig.getString("filePathFormat");
- break;
- }
- case 1: {
- conf.put("isBase64", "false");
- conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
- conf.put("allowFiles", this.getArray("imageAllowFiles"));
- conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
- savePath = this.jsonConfig.getString("imagePathFormat");
- break;
- }
- case 3: {
- conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
- conf.put("allowFiles", this.getArray("videoAllowFiles"));
- conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
- savePath = this.jsonConfig.getString("videoPathFormat");
- break;
- }
- case 2: {
- conf.put("filename", "scrawl");
- conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
- conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
- conf.put("isBase64", "true");
- savePath = this.jsonConfig.getString("scrawlPathFormat");
- break;
- }
- case 5: {
- conf.put("filename", "remote");
- conf.put("filter", this.getArray("catcherLocalDomain"));
- conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
- conf.put("allowFiles", this.getArray("catcherAllowFiles"));
- conf.put("fieldName", String.valueOf(this.jsonConfig.getString("catcherFieldName")) + "[]");
- savePath = this.jsonConfig.getString("catcherPathFormat");
- break;
- }
- case 7: {
- conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
- conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
- conf.put("count", this.jsonConfig.getInt("imageManagerListSize"));
- break;
- }
- case 6: {
- conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
- conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
- conf.put("count", this.jsonConfig.getInt("fileManagerListSize"));
- break;
- }
- }
- conf.put("savePath", savePath);
- conf.put("rootPath", this.realPath);
- return conf;
- }
- private void initEnv() throws FileNotFoundException, IOException {
- File file = new File(this.originalPath);
- if (!file.isAbsolute()) {
- file = new File(file.getAbsolutePath());
- }
- this.parentPath = file.getParent();
- String configContent = this.readFile(this.getConfigPath());
- try {
- JSONObject jsonConfig = new JSONObject(configContent);
- this.jsonConfig = jsonConfig;
- } catch (Exception e) {
- this.jsonConfig = null;
- }
- }
- private String getConfigPath() {
- return String.valueOf(this.parentPath) + File.separator + "config.json";
- }
- private String[] getArray(String key) {
- JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
- String[] result = new String[jsonArray.length()];
- for (int i = 0, len = jsonArray.length(); i < len; ++i) {
- result[i] = jsonArray.getString(i);
- }
- return result;
- }
- private String readFile(String path) throws IOException {
- StringBuilder builder = new StringBuilder();
- try {
- InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
- BufferedReader bfReader = new BufferedReader(reader);
- String tmpContent = null;
- while ((tmpContent = bfReader.readLine()) != null) {
- builder.append(tmpContent);
- }
- bfReader.close();
- } catch (UnsupportedEncodingException ex) {
- }
- return this.filter(builder.toString());
- }
- private String filter(String input) {
- return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
- }
- }
|