Commit 110dcdd5a0dc008b8288f2b8d6d220a3aa28e178
1 parent
8391898b
- 文件上传接口合并
- ImageUpload 组件回调 url 处理
Showing
3 changed files
with
21 additions
and
22 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
| @@ -3,12 +3,13 @@ package cn.iocoder.yudao.module.infra.controller.admin.file; | @@ -3,12 +3,13 @@ package cn.iocoder.yudao.module.infra.controller.admin.file; | ||
| 3 | import cn.hutool.core.io.FileUtil; | 3 | import cn.hutool.core.io.FileUtil; |
| 4 | import cn.hutool.core.io.IoUtil; | 4 | import cn.hutool.core.io.IoUtil; |
| 5 | import cn.hutool.core.util.IdUtil; | 5 | import cn.hutool.core.util.IdUtil; |
| 6 | +import cn.hutool.core.util.StrUtil; | ||
| 6 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 7 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 9 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 9 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; | 10 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
| 10 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; | 11 | import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; |
| 11 | -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.SimpleUploadRespVO; | 12 | +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.UploadRespVO; |
| 12 | import cn.iocoder.yudao.module.infra.convert.file.FileConvert; | 13 | import cn.iocoder.yudao.module.infra.convert.file.FileConvert; |
| 13 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; | 14 | import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; |
| 14 | import cn.iocoder.yudao.module.infra.service.file.FileService; | 15 | import cn.iocoder.yudao.module.infra.service.file.FileService; |
| @@ -45,24 +46,20 @@ public class FileController { | @@ -45,24 +46,20 @@ public class FileController { | ||
| 45 | @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), | 46 | @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), |
| 46 | @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) | 47 | @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) |
| 47 | }) | 48 | }) |
| 48 | - public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, | ||
| 49 | - @RequestParam("path") String path) throws Exception { | ||
| 50 | - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - @PostMapping("/simple-upload") | ||
| 54 | - @ApiOperation("简单上传文件") | ||
| 55 | - @ApiImplicitParams({ | ||
| 56 | - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), | ||
| 57 | - }) | ||
| 58 | - public CommonResult<SimpleUploadRespVO> uploadFile(@RequestParam("file") MultipartFile file) throws Exception { | ||
| 59 | - SimpleUploadRespVO simpleUploadRespVO = new SimpleUploadRespVO(); | ||
| 60 | - simpleUploadRespVO.setFileName(file.getOriginalFilename()); | ||
| 61 | - // TODO 日期路径, 随机文件名 | ||
| 62 | - String path = IdUtil.fastSimpleUUID() + "." + FileUtil.extName(file.getOriginalFilename()); | 49 | + public CommonResult<UploadRespVO> uploadFile(@RequestParam("file") MultipartFile file, |
| 50 | + @RequestParam(value = "path", required = false) String path) | ||
| 51 | + throws Exception { | ||
| 52 | + // 如果路径没传, 系统生成随机路径 | ||
| 53 | + if (StrUtil.isBlank(path)) { | ||
| 54 | + // TODO 生成带日期的路径, 目前 #getFileContent 不支持 | ||
| 55 | + path = IdUtil.fastSimpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); | ||
| 56 | + } | ||
| 63 | String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); | 57 | String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); |
| 64 | - simpleUploadRespVO.setFileUrl(fileUrl); | ||
| 65 | - return success(simpleUploadRespVO); | 58 | + // 返回结果 |
| 59 | + UploadRespVO uploadRespVO = new UploadRespVO(); | ||
| 60 | + uploadRespVO.setFileName(file.getOriginalFilename()); | ||
| 61 | + uploadRespVO.setFileUrl(fileUrl); | ||
| 62 | + return success(uploadRespVO); | ||
| 66 | } | 63 | } |
| 67 | 64 | ||
| 68 | @DeleteMapping("/delete") | 65 | @DeleteMapping("/delete") |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java renamed to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/UploadRespVO.java
| @@ -5,8 +5,8 @@ import io.swagger.annotations.ApiModelProperty; | @@ -5,8 +5,8 @@ import io.swagger.annotations.ApiModelProperty; | ||
| 5 | import lombok.Data; | 5 | import lombok.Data; |
| 6 | 6 | ||
| 7 | @Data | 7 | @Data |
| 8 | -@ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") | ||
| 9 | -public class SimpleUploadRespVO { | 8 | +@ApiModel(value = "管理后台 - 上传文件 VO") |
| 9 | +public class UploadRespVO { | ||
| 10 | 10 | ||
| 11 | @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") | 11 | @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") |
| 12 | private String fileName; | 12 | private String fileName; |
yudao-ui-admin/src/components/ImageUpload/index.vue
| @@ -76,8 +76,9 @@ export default { | @@ -76,8 +76,9 @@ export default { | ||
| 76 | dialogImageUrl: "", | 76 | dialogImageUrl: "", |
| 77 | dialogVisible: false, | 77 | dialogVisible: false, |
| 78 | hideUpload: false, | 78 | hideUpload: false, |
| 79 | + // todo /infra/file/upload 返回的都是带 host 的可访问地址, baseUrl 有点没必要 | ||
| 79 | baseUrl: process.env.VUE_APP_BASE_API, | 80 | baseUrl: process.env.VUE_APP_BASE_API, |
| 80 | - uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/simple-upload", // 上传的图片服务器地址 | 81 | + uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 上传的图片服务器地址 |
| 81 | headers: { | 82 | headers: { |
| 82 | Authorization: "Bearer " + getToken(), | 83 | Authorization: "Bearer " + getToken(), |
| 83 | }, | 84 | }, |
| @@ -187,7 +188,8 @@ export default { | @@ -187,7 +188,8 @@ export default { | ||
| 187 | let strs = ""; | 188 | let strs = ""; |
| 188 | separator = separator || ","; | 189 | separator = separator || ","; |
| 189 | for (let i in list) { | 190 | for (let i in list) { |
| 190 | - strs += list[i].url.replace(this.baseUrl, "") + separator; | 191 | + // 由于 infra-file 返回带有 host , 不需要替换 baseUrl // .replace(this.baseUrl, "") |
| 192 | + strs += list[i].url + separator; | ||
| 191 | } | 193 | } |
| 192 | return strs != '' ? strs.substr(0, strs.length - 1) : ''; | 194 | return strs != '' ? strs.substr(0, strs.length - 1) : ''; |
| 193 | } | 195 | } |