Bladeren bron

core-ai 文档解析限制文件后缀名

deason 1 maand geleden
bovenliggende
commit
898f663f09

+ 3 - 18
core-ai/src/main/java/com/qmth/boot/core/ai/model/ocr/ImageType.java

@@ -1,5 +1,7 @@
 package com.qmth.boot.core.ai.model.ocr;
 
+import com.qmth.boot.tools.io.FileUtils;
+
 /**
  * 图片类型
  */
@@ -22,11 +24,7 @@ public enum ImageType {
     }
 
     public static ImageType find(String fileName) {
-        String imgSuffix = getFileSuffix(fileName);
-        if (imgSuffix == null) {
-            return null;
-        }
-
+        String imgSuffix = FileUtils.getFileSuffix(fileName);
         switch (imgSuffix) {
             case ".jpg":
             case ".jpeg":
@@ -39,17 +37,4 @@ public enum ImageType {
         return null;
     }
 
-    /**
-     * 获取文件后缀名(包含".")
-     */
-    public static String getFileSuffix(String fileName) {
-        if (fileName != null) {
-            int index = fileName.lastIndexOf(".");
-            if (index > -1) {
-                return fileName.substring(index).toLowerCase();
-            }
-        }
-        return null;
-    }
-
 }

+ 35 - 0
tools-common/src/main/java/com/qmth/boot/tools/io/FileUtils.java

@@ -0,0 +1,35 @@
+package com.qmth.boot.tools.io;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class FileUtils {
+
+    /**
+     * 获取文件后缀名(包含".")
+     */
+    public static String getFileSuffix(String fileName) {
+        if (fileName != null) {
+            int index = fileName.lastIndexOf(".");
+            if (index > -1) {
+                return fileName.substring(index).toLowerCase();
+            }
+        }
+
+        return "";
+    }
+
+    /**
+     * 匹配文件后缀名
+     *
+     * @param reg        示例:(.jpeg|.jpg|.png)$
+     * @param fileSuffix 文件后缀名
+     * @return
+     */
+    public static boolean matchSuffix(String reg, String fileSuffix) {
+        Pattern pattern = Pattern.compile(reg);
+        Matcher matcher = pattern.matcher(fileSuffix);
+        return matcher.find();
+    }
+
+}