package com.ruoyi.project.common; import com.ruoyi.basic.service.StorageBlobService; import com.ruoyi.basic.utils.FileUtil; import com.ruoyi.framework.aspectj.lang.annotation.Anonymous; import com.ruoyi.framework.web.domain.R; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import org.springframework.core.io.FileSystemResource; import org.springframework.http.ContentDisposition; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; /** * 通用请求处理 * * @author ruoyi */ @Tag(name = "通用接口") @AllArgsConstructor @RestController @RequestMapping("/common") public class CommonController { private final StorageBlobService storageBlobService; private final FileUtil fileUtil; @PostMapping({"/upload"}) @Operation(summary = "文件上传") public R upload(@RequestParam("files") List files) { return R.ok(storageBlobService.upload(files, false)); } /** * 公共文件上传 * 此接口上传的文件永久有效,慎用 */ @PostMapping({"/public/upload"}) @Operation(summary = "文件上传") public R publicUpload(@RequestParam("files") List files) { return R.ok(storageBlobService.upload(files, true)); } @GetMapping("/download/{fileName}") @Anonymous public void download(@PathVariable String fileName, @RequestParam(value = "token", required = false) String token, @RequestParam(value = "publicKey", required = false) String publicKey, HttpServletResponse response) throws Exception { File file; if (publicKey != null) { file = fileUtil.compressFile(storageBlobService.getPublicFile(fileName, publicKey)); } else { file = fileUtil.compressFile(storageBlobService.getFileByToken(fileName, token)); } String originalFileName = storageBlobService.getDownloadFileName(fileName); String encodedFileName = URLEncoder.encode(originalFileName, StandardCharsets.UTF_8.name()).replace("+", "%20"); response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + encodedFileName); response.setContentLengthLong(file.length()); Files.copy(file.toPath(), response.getOutputStream()); } @GetMapping("/preview/{fileName}") @Anonymous public ResponseEntity preview(@PathVariable String fileName, @RequestParam(value = "token", required = false) String token, @RequestParam(value = "publicKey", required = false) String publicKey) throws Exception { File file; if (publicKey != null) { file = fileUtil.compressFile(storageBlobService.getPublicFile(fileName, publicKey)); } else { file = fileUtil.compressFile(storageBlobService.getFileByToken(fileName, token)); } String contentType = Files.probeContentType(file.toPath()); ContentDisposition contentDisposition = ContentDisposition.inline() .filename(fileName, StandardCharsets.UTF_8) .build(); return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType != null ? contentType : "application/octet-stream")) .contentLength(file.length()) .header("Content-Disposition", contentDisposition.toString()) .body(new FileSystemResource(file)); } }