diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
index 6bc522c48..ad9fdfe56 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
@@ -2,7 +2,6 @@
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.common.util.ConfigUtils;
-import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
import ai.chat2db.server.web.api.controller.ncx.service.ConverterService;
import ai.chat2db.server.web.api.controller.ncx.vo.UploadVO;
import lombok.SneakyThrows;
@@ -13,9 +12,11 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
+import ai.chat2db.server.web.api.util.FileUtils;
import java.io.File;
import java.util.Objects;
+import java.util.UUID;
/**
* ConverterController
@@ -30,36 +31,52 @@ public class ConverterController {
@Autowired
private ConverterService converterService;
+ /**
+ * 导出教程
+ * @see
+ *
+ * @param file file
+ * @return DataResult
+ **/
@SneakyThrows
@PostMapping("/ncx/upload")
- public DataResult uploadFile(@RequestParam("file") MultipartFile file) {
+ public DataResult ncxUploadFile(@RequestParam("file") MultipartFile file) {
// 验证文件后缀
- String fileExtension = getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
- if (!isAllowedExtension(fileExtension)) {
+ String fileExtension = FileUtils.getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
+ if (!fileExtension.equalsIgnoreCase(FileUtils.ConfigFile.NCX.name())) {
return DataResult.error("1", "上传的文件必须是ncx文件!");
}
- File temp = new File(ConfigUtils.CONFIG_BASE_PATH + File.separator + "temp.tmp");
+ File temp = new File(ConfigUtils.CONFIG_BASE_PATH + File.separator + UUID.randomUUID() + ".tmp");
file.transferTo(temp);
return DataResult.of(converterService.uploadFile(temp));
}
- private String getFileExtension(String fileName) {
- int dotIndex = fileName.lastIndexOf(".");
- if (dotIndex > 0) {
- return fileName.substring(dotIndex + 1).toLowerCase();
- } else {
- return "";
+ @SneakyThrows
+ @PostMapping("/dbp/upload")
+ public DataResult dbpUploadFile(@RequestParam("file") MultipartFile file) {
+ // 验证文件后缀
+ String fileExtension = FileUtils.getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
+ if (!fileExtension.equalsIgnoreCase(FileUtils.ConfigFile.DBP.name())) {
+ return DataResult.error("1", "上传的文件必须是ncx文件!");
}
+ File temp = new File(ConfigUtils.CONFIG_BASE_PATH + File.separator + UUID.randomUUID() + ".tmp");
+ file.transferTo(temp);
+ return DataResult.of(converterService.dbpUploadFile(temp));
}
- private boolean isAllowedExtension(String extension) {
- // 只允许上传的文件后缀
- String[] allowedExtensions = {"ncx"};
- for (String ext : allowedExtensions) {
- if (ext.equalsIgnoreCase(extension)) {
- return true;
- }
- }
- return false;
+
+ /**
+ * 导入datagrip的连接信息,通过 ctrl/cmd + c(shift多选)复制连接,再导入进来
+ * 目前复制的连接信息里面是没有密码的
+ *
+ * @param text text
+ * @return DataResult
+ **/
+ @SneakyThrows
+ @PostMapping("/datagrip/upload")
+ public DataResult datagripUploadFile(@RequestParam("text") String text) {
+ return DataResult.of(converterService.datagripUploadFile(text));
}
+
+
}
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/ConverterService.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/ConverterService.java
index 659581638..fc55f7a0e 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/ConverterService.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/ConverterService.java
@@ -13,4 +13,8 @@
public interface ConverterService {
UploadVO uploadFile(File file);
+
+ UploadVO dbpUploadFile(File file);
+
+ UploadVO datagripUploadFile(String text);
}
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
index d82375518..2fab62892 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
@@ -23,9 +23,14 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.SAXParser;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -44,6 +49,20 @@ public class ConverterServiceImpl implements ConverterService {
private static CommonCipher cipher;
+ /**
+ * 连接信息头部
+ **/
+ private static final String DATASOURCE_SETTINGS = "#DataSourceSettings#";
+ private static final String XML_HEADER = "";
+ /**
+ * xml连接信息开始标志位
+ **/
+ private static final String BEGIN = "#BEGIN#";
+ /**
+ * xml连接信息结束标志位
+ **/
+ private static final String END = "#END#";
+
@Autowired
private DataSourceMapper dataSourceMapper;
@@ -101,6 +120,54 @@ public UploadVO uploadFile(File file) {
return vo;
}
+
+ @Override
+ public UploadVO dbpUploadFile(File file) {
+ return null;
+ }
+
+ @SneakyThrows
+ @Override
+ public UploadVO datagripUploadFile(String text) {
+ UploadVO vo = new UploadVO();
+ if (!text.startsWith(DATASOURCE_SETTINGS)) {
+ throw new RuntimeException("连接信息的头部不正确!");
+ }
+ String[] items = text.split("\n");
+ List configs = new ArrayList<>();
+ for (int i = 0; i < items.length; i++) {
+ if (items[i].equals(BEGIN)) {
+ configs.add(items[i + 1]);
+ }
+ }
+ for (String config : configs) {
+ //1、创建一个DocumentBuilderFactory的对象
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ //2、创建一个DocumentBuilder的对象
+ //创建DocumentBuilder对象
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ //3、通过DocumentBuilder对象的parser方法加载xml文件到当前项目下
+ try (InputStream inputStream = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) {
+ Document document = db.parse(inputStream);
+ //获取Connection节点的集合
+ NodeList connectList = document.getElementsByTagName("data-source");
+ //选中第一个节点
+ NamedNodeMap itemMap = connectList.item(0).getAttributes();
+ //创建datasource
+ DataSourceDO dataSourceDO = new DataSourceDO();
+ dataSourceDO.setAlias(itemMap.getNamedItem("name").getNodeValue());
+ for (int i = 0; i < connectList.getLength(); i++) {
+ //通过 item(i)方法 获取一个Connection节点,nodeList的索引值从0开始
+ Node connect = connectList.item(i);
+ //获取Connection节点的所有属性集合
+ NamedNodeMap attrs = connect.getAttributes();
+ }
+ dataSourceMapper.insert(dataSourceDO);
+ }
+ }
+ return vo;
+ }
+
/**
* 写入到数据库
*
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/util/FileUtils.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/util/FileUtils.java
new file mode 100644
index 000000000..b1fdd2f54
--- /dev/null
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/util/FileUtils.java
@@ -0,0 +1,26 @@
+package ai.chat2db.server.web.api.util;
+
+/**
+ * FileUtil
+ *
+ * @author lzy
+ **/
+public class FileUtils {
+
+ public enum ConfigFile {
+ // navicat连接信息文件
+ NCX,
+ // dbeaver连接信息文件
+ DBP
+ }
+
+ public static String getFileExtension(String fileName) {
+ int dotIndex = fileName.lastIndexOf(".");
+ if (dotIndex > 0) {
+ return fileName.substring(dotIndex + 1).toLowerCase();
+ } else {
+ return "";
+ }
+ }
+
+}