SYYTe/fast-boot-system/src/main/java/net/maku/system/controller/SysFileUploadController.java
2022-08-24 11:06:45 +08:00

48 lines
1.6 KiB
Java

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);
}
}