From b30702bde7d8a55cc49423c225806f6795c97609 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 15 Feb 2023 18:57:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E6=89=A7=E8=A1=8C=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E6=97=B6=E9=87=8D=E5=A4=8D=E6=96=87=E4=BB=B6=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接口测试执行附件时重复文件判断逻辑处理:执行目录设置文件ID层,用于判断是否是重复文件 --- .../commons/utils/ApiFileUtil.java | 2 +- .../metersphere/utils/TemporaryFileUtil.java | 41 ++++++++++--------- .../metadata/service/FileMetadataService.java | 19 +++++---- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java index c6e3944511..cce14f38aa 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiFileUtil.java @@ -215,7 +215,7 @@ public class ApiFileUtil extends FileUtils { testElement.setProperty(JmxFileMetadataColumns.REF_FILE_UPDATE_TIME.name(), fileMetadata.getUpdateTime()); testElement.setProperty(JmxFileMetadataColumns.REF_FILE_PROJECT_ID.name(), fileMetadata.getProjectId()); } else { - path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName()); + path = temporaryFileUtil.generateFilePath(attachmentBodyFile.getProjectId(), attachmentBodyFile.getFileMetadataId(), attachmentBodyFile.getFileUpdateTime(), attachmentBodyFile.getName()); } testElement.setProperty(ElementConstants.FILENAME, path); diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/TemporaryFileUtil.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/TemporaryFileUtil.java index a6f8d5f42f..ef0e3e8992 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/TemporaryFileUtil.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/TemporaryFileUtil.java @@ -27,24 +27,24 @@ public class TemporaryFileUtil { + File.separator; } - public String generateFileDir(String folder, long updateTime) { + public String generateFileDir(String folder, String fileMetadataId, long updateTime) { if (StringUtils.isBlank(folder)) { folder = DEFAULT_FILE_FOLDER; } - if (updateTime == 0) { + if (StringUtils.isBlank(fileMetadataId)) { return fileFolder + folder + File.separator; } else { - return fileFolder + folder + File.separator + updateTime + File.separator; + String metadataIdFolder = fileFolder + folder + File.separator + fileMetadataId + File.separator; + return updateTime == 0 ? metadataIdFolder : metadataIdFolder + updateTime + File.separator; } - } - public String generateFilePath(String folder, long updateTime, String fileName) { - return generateFileDir(folder, updateTime) + fileName; + public String generateFilePath(String folder, String fileMetadataId, long updateTime, String fileName) { + return generateFileDir(folder, fileMetadataId, updateTime) + fileName; } - public File getFile(String folder, long updateTime, String fileName) { - File file = new File(generateFilePath(folder, updateTime, fileName)); + public File getFile(String folder, String fileMetadataId, long updateTime, String fileName) { + File file = new File(generateFilePath(folder, fileMetadataId, updateTime, fileName)); if (file.exists()) { return file; } else { @@ -52,26 +52,30 @@ public class TemporaryFileUtil { } } - public void saveFile(String folder, long updateTime, String fileName, byte[] fileBytes) { + public void saveFile(String folder, String fileMetadataId, long updateTime, String fileName, byte[] fileBytes) { //删除过期文件 - deleteOldFile(folder, updateTime, fileName); - this.createFile(generateFilePath(folder, updateTime, fileName), fileBytes); + deleteOldFile(folder, fileMetadataId, updateTime, fileName); + this.createFile(generateFilePath(folder, fileMetadataId, updateTime, fileName), fileBytes); } - public void saveFileByParamCheck(String folder, long updateTime, String fileName, byte[] fileBytes) { + public void saveFileByParamCheck(String folder, String fileMetadataId, long updateTime, String fileName, byte[] fileBytes) { if (fileBytes != null && StringUtils.isNotBlank(folder) && updateTime > 0 && StringUtils.isNotBlank(fileName) && fileBytes.length > 0) { //删除过期文件 - deleteOldFile(folder, updateTime, fileName); - this.createFile(generateFilePath(folder, updateTime, fileName), fileBytes); + deleteOldFile(folder, fileMetadataId, updateTime, fileName); + this.createFile(generateFilePath(folder, fileMetadataId, updateTime, fileName), fileBytes); } } //node也调用了该方法 - public void deleteOldFile(String folder, long lastUpdateTime, String deleteFileName) { + public void deleteOldFile(String folder, String fileMetadataId, long lastUpdateTime, String deleteFileName) { + if (StringUtils.isEmpty(fileMetadataId)) { + //本地文件不涉及到判断。 原因:在ms上不会将本地文件放在执行文件中;在node上本地文件再更新会存到Minio里。 + return; + } String newFileFolderName = String.valueOf(lastUpdateTime); List deleteFileList = new ArrayList<>(); - File file = new File(generateFileDir(folder, 0)); + File file = new File(generateFileDir(folder, fileMetadataId, 0)); //当前目录下存放的是以时间戳命名的文件夹,文件夹里存放着具体的文件。所以要删除这个 if (file.isDirectory()) { File[] checkFileFolders = file.listFiles(); @@ -81,9 +85,8 @@ public class TemporaryFileUtil { File[] checkFiles = checkFileFolder.listFiles(); if (checkFiles != null) { for (File checkFile : checkFiles) { - if (StringUtils.equals(checkFile.getName(), deleteFileName) - && !StringUtils.equals(checkFileFolder.getName(), newFileFolderName)) { - //文件名称相同,但是所属的时间戳文件夹与本次不相同的文件,是过期文件。 + if (!StringUtils.equals(checkFileFolder.getName(), newFileFolderName)) { + //MinIO文件名称可能会修改。所以这里只判断文件所处的时间戳文件夹是否对应 deleteFileList.add(checkFile); } } diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java index 76124402d6..01884c49b2 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/service/FileMetadataService.java @@ -325,6 +325,8 @@ public class FileMetadataService { if (StringUtils.isNotEmpty(fileMetadata.getStorage()) && StringUtils.isEmpty(fileMetadata.getResourceType())) { fileMetadata.setPath(FileUtils.getFilePath(fileMetadata)); } + //latest字段只能在git/pull时更新 + fileMetadata.setLatest(null); fileMetadataMapper.updateByPrimaryKeySelective(fileMetadata); } @@ -570,7 +572,7 @@ public class FileMetadataService { List downloadFileRequest = new ArrayList<>(); //检查是否存在已下载的文件 fileMetadataWithBLOBList.forEach(fileMetadata -> { - File file = temporaryFileUtil.getFile(fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getName()); + File file = temporaryFileUtil.getFile(fileMetadata.getProjectId(), fileMetadata.getId(), fileMetadata.getUpdateTime(), fileMetadata.getName()); if (file != null) { LoggerUtil.info("文件【" + fileMetadata.getUpdateTime() + "_" + fileMetadata.getName() + "】在执行目录【" + fileMetadata.getProjectId() + "】已找到,无需下载"); FileInfoDTO fileInfoDTO = new FileInfoDTO(fileMetadata.getId(), fileMetadata.getName(), fileMetadata.getProjectId(), fileMetadata.getUpdateTime(), fileMetadata.getStorage(), fileMetadata.getPath(), FileUtils.fileToByte(file)); @@ -583,7 +585,7 @@ public class FileMetadataService { List repositoryFileDTOList = fileManagerService.downloadFileBatch(downloadFileRequest); //将文件存储到执行文件目录中,避免多次执行时触发多次下载 if (CollectionUtils.isNotEmpty(repositoryFileDTOList)) { - repositoryFileDTOList.forEach(repositoryFile -> temporaryFileUtil.saveFileByParamCheck(repositoryFile.getProjectId(), repositoryFile.getFileLastUpdateTime(), repositoryFile.getFileName(), repositoryFile.getFileByte())); + repositoryFileDTOList.forEach(repositoryFile -> temporaryFileUtil.saveFileByParamCheck(repositoryFile.getProjectId(), repositoryFile.getId(), repositoryFile.getFileLastUpdateTime(), repositoryFile.getFileName(), repositoryFile.getFileByte())); fileInfoDTOList.addAll(repositoryFileDTOList); } return fileInfoDTOList; @@ -631,8 +633,8 @@ public class FileMetadataService { } public FileMetadata pullFromRepository(FileMetadata request) { - FileMetadata returnModel = null; FileMetadataWithBLOBs baseMetadata = fileMetadataMapper.selectByPrimaryKey(request.getId()); + FileMetadata returnModel = baseMetadata; if (StringUtils.equals(baseMetadata.getStorage(), StorageConstants.GIT.name()) && StringUtils.isNotEmpty(baseMetadata.getAttachInfo())) { RemoteFileAttachInfo baseAttachInfo = JSON.parseObject(baseMetadata.getAttachInfo(), RemoteFileAttachInfo.class); FileModule fileModule = fileModuleService.get(baseMetadata.getModuleId()); @@ -646,10 +648,13 @@ public class FileMetadataService { FileMetadataWithBLOBs newMetadata = this.genOtherVersionFileMetadata(baseMetadata, thisTime, gitFileAttachInfo); fileMetadataMapper.insert(newMetadata); - baseMetadata.setUpdateTime(thisTime); - baseMetadata.setLatest(false); - baseMetadata.setUpdateUser(SessionUtils.getUserId()); - fileMetadataMapper.updateByPrimaryKeySelective(baseMetadata); + FileMetadataWithBLOBs updateOldData = new FileMetadataWithBLOBs(); + updateOldData.setLatest(Boolean.FALSE); + FileMetadataExample example = new FileMetadataExample(); + example.createCriteria().andIdEqualTo(baseMetadata.getId()); + fileMetadataMapper.updateByExampleSelective(updateOldData, example); + + returnModel = newMetadata; } } }