refactor(文件管理): 文件重构后兼容性能测试引用及生成文件

This commit is contained in:
fit2-zhao 2022-08-17 13:34:29 +08:00 committed by f2c-ci-robot[bot]
parent f26dede034
commit ab2c94c527
11 changed files with 160 additions and 113 deletions

View File

@ -403,11 +403,6 @@
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -33,9 +33,9 @@ import io.metersphere.controller.request.ScheduleRequest;
import io.metersphere.dto.ScheduleDao;
import io.metersphere.i18n.Translator;
import io.metersphere.job.sechedule.ApiTestJob;
import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.performance.parse.EngineSourceParserFactory;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.FileService;
import io.metersphere.service.ScheduleService;
import io.metersphere.track.service.TestCaseService;
import org.apache.commons.collections.CollectionUtils;
@ -67,7 +67,7 @@ public class APITestService {
@Resource
private ApiTestFileMapper apiTestFileMapper;
@Resource
private FileService fileService;
private FileMetadataService fileMetadataService;
@Resource
private JMeterService jMeterService;
@Resource
@ -145,31 +145,6 @@ public class APITestService {
}
public void copy(SaveAPITestRequest request) {
ApiTestExample example = new ApiTestExample();
example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId());
if (apiTestMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("load_test_already_exists"));
}
// copy test
ApiTest copy = get(request.getId());
copy.setId(UUID.randomUUID().toString());
copy.setName(request.getName());
copy.setCreateTime(System.currentTimeMillis());
copy.setUpdateTime(System.currentTimeMillis());
copy.setStatus(APITestStatus.Saved.name());
copy.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
apiTestMapper.insert(copy);
// copy test file
ApiTestFile apiTestFile = getFileByTestId(request.getId());
if (apiTestFile != null) {
FileMetadata fileMetadata = fileService.copyFile(apiTestFile.getFileId());
apiTestFile.setTestId(copy.getId());
apiTestFile.setFileId(fileMetadata.getId());
apiTestFileMapper.insert(apiTestFile);
}
copyBodyFiles(copy.getId(), request.getId());
}
public void copyBodyFiles(String target, String source) {
@ -228,7 +203,7 @@ public class APITestService {
if (file == null) {
MSException.throwException(Translator.get("file_cannot_be_null"));
}
byte[] bytes = fileService.loadFileAsBytes(file.getFileId());
byte[] bytes = new byte[0];
// 解析 xml 处理 mock 数据
bytes = JmeterDocumentParser.parse(bytes);
InputStream is = new ByteArrayInputStream(bytes);
@ -293,7 +268,7 @@ public class APITestService {
}
private void saveFile(ApiTest apiTest, MultipartFile file) {
final FileMetadata fileMetadata = fileService.saveFile(file, apiTest.getProjectId());
final FileMetadata fileMetadata = fileMetadataService.saveFile(file, apiTest.getProjectId());
ApiTestFile apiTestFile = new ApiTestFile();
apiTestFile.setTestId(apiTest.getId());
apiTestFile.setFileId(fileMetadata.getId());
@ -308,7 +283,7 @@ public class APITestService {
if (!CollectionUtils.isEmpty(ApiTestFiles)) {
final List<String> fileIds = ApiTestFiles.stream().map(ApiTestFile::getFileId).collect(Collectors.toList());
fileService.deleteFileByIds(fileIds);
fileMetadataService.deleteBatch(fileIds);
}
}
@ -503,7 +478,7 @@ public class APITestService {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
try {
FileMetadata fileMetadata = fileService.insertFileByFileName(file, FileUtil.readAsByteArray(file), projectId);
FileMetadata fileMetadata = fileMetadataService.saveFile(FileUtil.readAsByteArray(file), file.getName(), file.length());
if (fileMetadata != null) {
fileMetadataList.add(fileMetadata);
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());

View File

@ -8,6 +8,8 @@ import java.io.IOException;
public interface FileRepository {
String saveFile(MultipartFile file, FileRequest request) throws IOException;
String saveFile(byte[] bytes, FileRequest request) throws IOException;
void delete(FileRequest request) throws Exception;
byte[] getFile(FileRequest request) throws Exception;

View File

@ -2,6 +2,7 @@ package io.metersphere.metadata.repository;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.i18n.Translator;
import io.metersphere.metadata.vo.FileRequest;
import org.apache.commons.lang3.StringUtils;
@ -16,13 +17,7 @@ public class LocalFileRepository implements FileRepository {
if (multipartFile == null || request == null || StringUtils.isEmpty(request.getFileName()) || StringUtils.isEmpty(request.getProjectId())) {
return null;
}
String path = StringUtils.join(FileUtils.BODY_FILE_DIR, "/", request.getProjectId());
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(StringUtils.join(path, "/", request.getFileName()));
File file = createFile(request);
try (InputStream in = multipartFile.getInputStream(); OutputStream out = new FileOutputStream(file)) {
file.createNewFile();
final int MAX = 4096;
@ -36,6 +31,18 @@ public class LocalFileRepository implements FileRepository {
return file.getPath();
}
@Override
public String saveFile(byte[] bytes, FileRequest request) throws IOException {
File file = createFile(request);
try (OutputStream ops = new FileOutputStream(file);) {
ops.write(bytes);
return file.getPath();
} catch (Exception e) {
LogUtil.info(e);
}
return null;
}
@Override
public void delete(FileRequest request) throws Exception {
String path = StringUtils.join(FileUtils.BODY_FILE_DIR + "/", request.getProjectId(), "/", request.getFileName());
@ -82,4 +89,14 @@ public class LocalFileRepository implements FileRepository {
}
return false;
}
private File createFile(FileRequest request) {
String path = StringUtils.join(FileUtils.BODY_FILE_DIR, "/", request.getProjectId());
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(StringUtils.join(path, "/", request.getFileName()));
return file;
}
}

View File

@ -12,6 +12,11 @@ public class MinIOFileRepository implements FileRepository {
return null;
}
@Override
public String saveFile(byte[] bytes, FileRequest request) throws IOException {
return null;
}
@Override
public void delete(FileRequest request) throws Exception {

View File

@ -4,13 +4,9 @@ import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.metadata.vo.FileRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Service
@ -25,6 +21,16 @@ public class FileManagerService {
}
}
public String upload(byte[] file, FileRequest request) {
try {
this.initStorage(request);
return FileCenter.getRepository(request.getStorage()).saveFile(file, request);
} catch (IOException e) {
LogUtil.error(e);
return null;
}
}
public boolean reName(String beforeName, String name, String projectId) {
try {
FileRequest request = new FileRequest();
@ -65,18 +71,6 @@ public class FileManagerService {
}
}
public MultipartFile getMultipartFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
return multipartFile;
} catch (Exception e) {
LogUtil.error(e);
return null;
}
}
public byte[] downloadFile(FileRequest request) {
try {
return FileCenter.getRepository(request.getStorage()).getFile(request);

View File

@ -61,41 +61,22 @@ public class FileMetadataService {
for (MultipartFile file : files) {
QueryProjectFileRequest request = new QueryProjectFileRequest();
request.setName(file.getOriginalFilename());
if (CollectionUtils.isEmpty(this.getProjectFiles(fileMetadata.getProjectId(), request))) {
result.add(this.saveFile(file, fileMetadata));
} else {
MSException.throwException(Translator.get("project_file_already_exists"));
}
}
}
return result;
}
public FileMetadata saveFile(MultipartFile file, FileMetadata fileMetadata) {
if (StringUtils.isEmpty(fileMetadata.getId())) {
fileMetadata.setId(UUID.randomUUID().toString());
}
this.initBase(fileMetadata);
if (StringUtils.isEmpty(fileMetadata.getName())) {
fileMetadata.setName(file.getOriginalFilename());
}
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
fileMetadata.setStorage(StorageConstants.LOCAL.name());
}
if (StringUtils.isEmpty(fileMetadata.getModuleId())) {
fileMetadata.setModuleId(fileModuleService.getDefaultNode(fileMetadata.getProjectId()).getId());
}
checkName(fileMetadata);
fileMetadata.setSize(file.getSize());
fileMetadata.setCreateTime(System.currentTimeMillis());
fileMetadata.setUpdateTime(System.currentTimeMillis());
String fileType = MetadataUtils.getFileType(fileMetadata.getName());
fileMetadata.setType(fileType);
if (StringUtils.isEmpty(fileMetadata.getCreateUser())) {
fileMetadata.setCreateUser(SessionUtils.getUserId());
}
if (StringUtils.isEmpty(fileMetadata.getUpdateUser())) {
fileMetadata.setUpdateUser(SessionUtils.getUserId());
}
// 上传文件
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
String path = fileManagerService.upload(file, request);
@ -110,8 +91,10 @@ public class FileMetadataService {
}
public FileMetadata saveFile(MultipartFile file) {
return saveFile(file, null);
public FileMetadata saveFile(MultipartFile file, String projectId) {
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setProjectId(projectId);
return saveFile(file, fileMetadata);
}
public List<FileMetadata> getProjectFiles(String projectId, QueryProjectFileRequest request) {
@ -280,16 +263,23 @@ public class FileMetadataService {
if (CollectionUtils.isEmpty(files)) {
return fileMetadata;
}
fileMetadata = fileMetadataMapper.selectByPrimaryKey(fileMetadata.getId());
if (fileMetadata == null) {
MSException.throwException("数据已经被删除!");
}
fileMetadata.setSize(files.get(0).getSize());
String fileType = MetadataUtils.getFileType(files.get(0).getOriginalFilename());
fileMetadata.setType(fileType);
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
fileMetadata.setStorage(StorageConstants.LOCAL.name());
}
// 上传文件
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
fileManagerService.coverFile(files.get(0), request);
this.update(fileMetadata);
// 更新关系数据
fileMetadata.setUpdateTime(System.currentTimeMillis());
fileMetadata.setUpdateUser(SessionUtils.getUserId());
fileMetadataMapper.updateByPrimaryKeySelective(fileMetadata);
return fileMetadata;
}
@ -325,23 +315,25 @@ public class FileMetadataService {
}
public void dumpFile(DumpFileRequest request, List<MultipartFile> files) {
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setProjectId(request.getProjectId());
fileMetadata.setModuleId(request.getModuleId());
if (CollectionUtils.isEmpty(files)) {
// 文件已经存储过了
String path = FileUtils.BODY_FILE_DIR + '/' + request.getResourceId() + '/' + request.getFileName();
String path = StringUtils.join(FileUtils.BODY_FILE_DIR, File.separator, request.getResourceId(), File.separator, request.getFileName());
if (request.isCsv()) {
path = StringUtils.join(FileUtils.BODY_FILE_DIR, File.separator, request.getResourceId(), "_", request.getFileName());
}
File file = new File(path);
if (!file.exists()) {
MSException.throwException("文件不存在!");
}
MultipartFile multipartFile = fileManagerService.getMultipartFile(file);
if (multipartFile != null) {
this.create(fileMetadata, new ArrayList<>() {{
this.add(multipartFile);
}});
if (request.isCsv()) {
this.saveFile(file, request.getFileName());
} else {
this.saveFile(file);
}
} else {
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setProjectId(request.getProjectId());
fileMetadata.setModuleId(request.getModuleId());
this.create(fileMetadata, files);
}
}
@ -371,4 +363,72 @@ public class FileMetadataService {
}
return null;
}
public boolean isFileExits(String fileId) {
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andIdEqualTo(fileId);
long fileCount = fileMetadataMapper.countByExample(example);
if (fileCount > 0) {
return true;
} else {
return false;
}
}
public FileMetadata saveFile(File file) {
if (file.exists()) {
byte[] bytes = FileUtils.fileToByte(file);
this.saveFile(bytes, file.getName(), file.length());
}
return null;
}
public void saveFile(File file, String name) {
if (file.exists()) {
byte[] bytes = FileUtils.fileToByte(file);
this.saveFile(bytes, name, file.length());
}
}
public FileMetadata saveFile(byte[] fileByte, String fileName, Long fileSize) {
final FileMetadata fileMetadata = new FileMetadata();
this.initBase(fileMetadata);
fileMetadata.setName(fileName);
fileMetadata.setSize(fileSize);
String fileType = MetadataUtils.getFileType(fileName);
fileMetadata.setType(fileType);
checkName(fileMetadata);
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
String path = fileManagerService.upload(fileByte, request);
fileMetadata.setPath(path);
fileMetadataMapper.insert(fileMetadata);
return fileMetadata;
}
private void initBase(FileMetadata fileMetadata) {
if (fileMetadata == null) {
fileMetadata = new FileMetadata();
}
if (StringUtils.isEmpty(fileMetadata.getId())) {
fileMetadata.setId(UUID.randomUUID().toString());
}
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
fileMetadata.setStorage(StorageConstants.LOCAL.name());
}
if (StringUtils.isEmpty(fileMetadata.getProjectId())) {
fileMetadata.setProjectId(SessionUtils.getCurrentProjectId());
}
if (StringUtils.isEmpty(fileMetadata.getModuleId())) {
fileMetadata.setModuleId(fileModuleService.getDefaultNode(fileMetadata.getProjectId()).getId());
}
fileMetadata.setCreateTime(System.currentTimeMillis());
fileMetadata.setUpdateTime(System.currentTimeMillis());
if (StringUtils.isEmpty(fileMetadata.getCreateUser())) {
fileMetadata.setCreateUser(SessionUtils.getUserId());
}
if (StringUtils.isEmpty(fileMetadata.getUpdateUser())) {
fileMetadata.setUpdateUser(SessionUtils.getUserId());
}
}
}

View File

@ -8,4 +8,5 @@ public class DumpFileRequest {
private String moduleId;
private String fileName;
private String projectId;
private boolean isCsv;
}

View File

@ -32,6 +32,7 @@ import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.performance.PerformanceReference;
import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.performance.base.GranularityData;
import io.metersphere.performance.base.VumProcessedStatus;
import io.metersphere.performance.dto.LoadModuleDTO;
@ -41,7 +42,6 @@ import io.metersphere.performance.engine.Engine;
import io.metersphere.performance.engine.EngineFactory;
import io.metersphere.performance.request.*;
import io.metersphere.service.ApiPerformanceService;
import io.metersphere.service.FileService;
import io.metersphere.service.QuotaService;
import io.metersphere.service.ScheduleService;
import io.metersphere.track.request.testplan.LoadCaseRequest;
@ -55,7 +55,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.aspectj.util.FileUtil;
import org.mybatis.spring.SqlSessionUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
@ -66,7 +65,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Socket;
@ -89,7 +87,7 @@ public class PerformanceTestService {
@Resource
private LoadTestFileMapper loadTestFileMapper;
@Resource
private FileService fileService;
private FileMetadataService fileMetadataService;
@Resource
private LoadTestReportMapper loadTestReportMapper;
@Resource
@ -219,7 +217,7 @@ public class PerformanceTestService {
}
private boolean loadTestFileExsits(String testId, String metaFileId) {
boolean fileExsits = fileService.isFileExsits(metaFileId);
boolean fileExsits = fileMetadataService.isFileExits(metaFileId);
LoadTestFileExample example = new LoadTestFileExample();
example.createCriteria().andTestIdEqualTo(testId).andFileIdEqualTo(metaFileId);
long loadTestFiles = loadTestFileMapper.countByExample(example);
@ -235,7 +233,7 @@ public class PerformanceTestService {
if (files != null) {
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);
FileMetadata fileMetadata = fileService.saveFile(file, loadTest.getProjectId());
FileMetadata fileMetadata = fileMetadataService.saveFile(file, loadTest.getProjectId());
LoadTestFile loadTestFile = new LoadTestFile();
loadTestFile.setTestId(loadTest.getId());
loadTestFile.setFileId(fileMetadata.getId());
@ -248,7 +246,7 @@ public class PerformanceTestService {
private void importFiles(List<String> importFileIds, String testId, Map<String, Integer> fileSorts) {
for (int i = 0; i < importFileIds.size(); i++) {
String fileId = importFileIds.get(i);
FileMetadata fileMetadata = fileService.getFileMetadataById(fileId);
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
LoadTestFile loadTestFile = new LoadTestFile();
loadTestFile.setTestId(testId);
loadTestFile.setFileId(fileId);
@ -626,8 +624,8 @@ public class PerformanceTestService {
List<LoadTestExportJmx> results = new ArrayList<>();
for (FileMetadata metadata : fileMetadataList) {
if (FileType.JMX.name().equals(metadata.getType())) {
FileContent fileContent = fileService.getFileContent(metadata.getId());
results.add(new LoadTestExportJmx(metadata.getName(), new String(fileContent.getFile(), StandardCharsets.UTF_8)));
byte[] content = fileMetadataService.loadFileAsBytes(metadata.getId());
results.add(new LoadTestExportJmx(metadata.getName(), new String(content, StandardCharsets.UTF_8)));
}
}
return results;
@ -814,9 +812,9 @@ public class PerformanceTestService {
}
List<LoadTestExportJmx> results = new ArrayList<>();
fileIds.forEach(id -> {
FileMetadata fileMetadata = fileService.getFileMetadataById(id);
FileContent fileContent = fileService.getFileContent(id);
results.add(new LoadTestExportJmx(fileMetadata.getName(), new String(fileContent.getFile(), StandardCharsets.UTF_8)));
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(id);
byte[] content = fileMetadataService.loadFileAsBytes(id);
results.add(new LoadTestExportJmx(fileMetadata.getName(), new String(content, StandardCharsets.UTF_8)));
});
return results;
@ -923,7 +921,7 @@ public class PerformanceTestService {
private void saveJmxFile(String jmx, String name, String projectId, String loadTestId) {
byte[] jmxBytes = jmx.getBytes(StandardCharsets.UTF_8);
String jmxName = name + "_" + System.currentTimeMillis() + ".jmx";
FileMetadata fileMetadata = fileService.saveFile(jmxBytes, jmxName, (long) jmxBytes.length);
FileMetadata fileMetadata = fileMetadataService.saveFile(jmxBytes, jmxName, (long) jmxBytes.length);
fileMetadata.setProjectId(projectId);
saveLoadTestFile(fileMetadata, loadTestId, 0);
}
@ -952,15 +950,15 @@ public class PerformanceTestService {
example.createCriteria()
.andTestIdEqualTo(testId);
loadTestFileMapper.deleteByExample(example);
fileService.deleteFileByIds(originFileIds);
fileMetadataService.deleteBatch(originFileIds);
}
private void saveUploadFile(File file, String loadTestId, int sort) {
if (file != null) {
FileMetadata fileMetadata = null;
try {
fileMetadata = fileService.saveFile(file, FileUtil.readAsByteArray(file));
} catch (IOException e) {
fileMetadata = fileMetadataService.saveFile(file);
} catch (Exception e) {
LogUtil.error(e);
}
saveLoadTestFile(fileMetadata, loadTestId, sort);

View File

@ -89,7 +89,7 @@ export default {
if (this.file && this.file.file) {
files.push(this.file.file);
}
let request = {id: getUUID(), resourceId: this.id, moduleId: moduleId, projectId: getCurrentProjectID(), fileName: this.file.name};
let request = {id: getUUID(), csv: true, resourceId: this.file.id, moduleId: moduleId, projectId: getCurrentProjectID(), fileName: this.file.name};
this.$fileUpload("/file/metadata/dump/file", null, files, request, (response) => {
this.$success(this.$t("organization.integration.successful_operation"));
});

View File

@ -90,7 +90,7 @@ export default {
});
},
save() {
if (!this.currentKey) {
if (!this.currentKey || this.currentKey ==='') {
this.$warning(this.$t('test_track.case.input_module'));
return;
}