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

@@ -17,6 +17,7 @@ import io.dataease.utils.JsonUtil;
import io.dataease.utils.ModelUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@@ -39,6 +40,9 @@ public class EngineManage {
@Resource
private CoreDatasourceMapper datasourceMapper;
@Value("${dataease.path.engine:jdbc:h2:/opt/dataease2.0/desktop_data;AUTO_SERVER=TRUE;AUTO_RECONNECT=TRUE;MODE=MySQL;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DATABASE_TO_UPPER=FALSE}")
private String engineUrl;
public CoreDeEngine info() throws DEException {
List<CoreDeEngine> deEngines = deEngineMapper.selectList(null);
@@ -111,7 +115,7 @@ public class EngineManage {
if (ModelUtils.isDesktop()) {
engine.setType(engineType.h2.name());
H2 h2 = new H2();
h2.setJdbc("jdbc:h2:/opt/dataease2.0/desktop_data;AUTO_SERVER=TRUE;AUTO_RECONNECT=TRUE;MODE=MySQL;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DATABASE_TO_UPPER=FALSE");
h2.setJdbc(engineUrl);
h2.setDataBase("PUBLIC");
h2.setUsername(env.getProperty("spring.datasource.username"));
h2.setPassword(env.getProperty("spring.datasource.password"));

View File

@@ -31,6 +31,7 @@ import org.apache.calcite.sql.parser.SqlParser;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
@@ -53,8 +54,10 @@ public class CalciteProvider extends Provider {
private EngineManage engineManage;
protected ExtendedJdbcClassLoader extendedJdbcClassLoader;
private Map<Long, ExtendedJdbcClassLoader> customJdbcClassLoaders = new HashMap<>();
private final String FILE_PATH = "/opt/dataease2.0/drivers";
private final String CUSTOM_PATH = "/opt/dataease2.0/custom-drivers/";
@Value("${dataease.path.driver:/opt/dataease2.0/drivers}")
private String FILE_PATH;
@Value("${dataease.path.custom-drivers:/opt/dataease2.0/custom-drivers/}")
private String CUSTOM_PATH;
private static String split = "DE";
@Resource

View File

@@ -19,7 +19,9 @@ import io.dataease.extensions.datasource.dto.DatasourceDTO;
import io.dataease.extensions.datasource.dto.DatasourceRequest;
import io.dataease.extensions.datasource.dto.TableField;
import io.dataease.utils.AuthUtils;
import io.dataease.utils.ConfigUtils;
import io.dataease.utils.JsonUtil;
import io.dataease.utils.ModelUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
@@ -35,9 +37,17 @@ import java.util.stream.Collectors;
public class ExcelUtils {
public static final String UFEFF = "\uFEFF";
private static String path = "/opt/dataease2.0/data/excel/";
private static String path = getExcelPath();
private static ObjectMapper objectMapper = new ObjectMapper();
public static String getExcelPath() {
if (ModelUtils.isDesktop()) {
return ConfigUtils.getConfig("dataease.path.excel", "/opt/dataease2.0/data/excel/");
} else {
return "/opt/dataease2.0/data/excel/";
}
}
private static TypeReference<List<TableField>> TableFieldListTypeReference = new TypeReference<List<TableField>>() {
};
@@ -333,7 +343,7 @@ public class ExcelUtils {
Double d = Double.valueOf(value);
double eps = 1e-10;
if (d - Math.floor(d) < eps) {
if(value.contains(".")){
if (value.contains(".")) {
return "DOUBLE";
}
return "LONG";

View File

@@ -2,7 +2,6 @@ package io.dataease.datasource.server;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dataease.api.ds.DatasourceDriverApi;
import io.dataease.api.ds.vo.DriveDTO;
import io.dataease.api.ds.vo.DriveJarDTO;
import io.dataease.datasource.dao.auto.entity.CoreDriver;
@@ -14,6 +13,7 @@ import io.dataease.utils.BeanUtils;
import io.dataease.utils.FileUtils;
import io.dataease.utils.Md5Utils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -21,17 +21,20 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/datasourceDriver")
public class DatasourceDriverServer implements DatasourceDriverApi {
private final static String DRIVER_PATH = "/opt/dataease2.0/custom-drivers/";
@Value("${dataease.path.custom-drivers:/opt/dataease2.0/custom-drivers/}")
private String DRIVER_PATH;
@Resource
private CoreDriverMapper coreDriverMapper;
@@ -70,7 +73,7 @@ public class DatasourceDriverServer implements DatasourceDriverApi {
}
@Override
public DriveDTO save(DriveDTO datasourceDrive){
public DriveDTO save(DriveDTO datasourceDrive) {
CoreDriver coreDriver = new CoreDriver();
BeanUtils.copyBean(coreDriver, datasourceDrive);
coreDriverMapper.insert(coreDriver);
@@ -78,7 +81,7 @@ public class DatasourceDriverServer implements DatasourceDriverApi {
}
@Override
public DriveDTO update(DriveDTO datasourceDrive){
public DriveDTO update(DriveDTO datasourceDrive) {
CoreDriver coreDriver = new CoreDriver();
BeanUtils.copyBean(coreDriver, datasourceDrive);
coreDriverMapper.updateById(coreDriver);
@@ -86,7 +89,7 @@ public class DatasourceDriverServer implements DatasourceDriverApi {
}
@Override
public void delete(String driverId){
public void delete(String driverId) {
coreDriverMapper.deleteById(driverId);
Map<String, Object> map = new HashMap<>();
map.put("deDriverId", driverId);
@@ -95,7 +98,7 @@ public class DatasourceDriverServer implements DatasourceDriverApi {
@Override
public List<DriveJarDTO> listDriverJar(String driverId){
public List<DriveJarDTO> listDriverJar(String driverId) {
List<DriveJarDTO> driveJarDTOS = new ArrayList<>();
QueryWrapper<CoreDriverJar> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("deDriverId", driverId);
@@ -108,28 +111,28 @@ public class DatasourceDriverServer implements DatasourceDriverApi {
}
@Override
public void deleteDriverJar(String jarId){
public void deleteDriverJar(String jarId) {
CoreDriverJar driverJar = coreDriverJarMapper.selectById(jarId);
coreDriverJarMapper.deleteById(jarId);
CoreDriver driver = coreDriverMapper.selectById(driverJar.getDeDriverId());
FileUtils.deleteFile(DRIVER_PATH + driverJar.getDeDriverId() + "/" + driverJar.getTransName());
//TODO 更新classloader
};
}
@Override
public DriveJarDTO uploadJar(@RequestParam("deDriverId") String deDriverId, @RequestParam("jarFile") MultipartFile jarFile) throws Exception{
public DriveJarDTO uploadJar(@RequestParam("deDriverId") String deDriverId, @RequestParam("jarFile") MultipartFile jarFile) throws Exception {
CoreDriver coreDriver = coreDriverMapper.selectById(deDriverId);
if(coreDriver == null){
if (coreDriver == null) {
throw new RuntimeException("DRIVER_NOT_FOUND");
}
String filename = jarFile.getOriginalFilename();
if(!filename.endsWith(".jar")){
if (!filename.endsWith(".jar")) {
throw new RuntimeException("NOT_JAR");
}
QueryWrapper<CoreDriverJar> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("fileName", filename);
if(!CollectionUtils.isEmpty(coreDriverJarMapper.selectList(queryWrapper))){
if (!CollectionUtils.isEmpty(coreDriverJarMapper.selectList(queryWrapper))) {
throw new Exception("A file with the same name already exists" + filename);
}

View File

@@ -103,7 +103,8 @@ public class ExportCenterManage implements BaseExportApi {
private final static String DATA_URL_TITLE = "data:image/jpeg;base64,";
private static final String exportData_path = "/opt/dataease2.0/data/exportData/";
@Value("${dataease.path.exportData:/opt/dataease2.0/data/exportData/}")
private String exportData_path;
public Integer getExtractPageSize() {
return extractPageSize;

View File

@@ -7,14 +7,13 @@ import io.dataease.chart.dao.auto.entity.CoreChartView;
import io.dataease.exception.DEException;
import io.dataease.font.dao.auto.entity.CoreFont;
import io.dataease.font.dao.auto.mapper.CoreFontMapper;
import io.dataease.utils.BeanUtils;
import io.dataease.utils.FileUtils;
import io.dataease.utils.IDUtils;
import io.dataease.utils.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@@ -37,7 +36,9 @@ import java.util.UUID;
@Component
public class FontManage {
private static String path = "/opt/dataease2.0/data/font/";
@Value("${dataease.path.font:/opt/dataease2.0/data/font/}")
private String path;
@Resource
private CoreFontMapper coreFontMapper;
@Autowired
@@ -152,7 +153,7 @@ public class FontManage {
return fontDtos;
}
private static FontDto saveFile(MultipartFile file, String fileNameUUID) throws DEException {
private FontDto saveFile(MultipartFile file, String fileNameUUID) throws DEException {
FontDto fontDto = new FontDto();
try {
String filename = file.getOriginalFilename();

View File

@@ -1,6 +1,7 @@
package io.dataease.listener;
import io.dataease.utils.ConfigUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
@@ -17,5 +18,8 @@ public class EhCacheStartListener implements ApplicationContextInitializer<Confi
factory.setResources(resource);
String property = Objects.requireNonNull(factory.getObject()).getProperty("dataease.login_timeout", "480");
System.setProperty("dataease.login_timeout", property);
String ehcache = ConfigUtils.getConfig("dataease.path.ehcache", "/opt/dataease2.0/cache");
System.setProperty("dataease.path.ehcache", ehcache);
}
}

View File

@@ -3,13 +3,13 @@ package io.dataease.visualization.server;
import io.dataease.api.visualization.StaticResourceApi;
import io.dataease.api.visualization.request.StaticResourceRequest;
import io.dataease.exception.DEException;
import io.dataease.log.DeLog;
import io.dataease.utils.FileUtils;
import io.dataease.utils.JsonUtil;
import io.dataease.utils.LogUtil;
import io.dataease.utils.StaticResourceUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -18,13 +18,9 @@ import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@@ -38,7 +34,8 @@ import java.util.Map;
@RequestMapping("/staticResource")
public class StaticResourceServer implements StaticResourceApi {
private final Path staticDir = Paths.get("/opt/dataease2.0/data/static-resource/");
@Value("${dataease.path.static-resource:/opt/dataease2.0/data/static-resource/}")
private String staticDir;
@Override
public void upload(String fileId, MultipartFile file) {

View File

@@ -1,8 +1,10 @@
package io.dataease.visualization.utils;
import io.dataease.engine.constant.DeTypeConstants;
import io.dataease.utils.ConfigUtils;
import io.dataease.utils.FileUtils;
import io.dataease.utils.LogUtil;
import io.dataease.utils.ModelUtils;
import io.dataease.visualization.bo.ExcelSheetModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -20,7 +22,15 @@ import java.util.regex.Pattern;
public class VisualizationExcelUtils {
private static final String suffix = ".xlsx";
private static final String BASE_ROOT = "/opt/dataease2.0/data/report/";
private static final String BASE_ROOT = getBaseRoot();
public static String getBaseRoot() {
if (ModelUtils.isDesktop()) {
return ConfigUtils.getConfig("dataease.path.report", "/opt/dataease2.0/data/report/");
} else {
return "/opt/dataease2.0/data/report/";
}
}
public static File exportExcel(List<ExcelSheetModel> sheets, String fileName, String folderId) throws Exception {
AtomicReference<String> realFileName = new AtomicReference<>(fileName);

View File

@@ -8,7 +8,7 @@
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
<persistence directory="/opt/dataease2.0/cache" />
<persistence directory="${dataease.path.ehcache}" />
<cache-template name="common-cache">
<expiry>

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;
}
}