fix(接口场景): 修复带文件的场景转性能测试时附件文件会重复保存的缺陷

修复带文件的场景转性能测试时附件文件会重复保存的缺陷
This commit is contained in:
song-tianyang 2022-01-25 10:46:29 +08:00 committed by song-tianyang
parent a797305c68
commit bdcb549033
3 changed files with 57 additions and 7 deletions

View File

@ -495,9 +495,11 @@ public class APITestService {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
try {
FileMetadata fileMetadata = fileService.saveFile(file, FileUtil.readAsByteArray(file),projectId);
FileMetadata fileMetadata = fileService.insertFileByFileName(file, FileUtil.readAsByteArray(file), projectId);
if (fileMetadata != null) {
fileMetadataList.add(fileMetadata);
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());
}
} catch (Exception e) {
LogUtil.error(e);
}

View File

@ -9,8 +9,8 @@ import io.metersphere.commons.exception.MSException;
import io.metersphere.performance.request.QueryProjectFileRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@ -121,6 +121,51 @@ public class FileService {
return fileMetadata;
}
public FileMetadata insertFileByFileName(File file, byte[] fileByte, String projectId) {
if (StringUtils.isEmpty(file.getName())) {
return null;
}else {
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(file.getName());
List<FileMetadata> fileMetadatasInDataBase = fileMetadataMapper.selectByExample(example);
if(CollectionUtils.isEmpty(fileMetadatasInDataBase)){
final FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setId(UUID.randomUUID().toString());
fileMetadata.setName(file.getName());
fileMetadata.setSize(file.length());
fileMetadata.setProjectId(projectId);
fileMetadata.setCreateTime(System.currentTimeMillis());
fileMetadata.setUpdateTime(System.currentTimeMillis());
FileType fileType = getFileType(fileMetadata.getName());
fileMetadata.setType(fileType.name());
fileMetadataMapper.insert(fileMetadata);
FileContent fileContent = new FileContent();
fileContent.setFileId(fileMetadata.getId());
fileContent.setFile(fileByte);
fileContentMapper.insert(fileContent);
return fileMetadata;
}else {
FileMetadata fileMetadata = fileMetadatasInDataBase.get(0);
fileMetadata.setName(file.getName());
fileMetadata.setSize(file.length());
fileMetadata.setProjectId(projectId);
fileMetadata.setUpdateTime(System.currentTimeMillis());
FileType fileType = getFileType(fileMetadata.getName());
fileMetadata.setType(fileType.name());
fileMetadataMapper.updateByPrimaryKeySelective(fileMetadata);
fileContentMapper.deleteByPrimaryKey(fileMetadata.getId());
FileContent fileContent = new FileContent();
fileContent.setFileId(fileMetadata.getId());
fileContent.setFile(fileByte);
fileContentMapper.insert(fileContent);
return fileMetadata;
}
}
}
public FileMetadata saveFile(File file, byte[] fileByte, String projectId) {
final FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setId(UUID.randomUUID().toString());

View File

@ -223,4 +223,7 @@ SET latest = 1;
UPDATE load_test
SET latest = 1;
ALTER TABLE file_metadata ADD index file_name (NAME);
ALTER TABLE file_content ADD index file_id_index (file_id);