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");