From 47bb7d29cefb179c382079a8e4ead7d4d84babfe Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Mon, 31 Oct 2022 14:20:04 +0800 Subject: [PATCH] =?UTF-8?q?perf(=E5=AE=9A=E6=97=B6=E6=8A=A5=E5=91=8A):=20?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=AE=8C=E6=B6=88=E6=81=AF=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/dataease/commons/utils/ExcelUtils.java | 12 ++++++++++-- .../sechedule/strategy/impl/EmailTaskHandler.java | 12 +++++++++++- .../io/dataease/plugins/server/XEmailTaskServer.java | 2 +- .../io/dataease/service/chart/ViewExportExcel.java | 6 +++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/io/dataease/commons/utils/ExcelUtils.java b/backend/src/main/java/io/dataease/commons/utils/ExcelUtils.java index 1602341ddb..78394de43e 100644 --- a/backend/src/main/java/io/dataease/commons/utils/ExcelUtils.java +++ b/backend/src/main/java/io/dataease/commons/utils/ExcelUtils.java @@ -18,8 +18,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtils { private static final String suffix = ".xlsx"; + private static final String BASE_ROOT = "/opt/dataease/data/"; - public static File exportExcel(List sheets, String fileName) throws Exception { + public static File exportExcel(List sheets, String fileName, String folderId) throws Exception { AtomicReference realFileName = new AtomicReference<>(fileName); Workbook wb = new XSSFWorkbook(); @@ -63,7 +64,14 @@ public class ExcelUtils { if (!StringUtils.endsWith(fileName, suffix)) { realFileName.set(realFileName.get() + suffix); } - File result = new File("/opt/dataease/data/" + realFileName.get()); + String folderPath = BASE_ROOT; + if (StringUtils.isNotBlank(folderId)) { + folderPath = BASE_ROOT + folderId + "/"; + } + if (!FileUtil.exist(folderPath)) { + FileUtil.mkdir(folderPath); + } + File result = new File(folderPath + realFileName.get()); BufferedOutputStream outputStream = FileUtil.getOutputStream(result); try { wb.write(outputStream); diff --git a/backend/src/main/java/io/dataease/job/sechedule/strategy/impl/EmailTaskHandler.java b/backend/src/main/java/io/dataease/job/sechedule/strategy/impl/EmailTaskHandler.java index 965648465f..a6dc65e2e8 100644 --- a/backend/src/main/java/io/dataease/job/sechedule/strategy/impl/EmailTaskHandler.java +++ b/backend/src/main/java/io/dataease/job/sechedule/strategy/impl/EmailTaskHandler.java @@ -1,5 +1,6 @@ package io.dataease.job.sechedule.strategy.impl; +import cn.hutool.core.io.FileUtil; import io.dataease.auth.entity.SysUserEntity; import io.dataease.auth.entity.TokenInfo; import io.dataease.auth.service.AuthUserService; @@ -160,6 +161,7 @@ public class EmailTaskHandler extends TaskHandler implements Job { EmailXpackService emailXpackService = SpringContextUtil.getBean(EmailXpackService.class); AuthUserServiceImpl userService = SpringContextUtil.getBean(AuthUserServiceImpl.class); SysUserService sysUserService = SpringContextUtil.getBean(SysUserService.class); + List files = null; try { XpackEmailTaskRequest taskForm = emailXpackService.taskForm(taskInstance.getTaskId()); if (ObjectUtils.isEmpty(taskForm) || CronUtils.taskExpire(taskForm.getEndTime())) { @@ -199,7 +201,7 @@ public class EmailTaskHandler extends TaskHandler implements Job { contentStr = new String(content, "UTF-8"); } - List files = null; + String viewIds = emailTemplateDTO.getViewIds(); ChartViewService chartViewService = SpringContextUtil.getBean(ChartViewService.class); List viewOptions = chartViewService.viewOptions(panelId); @@ -343,6 +345,14 @@ public class EmailTaskHandler extends TaskHandler implements Job { } catch (Exception e) { error(taskInstance, e); LogUtil.error(e.getMessage(), e); + } finally { + if (CollectionUtils.isNotEmpty(files)) { + files.forEach(file -> { + if (file.exists()) { + FileUtil.del(file); + } + }); + } } } diff --git a/backend/src/main/java/io/dataease/plugins/server/XEmailTaskServer.java b/backend/src/main/java/io/dataease/plugins/server/XEmailTaskServer.java index 09666aefbb..2378f8854a 100644 --- a/backend/src/main/java/io/dataease/plugins/server/XEmailTaskServer.java +++ b/backend/src/main/java/io/dataease/plugins/server/XEmailTaskServer.java @@ -233,7 +233,7 @@ public class XEmailTaskServer { ExcelSheetModel excelSheetModel = excelSheetModel(instanceDTOS); List sheetModels = new ArrayList<>(); sheetModels.add(excelSheetModel); - File file = ExcelUtils.exportExcel(sheetModels, null); + File file = ExcelUtils.exportExcel(sheetModels, null, null); InputStream inputStream = new FileInputStream(file); HttpServletResponse response = ServletUtils.response(); try { diff --git a/backend/src/main/java/io/dataease/service/chart/ViewExportExcel.java b/backend/src/main/java/io/dataease/service/chart/ViewExportExcel.java index e910178fb8..60129c6f5a 100644 --- a/backend/src/main/java/io/dataease/service/chart/ViewExportExcel.java +++ b/backend/src/main/java/io/dataease/service/chart/ViewExportExcel.java @@ -51,7 +51,7 @@ public class ViewExportExcel { Map stringChartExtRequestMap = buildViewRequest(panelDto, justView); List results = new ArrayList<>(); List sheets = viewIds.stream().map(viewId -> viewFiles(viewId, stringChartExtRequestMap.get(viewId))).collect(Collectors.toList()); - File excelFile = ExcelUtils.exportExcel(sheets, panelDto.getName()); + File excelFile = ExcelUtils.exportExcel(sheets, panelDto.getName(), panelDto.getId()); results.add(excelFile); return results; } @@ -62,7 +62,7 @@ public class ViewExportExcel { List> components = gson.fromJson(componentsJson, tokenType); String panelStyle = panelDto.getPanelStyle(); Map map = gson.fromJson(panelStyle, Map.class); - Map panelMap = (LinkedTreeMap)map.get("panel"); + Map panelMap = (LinkedTreeMap) map.get("panel"); double resultCount = Double.parseDouble(panelMap.get("resultCount").toString()); String resultMode = panelMap.get("resultMode").toString(); @@ -73,7 +73,7 @@ public class ViewExportExcel { ChartExtRequest chartExtRequest = new ChartExtRequest(); chartExtRequest.setQueryFrom("panel"); chartExtRequest.setFilter(chartExtFilterRequests); - chartExtRequest.setResultCount((int)resultCount); + chartExtRequest.setResultCount((int) resultCount); chartExtRequest.setResultMode(resultMode); result.put(entry.getKey(), chartExtRequest); }