diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 071c89f43..952effedd 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/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; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.SimpleUploadRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.UploadRespVO; import cn.iocoder.yudao.module.infra.convert.file.FileConvert; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.service.file.FileService; @@ -45,24 +46,20 @@ public class FileController { @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) }) - public CommonResult uploadFile(@RequestParam("file") MultipartFile file, - @RequestParam("path") String path) throws Exception { - return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); - } - - @PostMapping("/simple-upload") - @ApiOperation("简单上传文件") - @ApiImplicitParams({ - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class), - }) - public CommonResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception { - SimpleUploadRespVO simpleUploadRespVO = new SimpleUploadRespVO(); - simpleUploadRespVO.setFileName(file.getOriginalFilename()); - // TODO 日期路径, 随机文件名 - String path = IdUtil.fastSimpleUUID() + "." + FileUtil.extName(file.getOriginalFilename()); + public CommonResult uploadFile(@RequestParam("file") MultipartFile file, + @RequestParam(value = "path", required = false) String path) + throws Exception { + // 如果路径没传, 系统生成随机路径 + if (StrUtil.isBlank(path)) { + // TODO 生成带日期的路径, 目前 #getFileContent 不支持 + path = IdUtil.fastSimpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); + } String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); - simpleUploadRespVO.setFileUrl(fileUrl); - return success(simpleUploadRespVO); + // 返回结果 + UploadRespVO uploadRespVO = new UploadRespVO(); + uploadRespVO.setFileName(file.getOriginalFilename()); + uploadRespVO.setFileUrl(fileUrl); + return success(uploadRespVO); } @DeleteMapping("/delete") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/UploadRespVO.java similarity index 75% rename from yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java rename to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/UploadRespVO.java index 369d9f578..3dc8b04e9 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/SimpleUploadRespVO.java +++ b/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; import lombok.Data; @Data -@ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") -public class SimpleUploadRespVO { +@ApiModel(value = "管理后台 - 上传文件 VO") +public class UploadRespVO { @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") private String fileName; diff --git a/yudao-ui-admin/src/components/ImageUpload/index.vue b/yudao-ui-admin/src/components/ImageUpload/index.vue index 0abe65a6b..d3296d68a 100644 --- a/yudao-ui-admin/src/components/ImageUpload/index.vue +++ b/yudao-ui-admin/src/components/ImageUpload/index.vue @@ -76,8 +76,9 @@ export default { dialogImageUrl: "", dialogVisible: false, hideUpload: false, + // todo /infra/file/upload 返回的都是带 host 的可访问地址, baseUrl 有点没必要 baseUrl: process.env.VUE_APP_BASE_API, - uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/simple-upload", // 上传的图片服务器地址 + uploadImgUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 上传的图片服务器地址 headers: { Authorization: "Bearer " + getToken(), }, @@ -187,7 +188,8 @@ export default { let strs = ""; separator = separator || ","; for (let i in list) { - strs += list[i].url.replace(this.baseUrl, "") + separator; + // 由于 infra-file 返回带有 host , 不需要替换 baseUrl // .replace(this.baseUrl, "") + strs += list[i].url + separator; } return strs != '' ? strs.substr(0, strs.length - 1) : ''; }