chore: 将资源的固定路径优化为可读取yml配置中的路径

This commit is contained in:
junjun
2024-12-26 10:11:42 +08:00
committed by dataeaseShu
parent bbbc0120f5
commit 5f1f81da30
12 changed files with 106 additions and 34 deletions

View File

@@ -1,5 +1,8 @@
package io.dataease.constant;
import io.dataease.utils.ConfigUtils;
import io.dataease.utils.ModelUtils;
import java.io.File;
import static io.dataease.utils.StaticResourceUtils.ensureSuffix;
@@ -11,7 +14,7 @@ public class StaticResourceConstants {
public static final String FILE_SEPARATOR = File.separator;
public static final String USER_HOME = "/opt/dataease2.0/data";
public static final String USER_HOME = getHomeData();
public static String WORK_DIR = ensureSuffix(USER_HOME, FILE_SEPARATOR) + "static-resource" + FILE_SEPARATOR;
@@ -36,4 +39,11 @@ public class StaticResourceConstants {
*/
public static final String URL_SEPARATOR = "/";
public static String getHomeData() {
if (ModelUtils.isDesktop()) {
return ConfigUtils.getConfig("dataease.path.data", "/opt/dataease2.0/data");
} else {
return "/opt/dataease2.0/data";
}
}
}

View File

@@ -0,0 +1,29 @@
package io.dataease.utils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.util.Objects;
/**
* @Author Junjun
*/
public class ConfigUtils {
public static String getConfig(String key, String defaultValue) {
try {
String filePath = System.getProperty("user.dir");
filePath = filePath.replace("file:", "").substring(0, filePath.lastIndexOf("resources"));
Resource resource = new FileSystemResource(filePath + "resources" + File.separator + "config" + File.separator + "application.yml");
// Resource resource = new FileSystemResource("D:\\dataease\\electron\\dataease-desktop\\resources\\config\\application.yml");
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource);
String property = Objects.requireNonNull(factory.getObject()).getProperty(key, defaultValue);
return property.replaceAll("\\$\\{user.home}", System.getProperty("user.home").replaceAll("\\\\", "/"));
} catch (Exception e) {
}
return defaultValue;
}
}