refactor(测试计划): 导出用例临时文件问题

This commit is contained in:
WangXu10 2024-08-12 11:38:39 +08:00 committed by 刘瑞斌
parent 2dc7eb8675
commit 2876373f2d
2 changed files with 12 additions and 8 deletions

View File

@ -432,10 +432,10 @@ public class FunctionalCaseFileService {
Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
String fileType = ""; String fileType = "";
try { try {
tmpDir = new File(getClass().getClassLoader().getResource(StringUtils.EMPTY).getPath() + tmpDir = new File(LocalRepositoryDir.getSystemTempDir() + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr());
EXPORT_CASE_TMP_DIR + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr()); if (!tmpDir.exists() && !tmpDir.mkdirs()) {
// 生成tmp随机目录 throw new MSException(Translator.get("upload_fail"));
tmpDir.mkdirs(); }
//获取导出的ids集合 //获取导出的ids集合
List<File> batchExcels = new ArrayList<>(); List<File> batchExcels = new ArrayList<>();
List<String> ids = functionalCaseService.doSelectIds(request, request.getProjectId()); List<String> ids = functionalCaseService.doSelectIds(request, request.getProjectId());

View File

@ -9,6 +9,7 @@ import io.metersphere.functional.socket.ExportWebSocketHandler;
import io.metersphere.functional.xmind.domain.FunctionalCaseXmindDTO; import io.metersphere.functional.xmind.domain.FunctionalCaseXmindDTO;
import io.metersphere.functional.xmind.domain.FunctionalCaseXmindData; import io.metersphere.functional.xmind.domain.FunctionalCaseXmindData;
import io.metersphere.functional.xmind.utils.XmindExportUtil; import io.metersphere.functional.xmind.utils.XmindExportUtil;
import io.metersphere.sdk.constants.LocalRepositoryDir;
import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.constants.MsgType; import io.metersphere.sdk.constants.MsgType;
import io.metersphere.sdk.dto.ExportMsgDTO; import io.metersphere.sdk.dto.ExportMsgDTO;
@ -30,7 +31,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@ -115,12 +115,16 @@ public class FunctionalCaseXmindService {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return null; return null;
} }
File dir = null;
File tmpFile = null; File tmpFile = null;
try { try {
FunctionalCaseXmindData xmindData = buildXmindData(ids, request); FunctionalCaseXmindData xmindData = buildXmindData(ids, request);
tmpFile = new File(getClass().getClassLoader().getResource(StringUtils.EMPTY).getPath() + dir = new File(LocalRepositoryDir.getSystemTempDir());
if (!dir.exists() && !dir.mkdir()) {
throw new MSException(Translator.get("upload_fail"));
}
tmpFile = new File(LocalRepositoryDir.getSystemTempDir() +
File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr() + ".xmind"); File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr() + ".xmind");
tmpFile.createNewFile();
List<TemplateCustomFieldDTO> templateCustomFields = functionalCaseFileService.getCustomFields(request.getProjectId()); List<TemplateCustomFieldDTO> templateCustomFields = functionalCaseFileService.getCustomFields(request.getProjectId());
TemplateCustomFieldDTO templateCustomFieldDTO = templateCustomFields.stream().filter(item -> StringUtils.equalsIgnoreCase(item.getFieldName(), Translator.get("custom_field.functional_priority"))).findFirst().get(); TemplateCustomFieldDTO templateCustomFieldDTO = templateCustomFields.stream().filter(item -> StringUtils.equalsIgnoreCase(item.getFieldName(), Translator.get("custom_field.functional_priority"))).findFirst().get();
XmindExportUtil.export(xmindData, request, tmpFile, templateCustomFieldDTO); XmindExportUtil.export(xmindData, request, tmpFile, templateCustomFieldDTO);
@ -146,7 +150,7 @@ public class FunctionalCaseXmindService {
} }
LogUtils.error(e); LogUtils.error(e);
throw new MSException(e); throw new MSException(e);
}finally { } finally {
try { try {
FileUtils.delete(tmpFile); FileUtils.delete(tmpFile);
} catch (Exception e) { } catch (Exception e) {