根据艿艿的说明修改

This commit is contained in:
timfruit 2021-03-13 13:21:33 +08:00
parent 9891763015
commit 7089ef7651

View File

@ -1,6 +1,10 @@
package cn.iocoder.dashboard.modules.infra.controller.doc; package cn.iocoder.dashboard.modules.infra.controller.doc;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID; import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.dashboard.util.servlet.ServletUtils; import cn.iocoder.dashboard.util.servlet.ServletUtils;
import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig; import cn.smallbun.screw.core.engine.EngineConfig;
@ -11,7 +15,11 @@ import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -39,74 +47,78 @@ public class InfDbDocController {
@GetMapping("/export-html") @GetMapping("/export-html")
@ApiOperation("导出html格式的数据文档")
@ApiImplicitParams({
@ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class),
})
public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile, public void exportHtml(@RequestParam(defaultValue = "true") Boolean deleteFile,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
EngineFileType fileOutputType=EngineFileType.HTML; doExportFile(EngineFileType.HTML, deleteFile, response);
doExportFile(fileOutputType,deleteFile,response);
} }
@GetMapping("/export-word") @GetMapping("/export-word")
@ApiOperation("导出word格式的数据文档")
@ApiImplicitParams({
@ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class),
})
public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile, public void exportWord(@RequestParam(defaultValue = "true") Boolean deleteFile,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
EngineFileType fileOutputType=EngineFileType.WORD; doExportFile(EngineFileType.WORD, deleteFile, response);
doExportFile(fileOutputType,deleteFile,response);
} }
@GetMapping("/export-markdown") @GetMapping("/export-markdown")
@ApiOperation("导出markdown格式的数据文档")
@ApiImplicitParams({
@ApiImplicitParam(name = "deleteFile", value = "是否删除在服务器本地生成的数据库文档", example = "true", dataTypeClass = Boolean.class),
})
public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile, public void exportMarkdown(@RequestParam(defaultValue = "true") Boolean deleteFile,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
EngineFileType fileOutputType=EngineFileType.MD; doExportFile(EngineFileType.MD, deleteFile, response);
doExportFile(fileOutputType,deleteFile,response);
} }
private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile, private void doExportFile(EngineFileType fileOutputType, Boolean deleteFile,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
String docFileName=DOC_FILE_NAME+"_"+ UUID.fastUUID().toString(true); String docFileName = DOC_FILE_NAME + "_" + IdUtil.fastSimpleUUID();
String filePath= doExportFile(fileOutputType,docFileName); String filePath = doExportFile(fileOutputType, docFileName);
String downloadFileName=DOC_FILE_NAME+fileOutputType.getFileSuffix(); //下载后的文件名 String downloadFileName = DOC_FILE_NAME + fileOutputType.getFileSuffix(); //下载后的文件名
// 读取返回 // 读取返回
try (InputStream is=new FileInputStream(filePath)){//处理后关闭文件流才能删除 //IoUtil.readBytes 直接读取FileInputStream 不会关闭流有bug,所以用BufferedInputStream包装一下, 关闭流后才能删除文件
ServletUtils.writeAttachment(response,downloadFileName, StreamUtils.copyToByteArray(is)); byte[] content = IoUtil.readBytes(new BufferedInputStream(new FileInputStream(filePath)));
} //这里不用hutool工具类它的中文文件名编码有问题,导致在浏览器下载时有问题
handleDeleteFile(deleteFile,filePath); ServletUtils.writeAttachment(response, downloadFileName, content);
handleDeleteFile(deleteFile, filePath);
} }
/** /**
* 输出文件返回文件路径 * 输出文件返回文件路径
* @param fileOutputType *
* @param fileName * @param fileOutputType 文件类型
* @return * @param fileName 文件名, 无需 ".docx" 等文件后缀
* @return 生成的文件所在路径
*/ */
private String doExportFile(EngineFileType fileOutputType, String fileName){ private String doExportFile(EngineFileType fileOutputType, String fileName) {
try (HikariDataSource dataSource = buildDataSource()) { try (HikariDataSource dataSource = buildDataSource()) {
// 创建 screw 的配置 // 创建 screw 的配置
Configuration config = Configuration.builder() Configuration config = Configuration.builder()
.version(DOC_VERSION) // 版本 .version(DOC_VERSION) // 版本
.description(DOC_DESCRIPTION) // 描述 .description(DOC_DESCRIPTION) // 描述
.dataSource(dataSource) // 数据源 .dataSource(dataSource) // 数据源
.engineConfig(buildEngineConfig(fileOutputType,fileName)) // 引擎配置 .engineConfig(buildEngineConfig(fileOutputType, fileName)) // 引擎配置
.produceConfig(buildProcessConfig()) // 处理配置 .produceConfig(buildProcessConfig()) // 处理配置
.build(); .build();
// 执行 screw生成数据库文档 // 执行 screw生成数据库文档
new DocumentationExecute(config).execute(); new DocumentationExecute(config).execute();
return FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix();
String filePath=FILE_OUTPUT_DIR + File.separator + fileName + fileOutputType.getFileSuffix();
return filePath;
} }
} }
private void handleDeleteFile(Boolean deleteFile,String filePath){ private void handleDeleteFile(Boolean deleteFile, String filePath) {
if(!deleteFile){ if (!deleteFile) {
return; return;
} }
File file=new File(filePath); FileUtil.del(filePath);
file.delete();
} }
/** /**
@ -128,7 +140,7 @@ public class InfDbDocController {
/** /**
* 创建 screw 的引擎配置 * 创建 screw 的引擎配置
*/ */
private static EngineConfig buildEngineConfig(EngineFileType fileOutputType,String docFileName) { private static EngineConfig buildEngineConfig(EngineFileType fileOutputType, String docFileName) {
return EngineConfig.builder() return EngineConfig.builder()
.fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径 .fileOutputDir(FILE_OUTPUT_DIR) // 生成文件路径
.openOutputDir(false) // 打开目录 .openOutputDir(false) // 打开目录