SysFileController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.xunmei.file.controller;
  2. import com.xunmei.common.core.domain.R;
  3. import com.xunmei.common.core.utils.file.FileUtils;
  4. import com.xunmei.file.service.ISysFileService;
  5. import com.xunmei.file.utils.DesUtil;
  6. import com.xunmei.system.api.domain.SysFile;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.web.bind.annotation.*;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.util.Map;
  15. /**
  16. * 文件请求处理
  17. *
  18. * @author xunmei
  19. */
  20. @RestController
  21. @RequestMapping("/file")
  22. public class SysFileController {
  23. private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
  24. @Autowired
  25. private ISysFileService sysFileService;
  26. /**
  27. * 路径加密秘钥
  28. */
  29. @Value("${file.secretKey}")
  30. public String secretKey;
  31. @Value("${file.domain}")
  32. public String networkAdd;
  33. @Value("${file.prefix}")
  34. public String prefix;
  35. private static final String FILE_SUFFIX = "FILE_CODE_";
  36. /**
  37. * 文件上传请求
  38. */
  39. @PostMapping("/upload")
  40. public R<SysFile> upload(MultipartFile file) {
  41. try {
  42. // 上传并返回访问地址
  43. String url = sysFileService.uploadFile(file);
  44. SysFile sysFile = new SysFile();
  45. sysFile.setName(FileUtils.getName(url));
  46. String code = DesUtil.encrypt(url, secretKey);
  47. url = prefix + url;
  48. sysFile.setUrl(url);
  49. sysFile.setCode(code);
  50. return R.ok(sysFile);
  51. } catch (Exception e) {
  52. log.error("上传文件失败", e);
  53. return R.fail(e.getMessage());
  54. }
  55. }
  56. @PostMapping("/uploadFile")
  57. public R<SysFile> uploadFile(MultipartFile file, String busType) {
  58. try {
  59. // 上传并返回访问地址
  60. String url = sysFileService.uploadFile(file, busType);
  61. SysFile sysFile = new SysFile();
  62. sysFile.setName(FileUtils.getName(url));
  63. String code = DesUtil.encrypt(url, secretKey);
  64. url = prefix + url;
  65. sysFile.setUrl(url);
  66. sysFile.setCode(code);
  67. return R.ok(sysFile);
  68. } catch (Exception e) {
  69. log.error("上传文件失败", e);
  70. return R.fail(e.getMessage());
  71. }
  72. }
  73. @PostMapping("/downloadFile")
  74. public void downloadFile(HttpServletResponse response, @RequestBody SysFile sysFile) {
  75. try {
  76. String filePath = DesUtil.decode(sysFile.getCode(), secretKey);
  77. sysFileService.downloadFile(response, filePath);
  78. } catch (Exception e) {
  79. log.error("下载文件失败", e);
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. /**
  84. * 路径参数
  85. *
  86. * @param response
  87. * @param code
  88. */
  89. @GetMapping("/getFile/{code}")
  90. public void getFile(HttpServletResponse response, @PathVariable String code) {
  91. try {
  92. String filePath = DesUtil.decode(code, secretKey);
  93. sysFileService.downloadFile(response, filePath);
  94. } catch (Exception e) {
  95. log.error("下载文件失败", e);
  96. throw new RuntimeException(e);
  97. }
  98. }
  99. @PostMapping(value = "/generateEduTrainingPdf")
  100. R<String> generateEduTrainingPdf(@RequestBody Map<String, Object> data) throws Exception {
  101. return R.ok(sysFileService.generateEduTrainingPdf(data));
  102. }
  103. }