服务启动时减少io操作

This commit is contained in:
lzy
2023-10-25 18:07:01 +08:00
parent 0994138d10
commit ec75800781
4 changed files with 50 additions and 8 deletions

View File

@ -21,14 +21,9 @@ public class CopyTemplate {
/** /**
* 模板文件 * 模板文件
**/ **/
public static final List<String> TEMPLATE_FILE = Arrays.asList("template.html", "template_diy.docx", "sub_template_diy.docx"); private static final List<String> TEMPLATE_FILE = Arrays.asList("template.html", "template_diy.docx", "sub_template_diy.docx");
static { public void copyTemplateFile() {
//复制模板
copyTemplateFile();
}
public static void copyTemplateFile() {
String templateDir = ConfigUtils.CONFIG_BASE_PATH + File.separator + "template"; String templateDir = ConfigUtils.CONFIG_BASE_PATH + File.separator + "template";
File file = new File(templateDir); File file = new File(templateDir);
if (!file.exists()) { if (!file.exists()) {
@ -39,7 +34,7 @@ public class CopyTemplate {
} }
} }
public static void saveFile(String dir, String path, boolean isOverride) { public void saveFile(String dir, String path, boolean isOverride) {
if (!isOverride) { if (!isOverride) {
File file = new File(dir + File.separator + path); File file = new File(dir + File.separator + path);
if (file.exists()) { if (file.exists()) {

View File

@ -0,0 +1,26 @@
package ai.chat2db.server.start.listener;
import ai.chat2db.server.start.config.util.CopyTemplate;
import ai.chat2db.server.web.api.controller.rdb.doc.event.TemplateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* TemplateListener
*
* @author lzy
**/
@Component
public class TemplateListener {
@Autowired
private CopyTemplate copyTemplate;
@EventListener(classes = TemplateEvent.class)
public void copyTemplate() {
//复制模板
copyTemplate.copyTemplateFile();
}
}

View File

@ -11,6 +11,7 @@ import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter; import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
import ai.chat2db.server.web.api.controller.rdb.doc.DatabaseExportService; import ai.chat2db.server.web.api.controller.rdb.doc.DatabaseExportService;
import ai.chat2db.server.web.api.controller.rdb.doc.conf.ExportOptions; import ai.chat2db.server.web.api.controller.rdb.doc.conf.ExportOptions;
import ai.chat2db.server.web.api.controller.rdb.doc.event.TemplateEvent;
import ai.chat2db.server.web.api.controller.rdb.factory.ExportServiceFactory; 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;
@ -20,6 +21,7 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -48,6 +50,9 @@ public class RdbDocController {
@Autowired @Autowired
private RdbWebConverter rdbWebConverter; private RdbWebConverter rdbWebConverter;
@Autowired
private ApplicationContext applicationContext;
/** /**
* export data * export data
* *
@ -55,6 +60,8 @@ public class RdbDocController {
*/ */
@PostMapping("/export") @PostMapping("/export")
public void export(@Valid @RequestBody DataExportRequest request, HttpServletResponse response) throws Exception { public void export(@Valid @RequestBody DataExportRequest request, HttpServletResponse response) throws Exception {
//复制模板
applicationContext.publishEvent(new TemplateEvent("copy"));
ExportTypeEnum exportType = EasyEnumUtils.getEnum(ExportTypeEnum.class, request.getExportType()); ExportTypeEnum exportType = EasyEnumUtils.getEnum(ExportTypeEnum.class, request.getExportType());
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode( String fileName = URLEncoder.encode(

View File

@ -0,0 +1,14 @@
package ai.chat2db.server.web.api.controller.rdb.doc.event;
import org.springframework.context.ApplicationEvent;
/**
* TemplateEvent
*
* @author lzy
**/
public class TemplateEvent extends ApplicationEvent {
public TemplateEvent(String key) {
super(key);
}
}