refactor(图表): 优化上传图片拦截方式 #14149

This commit is contained in:
wangjiahao 2024-12-24 18:39:24 +08:00 committed by 王嘉豪
parent 40b14430ad
commit 764138952f
2 changed files with 119 additions and 1 deletions

View File

@ -0,0 +1,40 @@
package io.dataease.visualization.server;
public enum FileType {
/**
* JPEG
*/
JPEG("FFD8FF", "jpg"),
/**
* PNG
*/
PNG("89504E47", "png"),
/**
* GIF
*/
GIF("47494638", "gif");
private String value = "";
private String ext = "";
FileType(String value) {
this.value = value;
}
FileType(String value, String ext) {
this(value);
this.ext = ext;
}
public String getExt() {
return ext;
}
public String getValue() {
return value;
}
}

View File

@ -3,6 +3,7 @@ package io.dataease.visualization.server;
import io.dataease.api.visualization.StaticResourceApi;
import io.dataease.api.visualization.request.StaticResourceRequest;
import io.dataease.exception.DEException;
import io.dataease.log.DeLog;
import io.dataease.utils.FileUtils;
import io.dataease.utils.JsonUtil;
import io.dataease.utils.LogUtil;
@ -16,9 +17,14 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@ -67,7 +73,7 @@ public class StaticResourceServer implements StaticResourceApi {
return false;
}
// 判断是否为图片或SVG
return (mimeType != null && mimeType.startsWith("image/")) || isValidSVG(file);
return (isImageCheckType(file)) || isValidSVG(file);
}
public void saveFilesToServe(String staticResource) {
@ -143,4 +149,76 @@ public class StaticResourceServer implements StaticResourceApi {
return false;
}
}
/**
* @param is
* @return
* @throws IOException
* @author jiangzeyin
* @date 2016-8-17
*/
public static FileType getFileType(InputStream is) throws IOException {
byte[] src = new byte[28];
is.read(src, 0, 28);
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v).toUpperCase();
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
FileType[] fileTypes = FileType.values();
for (FileType fileType : fileTypes) {
if (stringBuilder.toString().startsWith(fileType.getValue())) {
return fileType;
}
}
return null;
}
private static Boolean isImageCheckType(MultipartFile file) {
try {
return getImageType(file.getInputStream()) != null;
} catch (Exception e) {
LogUtil.error(e.getMessage());
return false;
}
}
public static String getImageType(InputStream fileInputStream) {
byte[] b = new byte[10];
int l = -1;
try {
l = fileInputStream.read(b);
fileInputStream.close();
} catch (Exception e) {
return null;
}
if (l == 10) {
byte b0 = b[0];
byte b1 = b[1];
byte b2 = b[2];
byte b3 = b[3];
byte b6 = b[6];
byte b7 = b[7];
byte b8 = b[8];
byte b9 = b[9];
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
return "gif";
} else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
return "png";
} else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F') {
return "jpg";
} else {
return null;
}
} else {
return null;
}
}
}