diff --git a/fast-boot-system/src/main/java/net/maku/system/controller/SysFileUploadController.java b/fast-boot-system/src/main/java/net/maku/system/controller/SysFileUploadController.java new file mode 100644 index 0000000..2f44654 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/system/controller/SysFileUploadController.java @@ -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 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); + } +} diff --git a/fast-boot-system/src/main/java/net/maku/system/vo/SysFileUploadVO.java b/fast-boot-system/src/main/java/net/maku/system/vo/SysFileUploadVO.java new file mode 100644 index 0000000..76232d7 --- /dev/null +++ b/fast-boot-system/src/main/java/net/maku/system/vo/SysFileUploadVO.java @@ -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; +}