Commit ed097e5ead159cd3985a08112408c66ec824b4c9
1 parent
2372c25e
基于 tika 识别文件类型
Showing
6 changed files
with
49 additions
and
5 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -58,6 +58,7 @@ |
| 58 | 58 | <transmittable-thread-local.version>2.12.2</transmittable-thread-local.version> |
| 59 | 59 | <commons-net.version>3.8.0</commons-net.version> |
| 60 | 60 | <jsch.version>0.1.55</jsch.version> |
| 61 | + <tika-core.version>2.4.1</tika-core.version> | |
| 61 | 62 | <!-- 三方云服务相关 --> |
| 62 | 63 | <minio.version>8.2.2</minio.version> |
| 63 | 64 | <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version> |
| ... | ... | @@ -481,6 +482,12 @@ |
| 481 | 482 | </dependency> |
| 482 | 483 | |
| 483 | 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 | 491 | <groupId>org.apache.velocity</groupId> |
| 485 | 492 | <artifactId>velocity-engine-core</artifactId> |
| 486 | 493 | <version>${velocity.version}</version> | ... | ... |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java
yudao-framework/yudao-spring-boot-starter-file/pom.xml
| ... | ... | @@ -61,6 +61,11 @@ |
| 61 | 61 | <artifactId>jsch</artifactId> <!-- 解决 sftp 连接 --> |
| 62 | 62 | </dependency> |
| 63 | 63 | |
| 64 | + <dependency> | |
| 65 | + <groupId>org.apache.tika</groupId> | |
| 66 | + <artifactId>tika-core</artifactId> <!-- 文件类型的识别 --> | |
| 67 | + </dependency> | |
| 68 | + | |
| 64 | 69 | <!-- 三方云服务相关 --> |
| 65 | 70 | <dependency> |
| 66 | 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
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.file; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.io.FileTypeUtil; | |
| 4 | 3 | import cn.hutool.core.lang.Assert; |
| 5 | 4 | import cn.hutool.core.util.StrUtil; |
| 6 | 5 | import cn.hutool.crypto.digest.DigestUtil; |
| 7 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | 7 | import cn.iocoder.yudao.framework.file.core.client.FileClient; |
| 8 | +import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; | |
| 9 | 9 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| 10 | 10 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; |
| 11 | 11 | import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; |
| ... | ... | @@ -13,7 +13,6 @@ import lombok.SneakyThrows; |
| 13 | 13 | import org.springframework.stereotype.Service; |
| 14 | 14 | |
| 15 | 15 | import javax.annotation.Resource; |
| 16 | -import java.io.ByteArrayInputStream; | |
| 17 | 16 | |
| 18 | 17 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 19 | 18 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; |
| ... | ... | @@ -41,9 +40,10 @@ public class FileServiceImpl implements FileService { |
| 41 | 40 | @SneakyThrows |
| 42 | 41 | public String createFile(String name, String path, byte[] content) { |
| 43 | 42 | // 计算默认的 path 名 |
| 44 | - String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); | |
| 43 | + String type = FileTypeUtils.getMineType(content); | |
| 45 | 44 | if (StrUtil.isEmpty(path)) { |
| 46 | - path = DigestUtil.md5Hex(content) + '.' + type; | |
| 45 | + path = DigestUtil.md5Hex(content) | |
| 46 | + + '.' + StrUtil.subAfter(type, '/', true); // 文件的后缀 | |
| 47 | 47 | } |
| 48 | 48 | // 如果 name 为空,则使用 path 填充 |
| 49 | 49 | if (StrUtil.isEmpty(name)) { | ... | ... |