Commit 2621f2195d1bacfbc56d143ff827bd1d717e6a8d
1 parent
dd726f79
优化代码
Showing
4 changed files
with
25 additions
and
17 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java
| 1 | 1 | package cn.iocoder.yudao.framework.common.util.io; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.io.FileTypeUtil; | |
| 3 | 4 | import cn.hutool.core.io.FileUtil; |
| 5 | +import cn.hutool.core.io.file.FileNameUtil; | |
| 4 | 6 | import cn.hutool.core.util.IdUtil; |
| 7 | +import cn.hutool.core.util.StrUtil; | |
| 8 | +import cn.hutool.crypto.digest.DigestUtil; | |
| 5 | 9 | import lombok.SneakyThrows; |
| 6 | 10 | |
| 11 | +import java.io.ByteArrayInputStream; | |
| 7 | 12 | import java.io.File; |
| 8 | 13 | |
| 9 | 14 | /** |
| ... | ... | @@ -58,8 +63,20 @@ public class FileUtils { |
| 58 | 63 | return file; |
| 59 | 64 | } |
| 60 | 65 | |
| 61 | - | |
| 62 | - public static void main(String[] args) { | |
| 66 | + /** | |
| 67 | + * @param content 文件内容 | |
| 68 | + * @param originalName 原始文件名 | |
| 69 | + * @return path,唯一不可重复 | |
| 70 | + */ | |
| 71 | + public static String generatePath(byte[] content, String originalName) { | |
| 72 | + String sha256Hex = DigestUtil.sha256Hex(content); | |
| 73 | + // 如果存在name,则优先使用name的后缀 | |
| 74 | + if (StrUtil.isNotBlank(originalName)) { | |
| 75 | + String extName = FileNameUtil.extName(originalName); | |
| 76 | + return StrUtil.isBlank(extName) ? sha256Hex : sha256Hex + "." + extName; | |
| 77 | + } else { | |
| 78 | + return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content)); | |
| 79 | + } | |
| 63 | 80 | } |
| 64 | 81 | |
| 65 | 82 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/utils/FileTypeUtils.java
| ... | ... | @@ -16,7 +16,7 @@ public class FileTypeUtils { |
| 16 | 16 | /** |
| 17 | 17 | * 获得文件的 mineType,对于doc,jar等文件会有误差 |
| 18 | 18 | * |
| 19 | - * @param data 包含文件开头几千个字节的字节数组 | |
| 19 | + * @param data 文件内容 | |
| 20 | 20 | * @return mineType 无法识别时会返回“application/octet-stream” |
| 21 | 21 | */ |
| 22 | 22 | @SneakyThrows |
| ... | ... | @@ -37,7 +37,7 @@ public class FileTypeUtils { |
| 37 | 37 | /** |
| 38 | 38 | * 在拥有文件和数据的情况下,最好使用此方法,最为准确 |
| 39 | 39 | * |
| 40 | - * @param data 包含文件开头几千个字节的字节数组 | |
| 40 | + * @param data 文件内容 | |
| 41 | 41 | * @param name 文件名 |
| 42 | 42 | * @return mineType 无法识别时会返回“application/octet-stream” |
| 43 | 43 | */ | ... | ... |
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 | -import cn.hutool.core.io.file.FileNameUtil; | |
| 3 | +import cn.hutool.core.io.FileUtil; | |
| 5 | 4 | import cn.hutool.core.lang.Assert; |
| 6 | 5 | import cn.hutool.core.util.StrUtil; |
| 7 | -import cn.hutool.crypto.digest.DigestUtil; | |
| 8 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | +import cn.iocoder.yudao.framework.common.util.io.FileUtils; | |
| 9 | 8 | import cn.iocoder.yudao.framework.file.core.client.FileClient; |
| 10 | 9 | import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; |
| 11 | 10 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| ... | ... | @@ -15,7 +14,6 @@ import lombok.SneakyThrows; |
| 15 | 14 | import org.springframework.stereotype.Service; |
| 16 | 15 | |
| 17 | 16 | import javax.annotation.Resource; |
| 18 | -import java.io.ByteArrayInputStream; | |
| 19 | 17 | |
| 20 | 18 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 21 | 19 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; |
| ... | ... | @@ -45,14 +43,7 @@ public class FileServiceImpl implements FileService { |
| 45 | 43 | // 计算默认的 path 名 |
| 46 | 44 | String type = FileTypeUtils.getMineType(content, name); |
| 47 | 45 | if (StrUtil.isEmpty(path)) { |
| 48 | - String sha256Hex = DigestUtil.sha256Hex(content); | |
| 49 | - /* 如果存在name,则优先使用name的后缀 */ | |
| 50 | - if (StrUtil.isNotBlank(name)) { | |
| 51 | - String extName = FileNameUtil.extName(name); | |
| 52 | - path = StrUtil.isBlank(extName) ? sha256Hex : sha256Hex + "." + extName; | |
| 53 | - } else { | |
| 54 | - path = sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content), name); | |
| 55 | - } | |
| 46 | + path = FileUtils.generatePath(content, name); | |
| 56 | 47 | } |
| 57 | 48 | // 如果 name 为空,则使用 path 填充 |
| 58 | 49 | if (StrUtil.isEmpty(name)) { | ... | ... |
yudao-ui-admin/src/views/infra/file/index.vue
| ... | ... | @@ -37,7 +37,7 @@ |
| 37 | 37 | :width="'100px'"></image-preview> |
| 38 | 38 | <i v-else>无法预览,点击 |
| 39 | 39 | <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" |
| 40 | - :href="getFileUrl+scope.row.configId+'/get/' + scope.row.path">下载 | |
| 40 | + :href="getFileUrl + scope.row.configId + '/get/' + scope.row.path">下载 | |
| 41 | 41 | </el-link> |
| 42 | 42 | </i> |
| 43 | 43 | </template> | ... | ... |