From fec84f2682d3ad5715aac3190b27d131b32ed28b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 8 Jul 2022 21:30:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8E=20tika=20=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 7 +++++ .../yudao-spring-boot-starter-file/pom.xml | 5 ++++ .../file/core/utils/FileTypeUtils.java | 29 +++++++++++++++++++ .../infra/service/file/FileServiceImpl.java | 8 ++--- 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index ed1484fdb..61da91543 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -63,6 +63,7 @@ 2.12.2 3.8.0 0.1.55 + 2.4.1 8.2.2 4.5.25 @@ -523,6 +524,12 @@ ${easyexcel.verion} + + org.apache.tika + tika-core + ${tika-core.version} + + org.apache.velocity velocity-engine-core diff --git a/yudao-framework/yudao-spring-boot-starter-file/pom.xml b/yudao-framework/yudao-spring-boot-starter-file/pom.xml index 67b05223b..905e0f7c1 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-file/pom.xml @@ -61,6 +61,11 @@ jsch + + org.apache.tika + tika-core + + io.minio diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java new file mode 100644 index 000000000..d21b4879a --- /dev/null +++ b/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 = TransmittableThreadLocal.withInitial(Tika::new); + + /** + * 获得文件的 mineType + * + * @param data 文件内容 + * @return mineType + */ + @SneakyThrows + public static String getMineType(byte[] data) { + return TIKA.get().detect(new ByteArrayInputStream(data)); + } + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index b90e92752..98af005f7 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/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)) {