diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentService.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentService.java index 92570c7..eb83bfa 100644 --- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentService.java +++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentService.java @@ -32,7 +32,7 @@ public interface DocumentService { * @param documentKey * @return */ - Document getDocumentToken(String documentKey,String token); + Document getDocumentToken(String documentKey,String token, String uuid); /** @@ -57,7 +57,7 @@ public interface DocumentService { * @param userName * @return */ - DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token); + DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token,String uuid); /** * 编辑后保存文档实体文件 diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentServiceImpl.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentServiceImpl.java index 9097a12..9c23273 100644 --- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentServiceImpl.java +++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/onlyoffice/document/DocumentServiceImpl.java @@ -10,6 +10,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.dom4j.DocumentException; import org.hashids.Hashids; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -132,7 +133,7 @@ public class DocumentServiceImpl implements DocumentService{ return doc; } @Override - public Document getDocumentToken(String documentKey,String token) { + public Document getDocumentToken(String documentKey,String token, String uuid) { Document doc = null; try { @@ -145,7 +146,7 @@ public class DocumentServiceImpl implements DocumentService{ } // 从缓存中取出后,再绑定非必需缓存字段(节省缓存大小) // doc.setKey(documentKey); - doc.setUrl(fileUrlToken(doc.getTitle(),token)); + doc.setUrl(fileUrlToken(doc.getTitle(),token,uuid)); if (log.isDebugEnabled()) { log.info(doc.toString()); } @@ -199,9 +200,8 @@ public class DocumentServiceImpl implements DocumentService{ * @param * @return */ - private String fileUrlToken(String filename,String token) { - return String.format(DocumentConstants.OFFICE_API_DOC_FILE, getServerHost(), "?name="+filename+"&token="+token); -// return "http://192.168.0.58:20053/download?name="+filename; + private String fileUrlToken(String filename,String token, String uuid) { + return String.format(DocumentConstants.OFFICE_API_DOC_FILE, getServerHost(), "?name="+filename+"&uuid="+uuid+"&token="+token); } /** @@ -241,10 +241,9 @@ public class DocumentServiceImpl implements DocumentService{ .build(); } @Override - public DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token) { + public DocumentEditParam buildDocumentEditParamToken(String userId, String userName, String fileName,String token, String uuid) { return DocumentEditParam.builder() - .callbackUrl(callbackUrl(fileName)+"&token="+token) -// .callbackUrl(callbackUrl(fileName)) + .callbackUrl(callbackUrl(fileName)+"&uuid="+uuid+"&token="+token) .user(DocumentEditParam.UserBean.builder() .id(userId) .name(userName) diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/only/controller/admin/only/onlyofController.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/only/controller/admin/only/onlyofController.java index d51bc81..3700e96 100644 --- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/only/controller/admin/only/onlyofController.java +++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/only/controller/admin/only/onlyofController.java @@ -96,7 +96,6 @@ public class onlyofController { @Operation(summary = "获取文件列表") @PreAuthorize("@ss.hasPermission('only:onlyof:filelist')") public ResponseEntity listFile() { - System.out.println("fileList"); return new ResponseEntity(uploadService.list(), HttpStatus.OK); } @@ -107,47 +106,30 @@ public class onlyofController { * @return * @throws Exception */ - @PostMapping("/update") @Operation(summary = "更新文件编辑") @PreAuthorize("@ss.hasPermission('only:onlyof:update')") - public ResponseEntity upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception { - System.out.println("update"); + public ResponseEntity upload(@RequestParam("file") MultipartFile file,@RequestParam("uuid") String uuid,HttpServletRequest request) throws Exception { + uuid = uuid.isEmpty() ? uuid : uuid + "\\"; +// System.out.println("update"+file.getOriginalFilename()+uuid); if (file.isEmpty()){ throw new Exception("上传文件不能为空"); } onlyofficeDO upload = new onlyofficeDO(); String fileName = file.getOriginalFilename(); //更新保存文件信息到数据库 - FileUtil.saveFile(file.getInputStream(),filePath+file.getOriginalFilename()); + FileUtil.saveFile(file.getInputStream(),filePath+uuid+file.getOriginalFilename(),filePath+uuid); upload.setUploadDate(new Date()); - upload.setFilePath(filePath); + upload.setFilePath(filePath+uuid); upload.setFileName(file.getOriginalFilename()); upload.setFileSize(file.getSize()); - System.out.println("111"+upload); +// System.out.println("111"+upload); uploadService.save(upload); System.gc(); return new ResponseEntity("上传成功", HttpStatus.OK); } - /** - * 下载文档接口 - * @param name - * @param response - */ - @GetMapping("/download") - @Operation(summary = "下载文件") - @PreAuthorize("@ss.hasPermission('only:onlyof:download')") - public void download(String name, HttpServletResponse response) { -// System.out.println("download---"+name+"-----"+response+"-----"); - try { - FileUtil.downLoadFile(name,filePath,response); - } catch (IOException e) { - e.printStackTrace(); - } - } - /** * 编辑文件的接口 * @param name @@ -160,24 +142,44 @@ public class onlyofController { @GetMapping("/edit") @Operation(summary = "编辑文件") @PreAuthorize("@ss.hasPermission('only:onlyof:edit')") - public ModelAndView editDocFile(@RequestParam String name, String userName, String userId ,String token, Model model) { - System.out.println("edit"); -// System.out.println("token"+token); - String path = filePath+name; + public ModelAndView editDocFile(@RequestParam String name, String userName, String userId ,String uuid ,String token, Model model) { + String uuid2 = ""; + uuid2 = uuid.isEmpty() ? uuid : uuid + "\\"; + String path = filePath+uuid2+name; // System.out.println("path:"+path); +// System.out.println("edit"+uuid2); +// System.out.println("token"+token); ModelAndView mav = new ModelAndView(); - Document document = documentService.getDocumentToken(documentService.buildDocument(path, name),token); + Document document = documentService.getDocumentToken(documentService.buildDocument(path, name),token,uuid); mav.addObject("document", document); if (!documentService.canEdit(document)) { // System.out.println("demo"); return mav; } - mav.addObject("documentEditParam", documentService.buildDocumentEditParamToken(userId, userName, name,token)); + mav.addObject("documentEditParam", documentService.buildDocumentEditParamToken(userId, userName, name,token,uuid2)); mav.setViewName("editor"); // System.out.println(mav); return mav; } + /** + * 下载文档接口 + * @param name + * @param response + */ + @GetMapping("/download") + @Operation(summary = "下载文件") + @PreAuthorize("@ss.hasPermission('only:onlyof:download')") + public void download(String name, String uuid,HttpServletResponse response) { + uuid = uuid.equals("") ? uuid : uuid + "\\"; +// System.out.println("download: "+name+"---"+response+"---"+uuid); + try { + FileUtil.downLoadFile(name,filePath+uuid,response); + } catch (IOException e) { + e.printStackTrace(); + } + } + /** * 文件编辑打开保存后的回调函数 * @param request @@ -187,10 +189,10 @@ public class onlyofController { @RequestMapping("/callback") @Operation(summary = "回调文件") @PreAuthorize("@ss.hasPermission('only:onlyof:callback')") - public void saveDocumentFile(HttpServletRequest request, HttpServletResponse response) throws IOException { - System.out.println("callback"); + public void saveDocumentFile(HttpServletRequest request, HttpServletResponse response,String uuid) throws IOException { +// System.out.println("callback"+uuid); //处理编辑回调逻辑 - callBack(request, response); + callBack(request, response,uuid); } @@ -204,7 +206,7 @@ public class onlyofController { @Operation(summary = "文件状态") @PreAuthorize("@ss.hasPermission('only:onlyof:editStatus')") public ResponseEntity getDoucmentEditStatus(String name) throws ParseException { - System.out.println("editStatus"); +// System.out.println("editStatus"); String url = officeUrl+officeCommand; Map map = new HashMap(); map.put("c", "forcesave"); @@ -230,52 +232,51 @@ public class onlyofController { * @param response * @throws IOException */ - private void callBack(HttpServletRequest request, HttpServletResponse response) throws IOException { + /* 状态 + 0 - no document with the key identifier could be found, + 1 - document is being edited, + 2 - document is ready for saving, + 3 - document saving error has occurred, + 4 - document is closed with no changes, + 6 - document is being edited, but the current document state is saved, + 7 - error has occurred while force saving the document. + * */ + private void callBack(HttpServletRequest request, HttpServletResponse response, String uuid) throws IOException { PrintWriter writer = null; JSONObject jsonObj = null; - System.out.println("===saveeditedfile------------"); +// System.out.println("===saveeditedfile------------"); try { writer = response.getWriter(); Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A"); String body = scanner.hasNext() ? scanner.next() : ""; jsonObj = (JSONObject) new JSONParser().parse(body); -// jsonObj = (JSONObject) new JSONParser(body); - System.out.println(jsonObj); - System.out.println("===saveeditedfile:" + jsonObj.get("status")); - /* - 0 - no document with the key identifier could be found, - 1 - document is being edited, - 2 - document is ready for saving, - 3 - document saving error has occurred, - 4 - document is closed with no changes, - 6 - document is being edited, but the current document state is saved, - 7 - error has occurred while force saving the document. - * */ +// System.out.println(jsonObj); +// System.out.println("===saveeditedfile:" + jsonObj.get("status")); if ((long) jsonObj.get("status") == 2) { - FileUtil.callBackSaveDocument(jsonObj,filePath,request, response); + FileUtil.callBackSaveDocument(jsonObj,filePath+uuid,request, response); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (org.json.simple.parser.ParseException e) { + } catch (ParseException e) { throw new RuntimeException(e); } /* * status = 1,我们给onlyoffice的服务返回{"error":"0"}的信息,这样onlyoffice会认为回调接口是没问题的,这样就可以在线编辑文档了,否则的话会弹出窗口说明 * 在线编辑还没有关闭,前端有人下载文档时,强制保存最新内容 当status 是6时说明有人在编辑时下载文档 * */ - System.out.println(jsonObj.get("status")); +// System.out.println(jsonObj.get("status")); if ((long) jsonObj.get("status") == 6) { //处理当文档正在编辑为关闭时,下载文档 if (((String)jsonObj.get("userdata")).equals("sample userdata")){ - FileUtil.callBackSaveDocument(jsonObj,filePath,request, response); + FileUtil.callBackSaveDocument(jsonObj,filePath+uuid,request, response); } - System.out.println("====保存失败:"); +// System.out.println("====保存失败:"); writer.write("{\"error\":1}"); } else { //执行删除编辑时下载保存的文件: - FileUtil.deleteTempFile(filePath,request.getParameter("fileName")); + FileUtil.deleteTempFile(filePath+uuid,request.getParameter("fileName")); writer.write("{\"error\":0}"); } }