From 764138952f094d44f71a9301f2fe3172c2db924f Mon Sep 17 00:00:00 2001 From: wangjiahao <1522128093@qq.com> Date: Tue, 24 Dec 2024 18:39:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=9B=BE=E8=A1=A8):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=20#14149?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../visualization/server/FileType.java | 40 ++++++++++ .../server/StaticResourceServer.java | 80 ++++++++++++++++++- 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 core/core-backend/src/main/java/io/dataease/visualization/server/FileType.java diff --git a/core/core-backend/src/main/java/io/dataease/visualization/server/FileType.java b/core/core-backend/src/main/java/io/dataease/visualization/server/FileType.java new file mode 100644 index 0000000000..36c4ce0612 --- /dev/null +++ b/core/core-backend/src/main/java/io/dataease/visualization/server/FileType.java @@ -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; + } + +} \ No newline at end of file diff --git a/core/core-backend/src/main/java/io/dataease/visualization/server/StaticResourceServer.java b/core/core-backend/src/main/java/io/dataease/visualization/server/StaticResourceServer.java index 6fff4f87e2..f95103186d 100644 --- a/core/core-backend/src/main/java/io/dataease/visualization/server/StaticResourceServer.java +++ b/core/core-backend/src/main/java/io/dataease/visualization/server/StaticResourceServer.java @@ -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; + } + } }