OnlyOffice在线文档

FileService.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.oo.demo.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.oo.demo.dao.OnFileMapper;
  7. import com.oo.demo.entity.OnFile;
  8. import com.oo.demo.entity.WebsocketResult;
  9. import com.oo.onlyoffice.api.OnlyServiceAPI;
  10. import com.oo.onlyoffice.dto.edit.FileUser;
  11. import com.oo.onlyoffice.tools.SecurityUtils;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * @BelongsProject: onlyoffice-demo
  20. * @BelongsPackage: com.oo.demo.service
  21. * @CreateTime: 2023-08-02 09:57
  22. * @Description: TODO
  23. * @Version: 1.0
  24. */
  25. @Slf4j
  26. @Service
  27. public class FileService {
  28. @Autowired
  29. private OnlyServiceAPI onlyServiceAPI;
  30. @Autowired
  31. private DemoService demoService;
  32. @Autowired
  33. private OnFileService onFileService;
  34. /**
  35. * 文档服务器 保存文件回调
  36. * Defines the status of the document. Can have the following values:
  37. * 1 - document is being edited,
  38. * 2 - document is ready for saving,
  39. * 3 - document saving error has occurred,
  40. * 4 - document is closed with no changes,
  41. * 6 - document is being edited, but the current document state is saved,
  42. * 7 - error has occurred while force saving the document.
  43. *
  44. * @param jsonObject
  45. */
  46. @Transactional(rollbackFor = Exception.class)
  47. public WebsocketResult documentSave(JSONObject jsonObject) {
  48. WebsocketResult result = new WebsocketResult();
  49. String key = "";
  50. String fileId = jsonObject.getString("fileId");
  51. try {
  52. int status = jsonObject.getIntValue("status");
  53. log.info("status[{}]", status);
  54. if (6 == status) {
  55. log.info("开始保存文件");
  56. log.info("dayin:" + JSON.toJSONString(jsonObject));
  57. JSONArray jsonArray = jsonObject.getJSONObject("history").getJSONArray("changes");
  58. log.info("历史版本:" + JSON.toJSONString(jsonArray));
  59. JSONObject object = jsonArray.getJSONObject(0);
  60. String userId = object.getJSONObject("user").getString("id");
  61. FileUser fileUser = new FileUser();
  62. fileUser.setId(userId);
  63. SecurityUtils.setUserSession(fileUser);
  64. log.info("fileuser:" + JSON.toJSONString(fileUser));
  65. key = jsonObject.getString("key");
  66. log.info("key:" + JSON.toJSONString(key));
  67. //文件id
  68. Boolean isexistid = false;
  69. if (fileId != null && !"".equals(fileId)) {
  70. isexistid = true;
  71. }
  72. if (!isexistid) {
  73. fileId = onlyServiceAPI.getFileId(key);
  74. }
  75. log.info("fileId:" + JSON.toJSONString(fileId));
  76. if (fileId != null && !"".equals(fileId)) {
  77. //查询之前FileId对应的文件信息
  78. OnFile onfile = onFileService.getFileById(fileId);
  79. jsonObject.put("caseId", onfile.getCaseId());
  80. }
  81. //判断是否是最后一人进行保存
  82. int users = onlyServiceAPI.getUserNum(key);
  83. // if (users > 1) {
  84. // return null;
  85. // }
  86. //历史版本最大个数 获取当前文件的历史版本数量
  87. Integer histNum = onlyServiceAPI.getHistNum();
  88. log.info("历史版本最大个数" + histNum);
  89. /**
  90. * 如果有需要保存历史记录 可以进行相关操作
  91. */
  92. if (null != histNum) {
  93. }
  94. //处理文件的保存
  95. OnFile file = onlyServiceAPI.handlerStatus(jsonObject);
  96. //查询相同案件最新版本的文件信息
  97. if (fileId != null && !"".equals(fileId)) {
  98. file = getNewFileInofoByFileId(fileId);
  99. } else {
  100. file = getNewFileInofoByCaseId(file.getCaseId());
  101. }
  102. result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
  103. log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
  104. log.info("保存文件结束");
  105. SecurityUtils.removeUserSession();
  106. return result;
  107. } else if (0 == status || 2 == status || 4 == status) {
  108. onlyServiceAPI.close(jsonObject);
  109. log.info("文件关闭====",status);
  110. } else if (3 == status || 7 == status) {
  111. //保存文档错误
  112. onlyServiceAPI.close(jsonObject);
  113. log.info("保存文档错误====",status);
  114. } else if (1 == status) {
  115. log.info("文件正在编辑====",status);
  116. //文件服务的回调 获取key判断当前文档有多少人在这使用
  117. List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
  118. if ((Integer) actions.get(0).get("type") == 1) {
  119. log.info("当前用户有[{}]", jsonObject.getString("users"));
  120. List<String> users = JSONObject.parseArray(jsonObject.getString("users"), String.class);
  121. onlyServiceAPI.iskey(jsonObject.getString("key"), users.size());
  122. }
  123. if ((Integer) actions.get(0).get("type") == 0) {
  124. onlyServiceAPI.iskey(jsonObject.getString("key"), null);
  125. }
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. throw new RuntimeException(e);
  130. }
  131. return result;
  132. }
  133. @Autowired
  134. OnFileMapper onFileMapper;
  135. /**
  136. * 查询相同案件最新版本的文件信息
  137. *
  138. * @param caseId
  139. * @return
  140. */
  141. private OnFile getNewFileInofoByCaseId(String caseId) {
  142. OnFile file = new OnFile();
  143. QueryWrapper<OnFile> fileQueryWrapper = new QueryWrapper<>();
  144. fileQueryWrapper.eq("case_id", caseId);
  145. fileQueryWrapper.orderByDesc("created_time");
  146. List<OnFile> onFiles = onFileMapper.selectList(fileQueryWrapper);
  147. if (onFiles != null && onFiles.size() > 0) {
  148. file = onFiles.get(0);
  149. }
  150. log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
  151. return file;
  152. }
  153. /**
  154. * 根据文件的fileid查询文件信息
  155. */
  156. private OnFile getNewFileInofoByFileId(String fileId) {
  157. OnFile file = onFileMapper.selectById(fileId);
  158. if (file != null) {
  159. log.info("caseId:" + file.getCaseId() + ":fileinfo:" + JSON.toJSONString(file));
  160. } else {
  161. log.info("根据文件id未查询到文件信息");
  162. }
  163. return file;
  164. }
  165. }