package com.gkhy.labRiskManage.api.controller.attachment; import com.gkhy.labRiskManage.api.controller.attachment.converter.AttachmentApiConverter; import com.gkhy.labRiskManage.api.controller.common.BaseController; import com.gkhy.labRiskManage.application.attachment.service.AttachmentAppService; import com.gkhy.labRiskManage.application.attachment.service.dto.resp.AttachmentAppRespDTO; import com.gkhy.labRiskManage.commons.domain.Result; import com.gkhy.labRiskManage.commons.enums.ResultCode; import com.gkhy.labRiskManage.domain.attachment.enums.FileProjectConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @email 1603559716@qq.com * @author: zf * @date: 2023/5/6 * @time: 14:22 */ @RestController @RequestMapping("/attachment") public class AttachmentController extends BaseController { @Autowired private AttachmentAppService attachmentAppService; @Autowired private AttachmentApiConverter converter; /** * 根据标识查询文件具体信息 * @param key * @return */ @GetMapping("/get/{key}") public Result get(@PathVariable String key) { AttachmentAppRespDTO byKey = attachmentAppService.findByKey(key); return new Result(ResultCode.OK, converter.getApiRespDTO(byKey)); } /** * 根据id获取文件 * @param id * @return */ @GetMapping("/get/{id}") public Result get(@PathVariable Long id) { AttachmentAppRespDTO byId = attachmentAppService.findById(id); return new Result(ResultCode.OK,converter.getApiRespDTO(byId)); } /** * 删除文件数据 * @param id * @return */ @DeleteMapping("/delete/{id}") public Result delete(@PathVariable Long id) { attachmentAppService.delete(id); return Result.success(); } /** * @Description 上传文件(返回文件标识) * @Param [file, module] **/ @PostMapping("/upload/key") public Result uploadReturnKey(@RequestParam("file") MultipartFile file, @RequestParam("module") String module) { Object returnStr = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.KEY,getCurrentUserId()); return new Result(ResultCode.OK,returnStr); } /** * @Description 上传文件(返回文件信息) * @Param [file, module] **/ @PostMapping("/upload/detail") public Result uploadReturnDetail(@RequestParam("file") MultipartFile file, @RequestParam("module") String module) { Object detail = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.DETAIL,getCurrentUserId()); return new Result(ResultCode.OK,detail); } /** * @Description 上传文件(返回访问路径) * @Date 2021/4/20 19:28 * @Param [file, module] **/ @PostMapping("/upload/url") public Result uploadReturnUrl(@RequestParam("file") MultipartFile file, @RequestParam("module") String module) { Object returnStr = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.URL,getCurrentUserId()); return new Result(ResultCode.OK,returnStr); } /** * @return org.springframework.http.ResponseEntity * @Description 获取下载文件流 * @Param [key] **/ @GetMapping("/downloadFile/{key}") public void download(@PathVariable String key, HttpServletResponse response) { attachmentAppService.downloadForStream(response, key); } /** * @return void * @Description 文件下载(改写) * @Param [key, response, request] **/ @RequestMapping(value = "/downloadFileByKey/{key}", method = {RequestMethod.POST, RequestMethod.GET}) public void downloadFileByKey(@PathVariable String key, HttpServletResponse response, HttpServletRequest request) { attachmentAppService.downloadByKey(response, request, key); } /** * @Description 获取下载文件路径 * @Param [key] **/ @GetMapping("/downloadUrl/{key}") public Result downLoad( @PathVariable String key) { AttachmentAppRespDTO byKey = attachmentAppService.findByKey(key); return new Result(ResultCode.OK,byKey.getFileUrl()); } }