package com.dy.pipIrrGlobal.webCtrls; import com.dy.common.aop.SsoAop; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.pipIrrGlobal.dyFile.FileConstant; import com.dy.pipIrrGlobal.dyFile.FileOperate; import com.dy.pipIrrGlobal.dyFile.FileRestVo; import com.dy.pipIrrGlobal.dyFile.FileVo; import com.dy.pipIrrGlobal.pojoFi.WebFile; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.util.Date; /** * web文件上传 */ @Slf4j @RestController @RequestMapping(path="webFile") public class WebFileCtrl { @Autowired private FileOperate fileOp ; @Autowired private WebFileSv sv ; @Value("${dy.webFile.fmUrl}") private String fmUrl ; //支持的文件类型 @Value("${dy.webFile.supportedFileTypes}") private String supportedFileTypes ; /** * 上传照片图片文件 (当前只对png jpg格式图片支持缩略图) * @param file web端上传文件的post对象 * @return 返回结果 */ @PostMapping("/upPhoto") @SsoAop() //参考:https://blog.zhengru.top/posts/33486.html#%E5%8D%95%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0 public BaseResponse upPhoto(MultipartFile file) { try { if (file != null) { String[] fileNameGrp = fileOp.splitFileName(file) ; if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){ if(!fileNameGrp[1].trim().equals("")){ FileRestVo frVo = fileOp.saveFile(file, fmUrl, FileConstant.fileRequestMapping, FileConstant.filePostMapping_photo, FileConstant.NotRegionNum, fileNameGrp[1], null); String fileMainName = fileOp.getFileMainName(frVo.fileName) ; Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], (frVo.getFileWebUrl() + frVo.getFileWebPath()), fileOp.getImgFileZipPath(frVo.getFileWebUrl() + frVo.getFileWebPath()), frVo.fileWebDownloadPath + id) ; return BaseResponseUtils.buildSuccess(fvo) ; }else { return BaseResponseUtils.buildError("未得到上传文件的扩展名"); } }else { return BaseResponseUtils.buildError("未能拆分上传文件名称"); } } else { return BaseResponseUtils.buildError("未上传文件"); } } catch (Exception e) { log.error("上传照片文件异常", e); return BaseResponseUtils.buildException(e.getMessage()); } } /** * 上传录音音频文件 * @param file web端上传文件的post对象 * @return 返回结果 */ @PostMapping("/upPhone") @SsoAop() public BaseResponse upPhone(MultipartFile file) { try { if (file != null) { String[] fileNameGrp = fileOp.splitFileName(file) ; if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){ if(!fileNameGrp[1].trim().equals("")){ FileRestVo frVo = fileOp.saveFile(file, fmUrl, FileConstant.fileRequestMapping, FileConstant.filePostMapping_phone, FileConstant.NotRegionNum, fileNameGrp[1], null); String fileMainName = fileOp.getFileMainName(frVo.fileName) ; Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadPath + id) ; return BaseResponseUtils.buildSuccess(fvo) ; }else { return BaseResponseUtils.buildError("未得到上传文件的扩展名"); } }else { return BaseResponseUtils.buildError("未能拆分上传文件名称"); } } else { return BaseResponseUtils.buildError("未上传文件"); } } catch (Exception e) { log.error("上传照片文件异常", e); return BaseResponseUtils.buildException(e.getMessage()); } } /** * 上传录像视频文件 * @param file web端上传文件的post对象 * @return 返回结果 */ @PostMapping("/upVideo") @SsoAop() public BaseResponse upVideo(MultipartFile file) { try { if (file != null) { String[] fileNameGrp = fileOp.splitFileName(file) ; if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){ if(!fileNameGrp[1].trim().equals("")){ FileRestVo frVo = fileOp.saveFile(file, fmUrl, FileConstant.fileRequestMapping, FileConstant.filePostMapping_video, FileConstant.NotRegionNum, fileNameGrp[1], null); String fileMainName = fileOp.getFileMainName(frVo.fileName) ; Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadPath + id) ; return BaseResponseUtils.buildSuccess(fvo) ; }else { return BaseResponseUtils.buildError("未得到上传文件的扩展名"); } }else { return BaseResponseUtils.buildError("未能拆分上传文件名称"); } } else { return BaseResponseUtils.buildError("未上传文件"); } } catch (Exception e) { log.error("上传照片文件异常", e); return BaseResponseUtils.buildException(e.getMessage()); } } /** * 上传文档文件 * @param file web端上传文件的post对象 * @return 返回结果 */ @PostMapping("/upDocument") @SsoAop() public BaseResponse upDocument(MultipartFile file) { try { if (file != null) { String[] fileNameGrp = fileOp.splitFileName(file) ; if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){ if(!fileNameGrp[1].trim().equals("")){ String fileExtName = fileNameGrp[1]; if(!supportedFileTypes.contains(fileExtName)){ return BaseResponseUtils.buildError("请上传Word,Excel,PDF类型文档"); } FileRestVo frVo = fileOp.saveFile(file, fmUrl, FileConstant.fileRequestMapping, FileConstant.filePostMapping_document, FileConstant.NotRegionNum, fileNameGrp[1], null); String fileMainName = fileOp.getFileMainName(frVo.fileName) ; Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath); FileVo fvo = new FileVo(id, frVo.fileNameHash, fileNameGrp[0], fileNameGrp[1], frVo.getFileWebUrl() + frVo.getFileWebPath(), null, frVo.fileWebDownloadPath + id) ; return BaseResponseUtils.buildSuccess(fvo) ; }else { return BaseResponseUtils.buildError("未得到上传文件的扩展名"); } }else { return BaseResponseUtils.buildError("未能拆分上传文件名称"); } } else { return BaseResponseUtils.buildError("未上传文件"); } } catch (Exception e) { log.error("上传照片文件异常", e); return BaseResponseUtils.buildException(e.getMessage()); } } /** * 数据库存储 * @param orgName 文件原名称 * @param extName 文件扩展名 * @param newName 文件新名称 * @param hash 文件新名称的哈希值 * @param filePath 文件服务端存储相对路径 * @return 数据记录主键 */ private Long saveFileInfo(String orgName, String extName, String newName, Integer hash, String filePath){ WebFile po = new WebFile() ; po.orgName = orgName ; po.extName = extName ; po.newName = newName ; po.hash = hash ; po.filePath = filePath ; po.dt = new Date() ; return this.sv.save(po) ; } }