- 文件上传接口合并

- ImageUpload 组件回调 url 处理
pull/2/head
JeromeSoar 2022-04-25 12:27:53 +08:00
parent 8391898b26
commit 110dcdd5a0
3 changed files with 21 additions and 22 deletions

View File

@ -3,12 +3,13 @@ package cn.iocoder.yudao.module.infra.controller.admin.file;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil; 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.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; 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.FilePageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; 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.convert.file.FileConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.service.file.FileService; 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 = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class) @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
}) })
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, public CommonResult<UploadRespVO> uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("path") String path) throws Exception { @RequestParam(value = "path", required = false) String path)
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream()))); throws Exception {
} // 如果路径没传, 系统生成随机路径
if (StrUtil.isBlank(path)) {
@PostMapping("/simple-upload") // TODO 生成带日期的路径, 目前 #getFileContent 不支持
@ApiOperation("简单上传文件") path = IdUtil.fastSimpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
@ApiImplicitParams({ }
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
})
public CommonResult<SimpleUploadRespVO> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
SimpleUploadRespVO simpleUploadRespVO = new SimpleUploadRespVO();
simpleUploadRespVO.setFileName(file.getOriginalFilename());
// TODO 日期路径, 随机文件名
String path = IdUtil.fastSimpleUUID() + "." + FileUtil.extName(file.getOriginalFilename());
String fileUrl = fileService.createFile(path, IoUtil.readBytes(file.getInputStream())); 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") @DeleteMapping("/delete")

View File

@ -5,8 +5,8 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
@ApiModel(value = "管理后台 - 简单上传文件 VO", description = "简单上传文件, 不需要 path") @ApiModel(value = "管理后台 - 上传文件 VO")
public class SimpleUploadRespVO { public class UploadRespVO {
@ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg") @ApiModelProperty(value = "文件名", required = true, example = "yudao.jpg")
private String fileName; private String fileName;

View File

@ -76,8 +76,9 @@ export default {
dialogImageUrl: "", dialogImageUrl: "",
dialogVisible: false, dialogVisible: false,
hideUpload: false, hideUpload: false,
// todo /infra/file/upload host 访, baseUrl
baseUrl: process.env.VUE_APP_BASE_API, 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: { headers: {
Authorization: "Bearer " + getToken(), Authorization: "Bearer " + getToken(),
}, },
@ -187,7 +188,8 @@ export default {
let strs = ""; let strs = "";
separator = separator || ","; separator = separator || ",";
for (let i in list) { 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) : ''; return strs != '' ? strs.substr(0, strs.length - 1) : '';
} }