Explorar el Código

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

wangqiong hace 2 años
padre
commit
de93471c32

+ 11
- 5
src/main/java/com/oo/demo/controller/OnlyOfficeController.java Ver fichero

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

+ 14
- 2
src/main/java/com/oo/demo/service/DemoService.java Ver fichero

@@ -36,11 +36,23 @@ public class DemoService implements SaveFileProcessor {
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 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 49
         try {
50
+        if(fileId!=null&&fileId!=""){
51
+            onFileService.updateFileByFileId(file, map.get("fileType").toString(),fileId);
52
+        }else{
53
+            fileId = "";
43 54
             fileId = onFileService.saveFile(file, map.get("fileType").toString());
55
+        }
44 56
         } catch (Exception e) {
45 57
             e.printStackTrace();
46 58
         }

+ 29
- 2
src/main/java/com/oo/demo/service/FileService.java Ver fichero

@@ -52,6 +52,7 @@ public class FileService {
52 52
     public WebsocketResult documentSave(JSONObject jsonObject) {
53 53
         WebsocketResult result = new WebsocketResult();
54 54
         String key = "";
55
+        String fileId = jsonObject.getString("fileId");
55 56
         try {
56 57
             int status = jsonObject.getIntValue("status");
57 58
             log.info("status[{}]", status);
@@ -69,7 +70,13 @@ public class FileService {
69 70
                 key = jsonObject.getString("key");
70 71
                 log.info("key:" + JSON.toJSONString(key));
71 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 80
                 log.info("fileId:" + JSON.toJSONString(fileId));
74 81
                 if (fileId != null && !"".equals(fileId)) {
75 82
                     //查询之前FileId对应的文件信息
@@ -93,7 +100,11 @@ public class FileService {
93 100
                 //处理文件的保存
94 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 108
                 result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
98 109
                 log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
99 110
                 log.info("保存文件结束");
@@ -101,10 +112,13 @@ public class FileService {
101 112
                 return result;
102 113
             } else if (0 == status || 2 == status || 4 == status) {
103 114
                 onlyServiceAPI.close(jsonObject);
115
+                log.info("文件关闭====",status);
104 116
             } else if (3 == status || 7 == status) {
105 117
                 //保存文档错误
106 118
                 onlyServiceAPI.close(jsonObject);
119
+                log.info("保存文档错误====",status);
107 120
             } else if (1 == status) {
121
+                log.info("文件正在编辑====",status);
108 122
                 //文件服务的回调  获取key判断当前文档有多少人在这使用
109 123
                 List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
110 124
                 if ((Integer) actions.get(0).get("type") == 1) {
@@ -145,4 +159,17 @@ public class FileService {
145 159
         log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
146 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 Ver fichero

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

+ 37
- 0
src/main/java/com/oo/demo/service/OnFileServiceImpl.java Ver fichero

@@ -56,6 +56,14 @@ public class OnFileServiceImpl extends ServiceImpl<OnFileMapper, OnFile> impleme
56 56
         removeById(id);
57 57
     }
58 58
 
59
+    /**
60
+     * 新增文件信息到数据库
61
+     *
62
+     * @param bytes
63
+     * @param fileType
64
+     * @return
65
+     * @throws Exception
66
+     */
59 67
     public String saveFile(byte[] bytes, String fileType) throws Exception {
60 68
 
61 69
         String fileId = IdUtil.simpleUUID();
@@ -70,4 +78,33 @@ public class OnFileServiceImpl extends ServiceImpl<OnFileMapper, OnFile> impleme
70 78
         }
71 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 Ver fichero

@@ -170,7 +170,7 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
170 170
     @Override
171 171
     public OnFile handlerStatus(JSONObject jsonObject) throws Exception {
172 172
         OnFile file = new OnFile();
173
-        String fileId = null;
173
+        String fileId = jsonObject.getString("fileId");
174 174
         log.info("开始下载编辑器文件");
175 175
         int status = jsonObject.getIntValue("status");
176 176
         log.info("status[{}]:{}", status, jsonObject);
@@ -202,10 +202,11 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
202 202
                 if (caseId != null) {
203 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 210
                 log.info("文件信息1:" + JSON.toJSONString(tempFile.get().getFileInfo()));
210 211
                 log.info("文件信息2:" + JSON.toJSONString(map));
211 212
                 saveFileProcessor.saveAfterInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
@@ -310,7 +311,8 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
310 311
                 log.info("文件变动信息文件url:" + changesurl);
311 312
                 // 下载修改后文件
312 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 317
             removeTempFile(jsonObject);
316 318
             String id = (String) cache.get("getID_" + jsonObject.getString("key"));

+ 4
- 3
src/main/java/com/oo/onlyoffice/core/SaveFileProcessor.java Ver fichero

@@ -16,13 +16,14 @@ public interface SaveFileProcessor {
16 16
      void saveBeforeInitialization(Map<String, Object> map,byte[] bytes,String fileExtension) throws Exception;
17 17
 
18 18
     /**
19
-     *
19
+     * 存储onlyoffice文件信息
20 20
      * @param map     文件元信息
21 21
      * @param file    文件
22 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
      * 保存文件后进行自定义处理