refactor(文件管理): 文件重构后兼容性能测试引用及生成文件
This commit is contained in:
parent
f26dede034
commit
ab2c94c527
|
@ -403,11 +403,6 @@
|
||||||
<artifactId>poi-ooxml</artifactId>
|
<artifactId>poi-ooxml</artifactId>
|
||||||
<version>${poi.version}</version>
|
<version>${poi.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -33,9 +33,9 @@ import io.metersphere.controller.request.ScheduleRequest;
|
||||||
import io.metersphere.dto.ScheduleDao;
|
import io.metersphere.dto.ScheduleDao;
|
||||||
import io.metersphere.i18n.Translator;
|
import io.metersphere.i18n.Translator;
|
||||||
import io.metersphere.job.sechedule.ApiTestJob;
|
import io.metersphere.job.sechedule.ApiTestJob;
|
||||||
|
import io.metersphere.metadata.service.FileMetadataService;
|
||||||
import io.metersphere.performance.parse.EngineSourceParserFactory;
|
import io.metersphere.performance.parse.EngineSourceParserFactory;
|
||||||
import io.metersphere.plugin.core.MsTestElement;
|
import io.metersphere.plugin.core.MsTestElement;
|
||||||
import io.metersphere.service.FileService;
|
|
||||||
import io.metersphere.service.ScheduleService;
|
import io.metersphere.service.ScheduleService;
|
||||||
import io.metersphere.track.service.TestCaseService;
|
import io.metersphere.track.service.TestCaseService;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
|
@ -67,7 +67,7 @@ public class APITestService {
|
||||||
@Resource
|
@Resource
|
||||||
private ApiTestFileMapper apiTestFileMapper;
|
private ApiTestFileMapper apiTestFileMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private FileService fileService;
|
private FileMetadataService fileMetadataService;
|
||||||
@Resource
|
@Resource
|
||||||
private JMeterService jMeterService;
|
private JMeterService jMeterService;
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -145,31 +145,6 @@ public class APITestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void copy(SaveAPITestRequest request) {
|
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) {
|
public void copyBodyFiles(String target, String source) {
|
||||||
|
@ -228,7 +203,7 @@ public class APITestService {
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
MSException.throwException(Translator.get("file_cannot_be_null"));
|
MSException.throwException(Translator.get("file_cannot_be_null"));
|
||||||
}
|
}
|
||||||
byte[] bytes = fileService.loadFileAsBytes(file.getFileId());
|
byte[] bytes = new byte[0];
|
||||||
// 解析 xml 处理 mock 数据
|
// 解析 xml 处理 mock 数据
|
||||||
bytes = JmeterDocumentParser.parse(bytes);
|
bytes = JmeterDocumentParser.parse(bytes);
|
||||||
InputStream is = new ByteArrayInputStream(bytes);
|
InputStream is = new ByteArrayInputStream(bytes);
|
||||||
|
@ -293,7 +268,7 @@ public class APITestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveFile(ApiTest apiTest, MultipartFile file) {
|
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 apiTestFile = new ApiTestFile();
|
||||||
apiTestFile.setTestId(apiTest.getId());
|
apiTestFile.setTestId(apiTest.getId());
|
||||||
apiTestFile.setFileId(fileMetadata.getId());
|
apiTestFile.setFileId(fileMetadata.getId());
|
||||||
|
@ -308,7 +283,7 @@ public class APITestService {
|
||||||
|
|
||||||
if (!CollectionUtils.isEmpty(ApiTestFiles)) {
|
if (!CollectionUtils.isEmpty(ApiTestFiles)) {
|
||||||
final List<String> fileIds = ApiTestFiles.stream().map(ApiTestFile::getFileId).collect(Collectors.toList());
|
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);
|
File file = new File(filePath);
|
||||||
if (file.exists() && file.isFile()) {
|
if (file.exists() && file.isFile()) {
|
||||||
try {
|
try {
|
||||||
FileMetadata fileMetadata = fileService.insertFileByFileName(file, FileUtil.readAsByteArray(file), projectId);
|
FileMetadata fileMetadata = fileMetadataService.saveFile(FileUtil.readAsByteArray(file), file.getName(), file.length());
|
||||||
if (fileMetadata != null) {
|
if (fileMetadata != null) {
|
||||||
fileMetadataList.add(fileMetadata);
|
fileMetadataList.add(fileMetadata);
|
||||||
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());
|
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());
|
||||||
|
|
|
@ -8,6 +8,8 @@ import java.io.IOException;
|
||||||
public interface FileRepository {
|
public interface FileRepository {
|
||||||
String saveFile(MultipartFile file, FileRequest request) throws IOException;
|
String saveFile(MultipartFile file, FileRequest request) throws IOException;
|
||||||
|
|
||||||
|
String saveFile(byte[] bytes, FileRequest request) throws IOException;
|
||||||
|
|
||||||
void delete(FileRequest request) throws Exception;
|
void delete(FileRequest request) throws Exception;
|
||||||
|
|
||||||
byte[] getFile(FileRequest request) throws Exception;
|
byte[] getFile(FileRequest request) throws Exception;
|
||||||
|
|
|
@ -2,6 +2,7 @@ package io.metersphere.metadata.repository;
|
||||||
|
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
import io.metersphere.commons.utils.FileUtils;
|
import io.metersphere.commons.utils.FileUtils;
|
||||||
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.i18n.Translator;
|
import io.metersphere.i18n.Translator;
|
||||||
import io.metersphere.metadata.vo.FileRequest;
|
import io.metersphere.metadata.vo.FileRequest;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
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())) {
|
if (multipartFile == null || request == null || StringUtils.isEmpty(request.getFileName()) || StringUtils.isEmpty(request.getProjectId())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String path = StringUtils.join(FileUtils.BODY_FILE_DIR, "/", request.getProjectId());
|
File file = createFile(request);
|
||||||
File fileDir = new File(path);
|
|
||||||
if (!fileDir.exists()) {
|
|
||||||
fileDir.mkdirs();
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = new File(StringUtils.join(path, "/", request.getFileName()));
|
|
||||||
try (InputStream in = multipartFile.getInputStream(); OutputStream out = new FileOutputStream(file)) {
|
try (InputStream in = multipartFile.getInputStream(); OutputStream out = new FileOutputStream(file)) {
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
final int MAX = 4096;
|
final int MAX = 4096;
|
||||||
|
@ -36,6 +31,18 @@ public class LocalFileRepository implements FileRepository {
|
||||||
return file.getPath();
|
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
|
@Override
|
||||||
public void delete(FileRequest request) throws Exception {
|
public void delete(FileRequest request) throws Exception {
|
||||||
String path = StringUtils.join(FileUtils.BODY_FILE_DIR + "/", request.getProjectId(), "/", request.getFileName());
|
String path = StringUtils.join(FileUtils.BODY_FILE_DIR + "/", request.getProjectId(), "/", request.getFileName());
|
||||||
|
@ -82,4 +89,14 @@ public class LocalFileRepository implements FileRepository {
|
||||||
}
|
}
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,11 @@ public class MinIOFileRepository implements FileRepository {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveFile(byte[] bytes, FileRequest request) throws IOException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(FileRequest request) throws Exception {
|
public void delete(FileRequest request) throws Exception {
|
||||||
|
|
||||||
|
|
|
@ -4,13 +4,9 @@ import io.metersphere.commons.constants.StorageConstants;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.metadata.vo.FileRequest;
|
import io.metersphere.metadata.vo.FileRequest;
|
||||||
import org.apache.commons.lang.StringUtils;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@Service
|
@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) {
|
public boolean reName(String beforeName, String name, String projectId) {
|
||||||
try {
|
try {
|
||||||
FileRequest request = new FileRequest();
|
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) {
|
public byte[] downloadFile(FileRequest request) {
|
||||||
try {
|
try {
|
||||||
return FileCenter.getRepository(request.getStorage()).getFile(request);
|
return FileCenter.getRepository(request.getStorage()).getFile(request);
|
||||||
|
|
|
@ -61,41 +61,22 @@ public class FileMetadataService {
|
||||||
for (MultipartFile file : files) {
|
for (MultipartFile file : files) {
|
||||||
QueryProjectFileRequest request = new QueryProjectFileRequest();
|
QueryProjectFileRequest request = new QueryProjectFileRequest();
|
||||||
request.setName(file.getOriginalFilename());
|
request.setName(file.getOriginalFilename());
|
||||||
if (CollectionUtils.isEmpty(this.getProjectFiles(fileMetadata.getProjectId(), request))) {
|
result.add(this.saveFile(file, fileMetadata));
|
||||||
result.add(this.saveFile(file, fileMetadata));
|
|
||||||
} else {
|
|
||||||
MSException.throwException(Translator.get("project_file_already_exists"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileMetadata saveFile(MultipartFile file, FileMetadata fileMetadata) {
|
public FileMetadata saveFile(MultipartFile file, FileMetadata fileMetadata) {
|
||||||
if (StringUtils.isEmpty(fileMetadata.getId())) {
|
this.initBase(fileMetadata);
|
||||||
fileMetadata.setId(UUID.randomUUID().toString());
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(fileMetadata.getName())) {
|
if (StringUtils.isEmpty(fileMetadata.getName())) {
|
||||||
fileMetadata.setName(file.getOriginalFilename());
|
fileMetadata.setName(file.getOriginalFilename());
|
||||||
}
|
}
|
||||||
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
|
checkName(fileMetadata);
|
||||||
fileMetadata.setStorage(StorageConstants.LOCAL.name());
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(fileMetadata.getModuleId())) {
|
|
||||||
fileMetadata.setModuleId(fileModuleService.getDefaultNode(fileMetadata.getProjectId()).getId());
|
|
||||||
}
|
|
||||||
fileMetadata.setSize(file.getSize());
|
fileMetadata.setSize(file.getSize());
|
||||||
fileMetadata.setCreateTime(System.currentTimeMillis());
|
|
||||||
fileMetadata.setUpdateTime(System.currentTimeMillis());
|
|
||||||
String fileType = MetadataUtils.getFileType(fileMetadata.getName());
|
String fileType = MetadataUtils.getFileType(fileMetadata.getName());
|
||||||
fileMetadata.setType(fileType);
|
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());
|
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
|
||||||
String path = fileManagerService.upload(file, request);
|
String path = fileManagerService.upload(file, request);
|
||||||
|
@ -110,8 +91,10 @@ public class FileMetadataService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public FileMetadata saveFile(MultipartFile file) {
|
public FileMetadata saveFile(MultipartFile file, String projectId) {
|
||||||
return saveFile(file, null);
|
FileMetadata fileMetadata = new FileMetadata();
|
||||||
|
fileMetadata.setProjectId(projectId);
|
||||||
|
return saveFile(file, fileMetadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<FileMetadata> getProjectFiles(String projectId, QueryProjectFileRequest request) {
|
public List<FileMetadata> getProjectFiles(String projectId, QueryProjectFileRequest request) {
|
||||||
|
@ -280,16 +263,23 @@ public class FileMetadataService {
|
||||||
if (CollectionUtils.isEmpty(files)) {
|
if (CollectionUtils.isEmpty(files)) {
|
||||||
return fileMetadata;
|
return fileMetadata;
|
||||||
}
|
}
|
||||||
|
fileMetadata = fileMetadataMapper.selectByPrimaryKey(fileMetadata.getId());
|
||||||
|
if (fileMetadata == null) {
|
||||||
|
MSException.throwException("数据已经被删除!");
|
||||||
|
}
|
||||||
fileMetadata.setSize(files.get(0).getSize());
|
fileMetadata.setSize(files.get(0).getSize());
|
||||||
String fileType = MetadataUtils.getFileType(files.get(0).getOriginalFilename());
|
String fileType = MetadataUtils.getFileType(files.get(0).getOriginalFilename());
|
||||||
fileMetadata.setType(fileType);
|
fileMetadata.setType(fileType);
|
||||||
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
|
if (StringUtils.isEmpty(fileMetadata.getStorage())) {
|
||||||
fileMetadata.setStorage(StorageConstants.LOCAL.name());
|
fileMetadata.setStorage(StorageConstants.LOCAL.name());
|
||||||
}
|
}
|
||||||
|
// 上传文件
|
||||||
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
|
FileRequest request = new FileRequest(fileMetadata.getProjectId(), fileMetadata.getName(), fileMetadata.getType());
|
||||||
fileManagerService.coverFile(files.get(0), request);
|
fileManagerService.coverFile(files.get(0), request);
|
||||||
|
// 更新关系数据
|
||||||
this.update(fileMetadata);
|
fileMetadata.setUpdateTime(System.currentTimeMillis());
|
||||||
|
fileMetadata.setUpdateUser(SessionUtils.getUserId());
|
||||||
|
fileMetadataMapper.updateByPrimaryKeySelective(fileMetadata);
|
||||||
return fileMetadata;
|
return fileMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,23 +315,25 @@ public class FileMetadataService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dumpFile(DumpFileRequest request, List<MultipartFile> files) {
|
public void dumpFile(DumpFileRequest request, List<MultipartFile> files) {
|
||||||
FileMetadata fileMetadata = new FileMetadata();
|
|
||||||
fileMetadata.setProjectId(request.getProjectId());
|
|
||||||
fileMetadata.setModuleId(request.getModuleId());
|
|
||||||
if (CollectionUtils.isEmpty(files)) {
|
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);
|
File file = new File(path);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
MSException.throwException("文件不存在!");
|
MSException.throwException("文件不存在!");
|
||||||
}
|
}
|
||||||
MultipartFile multipartFile = fileManagerService.getMultipartFile(file);
|
if (request.isCsv()) {
|
||||||
if (multipartFile != null) {
|
this.saveFile(file, request.getFileName());
|
||||||
this.create(fileMetadata, new ArrayList<>() {{
|
} else {
|
||||||
this.add(multipartFile);
|
this.saveFile(file);
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
FileMetadata fileMetadata = new FileMetadata();
|
||||||
|
fileMetadata.setProjectId(request.getProjectId());
|
||||||
|
fileMetadata.setModuleId(request.getModuleId());
|
||||||
this.create(fileMetadata, files);
|
this.create(fileMetadata, files);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -371,4 +363,72 @@ public class FileMetadataService {
|
||||||
}
|
}
|
||||||
return null;
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,4 +8,5 @@ public class DumpFileRequest {
|
||||||
private String moduleId;
|
private String moduleId;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
private String projectId;
|
private String projectId;
|
||||||
|
private boolean isCsv;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ import io.metersphere.log.utils.ReflexObjectUtil;
|
||||||
import io.metersphere.log.vo.DetailColumn;
|
import io.metersphere.log.vo.DetailColumn;
|
||||||
import io.metersphere.log.vo.OperatingLogDetails;
|
import io.metersphere.log.vo.OperatingLogDetails;
|
||||||
import io.metersphere.log.vo.performance.PerformanceReference;
|
import io.metersphere.log.vo.performance.PerformanceReference;
|
||||||
|
import io.metersphere.metadata.service.FileMetadataService;
|
||||||
import io.metersphere.performance.base.GranularityData;
|
import io.metersphere.performance.base.GranularityData;
|
||||||
import io.metersphere.performance.base.VumProcessedStatus;
|
import io.metersphere.performance.base.VumProcessedStatus;
|
||||||
import io.metersphere.performance.dto.LoadModuleDTO;
|
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.engine.EngineFactory;
|
||||||
import io.metersphere.performance.request.*;
|
import io.metersphere.performance.request.*;
|
||||||
import io.metersphere.service.ApiPerformanceService;
|
import io.metersphere.service.ApiPerformanceService;
|
||||||
import io.metersphere.service.FileService;
|
|
||||||
import io.metersphere.service.QuotaService;
|
import io.metersphere.service.QuotaService;
|
||||||
import io.metersphere.service.ScheduleService;
|
import io.metersphere.service.ScheduleService;
|
||||||
import io.metersphere.track.request.testplan.LoadCaseRequest;
|
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.ExecutorType;
|
||||||
import org.apache.ibatis.session.SqlSession;
|
import org.apache.ibatis.session.SqlSession;
|
||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
import org.aspectj.util.FileUtil;
|
|
||||||
import org.mybatis.spring.SqlSessionUtils;
|
import org.mybatis.spring.SqlSessionUtils;
|
||||||
import org.redisson.api.RLock;
|
import org.redisson.api.RLock;
|
||||||
import org.redisson.api.RedissonClient;
|
import org.redisson.api.RedissonClient;
|
||||||
|
@ -66,7 +65,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
@ -89,7 +87,7 @@ public class PerformanceTestService {
|
||||||
@Resource
|
@Resource
|
||||||
private LoadTestFileMapper loadTestFileMapper;
|
private LoadTestFileMapper loadTestFileMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private FileService fileService;
|
private FileMetadataService fileMetadataService;
|
||||||
@Resource
|
@Resource
|
||||||
private LoadTestReportMapper loadTestReportMapper;
|
private LoadTestReportMapper loadTestReportMapper;
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -219,7 +217,7 @@ public class PerformanceTestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean loadTestFileExsits(String testId, String metaFileId) {
|
private boolean loadTestFileExsits(String testId, String metaFileId) {
|
||||||
boolean fileExsits = fileService.isFileExsits(metaFileId);
|
boolean fileExsits = fileMetadataService.isFileExits(metaFileId);
|
||||||
LoadTestFileExample example = new LoadTestFileExample();
|
LoadTestFileExample example = new LoadTestFileExample();
|
||||||
example.createCriteria().andTestIdEqualTo(testId).andFileIdEqualTo(metaFileId);
|
example.createCriteria().andTestIdEqualTo(testId).andFileIdEqualTo(metaFileId);
|
||||||
long loadTestFiles = loadTestFileMapper.countByExample(example);
|
long loadTestFiles = loadTestFileMapper.countByExample(example);
|
||||||
|
@ -235,7 +233,7 @@ public class PerformanceTestService {
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
for (int i = 0; i < files.size(); i++) {
|
for (int i = 0; i < files.size(); i++) {
|
||||||
MultipartFile file = files.get(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 loadTestFile = new LoadTestFile();
|
||||||
loadTestFile.setTestId(loadTest.getId());
|
loadTestFile.setTestId(loadTest.getId());
|
||||||
loadTestFile.setFileId(fileMetadata.getId());
|
loadTestFile.setFileId(fileMetadata.getId());
|
||||||
|
@ -248,7 +246,7 @@ public class PerformanceTestService {
|
||||||
private void importFiles(List<String> importFileIds, String testId, Map<String, Integer> fileSorts) {
|
private void importFiles(List<String> importFileIds, String testId, Map<String, Integer> fileSorts) {
|
||||||
for (int i = 0; i < importFileIds.size(); i++) {
|
for (int i = 0; i < importFileIds.size(); i++) {
|
||||||
String fileId = importFileIds.get(i);
|
String fileId = importFileIds.get(i);
|
||||||
FileMetadata fileMetadata = fileService.getFileMetadataById(fileId);
|
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
|
||||||
LoadTestFile loadTestFile = new LoadTestFile();
|
LoadTestFile loadTestFile = new LoadTestFile();
|
||||||
loadTestFile.setTestId(testId);
|
loadTestFile.setTestId(testId);
|
||||||
loadTestFile.setFileId(fileId);
|
loadTestFile.setFileId(fileId);
|
||||||
|
@ -626,8 +624,8 @@ public class PerformanceTestService {
|
||||||
List<LoadTestExportJmx> results = new ArrayList<>();
|
List<LoadTestExportJmx> results = new ArrayList<>();
|
||||||
for (FileMetadata metadata : fileMetadataList) {
|
for (FileMetadata metadata : fileMetadataList) {
|
||||||
if (FileType.JMX.name().equals(metadata.getType())) {
|
if (FileType.JMX.name().equals(metadata.getType())) {
|
||||||
FileContent fileContent = fileService.getFileContent(metadata.getId());
|
byte[] content = fileMetadataService.loadFileAsBytes(metadata.getId());
|
||||||
results.add(new LoadTestExportJmx(metadata.getName(), new String(fileContent.getFile(), StandardCharsets.UTF_8)));
|
results.add(new LoadTestExportJmx(metadata.getName(), new String(content, StandardCharsets.UTF_8)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
|
@ -814,9 +812,9 @@ public class PerformanceTestService {
|
||||||
}
|
}
|
||||||
List<LoadTestExportJmx> results = new ArrayList<>();
|
List<LoadTestExportJmx> results = new ArrayList<>();
|
||||||
fileIds.forEach(id -> {
|
fileIds.forEach(id -> {
|
||||||
FileMetadata fileMetadata = fileService.getFileMetadataById(id);
|
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(id);
|
||||||
FileContent fileContent = fileService.getFileContent(id);
|
byte[] content = fileMetadataService.loadFileAsBytes(id);
|
||||||
results.add(new LoadTestExportJmx(fileMetadata.getName(), new String(fileContent.getFile(), StandardCharsets.UTF_8)));
|
results.add(new LoadTestExportJmx(fileMetadata.getName(), new String(content, StandardCharsets.UTF_8)));
|
||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
@ -923,7 +921,7 @@ public class PerformanceTestService {
|
||||||
private void saveJmxFile(String jmx, String name, String projectId, String loadTestId) {
|
private void saveJmxFile(String jmx, String name, String projectId, String loadTestId) {
|
||||||
byte[] jmxBytes = jmx.getBytes(StandardCharsets.UTF_8);
|
byte[] jmxBytes = jmx.getBytes(StandardCharsets.UTF_8);
|
||||||
String jmxName = name + "_" + System.currentTimeMillis() + ".jmx";
|
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);
|
fileMetadata.setProjectId(projectId);
|
||||||
saveLoadTestFile(fileMetadata, loadTestId, 0);
|
saveLoadTestFile(fileMetadata, loadTestId, 0);
|
||||||
}
|
}
|
||||||
|
@ -952,15 +950,15 @@ public class PerformanceTestService {
|
||||||
example.createCriteria()
|
example.createCriteria()
|
||||||
.andTestIdEqualTo(testId);
|
.andTestIdEqualTo(testId);
|
||||||
loadTestFileMapper.deleteByExample(example);
|
loadTestFileMapper.deleteByExample(example);
|
||||||
fileService.deleteFileByIds(originFileIds);
|
fileMetadataService.deleteBatch(originFileIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveUploadFile(File file, String loadTestId, int sort) {
|
private void saveUploadFile(File file, String loadTestId, int sort) {
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
FileMetadata fileMetadata = null;
|
FileMetadata fileMetadata = null;
|
||||||
try {
|
try {
|
||||||
fileMetadata = fileService.saveFile(file, FileUtil.readAsByteArray(file));
|
fileMetadata = fileMetadataService.saveFile(file);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
saveLoadTestFile(fileMetadata, loadTestId, sort);
|
saveLoadTestFile(fileMetadata, loadTestId, sort);
|
||||||
|
|
|
@ -89,7 +89,7 @@ export default {
|
||||||
if (this.file && this.file.file) {
|
if (this.file && this.file.file) {
|
||||||
files.push(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.$fileUpload("/file/metadata/dump/file", null, files, request, (response) => {
|
||||||
this.$success(this.$t("organization.integration.successful_operation"));
|
this.$success(this.$t("organization.integration.successful_operation"));
|
||||||
});
|
});
|
||||||
|
|
|
@ -90,7 +90,7 @@ export default {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
save() {
|
save() {
|
||||||
if (!this.currentKey) {
|
if (!this.currentKey || this.currentKey ==='') {
|
||||||
this.$warning(this.$t('test_track.case.input_module'));
|
this.$warning(this.$t('test_track.case.input_module'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue