|
| 1 | +package top.cadecode.uniboot.controller; |
| 2 | + |
| 3 | +import cn.hutool.core.io.FileUtil; |
| 4 | +import cn.hutool.extra.servlet.ServletUtil; |
| 5 | +import io.swagger.annotations.Api; |
| 6 | +import io.swagger.annotations.ApiOperation; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.web.bind.annotation.GetMapping; |
| 10 | +import org.springframework.web.bind.annotation.PostMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestPart; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | +import org.springframework.web.multipart.MultipartFile; |
| 14 | +import top.cadecode.uniboot.common.annotation.ApiFormat; |
| 15 | +import top.cadecode.uniboot.common.enums.error.FrameErrorEnum; |
| 16 | +import top.cadecode.uniboot.common.exception.UniException; |
| 17 | +import top.cadecode.uniboot.common.util.AssertUtil; |
| 18 | +import top.cadecode.uniboot.framework.manager.FileUploadManager; |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +/** |
| 26 | + * 通用API |
| 27 | + * |
| 28 | + * @author Cade Li |
| 29 | + * @since 2023/4/9 |
| 30 | + */ |
| 31 | +@ApiFormat |
| 32 | +@Slf4j |
| 33 | +@RequiredArgsConstructor |
| 34 | +@Api(tags = "通用API") |
| 35 | +@RestController |
| 36 | +public class CommonController { |
| 37 | + |
| 38 | + @ApiOperation("上传文件") |
| 39 | + @PostMapping("common/upload") |
| 40 | + public boolean upload(@RequestPart("file") MultipartFile file) { |
| 41 | + String originalFilename = file.getOriginalFilename(); |
| 42 | + boolean checked = FileUploadManager.checkAllowedExtension(originalFilename); |
| 43 | + AssertUtil.isFalse(checked, FrameErrorEnum.EXTENSION_NOT_ALLOWED, originalFilename + "不被允许"); |
| 44 | + String renamedFileName = FileUploadManager.renameByUUID(originalFilename); |
| 45 | + String targetFilePath = FileUploadManager.uploadPath() + renamedFileName; |
| 46 | + try { |
| 47 | + File targetFile = FileUtil.file(targetFilePath); |
| 48 | + FileUtil.mkParentDirs(targetFile); |
| 49 | + file.transferTo(targetFile); |
| 50 | + } catch (IOException e) { |
| 51 | + throw UniException.of(FrameErrorEnum.UPLOAD_FILE_FAIL, e, originalFilename + "上传失败"); |
| 52 | + } |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + @ApiOperation("上传多文件") |
| 57 | + @PostMapping("common/upload_files") |
| 58 | + public int uploadFiles(@RequestPart("files") MultipartFile[] files) { |
| 59 | + int res = 0; |
| 60 | + for (MultipartFile file : files) { |
| 61 | + upload(file); |
| 62 | + res++; |
| 63 | + } |
| 64 | + return res; |
| 65 | + } |
| 66 | + |
| 67 | + @ApiOperation("下载文件") |
| 68 | + @GetMapping(FileUploadManager.DEFAULT_DOWNLOAD_API) |
| 69 | + public void download(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException { |
| 70 | + boolean checked = FileUploadManager.checkAllowedExtension(fileName); |
| 71 | + AssertUtil.isFalse(checked, FrameErrorEnum.EXTENSION_NOT_ALLOWED, fileName + "不被允许"); |
| 72 | + // 写文件流 |
| 73 | + String targetFilePath = FileUploadManager.downloadPath() + fileName; |
| 74 | + boolean exist = FileUtil.exist(targetFilePath); |
| 75 | + AssertUtil.isFalse(exist, FrameErrorEnum.FILE_NOT_FOUND, fileName + "不存在"); |
| 76 | + ServletUtil.write(response, new File(targetFilePath)); |
| 77 | + } |
| 78 | + |
| 79 | + @ApiOperation("下载临时文件") |
| 80 | + @GetMapping(FileUploadManager.DEFAULT_DOWNLOAD_TEMP_API) |
| 81 | + public void downloadTemp(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException { |
| 82 | + download(request, response, fileName); |
| 83 | + String targetFilePath = FileUploadManager.downloadPath() + fileName; |
| 84 | + FileUtil.del(targetFilePath); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments