mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-30 19:22:58 +08:00
修复Windows复制模板失败问题
This commit is contained in:
@ -0,0 +1,63 @@
|
|||||||
|
package ai.chat2db.server.start.config.util;
|
||||||
|
|
||||||
|
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CopyTemplate
|
||||||
|
*
|
||||||
|
* @author lzy
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class CopyTemplate {
|
||||||
|
/**
|
||||||
|
* 模板文件
|
||||||
|
**/
|
||||||
|
public static final List<String> TEMPLATE_FILE = Arrays.asList("template.html", "template_diy.docx", "sub_template_diy.docx");
|
||||||
|
|
||||||
|
static {
|
||||||
|
//复制模板
|
||||||
|
copyTemplateFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copyTemplateFile() {
|
||||||
|
String templateDir = ConfigUtils.CONFIG_BASE_PATH + File.separator + "template";
|
||||||
|
File file = new File(templateDir);
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.mkdir();
|
||||||
|
}
|
||||||
|
for (String template : TEMPLATE_FILE) {
|
||||||
|
saveFile(templateDir, template, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void saveFile(String dir, String path, boolean isOverride) {
|
||||||
|
if (!isOverride) {
|
||||||
|
File file = new File(dir + File.separator + path);
|
||||||
|
if (file.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try (// 模板文件输入输出地址 读取resources下文件
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(dir + File.separator + path);
|
||||||
|
//返回读取指定资源的输入流
|
||||||
|
InputStream inputStream = ConfigUtils.class.getClassLoader().getResourceAsStream("template" + File.separator + path)) {
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
int n = 0;
|
||||||
|
while (-1 != (n = inputStream.read(buffer))) {
|
||||||
|
outputStream.write(buffer, 0, n);
|
||||||
|
}
|
||||||
|
outputStream.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("saveFile error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,10 +7,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure information on the user side
|
* Configure information on the user side
|
||||||
@ -27,10 +23,6 @@ public class ConfigUtils {
|
|||||||
public static File versionFile = null;
|
public static File versionFile = null;
|
||||||
public static File configFile;
|
public static File configFile;
|
||||||
private static ConfigJson config = null;
|
private static ConfigJson config = null;
|
||||||
/**
|
|
||||||
* 模板文件
|
|
||||||
**/
|
|
||||||
public static final List<String> TEMPLATE_FILE = Arrays.asList("template.html", "template_diy.docx", "sub_template_diy.docx");
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
||||||
@ -46,8 +38,6 @@ public class ConfigUtils {
|
|||||||
if (!configFile.exists()) {
|
if (!configFile.exists()) {
|
||||||
FileUtil.writeUtf8String(JSON.toJSONString(new ConfigJson()), configFile);
|
FileUtil.writeUtf8String(JSON.toJSONString(new ConfigJson()), configFile);
|
||||||
}
|
}
|
||||||
//复制模板
|
|
||||||
copyTemplateFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateVersion(String version) {
|
public static void updateVersion(String version) {
|
||||||
@ -97,37 +87,4 @@ public class ConfigUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyTemplateFile() {
|
|
||||||
String templateDir = CONFIG_BASE_PATH + File.separator + "template";
|
|
||||||
File file = new File(templateDir);
|
|
||||||
if (!file.exists()) {
|
|
||||||
file.mkdir();
|
|
||||||
}
|
|
||||||
for (String template : TEMPLATE_FILE) {
|
|
||||||
saveFile(templateDir, template, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void saveFile(String dir, String path, boolean isOverride) {
|
|
||||||
if (!isOverride) {
|
|
||||||
File file = new File(dir + File.separator + path);
|
|
||||||
if (file.exists()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try (// 模板文件输入输出地址 读取resources下文件
|
|
||||||
FileOutputStream outputStream = new FileOutputStream(dir + File.separator + path);
|
|
||||||
//返回读取指定资源的输入流
|
|
||||||
InputStream inputStream = ConfigUtils.class.getClassLoader().getResourceAsStream("template" + File.separator + path)) {
|
|
||||||
byte[] buffer = new byte[4096];
|
|
||||||
int n = 0;
|
|
||||||
while (-1 != (n = inputStream.read(buffer))) {
|
|
||||||
outputStream.write(buffer, 0, n);
|
|
||||||
}
|
|
||||||
outputStream.flush();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("saveFile error", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,7 @@ import ai.chat2db.server.web.api.controller.rdb.factory.ExportServiceFactory;
|
|||||||
import ai.chat2db.server.web.api.controller.rdb.request.DataExportRequest;
|
import ai.chat2db.server.web.api.controller.rdb.request.DataExportRequest;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.TableVO;
|
import ai.chat2db.server.web.api.controller.rdb.vo.TableVO;
|
||||||
import ai.chat2db.spi.model.Table;
|
import ai.chat2db.spi.model.Table;
|
||||||
import ai.chat2db.spi.sql.Chat2DBContext;
|
|
||||||
import ai.chat2db.spi.util.JdbcUtils;
|
|
||||||
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DatePattern;
|
||||||
import com.alibaba.druid.DbType;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
Reference in New Issue
Block a user