Просмотр исходного кода

Merge branch 'bgy' of SH-Arbitrate/OnlyOffice-Backend into dev

bgy 2 лет назад
Родитель
Сommit
ce32f7edfa

+ 11
- 5
src/main/java/com/oo/demo/controller/OnlyOfficeController.java Просмотреть файл

@@ -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 Просмотреть файл

@@ -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 Просмотреть файл

@@ -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 Просмотреть файл

@@ -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 Просмотреть файл

@@ -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
 }

+ 22
- 10
src/main/java/com/oo/onlyoffice/api/impl/OnlyServiceAPIImpl.java Просмотреть файл

@@ -81,29 +81,39 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
81 81
             return documentEdit(map, collaborativeEditing, clientType);
82 82
         }
83 83
         if (VIEW.equals(mode)) {
84
-            return documentView(map, clientType);
84
+            return documentView(map, collaborativeEditing, clientType);
85 85
         }
86
+//        if (VIEW.equals(mode)) {
87
+//            return documentView(map, clientType);
88
+//        }
86 89
         return null;
87 90
     }
88 91
 
89 92
     private Map documentEdit(Map<String, Object> map, boolean collaborativeEditing, String clientType) {
90
-        FileConfig fileConfigDTO = openEditConfig(map, "edit", collaborativeEditing,clientType);
93
+        FileConfig fileConfigDTO = openEditConfig(map, "edit", collaborativeEditing, clientType);
91 94
         String json = JSON.toJSONString(fileConfigDTO);
92 95
         Map<String, Object> config = JSON.parseObject(json, Map.class);
93 96
         config.put("type", clientType);
94 97
         return config;
95 98
     }
96 99
 
100
+    private Map documentView(Map<String, Object> map, boolean collaborativeEditing, String clientType) {
101
+        FileConfig fileConfigDTO = openEditConfig(map, "view", collaborativeEditing, clientType);
102
+        String json = JSON.toJSONString(fileConfigDTO);
103
+        Map<String, Object> config = JSON.parseObject(json, Map.class);
104
+        config.put("type", clientType);
105
+        return config;
106
+    }
97 107
 
98
-    private Map documentView(Map<String, Object> map,String clientType) {
99
-        FileConfig fileConfigDTO = openEditConfig(map, "view", false,clientType);
108
+    private Map documentView(Map<String, Object> map, String clientType) {
109
+        FileConfig fileConfigDTO = openEditConfig(map, "view", false, clientType);
100 110
         String json = JSON.toJSONString(fileConfigDTO);
101 111
         Map<String, Object> config = JSON.parseObject(json, Map.class);
102 112
         config.put("type", clientType);
103 113
         return config;
104 114
     }
105 115
 
106
-    private FileConfig openEditConfig(Map<String, Object> map, String mode, boolean collaborativeEditing,String clientType) {
116
+    private FileConfig openEditConfig(Map<String, Object> map, String mode, boolean collaborativeEditing, String clientType) {
107 117
         try {
108 118
             map.put("mode", mode);
109 119
             log.info("开始生成文件信息");
@@ -111,7 +121,7 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
111 121
             FileMetadata tempFileInfo = fileHandler.handlerFile(map, collaborativeEditing);
112 122
             //生成配置文件 TODO: 控制文件权限
113 123
             log.info("开始生成编辑器配置信息");
114
-            FileConfig fileConfigDTO = onlyOfficeConfigFactory.buildInitConfig(tempFileInfo.getUrl(), mode, tempFileInfo.getKey(), tempFileInfo.getOldName(),clientType);
124
+            FileConfig fileConfigDTO = onlyOfficeConfigFactory.buildInitConfig(tempFileInfo.getUrl(), mode, tempFileInfo.getKey(), tempFileInfo.getOldName(), clientType);
115 125
             log.info("生成编辑器配置信息结束");
116 126
             // TODO: 添加更多详细的自定义信息
117 127
             return fileConfigDTO;
@@ -160,7 +170,7 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
160 170
     @Override
161 171
     public OnFile handlerStatus(JSONObject jsonObject) throws Exception {
162 172
         OnFile file = new OnFile();
163
-        String fileId = null;
173
+        String fileId = jsonObject.getString("fileId");
164 174
         log.info("开始下载编辑器文件");
165 175
         int status = jsonObject.getIntValue("status");
166 176
         log.info("status[{}]:{}", status, jsonObject);
@@ -192,10 +202,11 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
192 202
                 if (caseId != null) {
193 203
                     fileInfoMap.put("caseId", caseId);
194 204
                 }
195
-                saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
205
+                //保存文件前进行自定义处理(该方法暂时没有实现业务逻辑)
206
+                //saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
196 207
 
197 208
                 // 保存文件
198
-                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);
199 210
                 log.info("文件信息1:" + JSON.toJSONString(tempFile.get().getFileInfo()));
200 211
                 log.info("文件信息2:" + JSON.toJSONString(map));
201 212
                 saveFileProcessor.saveAfterInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
@@ -300,7 +311,8 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
300 311
                 log.info("文件变动信息文件url:" + changesurl);
301 312
                 // 下载修改后文件
302 313
                 byte[] fileByte = FileUtil.getFileByte(url);
303
-                saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("key"));
314
+                saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("fileId"));
315
+                log.info("zouzhele==================");
304 316
             }
305 317
             removeTempFile(jsonObject);
306 318
             String id = (String) cache.get("getID_" + jsonObject.getString("key"));

+ 11
- 6
src/main/java/com/oo/onlyoffice/core/FileConfigFactory.java Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 package com.oo.onlyoffice.core;
2 2
 
3 3
 import cn.hutool.core.date.DateUtil;
4
+import com.alibaba.fastjson2.JSONObject;
4 5
 import com.oo.onlyoffice.config.OnlyProperties;
5 6
 import com.oo.onlyoffice.dto.DocumentConfig;
6 7
 import com.oo.onlyoffice.dto.EditorConfig;
@@ -43,7 +44,7 @@ public class FileConfigFactory implements OnlyOfficeConfigFactory {
43 44
      * @return 配置信息
44 45
      */
45 46
     @Override
46
-    public FileConfig buildInitConfig(String fileUrl, String mode, String key, String fileName,String clientType) {
47
+    public FileConfig buildInitConfig(String fileUrl, String mode, String key, String fileName, String clientType) {
47 48
 
48 49
         Map<String, Object> map = new HashMap<>();
49 50
 
@@ -74,11 +75,15 @@ public class FileConfigFactory implements OnlyOfficeConfigFactory {
74 75
         String callBackUrl = onlyProperties.getLocalhostAddress() + onlyProperties.getCallBackUrl();
75 76
 
76 77
         EditorConfig editorConfig = new EditorConfig(callBackUrl, mode);
77
-        editorConfig.setFileCustomization(getFileCustomization(mode,clientType));
78
+        editorConfig.setFileCustomization(getFileCustomization(mode, clientType));
78 79
         editorConfig.setFileUser(user);
79 80
         editorConfig.setPlugins(getPlugins());
80
-
81
-
81
+        //共同编辑"coEditing"配置:
82
+        //        "coEditing": {           "mode": "fast","change": true}
83
+        JSONObject coEditing = new JSONObject();
84
+        coEditing.put("mode", "fast");
85
+        coEditing.put("change", "true");
86
+        editorConfig.setCoEditing(coEditing);
82 87
         fileConfigDTO.setEditorConfig(editorConfig);
83 88
         map.put("editorConfig", editorConfig);
84 89
 
@@ -112,13 +117,13 @@ public class FileConfigFactory implements OnlyOfficeConfigFactory {
112 117
         return LoadConfigUtil.getCustomization();
113 118
     }
114 119
 
115
-    private FileCustomization getFileCustomization(String mode,String clientType) {
120
+    private FileCustomization getFileCustomization(String mode, String clientType) {
116 121
         FileCustomization customization = LoadConfigUtil.getCustomization();
117 122
         if (mode != null && !(mode.trim()).equals("") && mode.equals("edit")) {
118 123
             customization.setAutosave(true);
119 124
         }
120 125
         customization.setMobileForceView(false);
121
-        if(clientType!=null&&clientType!=""&&"mobile".equals(clientType)){
126
+        if (clientType != null && clientType != "" && "mobile".equals(clientType)) {
122 127
             customization.setMobileForceView(true);
123 128
         }
124 129
         return customization;

+ 4
- 3
src/main/java/com/oo/onlyoffice/core/SaveFileProcessor.java Просмотреть файл

@@ -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
      * 保存文件后进行自定义处理

+ 17
- 2
src/main/java/com/oo/onlyoffice/core/handler/FileHandlerImpl.java Просмотреть файл

@@ -77,8 +77,23 @@ public class FileHandlerImpl implements FileHandler {
77 77
                     cache.set(user.getId() + "_" + id, key);
78 78
                     cache.set("getID_" + key, id);
79 79
                 }
80
-            } else {//查看模式
81
-                key = id;
80
+            } else {
81
+                //查看模式
82
+                if (collaborativeEditing) {
83
+                    if (cache.get("collaborativeEditing_" + id) != null) {
84
+                        key = (String) cache.get("collaborativeEditing_" + id);
85
+                    } else {
86
+                        key = IdUtil.simpleUUID();
87
+                        cache.set("collaborativeEditing_" + id, key);
88
+                        cache.set("getID_" + key, id);
89
+                    }
90
+                } else {
91
+                    key = IdUtil.simpleUUID();
92
+                    FileUser user = SecurityUtils.getUserSession();
93
+                    cache.set(user.getId() + "_" + id, key);
94
+                    cache.set("getID_" + key, id);
95
+                }
96
+              //  key = id;
82 97
             }
83 98
 
84 99
 

+ 21
- 5
src/main/resources/application.yml Просмотреть файл

@@ -22,13 +22,13 @@ spring:
22 22
   datasource:
23 23
     type: com.alibaba.druid.pool.DruidDataSource
24 24
     driver-class-name: com.mysql.cj.jdbc.Driver
25
-    url: jdbc:mysql://121.40.189.20:3306/onlyoffice?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false
25
+    url: jdbc:mysql://121.40.189.20:3306/onlyoffice_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false
26 26
     username: root
27 27
     password: YMzc157#
28 28
   redis:
29 29
     host: 121.40.189.20
30 30
     port: 6389
31
-    database: 0
31
+    database: 1
32 32
 
33 33
 mybatis-plus:
34 34
   #  global-config:
@@ -45,13 +45,29 @@ spring:
45 45
   config:
46 46
     activate:
47 47
       on-profile: dev
48
-filepath: C:/onlyoffice/data/files
49
-serverhost: http://172.16.0.254
48
+  datasource:
49
+    type: com.alibaba.druid.pool.DruidDataSource
50
+    driver-class-name: com.mysql.cj.jdbc.Driver
51
+    url: jdbc:mysql://121.40.189.20:3306/onlyoffice_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false
52
+    username: root
53
+    password: YMzc157#
54
+filepath: /home/ruoyi/uploadPath/onlyoffice/data/files
55
+serverhost: http://121.40.189.20
50 56
 ---
51 57
 #生产环境属性配置
52 58
 spring:
53 59
   config:
54 60
     activate:
55 61
       on-profile: prod
56
-filepath: /onlyoffice/data/files
62
+  redis:
63
+    host: 121.40.189.20
64
+    port: 6389
65
+    database: 0
66
+  datasource:
67
+    type: com.alibaba.druid.pool.DruidDataSource
68
+    driver-class-name: com.mysql.cj.jdbc.Driver
69
+    url: jdbc:mysql://121.40.189.20:3306/onlyoffice?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false
70
+    username: root
71
+    password: YMzc157#
72
+filepath: /home/ruoyi/uploadPath/onlyoffice/data/files
57 73
 serverhost: http://121.40.189.20