根据艿艿的说明修改

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,47 +47,54 @@ 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工具类它的中文文件名编码有问题,导致在浏览器下载时有问题
ServletUtils.writeAttachment(response, downloadFileName, content);
handleDeleteFile(deleteFile, filePath); 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()) {
@ -95,9 +110,7 @@ public class InfDbDocController {
// 执行 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;
} }
} }
@ -105,8 +118,7 @@ public class InfDbDocController {
if (!deleteFile) { if (!deleteFile) {
return; return;
} }
File file=new File(filePath); FileUtil.del(filePath);
file.delete();
} }
/** /**