feat(项目管理): 针对文件后缀为.unknown类型的文件做无后缀处理
This commit is contained in:
parent
3fe26e24ae
commit
55a3304800
|
@ -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<FileInformationResponse> list(FileMetadataTableRequest request) {
|
||||
List<FileInformationResponse> returnList = new ArrayList<>();
|
||||
FileMetadataUtils.transformRequestFileType(request);
|
||||
FileManagementQuery pageDTO = new FileManagementQuery(request);
|
||||
List<FileMetadata> 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<String> getFileType(String projectId, String storage) {
|
||||
return extFileMetadataMapper.selectFileTypeByProjectId(projectId, storage);
|
||||
List<String> fileTypes = extFileMetadataMapper.selectFileTypeByProjectId(projectId, storage);
|
||||
FileMetadataUtils.transformEmptyFileType(fileTypes);
|
||||
return fileTypes;
|
||||
}
|
||||
|
||||
public void changeJarFileStatus(String fileId, boolean enable, String operator) {
|
||||
|
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
这是证明文件没后缀的
|
Loading…
Reference in New Issue