private WebErrors validateUpload(MultipartFile file, HttpServletRequest request) { int fileSize = (int) (file.getSize() / 1024); WebErrors errors = WebErrors.create(request); if (errors.ifNull(file, "file")) { return errors; } CmsUser user = CmsUtils.getUser(request); String origName = file.getOriginalFilename(); String ext = FilenameUtils.getExtension(origName).toLowerCase(Locale.ENGLISH); if (!Constants.LIBRARY_SUFFIX.contains(ext)) { errors.addErrorCode("error.uploadValidFile", ext); } // 非允许的后缀 if (!user.isAllowSuffix(ext)) { errors.addErrorCode("upload.error.invalidsuffix", ext); return errors; } // 超过附件大小限制 if (!user.isAllowMaxFile((int) (file.getSize() / 1024))) { errors.addErrorCode("upload.error.toolarge", origName, user.getGroup().getAllowMaxFile()); return errors; } // 超过每日上传限制 if (!user.isAllowPerDay(fileSize)) { long laveSize = user.getGroup().getAllowPerDay() - user.getUploadSize(); if (laveSize < 0) { laveSize = 0; } errors.addErrorCode("upload.error.dailylimit", laveSize); } return errors; }
/** * 会员文库添加 * * @param request * @param model * @return */ @RequestMapping(value = "/member/doc_add.jspx") public String add(HttpServletRequest request, HttpServletResponse response, ModelMap model) { CmsUser user = CmsUtils.getUser(request); CmsSite site = CmsUtils.getSite(request); FrontUtils.frontData(request, model, site); MemberConfig mcfg = site.getConfig().getMemberConfig(); // 没有开启会员功能 if (!mcfg.isMemberOn()) { return FrontUtils.showMessage(request, model, "member.memberClose"); } if (user == null) { return FrontUtils.showLogin(request, model, site); } boolean hasPermission = checkUserUploadTotal(user.getGroup(), user.getFileTotal()); return super.add(hasPermission, DOC_ADD, request, response, model); }
@RequestMapping("/member/doc_upload.jspx") public String uploadDoc( @RequestParam(value = "doc", required = false) MultipartFile file, String docNum, HttpServletRequest request, HttpServletResponse response, ModelMap model) { WebErrors errors = validateUpload(file, request); CmsUser user = CmsUtils.getUser(request); CmsSite site = CmsUtils.getSite(request); if (errors.hasErrors()) { model.addAttribute("error", errors.getErrors().get(0)); return FrontUtils.getTplPath(request, site.getSolutionPath(), TPLDIR_MEMBER, DOC_UPLOAD); } errors = validateUploadFileSize( file, CmsUtils.getUser(request).getGroup().getAllowFileSize(), request); if (errors.hasErrors()) { model.addAttribute("error", errors.getErrors().get(0)); return FrontUtils.showError(request, response, model, errors); } String origName = file.getOriginalFilename(); String ext = FilenameUtils.getExtension(origName).toLowerCase(Locale.ENGLISH); // TODO 检查允许上传的后缀 try { String fileUrl; String ctx = request.getContextPath(); fileUrl = fileRepository.storeByExt(site.getLibraryPath(), ext, file); // 加上部署路径 fileUrl = ctx + fileUrl; model.addAttribute("docPath", fileUrl); model.addAttribute("docName", origName); model.addAttribute("docNum", docNum); model.addAttribute("docExt", ext); } catch (IllegalStateException e) { model.addAttribute("error", e.getMessage()); log.error("upload file error!", e); } catch (IOException e) { model.addAttribute("error", e.getMessage()); log.error("upload file error!", e); } cmsUserMng.updateUploadSize( user.getId(), Integer.parseInt(String.valueOf(file.getSize() / 1024))); return FrontUtils.getTplPath(request, site.getSolutionPath(), TPLDIR_MEMBER, DOC_UPLOAD); }