diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index e56496eb39..386a0db132 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -459,7 +459,7 @@ public class APITestService { * @return * @author song tianyang */ - public JmxInfoDTO updateJmxString(String jmx,String projectId) { + public JmxInfoDTO updateJmxString(String jmx, String projectId) { jmx = this.updateJmxMessage(jmx); //获取要转化的文件 @@ -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); - fileMetadataList.add(fileMetadata); - attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName()); + 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); } @@ -623,7 +625,7 @@ public class APITestService { HashTree hashTree = runRequest.getTestElement().generateHashTree(config); String jmxString = runRequest.getTestElement().getJmx(hashTree); //将jmx处理封装为通用方法 - JmxInfoDTO dto = updateJmxString(jmxString,runRequest.getProjectId()); + JmxInfoDTO dto = updateJmxString(jmxString, runRequest.getProjectId()); dto.setName(runRequest.getName() + ".jmx"); return dto; } diff --git a/backend/src/main/java/io/metersphere/service/FileService.java b/backend/src/main/java/io/metersphere/service/FileService.java index 95957a861e..9e01561a11 100644 --- a/backend/src/main/java/io/metersphere/service/FileService.java +++ b/backend/src/main/java/io/metersphere/service/FileService.java @@ -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; @@ -44,7 +44,7 @@ public class FileService { public void setFileContent(String fileId, byte[] content) { FileContent record = new FileContent(); - record .setFile(content); + record.setFile(content); record.setFileId(fileId); fileContentMapper.updateByPrimaryKeySelective(record); } @@ -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 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()); diff --git a/backend/src/main/resources/db/migration/V106__v1.17_release.sql b/backend/src/main/resources/db/migration/V106__v1.17_release.sql index 9bcf45f88d..eb0b64ef5f 100644 --- a/backend/src/main/resources/db/migration/V106__v1.17_release.sql +++ b/backend/src/main/resources/db/migration/V106__v1.17_release.sql @@ -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); +