package com.xunmei.file.controller; import com.xunmei.common.core.domain.R; import com.xunmei.common.core.utils.file.FileUtils; import com.xunmei.file.service.ISysFileService; import com.xunmei.file.utils.DesUtil; import com.xunmei.system.api.domain.SysFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * 文件请求处理 * * @author xunmei */ @RestController @RequestMapping("/file") public class SysFileController { private static final Logger log = LoggerFactory.getLogger(SysFileController.class); @Autowired private ISysFileService sysFileService; /** * 路径加密秘钥 */ @Value("${file.secretKey}") public String secretKey; @Value("${file.domain}") public String networkAdd; @Value("${file.prefix}") public String prefix; private static final String FILE_SUFFIX = "FILE_CODE_"; /** * 文件上传请求 */ @PostMapping("/upload") public R upload(MultipartFile file) { try { // 上传并返回访问地址 String url = sysFileService.uploadFile(file); SysFile sysFile = new SysFile(); sysFile.setName(FileUtils.getName(url)); String code = DesUtil.encrypt(url, secretKey); url = prefix + url; sysFile.setUrl(url); sysFile.setCode(code); return R.ok(sysFile); } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } @PostMapping("/uploadFile") public R uploadFile(MultipartFile file, String busType) { try { // 上传并返回访问地址 String url = sysFileService.uploadFile(file, busType); SysFile sysFile = new SysFile(); sysFile.setName(FileUtils.getName(url)); String code = DesUtil.encrypt(url, secretKey); url = prefix + url; sysFile.setUrl(url); sysFile.setCode(code); return R.ok(sysFile); } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } @PostMapping("/downloadFile") public void downloadFile(HttpServletResponse response, @RequestBody SysFile sysFile) { try { String filePath = DesUtil.decode(sysFile.getCode(), secretKey); sysFileService.downloadFile(response, filePath); } catch (Exception e) { log.error("下载文件失败", e); throw new RuntimeException(e); } } /** * 路径参数 * * @param response * @param code */ @GetMapping("/getFile/{code}") public void getFile(HttpServletResponse response, @PathVariable String code) { try { String filePath = DesUtil.decode(code, secretKey); sysFileService.downloadFile(response, filePath); } catch (Exception e) { log.error("下载文件失败", e); throw new RuntimeException(e); } } @PostMapping(value = "/generateEduTrainingPdf") R generateEduTrainingPdf(@RequestBody Map data) throws Exception { return R.ok(sysFileService.generateEduTrainingPdf(data)); } }