From 9a7fffca2b343186c63e63caa01dd1b0cda371af Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Sat, 10 Aug 2024 23:26:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E7=94=A8=E4=BE=8B=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/FunctionalCaseFileService.java | 26 ++++++++++++------- .../service/FunctionalCaseXmindService.java | 17 +++++++++--- .../xmind/utils/XmindExportUtil.java | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java index 0f894c4bbb..7889098880 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java @@ -56,7 +56,6 @@ import io.metersphere.system.utils.ServiceUtils; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; @@ -436,7 +435,6 @@ public class FunctionalCaseFileService { tmpDir = new File(getClass().getClassLoader().getResource(StringUtils.EMPTY).getPath() + EXPORT_CASE_TMP_DIR + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr()); // 生成tmp随机目录 - MsFileUtils.deleteDir(tmpDir.getPath()); tmpDir.mkdirs(); //获取导出的ids集合 List batchExcels = new ArrayList<>(); @@ -477,6 +475,12 @@ public class FunctionalCaseFileService { } LogUtils.error(e); throw new MSException(e); + } finally { + try { + MsFileUtils.deleteDir(tmpDir.getPath()); + } catch (Exception e) { + throw new MSException(e); + } } return null; } @@ -497,16 +501,13 @@ public class FunctionalCaseFileService { exportTaskMapper.updateByPrimaryKeySelective(exportTask); } - public void uploadFileToMinio(String fileType, File file, String fileId) { + public void uploadFileToMinio(String fileType, File file, String fileId) throws Exception { FileRequest fileRequest = new FileRequest(); fileRequest.setFileName(fileId.concat(".").concat(fileType)); fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir()); fileRequest.setStorage(StorageType.MINIO.name()); - try { - FileInputStream inputStream = new FileInputStream(file); + try (FileInputStream inputStream = new FileInputStream(file)) { fileService.upload(inputStream, fileRequest); - } catch (Exception e) { - throw new MSException("save file error"); } } @@ -539,9 +540,16 @@ public class FunctionalCaseFileService { List excelData = parseCaseData2ExcelData(subIds, rowMergeInfo, request, customFields, moduleMap, parameter.getParamValue()); List> data = parseExcelData2List(headList, excelData); - File createFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getSystemTempDir() + File.separator + "Metersphere_case_" + project.getName() + count.get() + ".xlsx")); + File createFile = new File(tmpZipPath + File.separatorChar + "Metersphere_case_" + project.getName() + count.get() + ".xlsx"); + if (!createFile.exists()) { + try { + createFile.createNewFile(); + } catch (IOException e) { + throw new MSException(e); + } + } //生成临时EXCEL - EasyExcel.write(createFile.getAbsolutePath()) + EasyExcel.write(createFile) .head(Optional.ofNullable(headList).orElse(new ArrayList<>())) .registerWriteHandler(handler) .registerWriteHandler(writeHandler) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseXmindService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseXmindService.java index 3fee3899f0..fddad4b378 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseXmindService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseXmindService.java @@ -9,7 +9,6 @@ import io.metersphere.functional.socket.ExportWebSocketHandler; import io.metersphere.functional.xmind.domain.FunctionalCaseXmindDTO; import io.metersphere.functional.xmind.domain.FunctionalCaseXmindData; import io.metersphere.functional.xmind.utils.XmindExportUtil; -import io.metersphere.sdk.constants.LocalRepositoryDir; import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.MsgType; import io.metersphere.sdk.dto.ExportMsgDTO; @@ -21,15 +20,17 @@ import io.metersphere.system.constants.ExportConstants; import io.metersphere.system.dto.sdk.BaseTreeNode; import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO; import io.metersphere.system.manager.ExportTaskManager; +import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -114,10 +115,12 @@ public class FunctionalCaseXmindService { if (CollectionUtils.isEmpty(ids)) { return null; } - + File tmpFile = null; try { FunctionalCaseXmindData xmindData = buildXmindData(ids, request); - File tmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getSystemTempDir() + File.separator + request.getFileId() + "." + XMIND)); + tmpFile = new File(getClass().getClassLoader().getResource(StringUtils.EMPTY).getPath() + + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr() + ".xmind"); + tmpFile.createNewFile(); List templateCustomFields = functionalCaseFileService.getCustomFields(request.getProjectId()); TemplateCustomFieldDTO templateCustomFieldDTO = templateCustomFields.stream().filter(item -> StringUtils.equalsIgnoreCase(item.getFieldName(), Translator.get("custom_field.functional_priority"))).findFirst().get(); XmindExportUtil.export(xmindData, request, tmpFile, templateCustomFieldDTO); @@ -143,6 +146,12 @@ public class FunctionalCaseXmindService { } LogUtils.error(e); throw new MSException(e); + }finally { + try { + FileUtils.delete(tmpFile); + } catch (Exception e) { + throw new MSException(e); + } } return null; } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/xmind/utils/XmindExportUtil.java b/backend/services/case-management/src/main/java/io/metersphere/functional/xmind/utils/XmindExportUtil.java index 6d4145c5b9..0999620322 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/xmind/utils/XmindExportUtil.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/xmind/utils/XmindExportUtil.java @@ -368,7 +368,7 @@ public class XmindExportUtil { public static void export(FunctionalCaseXmindData xmindData, FunctionalCaseExportRequest request, File tmpFile, TemplateCustomFieldDTO priority) { IWorkbook workBook = createXmindByCaseData(xmindData, false, null, request, priority); try { - workBook.save(tmpFile.getAbsolutePath()); + workBook.save(tmpFile.getPath()); } catch (UnsupportedEncodingException e) { LogUtils.error(e.getMessage(), e); throw new MSException("Utf-8 encoding is not supported");