diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java index 4e2a15153f..3611bf9b81 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java @@ -13,6 +13,7 @@ import io.metersphere.project.mapper.FileMetadataMapper; import io.metersphere.project.mapper.FileMetadataRepositoryMapper; import io.metersphere.project.mapper.FileModuleRepositoryMapper; import io.metersphere.project.utils.FileDownloadUtils; +import io.metersphere.project.utils.FileMetadataUtils; import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.StorageType; @@ -51,6 +52,7 @@ import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class FileMetadataService { + private static final String JAR_FILE_PREFIX = "jar"; @Resource @@ -93,6 +95,7 @@ public class FileMetadataService { public List list(FileMetadataTableRequest request) { List returnList = new ArrayList<>(); + FileMetadataUtils.transformRequestFileType(request); FileManagementQuery pageDTO = new FileManagementQuery(request); List fileMetadataList = extFileMetadataMapper.selectByKeywordAndFileType(pageDTO); fileMetadataList.forEach(fileMetadata -> { @@ -129,15 +132,15 @@ public class FileMetadataService { } } - private void parseAndSetFileNameAndType(String filePath, @NotNull FileMetadata fileMetadata) { + private void parseAndSetFileNameType(String filePath, @NotNull FileMetadata fileMetadata) { String fileName = TempFileUtils.getFileNameByPath(filePath); - if (StringUtils.lastIndexOf(fileName, ".") > 0) { + if (FileMetadataUtils.isUnknownFile(fileName)) { + fileMetadata.setName(fileName); + fileMetadata.setType(StringUtils.EMPTY); + } else { //采用这种判断方式,可以避免将隐藏文件的后缀名作为文件类型 fileMetadata.setName(StringUtils.substring(fileName, 0, fileName.lastIndexOf("."))); fileMetadata.setType(StringUtils.substring(fileName, fileName.lastIndexOf(".") + 1)); - } else { - fileMetadata.setName(fileName); - fileMetadata.setType(StringUtils.EMPTY); } } @@ -146,7 +149,7 @@ public class FileMetadataService { throw new MSException(Translator.get("file.size.is.too.large")); } FileMetadata fileMetadata = new FileMetadata(); - this.parseAndSetFileNameAndType(filePath, fileMetadata); + this.parseAndSetFileNameType(filePath, fileMetadata); //如果开启了开关,检查是否是jar文件 if (enable) { this.checkEnableFile(fileMetadata.getType()); @@ -443,7 +446,7 @@ public class FileMetadataService { FileMetadata fileMetadata = new FileMetadata(); // 解析新上传的文件名和文件类型 String uploadFilePath = StringUtils.trim(uploadFile.getOriginalFilename()); - this.parseAndSetFileNameAndType(uploadFilePath, fileMetadata); + this.parseAndSetFileNameType(uploadFilePath, fileMetadata); //部分文件信息内容要和旧版本的信息内容保持一致 this.genNewFileVersionByOldFile(oldFile, fileMetadata, operator); // 存储文件 @@ -507,7 +510,9 @@ public class FileMetadataService { } public List getFileType(String projectId, String storage) { - return extFileMetadataMapper.selectFileTypeByProjectId(projectId, storage); + List fileTypes = extFileMetadataMapper.selectFileTypeByProjectId(projectId, storage); + FileMetadataUtils.transformEmptyFileType(fileTypes); + return fileTypes; } public void changeJarFileStatus(String fileId, boolean enable, String operator) { diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileMetadataUtils.java b/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileMetadataUtils.java new file mode 100644 index 0000000000..7546721ffa --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/utils/FileMetadataUtils.java @@ -0,0 +1,50 @@ +package io.metersphere.project.utils; + +import io.metersphere.project.dto.filemanagement.request.FileMetadataTableRequest; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; + +/** + * fileMetadata工具类 + */ +public class FileMetadataUtils { + + public static final String FILE_TYPE_EMPTY = "unknown"; + + private FileMetadataUtils() { + } + + /** + * 由于unknown属于位置文件类型,对应的文件类型存储是空字符串,这里进行转化 + * + * @param request 请求 + */ + public static void transformRequestFileType(FileMetadataTableRequest request) { + if (StringUtils.equals(request.getFileType(), FILE_TYPE_EMPTY)) { + request.setFileType(""); + } + } + + /** + * 将空文件类型转换为unknown + * + * @param fileTypes 文件类型 + */ + public static void transformEmptyFileType(List fileTypes) { + if (fileTypes.contains(StringUtils.EMPTY)) { + fileTypes.remove(StringUtils.EMPTY); + fileTypes.add(FILE_TYPE_EMPTY); + } + } + + /** + * 没有文件后缀 、 以"."结尾、 以"."开头的隐藏文件、以.unknown结尾的文件,都认为是未知文件 + * + * @param filePath + * @return + */ + public static boolean isUnknownFile(String filePath) { + return StringUtils.endsWith(filePath, ".") || StringUtils.endsWithIgnoreCase(filePath, ".unknown") || filePath.indexOf(".") < 1; + } +} diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java index f6adc1d7e1..914de24285 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java @@ -13,6 +13,7 @@ import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileModuleService; import io.metersphere.project.utils.FileManagementBaseUtils; import io.metersphere.project.utils.FileManagementRequestUtils; +import io.metersphere.project.utils.FileMetadataUtils; import io.metersphere.sdk.constants.*; import io.metersphere.sdk.util.FileAssociationSourceUtil; import io.metersphere.sdk.util.JSON; @@ -479,7 +480,7 @@ public class FileManagementControllerTests extends BaseTest { returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); FILE_ID_PATH.put(returnId, filePath); - uploadedFileTypes.add(StringUtils.EMPTY); + uploadedFileTypes.add(FileMetadataUtils.FILE_TYPE_EMPTY); //检查文件类型是不是为空 FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(returnId); @@ -509,7 +510,7 @@ public class FileManagementControllerTests extends BaseTest { Assertions.assertTrue(uploadedFileTypes.contains(fileType)); } - //没后缀的文件 (同时上传到a1-a1节点) + /* 没后缀的文件和后缀是.unknown的文件 (同时上传到a1-a1节点) */ filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/noSuffixFile")).getPath(); file = new MockMultipartFile("file", "noSuffixFile", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath)); paramMap = new LinkedMultiValueMap<>(); @@ -520,6 +521,16 @@ public class FileManagementControllerTests extends BaseTest { checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); FILE_ID_PATH.put(returnId, filePath); + filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/noSuffixFile.unknown")).getPath(); + file = new MockMultipartFile("file", "noSuffixFile.unknown", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath)); + paramMap = new LinkedMultiValueMap<>(); + paramMap.add("file", file); + paramMap.add("request", JSON.toJSONString(fileUploadRequest)); + mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); + returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); + checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + FILE_ID_PATH.put(returnId, filePath); + //检查文件类型获取接口有没有获取到数据 fileTypes = this.getFileType(); Assertions.assertEquals(fileTypes.size(), uploadedFileTypes.size()); diff --git a/backend/services/project-management/src/test/resources/file/noSuffixFile.unknown b/backend/services/project-management/src/test/resources/file/noSuffixFile.unknown new file mode 100644 index 0000000000..8991f1b604 --- /dev/null +++ b/backend/services/project-management/src/test/resources/file/noSuffixFile.unknown @@ -0,0 +1 @@ +这是证明文件没后缀的 \ No newline at end of file