基于 tika 识别文件类型

This commit is contained in:
YunaiV
2022-07-08 21:26:34 +08:00
parent 2372c25e8d
commit ed097e5ead
6 changed files with 49 additions and 5 deletions

View File

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

View File

@ -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)) {