Skip to content

Commit d211902

Browse files
committed
fix: modify resource upload
1 parent 4eaee7a commit d211902

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

base/src/main/java/com/tinyengine/it/common/utils/ImageThumbnailGenerator.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.ByteArrayInputStream;
2929
import java.io.ByteArrayOutputStream;
3030
import java.io.IOException;
31+
import java.nio.charset.StandardCharsets;
3132
import java.util.Base64;
3233
import java.util.HashMap;
3334
import java.util.Map;
@@ -238,25 +239,66 @@ public static String extractContentType(String base64Data) {
238239
}
239240

240241
/**
241-
* 检查文件是否为图片类型
242+
* 图片验证
242243
*/
243244
public static boolean validateByImageIO(MultipartFile file) {
244245
try {
246+
String filename = file.getOriginalFilename();
247+
if (filename == null) {
248+
return false;
249+
}
250+
// 获取文件扩展名
251+
String extension = getFileExtension(filename).toLowerCase();
252+
253+
if ("svg".equals(extension)) {
254+
return isSvgFile(file);
255+
}
256+
245257
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
246-
return image != null; // 如果是图片,返回true
258+
return image != null;
247259
} catch (IOException e) {
248260
return false;
249261
}
250262
}
251263

252264
/**
253-
* 将MultipartFile转换为Base64字符串
265+
* SVG文件验证
266+
*/
267+
private static boolean isSvgFile(MultipartFile file) {
268+
try {
269+
byte[] bytes = file.getBytes();
270+
String content = new String(bytes, StandardCharsets.UTF_8);
271+
272+
// 简单检查:包含<svg标签和SVG命名空间
273+
return content.contains("<svg") &&
274+
(content.contains("xmlns=\"http://www.w3.org/2000/svg\"") ||
275+
content.contains("xmlns='http://www.w3.org/2000/svg'"));
276+
277+
} catch (IOException e) {
278+
return false;
279+
}
280+
}
281+
/**
282+
* 获取文件扩展名
283+
*/
284+
private static String getFileExtension(String filename) {
285+
if (filename == null || !filename.contains(".")) {
286+
return "";
287+
}
288+
return filename.substring(filename.lastIndexOf(".") + 1);
289+
}
290+
/**
291+
* 简化版Base64转换
254292
*/
255293
public static String convertToBase64(MultipartFile file) throws IOException {
256294
String mimeType = file.getContentType();
257295
byte[] fileBytes = file.getBytes();
258296
String base64 = Base64.getEncoder().encodeToString(fileBytes);
259-
297+
// 如果是SVG文件,修正MIME类型
298+
String filename = file.getOriginalFilename();
299+
if (filename != null && filename.toLowerCase().endsWith(".svg")) {
300+
mimeType = "image/svg+xml";
301+
}
260302
return "data:" + mimeType + ";base64," + base64;
261303
}
262304

0 commit comments

Comments
 (0)