浏览代码

基于 tika 识别文件类型

YunaiV 2 年之前
父节点
当前提交
ed097e5ead

+ 7 - 0
yudao-dependencies/pom.xml

@@ -58,6 +58,7 @@
         <transmittable-thread-local.version>2.12.2</transmittable-thread-local.version>
         <commons-net.version>3.8.0</commons-net.version>
         <jsch.version>0.1.55</jsch.version>
+        <tika-core.version>2.4.1</tika-core.version>
         <!-- 三方云服务相关 -->
         <minio.version>8.2.2</minio.version>
         <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version>
@@ -480,6 +481,12 @@
                 <version>${easyexcel.verion}</version>
             </dependency>
 
+            <dependency>
+                <groupId>org.apache.tika</groupId>
+                <artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
+                <version>${tika-core.version}</version>
+            </dependency>
+
             <dependency>
                 <groupId>org.apache.velocity</groupId>
                 <artifactId>velocity-engine-core</artifactId>

+ 4 - 0
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java

@@ -58,4 +58,8 @@ public class FileUtils {
         return file;
     }
 
+
+    public static void main(String[] args) {
+    }
+
 }

+ 5 - 0
yudao-framework/yudao-spring-boot-starter-file/pom.xml

@@ -61,6 +61,11 @@
             <artifactId>jsch</artifactId> <!-- 解决 sftp 连接 -->
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.tika</groupId>
+            <artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
+        </dependency>
+
         <!-- 三方云服务相关 -->
         <dependency>
             <groupId>io.minio</groupId>

+ 29 - 0
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java

@@ -0,0 +1,29 @@
+package cn.iocoder.yudao.framework.file.core.utils;
+
+import com.alibaba.ttl.TransmittableThreadLocal;
+import lombok.SneakyThrows;
+import org.apache.tika.Tika;
+
+import java.io.ByteArrayInputStream;
+
+/**
+ * 文件类型 Utils
+ *
+ * @author 芋道源码
+ */
+public class FileTypeUtils {
+
+    private static final ThreadLocal<Tika> TIKA = TransmittableThreadLocal.withInitial(Tika::new);
+
+    /**
+     * 获得文件的 mineType
+     *
+     * @param data 文件内容
+     * @return mineType
+     */
+    @SneakyThrows
+    public static String getMineType(byte[] data) {
+        return TIKA.get().detect(new ByteArrayInputStream(data));
+    }
+
+}

+ 0 - 1
yudao-module-infra/yudao-module-infra-biz/pom.xml

@@ -119,7 +119,6 @@
             <groupId>cn.iocoder.boot</groupId>
             <artifactId>yudao-spring-boot-starter-file</artifactId>
         </dependency>
-
     </dependencies>
 
 </project>

+ 4 - 4
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java

@@ -1,11 +1,11 @@
 package cn.iocoder.yudao.module.infra.service.file;
 
-import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.digest.DigestUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.file.core.client.FileClient;
+import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
 import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
@@ -13,7 +13,6 @@ import lombok.SneakyThrows;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.ByteArrayInputStream;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
@@ -41,9 +40,10 @@ public class FileServiceImpl implements FileService {
     @SneakyThrows
     public String createFile(String name, String path, byte[] content) {
         // 计算默认的 path 名
-        String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
+        String type = FileTypeUtils.getMineType(content);
         if (StrUtil.isEmpty(path)) {
-            path = DigestUtil.md5Hex(content) + '.' + type;
+            path = DigestUtil.md5Hex(content)
+                    + '.' + StrUtil.subAfter(type, '/', true); // 文件的后缀
         }
         // 如果 name 为空,则使用 path 填充
         if (StrUtil.isEmpty(name)) {