Commit 98917630154af5664b5d1cc506144fa82d75358c
1 parent
4e5430b5
完成导出数据库文档
Showing
1 changed file
with
63 additions
and
18 deletions
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.extra.servlet.ServletUtil; | |
| 3 | +import cn.hutool.core.lang.UUID; | |
| 4 | +import cn.iocoder.dashboard.util.servlet.ServletUtils; | |
| 4 | 5 | import cn.smallbun.screw.core.Configuration; |
| 5 | 6 | import cn.smallbun.screw.core.engine.EngineConfig; |
| 6 | 7 | import cn.smallbun.screw.core.engine.EngineFileType; |
| ... | ... | @@ -11,17 +12,15 @@ import com.zaxxer.hikari.HikariConfig; |
| 11 | 12 | import com.zaxxer.hikari.HikariDataSource; |
| 12 | 13 | import io.swagger.annotations.Api; |
| 13 | 14 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
| 14 | -import org.springframework.http.MediaType; | |
| 15 | +import org.springframework.util.StreamUtils; | |
| 15 | 16 | import org.springframework.web.bind.annotation.GetMapping; |
| 16 | 17 | import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestParam; | |
| 17 | 19 | import org.springframework.web.bind.annotation.RestController; |
| 18 | 20 | |
| 19 | 21 | import javax.annotation.Resource; |
| 20 | 22 | 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; | |
| 23 | +import java.io.*; | |
| 25 | 24 | import java.util.Collections; |
| 26 | 25 | |
| 27 | 26 | @Api(tags = "数据库文档") |
| ... | ... | @@ -34,34 +33,80 @@ public class InfDbDocController { |
| 34 | 33 | |
| 35 | 34 | private static final String FILE_OUTPUT_DIR = System.getProperty("java.io.tmpdir") + File.separator |
| 36 | 35 | + "db-doc"; |
| 37 | - private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.HTML; // 可以设置 Word 或者 Markdown 格式 | |
| 38 | 36 | private static final String DOC_FILE_NAME = "数据库文档"; |
| 39 | 37 | private static final String DOC_VERSION = "1.0.0"; |
| 40 | 38 | private static final String DOC_DESCRIPTION = "文档描述"; |
| 41 | 39 | |
| 42 | - @Resource | |
| 43 | - private DataSource dataSource; | |
| 44 | 40 | |
| 45 | 41 | @GetMapping("/export-html") |
| 46 | - public synchronized void exportHtml(HttpServletResponse response) throws FileNotFoundException { | |
| 42 | + public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile, | |
| 43 | + HttpServletResponse response) throws IOException { | |
| 44 | + EngineFileType fileOutputType=EngineFileType.HTML; | |
| 45 | + doExportFile(fileOutputType,deleteFile,response); | |
| 46 | + } | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + @GetMapping("/export-word") | |
| 51 | + public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile, | |
| 52 | + HttpServletResponse response) throws IOException { | |
| 53 | + EngineFileType fileOutputType=EngineFileType.WORD; | |
| 54 | + doExportFile(fileOutputType,deleteFile,response); | |
| 55 | + } | |
| 56 | + | |
| 57 | + | |
| 58 | + @GetMapping("/export-markdown") | |
| 59 | + public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile, | |
| 60 | + HttpServletResponse response) throws IOException { | |
| 61 | + EngineFileType fileOutputType=EngineFileType.MD; | |
| 62 | + doExportFile(fileOutputType,deleteFile,response); | |
| 63 | + } | |
| 64 | + | |
| 65 | + private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile, | |
| 66 | + HttpServletResponse response) throws IOException { | |
| 67 | + String docFileName=DOC_FILE_NAME+"_"+ UUID.fastUUID().toString(true); | |
| 68 | + String filePath= doExportFile(fileOutputType,docFileName); | |
| 69 | + String downloadFileName=DOC_FILE_NAME+fileOutputType.getFileSuffix(); //下载后的文件名 | |
| 70 | + // 读取,返回 | |
| 71 | + try (InputStream is=new FileInputStream(filePath)){//处理后关闭文件流才能删除 | |
| 72 | + ServletUtils.writeAttachment(response,downloadFileName, StreamUtils.copyToByteArray(is)); | |
| 73 | + } | |
| 74 | + handleDeleteFile(deleteFile,filePath); | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * 输出文件,返回文件路径 | |
| 80 | + * @param fileOutputType | |
| 81 | + * @param fileName | |
| 82 | + * @return | |
| 83 | + */ | |
| 84 | + private String doExportFile(EngineFileType fileOutputType, String fileName){ | |
| 47 | 85 | try (HikariDataSource dataSource = buildDataSource()) { |
| 48 | 86 | // 创建 screw 的配置 |
| 49 | 87 | Configuration config = Configuration.builder() |
| 50 | 88 | .version(DOC_VERSION) // 版本 |
| 51 | 89 | .description(DOC_DESCRIPTION) // 描述 |
| 52 | 90 | .dataSource(dataSource) // 数据源 |
| 53 | - .engineConfig(buildEngineConfig()) // 引擎配置 | |
| 91 | + .engineConfig(buildEngineConfig(fileOutputType,fileName)) // 引擎配置 | |
| 54 | 92 | .produceConfig(buildProcessConfig()) // 处理配置 |
| 55 | 93 | .build(); |
| 56 | 94 | |
| 57 | 95 | // 执行 screw,生成数据库文档 |
| 58 | 96 | new DocumentationExecute(config).execute(); |
| 59 | 97 | |
| 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); | |
| 98 | + | |
| 99 | + String filePath=FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix(); | |
| 100 | + return filePath; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + private void handleDeleteFile(Boolean deleteFile,String filePath){ | |
| 105 | + if(!deleteFile){ | |
| 106 | + return; | |
| 64 | 107 | } |
| 108 | + File file=new File(filePath); | |
| 109 | + file.delete(); | |
| 65 | 110 | } |
| 66 | 111 | |
| 67 | 112 | /** |
| ... | ... | @@ -83,13 +128,13 @@ public class InfDbDocController { |
| 83 | 128 | /** |
| 84 | 129 | * 创建 screw 的引擎配置 |
| 85 | 130 | */ |
| 86 | - private static EngineConfig buildEngineConfig() { | |
| 131 | + private static EngineConfig buildEngineConfig(EngineFileType fileOutputType,String docFileName) { | |
| 87 | 132 | return EngineConfig.builder() |
| 88 | 133 | .fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径 |
| 89 | 134 | .openOutputDir(false) // 打开目录 |
| 90 | - .fileType(FILE_OUTPUT_TYPE) // 文件类型 | |
| 135 | + .fileType(fileOutputType) // 文件类型 | |
| 91 | 136 | .produceType(EngineTemplateType.freemarker) // 文件类型 |
| 92 | - .fileName(DOC_FILE_NAME) // 自定义文件名称 | |
| 137 | + .fileName(docFileName) // 自定义文件名称 | |
| 93 | 138 | .build(); |
| 94 | 139 | } |
| 95 | 140 | ... | ... |