Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions base/src/main/java/com/tinyengine/it/common/enums/Enums.java
Original file line number Diff line number Diff line change
Expand Up @@ -908,4 +908,99 @@ public String getValue() {
return value;
}
}

public enum FileType {
/**
* File type zip.
*/
ZIP("application/zip"),

/**
* File type x-zip.
*/
XZIP("application/x-zip-compressed"),

/**
* File type json.
*/
JSON("application/json"),

/**
* File type text.
*/
TXT("text/plain"),

/**
* File type html.
*/
HTML("text/html"),
/**
* File type png.
*/
PNG("image/png"),

/**
* File type jpg.
*/
JPG("image/jpeg");
private final String value;

FileType(String value) {
this.value = value;
}

/**
* Gets value.
*
* @return the value
*/
public String getValue() {
return value;
}
}

public enum FileNameEnd {
/**
* File name end .zip.
*/
ZIP(".zip"),

/**
* File name end .json.
*/
JSON(".json"),

/**
* File name end .text.
*/
TXT(".txt"),

/**
* File name end .html.
*/
HTML(".html"),
/**
* File name end .png.
*/
PNG(".png"),

/**
* File name end .jpg.
*/
JPG(".jpeg");
private final String value;

FileNameEnd(String value) {
this.value = value;
}

/**
* Gets value.
*
* @return the value
*/
public String getValue() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
package com.tinyengine.it.common.utils;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.exception.ServiceException;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -59,14 +62,18 @@ public static boolean checkPathHasCrossDir(String dirOrFileName) {
* @param fileTypeMap the fileTypeMap
* @return true or false
*/
public static boolean checkFileType(MultipartFile file, Map<String, String> fileTypeMap) {
public static boolean checkFileType(MultipartFile file, Map<String, List<String>> fileTypeMap) {
if (Objects.isNull(file) || fileTypeMap.isEmpty()) {
throw new ServiceException(ExceptionEnum.CM307.getResultCode(), ExceptionEnum.CM307.getResultMsg());
}
String originalFileName = file.getOriginalFilename();
for (Map.Entry<String, String> entry : fileTypeMap.entrySet()) {
String contentType = file.getContentType();

for (Map.Entry<String, List<String>> entry : fileTypeMap.entrySet()) {
if (originalFileName.endsWith(entry.getKey())) {
return checkFileType(file, entry.getKey(), entry.getValue());
if (entry.getValue().contains(contentType)) {
return true;
}
}
}
return false;
Expand Down Expand Up @@ -177,5 +184,19 @@ private static String getFileName(String filePath) {
return file.getName();
}

/**
* Verify json file.
*
* @param file the file
*/
public static void isValidJson(MultipartFile file) {
ObjectMapper objectMapper = new ObjectMapper();
try {
// 将 MultipartFile 转换为 InputStream 并解析 JSON
objectMapper.readTree(file.getInputStream());
} catch (IOException e) {
throw new ServiceException(ExceptionEnum.CM308.getResultCode(), ExceptionEnum.CM308.getResultMsg());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.tinyengine.it.controller;

import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.enums.Enums;
import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.log.SystemControllerLog;
import com.tinyengine.it.common.utils.SecurityFileCheckUtil;
Expand Down Expand Up @@ -74,6 +75,12 @@ public Result<FileResult> bundleCreateComponent(@RequestParam MultipartFile file
return Result.failed(ExceptionEnum.CM307);
}
SecurityFileCheckUtil.validFileName(file.getOriginalFilename());
boolean checkFileType = SecurityFileCheckUtil.checkFileType(file, Enums.FileNameEnd.JSON.getValue(),
Enums.FileType.JSON.getValue());
if (!checkFileType) {
return Result.failed(ExceptionEnum.CM308);
}
SecurityFileCheckUtil.isValidJson(file);
// 返回插入和更新的条数
return componentService.readFileAndBulkCreate(file);
}
Expand All @@ -98,6 +105,12 @@ public Result<BundleResultDto> bundleSplit(@RequestParam MultipartFile file) {
return Result.failed(ExceptionEnum.CM307);
}
SecurityFileCheckUtil.validFileName(file.getOriginalFilename());
boolean checkFileType = SecurityFileCheckUtil.checkFileType(file, Enums.FileNameEnd.JSON.getValue(),
Enums.FileType.JSON.getValue());
if (!checkFileType) {
return Result.failed(ExceptionEnum.CM308);
}
SecurityFileCheckUtil.isValidJson(file);
return componentService.bundleSplit(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.tinyengine.it.controller;

import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.enums.Enums;
import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.exception.ServiceException;
import com.tinyengine.it.common.log.SystemControllerLog;
Expand Down Expand Up @@ -46,6 +47,8 @@
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -248,7 +251,14 @@ public Result<FileResult> updateI18nSingleFile(
if (file.isEmpty()) {
return Result.failed(ExceptionEnum.CM307);
}
Map<String, List<String>> fileTypeMap = new HashMap<>();
fileTypeMap.put(Enums.FileNameEnd.ZIP.getValue(), Arrays.asList(Enums.FileType.ZIP.getValue(), Enums.FileType.XZIP.getValue()));
fileTypeMap.put(Enums.FileNameEnd.JSON.getValue(), Arrays.asList(Enums.FileType.JSON.getValue()));
SecurityFileCheckUtil.validFileName(file.getOriginalFilename());
boolean checkFileType = SecurityFileCheckUtil.checkFileType(file, fileTypeMap);
if (!checkFileType) {
return Result.failed(ExceptionEnum.CM325);
}
// 返回插入和更新的条数
result = i18nEntryService.readSingleFileAndBulkCreate(file, id);
}
Expand Down Expand Up @@ -285,6 +295,11 @@ public Result<FileResult> updateI18nMultiFile(
return Result.failed(ExceptionEnum.CM307);
}
SecurityFileCheckUtil.validFileName(file.getOriginalFilename());
boolean checkFileType = SecurityFileCheckUtil.checkFileType(file, Enums.FileNameEnd.JSON.getValue(),
Enums.FileType.JSON.getValue());
if (!checkFileType) {
return Result.failed(ExceptionEnum.CM308);
}
// 返回插入和更新的条数
result = i18nEntryService.readFilesAndbulkCreate(key, file, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.exception.ServiceException;
import com.tinyengine.it.common.log.SystemServiceLog;
import com.tinyengine.it.common.utils.SecurityFileCheckUtil;
import com.tinyengine.it.common.utils.Utils;
import com.tinyengine.it.mapper.I18nEntryMapper;
import com.tinyengine.it.mapper.I18nLangMapper;
Expand Down Expand Up @@ -326,7 +327,8 @@ public Result<FileResult> readSingleFileAndBulkCreate(MultipartFile file, int ho
List<EntriesItem> entriesArr = new ArrayList<>();
String contentType = file.getContentType();

if (Objects.equals(contentType, Enums.MimeType.JSON.getValue())) {
if (Objects.equals(contentType, Enums.FileType.JSON.getValue())) {
SecurityFileCheckUtil.isValidJson(file);
Result<JsonFile> parseJsonFileStreamResult = Utils.parseJsonFileStream(file);
if (!parseJsonFileStreamResult.isSuccess()) {
return Result.failed(ExceptionEnum.CM001);
Expand Down Expand Up @@ -357,6 +359,7 @@ public Result<FileResult> readSingleFileAndBulkCreate(MultipartFile file, int ho
@SystemServiceLog(description = "readFilesAndbulkCreate 批量上传词条数据")
@Override
public Result<FileResult> readFilesAndbulkCreate(String lang, MultipartFile file, int host) throws Exception {
SecurityFileCheckUtil.isValidJson(file);
Result<JsonFile> parseJsonFileStreamResult = Utils.parseJsonFileStream(file);
// 解析 JSON 数据
if (!parseJsonFileStreamResult.isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ public Result<FileResult> readFileAndBulkCreate(MultipartFile file) {
@Override
@SystemServiceLog(description = "bundleSplit 拆分bundle.json实现方法")
public Result<BundleResultDto> bundleSplit(MultipartFile file) {
// 检验文件
boolean isFileCheck = this.checkFile(file);
if (!isFileCheck) {
return Result.failed(ExceptionEnum.CM325);
}
// 获取bundle.json数据
Result<JsonFile> result = Utils.parseJsonFileStream(file);
if (!result.isSuccess()) {
Expand Down Expand Up @@ -392,13 +387,4 @@ private List<Component> buildComponentList(BundleDto bundleDto, List<Map<String,
}
return componentList;
}
public boolean checkFile(MultipartFile file) {
Map<String, String> fileTypeMap = new HashMap<>();
fileTypeMap.put(".json", "application/json");
boolean isCheckFileType = SecurityFileCheckUtil.checkFileType(file, fileTypeMap);
if (!isCheckFileType) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.when;

import cn.hutool.core.io.IoUtil;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.dto.DeleteI18nEntry;
import com.tinyengine.it.model.dto.FileResult;
Expand All @@ -36,6 +37,7 @@
import org.mockito.MockitoAnnotations;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -130,10 +132,13 @@ void testUpdateI18nSingleFile() throws Exception {
Result<FileResult> mockData = new Result<>();
mockData.setSuccess(true);
when(i18nEntryService.readSingleFileAndBulkCreate(any(MultipartFile.class), anyInt()))
.thenReturn(mockData);
.thenReturn(mockData);
MultipartFile file = Mockito.mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn("example.json");
when(file.isEmpty()).thenReturn(false);
when(file.getContentType()).thenReturn("application/json");
when(file.getOriginalFilename()).thenReturn("originalName.json");
when(file.getName()).thenReturn("123");
when(file.getBytes()).thenReturn("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8));
when(file.getInputStream()).thenReturn(IoUtil.toStream("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8)));
HashMap<String, MultipartFile> filesMap = new HashMap<String, MultipartFile>() {{
put("filesMap", file);
}};
Expand All @@ -145,10 +150,13 @@ void testUpdateI18nSingleFile() throws Exception {
@Test
void testUpdateI18nMultiFile() throws Exception {
when(i18nEntryService.readFilesAndbulkCreate(anyString(), any(MultipartFile.class), anyInt()))
.thenReturn(new Result<FileResult>());
.thenReturn(new Result<FileResult>());
MultipartFile file = Mockito.mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn("example.json");
when(file.isEmpty()).thenReturn(false);
when(file.getContentType()).thenReturn("application/json");
when(file.getOriginalFilename()).thenReturn("originalName.json");
when(file.getName()).thenReturn("123");
when(file.getBytes()).thenReturn("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8));
when(file.getInputStream()).thenReturn(IoUtil.toStream("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8)));
HashMap<String, MultipartFile> filesMap = new HashMap<String, MultipartFile>() {{
put("filesMap", file);
}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void testReadSingleFileAndBulkCreate() throws Exception {
when(file.getOriginalFilename()).thenReturn("originalName");
when(file.getName()).thenReturn("123");
when(file.getBytes()).thenReturn("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8));
when(file.getInputStream()).thenReturn(IoUtil.toStream("test".getBytes(StandardCharsets.UTF_8)));
when(file.getInputStream()).thenReturn(IoUtil.toStream("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8)));

Result<FileResult> result = i18nEntryServiceImpl.readSingleFileAndBulkCreate(file, 0);

Expand All @@ -299,7 +299,7 @@ void testReadFilesAndbulkCreate() throws Exception {
when(file.getOriginalFilename()).thenReturn("originalName");
when(file.getName()).thenReturn("123");
when(file.getBytes()).thenReturn("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8));
when(file.getInputStream()).thenReturn(IoUtil.toStream("test".getBytes(StandardCharsets.UTF_8)));
when(file.getInputStream()).thenReturn(IoUtil.toStream("{\"name\":\"value\"}".getBytes(StandardCharsets.UTF_8)));
// file not existed
Result<FileResult> result = i18nEntryServiceImpl.readFilesAndbulkCreate("1", file, 0);
Assertions.assertNull(result.getData());
Expand Down
Loading