Commit da0ba105033c3d51865793274f7c06644dde32a8

Authored by jiangqiang
1 parent 0ed33217

修改mimetype字段为type,修改获取文件后缀名方式,目前图片文件预览还有些问题

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FilePageReqVO.java
... ... @@ -21,8 +21,8 @@ public class FilePageReqVO extends PageParam {
21 21 @ApiModelProperty(value = "文件路径", example = "yudao", notes = "模糊匹配")
22 22 private String path;
23 23  
24   - @ApiModelProperty(value = "文件扩展名", example = "jpg", notes = "模糊匹配")
25   - private String extName;
  24 + @ApiModelProperty(value = "文件类型", example = "application/octet-stream", notes = "模糊匹配")
  25 + private String type;
26 26  
27 27 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
28 28 @ApiModelProperty(value = "开始创建时间")
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileRespVO.java
... ... @@ -22,11 +22,8 @@ public class FileRespVO {
22 22 @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
23 23 private String url;
24 24  
25   - @ApiModelProperty(value = "文件扩展名", example = "jpg")
26   - private String extName;
27   -
28 25 @ApiModelProperty(value = "文件MIME类型", example = "application/octet-stream")
29   - private String mimeType;
  26 + private String type;
30 27  
31 28 @ApiModelProperty(value = "文件大小", example = "2048", required = true)
32 29 private Integer size;
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java
... ... @@ -45,16 +45,11 @@ public class FileDO extends BaseDO {
45 45 * 访问地址
46 46 */
47 47 private String url;
48   - /**
49   - * 文件扩展名
50   - * <p>
51   - * 通过 {@link cn.hutool.core.io.FileTypeUtil#getType(InputStream)} 获取
52   - */
53   - private String extName;
  48 +
54 49 /**
55 50 * 文件的MIME类型,默认为"application/octet-stream"
56 51 */
57   - private String mimeType;
  52 + private String type;
58 53 /**
59 54 * 文件大小
60 55 */
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileMapper.java
... ... @@ -18,7 +18,7 @@ public interface FileMapper extends BaseMapperX&lt;FileDO&gt; {
18 18 default PageResult<FileDO> selectPage(FilePageReqVO reqVO) {
19 19 return selectPage(reqVO, new LambdaQueryWrapperX<FileDO>()
20 20 .likeIfPresent(FileDO::getPath, reqVO.getPath())
21   - .likeIfPresent(FileDO::getExtName, reqVO.getExtName())
  21 + .likeIfPresent(FileDO::getType, reqVO.getType())
22 22 .betweenIfPresent(FileDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
23 23 .orderByDesc(FileDO::getId));
24 24 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
... ... @@ -41,12 +41,12 @@ public class FileServiceImpl implements FileService {
41 41 @Override
42 42 @SneakyThrows
43 43 public String createFile(String name, String path, String mimeType, byte[] content) {
44   - //获取文件的真实扩展名
45   - String extName = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
46   - FileNameUtil.extName(name);
  44 + //获取文件的扩展名
  45 + String extName = FileNameUtil.extName(name);
47 46 if (StrUtil.isEmpty(path)) {
48 47 //使用sha256计算文件都唯一路径,降低碰撞概率
49   - path = DigestUtil.sha256Hex(content) + '.' + extName;
  48 + String sha256Hex = DigestUtil.sha256Hex(content);
  49 + path = StrUtil.isBlank(extName) ? sha256Hex : (sha256Hex + '.' + extName);
50 50 }
51 51 // 如果 name 为空,则使用 path 填充
52 52 if (StrUtil.isEmpty(name)) {
... ... @@ -64,8 +64,7 @@ public class FileServiceImpl implements FileService {
64 64 file.setName(name);
65 65 file.setPath(path);
66 66 file.setUrl(url);
67   - file.setExtName(extName);
68   - file.setMimeType(mimeType);
  67 + file.setType(mimeType);
69 68 file.setSize(content.length);
70 69 fileMapper.insert(file);
71 70 return url;
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java
... ... @@ -40,16 +40,11 @@ public class FileServiceTest extends BaseDbUnitTest {
40 40 // mock 数据
41 41 FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到
42 42 o.setPath("yunai");
43   - o.setExtName("jpg");
44 43 o.setCreateTime(buildTime(2021, 1, 15));
45 44 });
46 45 fileMapper.insert(dbFile);
47 46 // 测试 path 不匹配
48 47 fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("tudou")));
49   - // 测试 type 不匹配
50   - fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
51   - o.setExtName("png");
52   - }));
53 48 // 测试 createTime 不匹配
54 49 fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
55 50 o.setCreateTime(buildTime(2020, 1, 15));
... ... @@ -90,7 +85,6 @@ public class FileServiceTest extends BaseDbUnitTest {
90 85 assertEquals(10L, file.getConfigId());
91 86 assertEquals(path, file.getPath());
92 87 assertEquals(url, file.getUrl());
93   - assertEquals("jpg", file.getExtName());
94 88 assertEquals(content.length, file.getSize());
95 89 }
96 90  
... ...
yudao-ui-admin/src/views/infra/file/index.vue
... ... @@ -30,11 +30,10 @@
30 30 <el-table-column :show-overflow-tooltip="true" label="文件路径" align="center" min-width="300" prop="path" />
31 31 <el-table-column :show-overflow-tooltip="true" label="文件 URL" align="center" min-width="400" prop="url" />
32 32 <el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" />
33   - <el-table-column label="文件类型" align="center" prop="mimeType" width="210" />
34   - <el-table-column label="文件扩展名" align="center" prop="extName" width="80" />
  33 + <el-table-column label="文件类型" align="center" prop="type" width="210" />
35 34 <!-- <el-table-column label="文件内容" align="center" prop="content">-->
36 35 <!-- <template slot-scope="scope">-->
37   -<!-- <img v-if="scope.row.extName === 'jpg' || scope.row.extName === 'png' || scope.row.extName === 'gif'"-->
  36 +<!-- <img v-if="scope.row.type&&scope.row.type.indexOf('image/') === 0"-->
38 37 <!-- width="200px" :src="getFileUrl + scope.row.id">-->
39 38 <!-- <i v-else>非图片,无法预览</i>-->
40 39 <!-- </template>-->
... ... @@ -101,8 +100,7 @@ export default {
101 100 queryParams: {
102 101 pageNo: 1,
103 102 pageSize: 10,
104   - path: null,
105   - extName: null,
  103 + path: null
106 104 },
107 105 // 用户导入参数
108 106 upload: {
... ...