新增文件上传功能

This commit is contained in:
阿沐 2022-08-24 11:06:45 +08:00
parent f54b582ce8
commit 8fe006ef63
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package net.maku.system.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.Result;
import net.maku.storage.service.StorageService;
import net.maku.system.vo.SysFileUploadVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件上传
*
* @author 阿沐 babamu@126.com
*/
@RestController
@RequestMapping("sys/file")
@Tag(name = "文件上传")
@AllArgsConstructor
public class SysFileUploadController {
private final StorageService storageService;
@PostMapping("upload")
@Operation(summary = "上传")
public Result<SysFileUploadVO> upload(@RequestParam("file") MultipartFile file) throws Exception {
if (file.isEmpty()) {
return Result.error("请选择需要上传的文件");
}
// 上传路径
String path = storageService.getPath(file.getOriginalFilename());
// 上传文件
String url = storageService.upload(file.getBytes(), path);
SysFileUploadVO vo = new SysFileUploadVO();
vo.setUrl(url);
vo.setSize(file.getSize());
vo.setName(file.getOriginalFilename());
vo.setPlatform(storageService.properties.getConfig().getType().name());
return Result.ok(vo);
}
}

View File

@ -0,0 +1,29 @@
package net.maku.system.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
/**
* 文件上传
*
* @author 阿沐 babamu@126.com
*/
@Data
@Schema(description = "文件上传")
public class SysFileUploadVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "文件名称")
private String name;
@Schema(description = "文件地址")
private String url;
@Schema(description = "文件大小")
private Long size;
@Schema(description = "存储平台")
private String platform;
}