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

This commit is contained in:
WangXu10 2024-08-10 23:26:25 +08:00 committed by 刘瑞斌
parent 5d00e2996f
commit 9a7fffca2b
3 changed files with 31 additions and 14 deletions

View File

@ -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<File> 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<FunctionalCaseExcelData> excelData = parseCaseData2ExcelData(subIds, rowMergeInfo, request, customFields, moduleMap, parameter.getParamValue());
List<List<Object>> 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)

View File

@ -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<TemplateCustomFieldDTO> 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;
}

View File

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