Bläddra i källkod

优化回调保存接口(若传递文件id则每次保存只更新文件)

wangqiong 2 år sedan
förälder
incheckning
de93471c32

+ 11
- 5
src/main/java/com/oo/demo/controller/OnlyOfficeController.java Visa fil

236
      * 6 - 正在编辑文档,但保存当前文档状态
236
      * 6 - 正在编辑文档,但保存当前文档状态
237
      * 7 - 强制保存文档时发生错误
237
      * 7 - 强制保存文档时发生错误
238
      */
238
      */
239
-    @RequestMapping("/onlyOffice/save")
239
+    @RequestMapping("/onlyOffice/save/{fileId}")
240
     @ResponseBody
240
     @ResponseBody
241
-    public void saveFile(HttpServletRequest request, HttpServletResponse response) {
241
+    public void saveFile(HttpServletRequest request, HttpServletResponse response,@PathVariable(required = false) String fileId) {
242
         PrintWriter writer = null;
242
         PrintWriter writer = null;
243
         try {
243
         try {
244
             writer = response.getWriter();
244
             writer = response.getWriter();
246
             Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
246
             Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
247
             String body = scanner.hasNext() ? scanner.next() : "";
247
             String body = scanner.hasNext() ? scanner.next() : "";
248
             JSONObject jsonObject = JSONObject.parseObject(body);
248
             JSONObject jsonObject = JSONObject.parseObject(body);
249
+            if(fileId!=null){
250
+                jsonObject.put("fileId",fileId);
251
+            }else{
252
+                jsonObject.put("fileId","");
253
+            }
249
             log.info("{}", jsonObject);
254
             log.info("{}", jsonObject);
250
             WebsocketResult result = fileService.documentSave(jsonObject);
255
             WebsocketResult result = fileService.documentSave(jsonObject);
251
             /*
256
             /*
258
                 userId = userSession.getId();
263
                 userId = userSession.getId();
259
             }
264
             }
260
             WebSocketServer.sendInfo(JSON.toJSONString(result.getOnFile()), userId);
265
             WebSocketServer.sendInfo(JSON.toJSONString(result.getOnFile()), userId);
261
-            if (Objects.nonNull(writer)) {
262
-                writer.write("{\"error\":0}");
263
-            }
266
+//            if (Objects.nonNull(writer)) {
267
+//                writer.write("{\"error\":0}");
268
+//            }
269
+            writer.write("{\"error\":0}");
264
         } catch (Exception e) {
270
         } catch (Exception e) {
265
             e.printStackTrace();
271
             e.printStackTrace();
266
             log.info("报错信息" + e.getMessage());
272
             log.info("报错信息" + e.getMessage());

+ 14
- 2
src/main/java/com/oo/demo/service/DemoService.java Visa fil

36
 
36
 
37
     }
37
     }
38
 
38
 
39
+    /**
40
+     * 存储onlyoffice文件信息
41
+     * @param map     文件元信息
42
+     * @param file    文件
43
+     * @param changes 文件变动信息
44
+     * @param fileId 文件id
45
+     * @return
46
+     */
39
     @Override
47
     @Override
40
-    public Map<String, Object> save(Map<String, Object> map, byte[] file, byte[] changes, String key) {
41
-        String fileId = "";
48
+    public Map<String, Object> save(Map<String, Object> map, byte[] file, byte[] changes, String fileId) {
42
         try {
49
         try {
50
+        if(fileId!=null&&fileId!=""){
51
+            onFileService.updateFileByFileId(file, map.get("fileType").toString(),fileId);
52
+        }else{
53
+            fileId = "";
43
             fileId = onFileService.saveFile(file, map.get("fileType").toString());
54
             fileId = onFileService.saveFile(file, map.get("fileType").toString());
55
+        }
44
         } catch (Exception e) {
56
         } catch (Exception e) {
45
             e.printStackTrace();
57
             e.printStackTrace();
46
         }
58
         }

+ 29
- 2
src/main/java/com/oo/demo/service/FileService.java Visa fil

52
     public WebsocketResult documentSave(JSONObject jsonObject) {
52
     public WebsocketResult documentSave(JSONObject jsonObject) {
53
         WebsocketResult result = new WebsocketResult();
53
         WebsocketResult result = new WebsocketResult();
54
         String key = "";
54
         String key = "";
55
+        String fileId = jsonObject.getString("fileId");
55
         try {
56
         try {
56
             int status = jsonObject.getIntValue("status");
57
             int status = jsonObject.getIntValue("status");
57
             log.info("status[{}]", status);
58
             log.info("status[{}]", status);
69
                 key = jsonObject.getString("key");
70
                 key = jsonObject.getString("key");
70
                 log.info("key:" + JSON.toJSONString(key));
71
                 log.info("key:" + JSON.toJSONString(key));
71
                 //文件id
72
                 //文件id
72
-                String fileId = onlyServiceAPI.getFileId(key);
73
+                Boolean isexistid = false;
74
+                if (fileId != null && !"".equals(fileId)) {
75
+                    isexistid = true;
76
+                }
77
+                if (!isexistid) {
78
+                    fileId = onlyServiceAPI.getFileId(key);
79
+                }
73
                 log.info("fileId:" + JSON.toJSONString(fileId));
80
                 log.info("fileId:" + JSON.toJSONString(fileId));
74
                 if (fileId != null && !"".equals(fileId)) {
81
                 if (fileId != null && !"".equals(fileId)) {
75
                     //查询之前FileId对应的文件信息
82
                     //查询之前FileId对应的文件信息
93
                 //处理文件的保存
100
                 //处理文件的保存
94
                 OnFile file = onlyServiceAPI.handlerStatus(jsonObject);
101
                 OnFile file = onlyServiceAPI.handlerStatus(jsonObject);
95
                 //查询相同案件最新版本的文件信息
102
                 //查询相同案件最新版本的文件信息
96
-                file = getNewFileInofoByCaseId(file.getCaseId());
103
+                if (fileId != null && !"".equals(fileId)) {
104
+                    file = getNewFileInofoByFileId(fileId);
105
+                } else {
106
+                    file = getNewFileInofoByCaseId(file.getCaseId());
107
+                }
97
                 result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
108
                 result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
98
                 log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
109
                 log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
99
                 log.info("保存文件结束");
110
                 log.info("保存文件结束");
101
                 return result;
112
                 return result;
102
             } else if (0 == status || 2 == status || 4 == status) {
113
             } else if (0 == status || 2 == status || 4 == status) {
103
                 onlyServiceAPI.close(jsonObject);
114
                 onlyServiceAPI.close(jsonObject);
115
+                log.info("文件关闭====",status);
104
             } else if (3 == status || 7 == status) {
116
             } else if (3 == status || 7 == status) {
105
                 //保存文档错误
117
                 //保存文档错误
106
                 onlyServiceAPI.close(jsonObject);
118
                 onlyServiceAPI.close(jsonObject);
119
+                log.info("保存文档错误====",status);
107
             } else if (1 == status) {
120
             } else if (1 == status) {
121
+                log.info("文件正在编辑====",status);
108
                 //文件服务的回调  获取key判断当前文档有多少人在这使用
122
                 //文件服务的回调  获取key判断当前文档有多少人在这使用
109
                 List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
123
                 List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
110
                 if ((Integer) actions.get(0).get("type") == 1) {
124
                 if ((Integer) actions.get(0).get("type") == 1) {
145
         log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
159
         log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
146
         return file;
160
         return file;
147
     }
161
     }
162
+
163
+    /**
164
+     * 根据文件的fileid查询文件信息
165
+     */
166
+    private OnFile getNewFileInofoByFileId(String fileId) {
167
+        OnFile file = onFileMapper.selectById(fileId);
168
+        if (file != null) {
169
+            log.info("caseId:" + file.getCaseId() + ":fileinfo:" + JSON.toJSONString(file));
170
+        } else {
171
+            log.info("根据文件id未查询到文件信息");
172
+        }
173
+        return file;
174
+    }
148
 }
175
 }

+ 15
- 1
src/main/java/com/oo/demo/service/OnFileService.java Visa fil

4
 import com.oo.demo.entity.OnFile;
4
 import com.oo.demo.entity.OnFile;
5
 
5
 
6
 import javax.servlet.http.HttpServletResponse;
6
 import javax.servlet.http.HttpServletResponse;
7
-import java.io.File;
8
 
7
 
9
 
8
 
10
 public interface OnFileService extends IService<OnFile> {
9
 public interface OnFileService extends IService<OnFile> {
11
     /**
10
     /**
12
      * 保存onlyoffice文件
11
      * 保存onlyoffice文件
12
+     *
13
      * @param bytes
13
      * @param bytes
14
      * @param fileType
14
      * @param fileType
15
      * @return
15
      * @return
17
      */
17
      */
18
     String saveFile(byte[] bytes, String fileType) throws Exception;
18
     String saveFile(byte[] bytes, String fileType) throws Exception;
19
 
19
 
20
+    /**
21
+     * 通过文件id更新onlyoffice文件
22
+     *
23
+     * @param bytes
24
+     * @param fileType
25
+     * @param fileId
26
+     * @return
27
+     * @throws Exception
28
+     */
29
+    String updateFileByFileId(byte[] bytes, String fileType, String fileId) throws Exception;
30
+
20
     /**
31
     /**
21
      * 删除onlyoffice文件
32
      * 删除onlyoffice文件
33
+     *
22
      * @param id
34
      * @param id
23
      */
35
      */
24
     void removeFile(String id);
36
     void removeFile(String id);
25
 
37
 
26
     /**
38
     /**
27
      * 下载onlyoffice文件
39
      * 下载onlyoffice文件
40
+     *
28
      * @param id
41
      * @param id
29
      * @param isBrowser
42
      * @param isBrowser
30
      * @param response
43
      * @param response
33
 
46
 
34
     /**
47
     /**
35
      * 根据文件id查找文件信息
48
      * 根据文件id查找文件信息
49
+     *
36
      * @param id
50
      * @param id
37
      * @return
51
      * @return
38
      */
52
      */

+ 37
- 0
src/main/java/com/oo/demo/service/OnFileServiceImpl.java Visa fil

56
         removeById(id);
56
         removeById(id);
57
     }
57
     }
58
 
58
 
59
+    /**
60
+     * 新增文件信息到数据库
61
+     *
62
+     * @param bytes
63
+     * @param fileType
64
+     * @return
65
+     * @throws Exception
66
+     */
59
     public String saveFile(byte[] bytes, String fileType) throws Exception {
67
     public String saveFile(byte[] bytes, String fileType) throws Exception {
60
 
68
 
61
         String fileId = IdUtil.simpleUUID();
69
         String fileId = IdUtil.simpleUUID();
70
         }
78
         }
71
         return fileId;
79
         return fileId;
72
     }
80
     }
81
+
82
+    /**
83
+     * 更新文件信息到数据库
84
+     *
85
+     * @param bytes
86
+     * @param fileType
87
+     * @param fileId
88
+     * @return
89
+     * @throws Exception
90
+     */
91
+    public String updateFileByFileId(byte[] bytes, String fileType, String fileId) throws Exception {
92
+        Boolean ishaveid = false;
93
+        if (fileId != null && fileId != "") {
94
+            ishaveid = true;
95
+        }
96
+        if (!ishaveid) {
97
+            fileId = IdUtil.simpleUUID();
98
+        }
99
+        File file = new File(path + "/" + fileId + "." + fileType);
100
+        File parentFile = file.getParentFile();
101
+        if (!parentFile.exists()) {
102
+            parentFile.mkdirs();
103
+        }
104
+        try (FileOutputStream out = new FileOutputStream(path + "/" + fileId + "." + fileType)) {
105
+            out.write(bytes);
106
+            out.flush();
107
+        }
108
+        return fileId;
109
+    }
73
 }
110
 }

+ 6
- 4
src/main/java/com/oo/onlyoffice/api/impl/OnlyServiceAPIImpl.java Visa fil

170
     @Override
170
     @Override
171
     public OnFile handlerStatus(JSONObject jsonObject) throws Exception {
171
     public OnFile handlerStatus(JSONObject jsonObject) throws Exception {
172
         OnFile file = new OnFile();
172
         OnFile file = new OnFile();
173
-        String fileId = null;
173
+        String fileId = jsonObject.getString("fileId");
174
         log.info("开始下载编辑器文件");
174
         log.info("开始下载编辑器文件");
175
         int status = jsonObject.getIntValue("status");
175
         int status = jsonObject.getIntValue("status");
176
         log.info("status[{}]:{}", status, jsonObject);
176
         log.info("status[{}]:{}", status, jsonObject);
202
                 if (caseId != null) {
202
                 if (caseId != null) {
203
                     fileInfoMap.put("caseId", caseId);
203
                     fileInfoMap.put("caseId", caseId);
204
                 }
204
                 }
205
-                saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
205
+                //保存文件前进行自定义处理(该方法暂时没有实现业务逻辑)
206
+                //saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
206
 
207
 
207
                 // 保存文件
208
                 // 保存文件
208
-                Map<String, Object> map = saveFileProcessor.save(tempFile.get().getFileInfo(), fileByte, changes, key);
209
+                Map<String, Object> map = saveFileProcessor.save(tempFile.get().getFileInfo(), fileByte, changes, fileId);
209
                 log.info("文件信息1:" + JSON.toJSONString(tempFile.get().getFileInfo()));
210
                 log.info("文件信息1:" + JSON.toJSONString(tempFile.get().getFileInfo()));
210
                 log.info("文件信息2:" + JSON.toJSONString(map));
211
                 log.info("文件信息2:" + JSON.toJSONString(map));
211
                 saveFileProcessor.saveAfterInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
212
                 saveFileProcessor.saveAfterInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
310
                 log.info("文件变动信息文件url:" + changesurl);
311
                 log.info("文件变动信息文件url:" + changesurl);
311
                 // 下载修改后文件
312
                 // 下载修改后文件
312
                 byte[] fileByte = FileUtil.getFileByte(url);
313
                 byte[] fileByte = FileUtil.getFileByte(url);
313
-                saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("key"));
314
+                saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("fileId"));
315
+                log.info("zouzhele==================");
314
             }
316
             }
315
             removeTempFile(jsonObject);
317
             removeTempFile(jsonObject);
316
             String id = (String) cache.get("getID_" + jsonObject.getString("key"));
318
             String id = (String) cache.get("getID_" + jsonObject.getString("key"));

+ 4
- 3
src/main/java/com/oo/onlyoffice/core/SaveFileProcessor.java Visa fil

16
      void saveBeforeInitialization(Map<String, Object> map,byte[] bytes,String fileExtension) throws Exception;
16
      void saveBeforeInitialization(Map<String, Object> map,byte[] bytes,String fileExtension) throws Exception;
17
 
17
 
18
     /**
18
     /**
19
-     *
19
+     * 存储onlyoffice文件信息
20
      * @param map     文件元信息
20
      * @param map     文件元信息
21
      * @param file    文件
21
      * @param file    文件
22
      * @param changes 文件变动信息
22
      * @param changes 文件变动信息
23
-     * @return 返回新的文件信息
23
+     * @param fileId 文件id
24
+     * @return
24
      */
25
      */
25
-    Map<String, Object> save(Map<String, Object> map,byte[] file, byte[] changes,String key);
26
+    Map<String, Object> save(Map<String, Object> map,byte[] file, byte[] changes,String fileId);
26
 
27
 
27
     /**
28
     /**
28
      * 保存文件后进行自定义处理
29
      * 保存文件后进行自定义处理