Commit 4048e5980504c19ff74b605d8636f65f885c04ea

Authored by 芋道源码
Committed by Gitee
2 parents 4dc484f5 562f4cb9

!14 完成导出数据库文档

Merge pull request !14 from timfruit/feature/screw-export-dbdoc
src/main/java/cn/iocoder/dashboard/modules/infra/controller/doc/InfDbDocController.java
1 1 package cn.iocoder.dashboard.modules.infra.controller.doc;
2 2  
  3 +import cn.hutool.core.io.FileUtil;
  4 +import cn.hutool.core.io.IoUtil;
  5 +import cn.hutool.core.lang.UUID;
  6 +import cn.hutool.core.util.IdUtil;
3 7 import cn.hutool.extra.servlet.ServletUtil;
  8 +import cn.iocoder.dashboard.util.servlet.ServletUtils;
4 9 import cn.smallbun.screw.core.Configuration;
5 10 import cn.smallbun.screw.core.engine.EngineConfig;
6 11 import cn.smallbun.screw.core.engine.EngineFileType;
... ... @@ -10,18 +15,20 @@ import cn.smallbun.screw.core.process.ProcessConfig;
10 15 import com.zaxxer.hikari.HikariConfig;
11 16 import com.zaxxer.hikari.HikariDataSource;
12 17 import io.swagger.annotations.Api;
  18 +import io.swagger.annotations.ApiImplicitParam;
  19 +import io.swagger.annotations.ApiImplicitParams;
  20 +import io.swagger.annotations.ApiOperation;
13 21 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
14 22 import org.springframework.http.MediaType;
  23 +import org.springframework.util.StreamUtils;
15 24 import org.springframework.web.bind.annotation.GetMapping;
16 25 import org.springframework.web.bind.annotation.RequestMapping;
  26 +import org.springframework.web.bind.annotation.RequestParam;
17 27 import org.springframework.web.bind.annotation.RestController;
18 28  
19 29 import javax.annotation.Resource;
20 30 import javax.servlet.http.HttpServletResponse;
21   -import javax.sql.DataSource;
22   -import java.io.File;
23   -import java.io.FileInputStream;
24   -import java.io.FileNotFoundException;
  31 +import java.io.*;
25 32 import java.util.Collections;
26 33  
27 34 @Api(tags = "数据库文档")
... ... @@ -34,34 +41,79 @@ public class InfDbDocController {
34 41  
35 42 private static final String FILE_OUTPUT_DIR = System.getProperty("java.io.tmpdir") + File.separator
36 43 + "db-doc";
37   - private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.HTML; // 可以设置 Word 或者 Markdown 格式
38 44 private static final String DOC_FILE_NAME = "数据库文档";
39 45 private static final String DOC_VERSION = "1.0.0";
40 46 private static final String DOC_DESCRIPTION = "文档描述";
41 47  
42   - @Resource
43   - private DataSource dataSource;
44 48  
45 49 @GetMapping("/export-html")
46   - public synchronized void exportHtml(HttpServletResponse response) throws FileNotFoundException {
  50 + @ApiOperation("导出html格式的数据文档")
  51 + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
  52 + public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile,
  53 + HttpServletResponse response) throws IOException {
  54 + doExportFile(EngineFileType.HTML, deleteFile, response);
  55 + }
  56 +
  57 + @GetMapping("/export-word")
  58 + @ApiOperation("导出word格式的数据文档")
  59 + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
  60 + public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile,
  61 + HttpServletResponse response) throws IOException {
  62 + doExportFile(EngineFileType.WORD, deleteFile, response);
  63 + }
  64 +
  65 + @GetMapping("/export-markdown")
  66 + @ApiOperation("导出markdown格式的数据文档")
  67 + @ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class)
  68 + public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile,
  69 + HttpServletResponse response) throws IOException {
  70 + doExportFile(EngineFileType.MD, deleteFile, response);
  71 + }
  72 +
  73 + private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile,
  74 + HttpServletResponse response) throws IOException {
  75 + String docFileName = DOC_FILE_NAME + "_" + IdUtil.fastSimpleUUID();
  76 + String filePath = doExportFile(fileOutputType, docFileName);
  77 + String downloadFileName = DOC_FILE_NAME + fileOutputType.getFileSuffix(); //下载后的文件名
  78 + try {
  79 + // 读取,返回
  80 + //这里不用hutool工具类,它的中文文件名编码有问题,导致在浏览器下载时有问题
  81 + ServletUtils.writeAttachment(response, downloadFileName, FileUtil.readBytes(filePath));
  82 + }finally {
  83 + handleDeleteFile(deleteFile, filePath);
  84 + }
  85 + }
  86 +
  87 + /**
  88 + * 输出文件,返回文件路径
  89 + *
  90 + * @param fileOutputType 文件类型
  91 + * @param fileName 文件名, 无需 ".docx" 等文件后缀
  92 + * @return 生成的文件所在路径
  93 + */
  94 + private String doExportFile(EngineFileType fileOutputType, String fileName) {
47 95 try (HikariDataSource dataSource = buildDataSource()) {
48 96 // 创建 screw 的配置
49 97 Configuration config = Configuration.builder()
50 98 .version(DOC_VERSION) // 版本
51 99 .description(DOC_DESCRIPTION) // 描述
52 100 .dataSource(dataSource) // 数据源
53   - .engineConfig(buildEngineConfig()) // 引擎配置
  101 + .engineConfig(buildEngineConfig(fileOutputType, fileName)) // 引擎配置
54 102 .produceConfig(buildProcessConfig()) // 处理配置
55 103 .build();
56 104  
57 105 // 执行 screw,生成数据库文档
58 106 new DocumentationExecute(config).execute();
59 107  
60   - // 读取,返回
61   - ServletUtil.write(response,
62   - new FileInputStream(FILE_OUTPUT_DIR + File.separator + DOC_FILE_NAME + FILE_OUTPUT_TYPE.getFileSuffix()),
63   - MediaType.TEXT_HTML_VALUE);
  108 + return FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix();
  109 + }
  110 + }
  111 +
  112 + private void handleDeleteFile(Boolean deleteFile, String filePath) {
  113 + if (!deleteFile) {
  114 + return;
64 115 }
  116 + FileUtil.del(filePath);
65 117 }
66 118  
67 119 /**
... ... @@ -83,13 +135,13 @@ public class InfDbDocController {
83 135 /**
84 136 * 创建 screw 的引擎配置
85 137 */
86   - private static EngineConfig buildEngineConfig() {
  138 + private static EngineConfig buildEngineConfig(EngineFileType fileOutputType, String docFileName) {
87 139 return EngineConfig.builder()
88 140 .fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径
89 141 .openOutputDir(false) // 打开目录
90   - .fileType(FILE_OUTPUT_TYPE) // 文件类型
  142 + .fileType(fileOutputType) // 文件类型
91 143 .produceType(EngineTemplateType.freemarker) // 文件类型
92   - .fileName(DOC_FILE_NAME) // 自定义文件名称
  144 + .fileName(docFileName) // 自定义文件名称
93 145 .build();
94 146 }
95 147  
... ...