1. 重构附件加密下载验证码获取流程

2. 新增简写命名字典功能
This commit is contained in:
chenbowen
2025-07-24 14:03:52 +08:00
parent b7d2cf3802
commit 9f1fad0096
5 changed files with 87 additions and 33 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@@ -51,6 +52,38 @@ public class FileController {
@Value("${yudao.kkfile:}")
private String onlinePreview;
@GetMapping("/download-url")
@Operation(summary = "获取文件下载地址", description = "根据 fileId 返回文件下载 url")
public CommonResult<FileRespVO> getDownloadUrl(@RequestParam("fileId") Long fileId) {
FileDO fileDO = fileService.getActiveFileById(fileId);
if (fileDO == null) {
return CommonResult.error(HttpStatus.NOT_FOUND.value(), "文件不存在");
}
// FileDO 转换为 FileRespVO
FileRespVO fileRespVO = BeanUtils.toBean(fileDO, FileRespVO.class);
if (StrUtil.isEmpty(onlinePreview) || StrUtil.isEmpty(fileRespVO.getUrl())) {
return CommonResult.error(HttpStatus.BAD_REQUEST.value(), "文件 URL 为空");
}
return success(fileRespVO);
}
@GetMapping("/preview-url")
@Operation(summary = "获取文件预览地址", description = "根据 fileId 返回文件预览 urlkkfile")
public CommonResult<FileRespVO> getPreviewUrl(@RequestParam("fileId") Long fileId) {
FileDO fileDO = fileService.getActiveFileById(fileId);
if (fileDO == null) {
return CommonResult.error(HttpStatus.NOT_FOUND.value(), "文件不存在");
}
// FileDO 转换为 FileRespVO
FileRespVO fileRespVO = BeanUtils.toBean(fileDO, FileRespVO.class);
if (StrUtil.isEmpty(onlinePreview) || StrUtil.isEmpty(fileRespVO.getUrl())) {
return CommonResult.error(HttpStatus.BAD_REQUEST.value(), "在线预览地址未配置或文件 URL 为空");
}
String previewUrl = onlinePreview + URLEncoder.encode(Base64.getEncoder().encodeToString(fileRespVO.getUrl().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
fileRespVO.setPreviewUrl(previewUrl);
return success(fileRespVO);
}
@PostMapping("/upload")
@Operation(summary = "上传文件", description = "模式一:后端上传文件")
@@ -135,14 +168,6 @@ public class FileController {
return success(BeanUtils.toBean(pageResult, FileRespVO.class));
}
@GetMapping("/verification-code")
@Operation(summary = "获取加密文件下载验证码 ")
public CommonResult<String> getVerificationCode(@RequestParam("fileId") Long fileId) {
Long userId = getLoginUserId();
String code = fileService.generateFileVerificationCode(fileId, userId);
return success(code);
}
@GetMapping("/verify-and-download")
@Operation(summary = "校验验证码并下载解密文件")
public void verifyAndDownload(@Valid @RequestParam Long fileId, @RequestParam String code, HttpServletResponse response) throws Exception {
@@ -157,15 +182,18 @@ public class FileController {
}
@GetMapping("/generate-download-code")
@Operation(summary = "获取下载验证码")
public CommonResult<String> preDownloadEncrypt(@RequestParam("fileId") Long fileId) {
public CommonResult<FileRespVO> preDownloadEncrypt(@RequestParam("fileId") Long fileId) {
Long userId = getLoginUserId();
FileDO activeFileById = new FileDO();
FileDO activeFileById = fileService.getActiveFileById(fileId);
if (activeFileById == null) {
return CommonResult.error(HttpStatus.NOT_FOUND.value(), "文件不存在");
}
FileRespVO fileRespVO = BeanUtils.toBean(activeFileById, FileRespVO.class);
try {
activeFileById = fileService.getActiveFileById(fileId);
fileService.generateFileVerificationCode(fileId, userId);
return CommonResult.customize(activeFileById.getName(), HttpStatus.OK.value(), "验证码已生成,请使用验证码下载文件");
return CommonResult.customize(fileRespVO, HttpStatus.OK.value(), "验证码已生成,请使用验证码下载文件");
} catch (ServiceException e) {
return CommonResult.customize(activeFileById.getName(), HttpStatus.BAD_REQUEST.value(), e.getMessage());
return CommonResult.customize(fileRespVO, HttpStatus.OK.value(), e.getMessage());
}
}
}