Commit ed097e5ead159cd3985a08112408c66ec824b4c9

Authored by YunaiV
1 parent 2372c25e

基于 tika 识别文件类型

yudao-dependencies/pom.xml
@@ -58,6 +58,7 @@ @@ -58,6 +58,7 @@
58 <transmittable-thread-local.version>2.12.2</transmittable-thread-local.version> 58 <transmittable-thread-local.version>2.12.2</transmittable-thread-local.version>
59 <commons-net.version>3.8.0</commons-net.version> 59 <commons-net.version>3.8.0</commons-net.version>
60 <jsch.version>0.1.55</jsch.version> 60 <jsch.version>0.1.55</jsch.version>
  61 + <tika-core.version>2.4.1</tika-core.version>
61 <!-- 三方云服务相关 --> 62 <!-- 三方云服务相关 -->
62 <minio.version>8.2.2</minio.version> 63 <minio.version>8.2.2</minio.version>
63 <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version> 64 <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version>
@@ -481,6 +482,12 @@ @@ -481,6 +482,12 @@
481 </dependency> 482 </dependency>
482 483
483 <dependency> 484 <dependency>
  485 + <groupId>org.apache.tika</groupId>
  486 + <artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
  487 + <version>${tika-core.version}</version>
  488 + </dependency>
  489 +
  490 + <dependency>
484 <groupId>org.apache.velocity</groupId> 491 <groupId>org.apache.velocity</groupId>
485 <artifactId>velocity-engine-core</artifactId> 492 <artifactId>velocity-engine-core</artifactId>
486 <version>${velocity.version}</version> 493 <version>${velocity.version}</version>
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java
@@ -58,4 +58,8 @@ public class FileUtils { @@ -58,4 +58,8 @@ public class FileUtils {
58 return file; 58 return file;
59 } 59 }
60 60
  61 +
  62 + public static void main(String[] args) {
  63 + }
  64 +
61 } 65 }
yudao-framework/yudao-spring-boot-starter-file/pom.xml
@@ -61,6 +61,11 @@ @@ -61,6 +61,11 @@
61 <artifactId>jsch</artifactId> <!-- 解决 sftp 连接 --> 61 <artifactId>jsch</artifactId> <!-- 解决 sftp 连接 -->
62 </dependency> 62 </dependency>
63 63
  64 + <dependency>
  65 + <groupId>org.apache.tika</groupId>
  66 + <artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
  67 + </dependency>
  68 +
64 <!-- 三方云服务相关 --> 69 <!-- 三方云服务相关 -->
65 <dependency> 70 <dependency>
66 <groupId>io.minio</groupId> 71 <groupId>io.minio</groupId>
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.file.core.utils;
  2 +
  3 +import com.alibaba.ttl.TransmittableThreadLocal;
  4 +import lombok.SneakyThrows;
  5 +import org.apache.tika.Tika;
  6 +
  7 +import java.io.ByteArrayInputStream;
  8 +
  9 +/**
  10 + * 文件类型 Utils
  11 + *
  12 + * @author 芋道源码
  13 + */
  14 +public class FileTypeUtils {
  15 +
  16 + private static final ThreadLocal<Tika> TIKA = TransmittableThreadLocal.withInitial(Tika::new);
  17 +
  18 + /**
  19 + * 获得文件的 mineType
  20 + *
  21 + * @param data 文件内容
  22 + * @return mineType
  23 + */
  24 + @SneakyThrows
  25 + public static String getMineType(byte[] data) {
  26 + return TIKA.get().detect(new ByteArrayInputStream(data));
  27 + }
  28 +
  29 +}
yudao-module-infra/yudao-module-infra-biz/pom.xml
@@ -119,7 +119,6 @@ @@ -119,7 +119,6 @@
119 <groupId>cn.iocoder.boot</groupId> 119 <groupId>cn.iocoder.boot</groupId>
120 <artifactId>yudao-spring-boot-starter-file</artifactId> 120 <artifactId>yudao-spring-boot-starter-file</artifactId>
121 </dependency> 121 </dependency>
122 -  
123 </dependencies> 122 </dependencies>
124 123
125 </project> 124 </project>
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
1 package cn.iocoder.yudao.module.infra.service.file; 1 package cn.iocoder.yudao.module.infra.service.file;
2 2
3 -import cn.hutool.core.io.FileTypeUtil;  
4 import cn.hutool.core.lang.Assert; 3 import cn.hutool.core.lang.Assert;
5 import cn.hutool.core.util.StrUtil; 4 import cn.hutool.core.util.StrUtil;
6 import cn.hutool.crypto.digest.DigestUtil; 5 import cn.hutool.crypto.digest.DigestUtil;
7 import cn.iocoder.yudao.framework.common.pojo.PageResult; 6 import cn.iocoder.yudao.framework.common.pojo.PageResult;
8 import cn.iocoder.yudao.framework.file.core.client.FileClient; 7 import cn.iocoder.yudao.framework.file.core.client.FileClient;
  8 +import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
9 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; 9 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
10 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; 10 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
11 import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; 11 import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
@@ -13,7 +13,6 @@ import lombok.SneakyThrows; @@ -13,7 +13,6 @@ import lombok.SneakyThrows;
13 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
14 14
15 import javax.annotation.Resource; 15 import javax.annotation.Resource;
16 -import java.io.ByteArrayInputStream;  
17 16
18 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 17 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
19 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; 18 import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
@@ -41,9 +40,10 @@ public class FileServiceImpl implements FileService { @@ -41,9 +40,10 @@ public class FileServiceImpl implements FileService {
41 @SneakyThrows 40 @SneakyThrows
42 public String createFile(String name, String path, byte[] content) { 41 public String createFile(String name, String path, byte[] content) {
43 // 计算默认的 path 名 42 // 计算默认的 path 名
44 - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); 43 + String type = FileTypeUtils.getMineType(content);
45 if (StrUtil.isEmpty(path)) { 44 if (StrUtil.isEmpty(path)) {
46 - path = DigestUtil.md5Hex(content) + '.' + type; 45 + path = DigestUtil.md5Hex(content)
  46 + + '.' + StrUtil.subAfter(type, '/', true); // 文件的后缀
47 } 47 }
48 // 如果 name 为空,则使用 path 填充 48 // 如果 name 为空,则使用 path 填充
49 if (StrUtil.isEmpty(name)) { 49 if (StrUtil.isEmpty(name)) {