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