Files
OnlyOffice-Backend/src/main/java/com/oo/demo/service/FileService.java
T

149 lines
6.0 KiB
Java
Raw Normal View History

2024-03-14 09:05:00 +08:00
package com.oo.demo.service;
2024-03-14 17:59:11 +08:00
import com.alibaba.fastjson.JSON;
2024-03-14 09:05:00 +08:00
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
2024-03-15 16:02:12 +08:00
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oo.demo.dao.OnFileMapper;
2024-03-14 17:59:11 +08:00
import com.oo.demo.entity.OnFile;
import com.oo.demo.entity.WebsocketResult;
2024-03-14 09:05:00 +08:00
import com.oo.onlyoffice.api.OnlyServiceAPI;
import com.oo.onlyoffice.dto.edit.FileUser;
import com.oo.onlyoffice.tools.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
/**
* @BelongsProject: onlyoffice-demo
* @BelongsPackage: com.oo.demo.service
* @CreateTime: 2023-08-02 09:57
* @Description: TODO
* @Version: 1.0
*/
@Slf4j
@Service
public class FileService {
@Autowired
private OnlyServiceAPI onlyServiceAPI;
@Autowired
private DemoService demoService;
2024-03-15 16:02:12 +08:00
@Autowired
private OnFileService onFileService;
2024-03-14 09:05:00 +08:00
/**
* 文档服务器 保存文件回调
2024-03-15 16:02:12 +08:00
* Defines the status of the document. Can have the following values:
* 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.
2024-03-14 09:05:00 +08:00
*
* @param jsonObject
*/
@Transactional(rollbackFor = Exception.class)
2024-03-14 17:59:11 +08:00
public WebsocketResult documentSave(JSONObject jsonObject) {
WebsocketResult result = new WebsocketResult();
2024-03-14 09:05:00 +08:00
String key = "";
try {
int status = jsonObject.getIntValue("status");
log.info("status[{}]", status);
if (6 == status) {
log.info("开始保存文件");
2024-03-14 17:59:11 +08:00
log.info("dayin:" + JSON.toJSONString(jsonObject));
2024-03-14 09:05:00 +08:00
JSONArray jsonArray = jsonObject.getJSONObject("history").getJSONArray("changes");
2024-03-14 17:59:11 +08:00
log.info("历史版本:" + JSON.toJSONString(jsonArray));
2024-03-14 09:05:00 +08:00
JSONObject object = jsonArray.getJSONObject(0);
String userId = object.getJSONObject("user").getString("id");
FileUser fileUser = new FileUser();
fileUser.setId(userId);
SecurityUtils.setUserSession(fileUser);
2024-03-14 17:59:11 +08:00
log.info("fileuser:" + JSON.toJSONString(fileUser));
2024-03-14 09:05:00 +08:00
key = jsonObject.getString("key");
2024-03-14 17:59:11 +08:00
log.info("key:" + JSON.toJSONString(key));
2024-03-14 09:05:00 +08:00
//文件id
2024-03-14 17:59:11 +08:00
String fileId = onlyServiceAPI.getFileId(key);
log.info("fileId:" + JSON.toJSONString(fileId));
2024-03-15 16:02:12 +08:00
if (fileId != null && !"".equals(fileId)) {
//查询之前FileId对应的文件信息
OnFile onfile = onFileService.getFileById(fileId);
jsonObject.put("caseId", onfile.getCaseId());
}
2024-03-14 09:05:00 +08:00
//判断是否是最后一人进行保存
int users = onlyServiceAPI.getUserNum(key);
2024-03-14 17:59:11 +08:00
// if (users > 1) {
// return null;
// }
2024-03-14 09:05:00 +08:00
//历史版本最大个数 获取当前文件的历史版本数量
Integer histNum = onlyServiceAPI.getHistNum();
2024-03-14 17:59:11 +08:00
log.info("历史版本最大个数" + histNum);
2024-03-14 09:05:00 +08:00
/**
* 如果有需要保存历史记录 可以进行相关操作
*/
if (null != histNum) {
}
//处理文件的保存
2024-03-14 17:59:11 +08:00
OnFile file = onlyServiceAPI.handlerStatus(jsonObject);
2024-03-15 16:02:12 +08:00
//查询相同案件最新版本的文件信息
file = getNewFileInofoByCaseId(file.getCaseId());
2024-03-14 17:59:11 +08:00
result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
2024-03-14 09:05:00 +08:00
log.info("保存文件结束");
SecurityUtils.removeUserSession();
return result;
} else if (0 == status || 2 == status || 4 == status) {
onlyServiceAPI.close(jsonObject);
} else if (3 == status || 7 == status) {
//保存文档错误
onlyServiceAPI.close(jsonObject);
} else if (1 == status) {
//文件服务的回调 获取key判断当前文档有多少人在这使用
List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
if ((Integer) actions.get(0).get("type") == 1) {
log.info("当前用户有[{}]", jsonObject.getString("users"));
List<String> users = JSONObject.parseArray(jsonObject.getString("users"), String.class);
onlyServiceAPI.iskey(jsonObject.getString("key"), users.size());
}
if ((Integer) actions.get(0).get("type") == 0) {
onlyServiceAPI.iskey(jsonObject.getString("key"), null);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
2024-03-14 17:59:11 +08:00
2024-03-14 09:05:00 +08:00
return result;
}
2024-03-15 16:02:12 +08:00
@Autowired
OnFileMapper onFileMapper;
2024-03-14 09:05:00 +08:00
2024-03-15 16:02:12 +08:00
/**
* 查询相同案件最新版本的文件信息
*
* @param caseId
* @return
*/
private OnFile getNewFileInofoByCaseId(String caseId) {
OnFile file = new OnFile();
QueryWrapper<OnFile> fileQueryWrapper = new QueryWrapper<>();
fileQueryWrapper.eq("case_id", caseId);
fileQueryWrapper.orderByDesc("created_time");
List<OnFile> onFiles = onFileMapper.selectList(fileQueryWrapper);
if (onFiles != null && onFiles.size() > 0) {
file = onFiles.get(0);
}
log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
return file;
}
2024-03-14 09:05:00 +08:00
}