优化回调保存接口(若传递文件id则每次保存只更新文件)
This commit is contained in:
@@ -236,9 +236,9 @@ public class OnlyOfficeController {
|
||||
* 6 - 正在编辑文档,但保存当前文档状态
|
||||
* 7 - 强制保存文档时发生错误
|
||||
*/
|
||||
@RequestMapping("/onlyOffice/save")
|
||||
@RequestMapping("/onlyOffice/save/{fileId}")
|
||||
@ResponseBody
|
||||
public void saveFile(HttpServletRequest request, HttpServletResponse response) {
|
||||
public void saveFile(HttpServletRequest request, HttpServletResponse response,@PathVariable(required = false) String fileId) {
|
||||
PrintWriter writer = null;
|
||||
try {
|
||||
writer = response.getWriter();
|
||||
@@ -246,6 +246,11 @@ public class OnlyOfficeController {
|
||||
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
|
||||
String body = scanner.hasNext() ? scanner.next() : "";
|
||||
JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
if(fileId!=null){
|
||||
jsonObject.put("fileId",fileId);
|
||||
}else{
|
||||
jsonObject.put("fileId","");
|
||||
}
|
||||
log.info("{}", jsonObject);
|
||||
WebsocketResult result = fileService.documentSave(jsonObject);
|
||||
/*
|
||||
@@ -258,9 +263,10 @@ public class OnlyOfficeController {
|
||||
userId = userSession.getId();
|
||||
}
|
||||
WebSocketServer.sendInfo(JSON.toJSONString(result.getOnFile()), userId);
|
||||
if (Objects.nonNull(writer)) {
|
||||
writer.write("{\"error\":0}");
|
||||
}
|
||||
// if (Objects.nonNull(writer)) {
|
||||
// writer.write("{\"error\":0}");
|
||||
// }
|
||||
writer.write("{\"error\":0}");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info("报错信息" + e.getMessage());
|
||||
|
||||
@@ -36,11 +36,23 @@ public class DemoService implements SaveFileProcessor {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储onlyoffice文件信息
|
||||
* @param map 文件元信息
|
||||
* @param file 文件
|
||||
* @param changes 文件变动信息
|
||||
* @param fileId 文件id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> save(Map<String, Object> map, byte[] file, byte[] changes, String key) {
|
||||
String fileId = "";
|
||||
public Map<String, Object> save(Map<String, Object> map, byte[] file, byte[] changes, String fileId) {
|
||||
try {
|
||||
if(fileId!=null&&fileId!=""){
|
||||
onFileService.updateFileByFileId(file, map.get("fileType").toString(),fileId);
|
||||
}else{
|
||||
fileId = "";
|
||||
fileId = onFileService.saveFile(file, map.get("fileType").toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public class FileService {
|
||||
public WebsocketResult documentSave(JSONObject jsonObject) {
|
||||
WebsocketResult result = new WebsocketResult();
|
||||
String key = "";
|
||||
String fileId = jsonObject.getString("fileId");
|
||||
try {
|
||||
int status = jsonObject.getIntValue("status");
|
||||
log.info("status[{}]", status);
|
||||
@@ -69,7 +70,13 @@ public class FileService {
|
||||
key = jsonObject.getString("key");
|
||||
log.info("key:" + JSON.toJSONString(key));
|
||||
//文件id
|
||||
String fileId = onlyServiceAPI.getFileId(key);
|
||||
Boolean isexistid = false;
|
||||
if (fileId != null && !"".equals(fileId)) {
|
||||
isexistid = true;
|
||||
}
|
||||
if (!isexistid) {
|
||||
fileId = onlyServiceAPI.getFileId(key);
|
||||
}
|
||||
log.info("fileId:" + JSON.toJSONString(fileId));
|
||||
if (fileId != null && !"".equals(fileId)) {
|
||||
//查询之前FileId对应的文件信息
|
||||
@@ -93,7 +100,11 @@ public class FileService {
|
||||
//处理文件的保存
|
||||
OnFile file = onlyServiceAPI.handlerStatus(jsonObject);
|
||||
//查询相同案件最新版本的文件信息
|
||||
file = getNewFileInofoByCaseId(file.getCaseId());
|
||||
if (fileId != null && !"".equals(fileId)) {
|
||||
file = getNewFileInofoByFileId(fileId);
|
||||
} else {
|
||||
file = getNewFileInofoByCaseId(file.getCaseId());
|
||||
}
|
||||
result = WebsocketResult.builder().onFile(file).userId(SecurityUtils.getUserSession().getId()).build();
|
||||
log.info("处理文件的保存:" + JSON.toJSONString(jsonObject));
|
||||
log.info("保存文件结束");
|
||||
@@ -101,10 +112,13 @@ public class FileService {
|
||||
return result;
|
||||
} else if (0 == status || 2 == status || 4 == status) {
|
||||
onlyServiceAPI.close(jsonObject);
|
||||
log.info("文件关闭====",status);
|
||||
} else if (3 == status || 7 == status) {
|
||||
//保存文档错误
|
||||
onlyServiceAPI.close(jsonObject);
|
||||
log.info("保存文档错误====",status);
|
||||
} else if (1 == status) {
|
||||
log.info("文件正在编辑====",status);
|
||||
//文件服务的回调 获取key判断当前文档有多少人在这使用
|
||||
List<Map> actions = JSONObject.parseArray(jsonObject.getString("actions"), Map.class);
|
||||
if ((Integer) actions.get(0).get("type") == 1) {
|
||||
@@ -145,4 +159,17 @@ public class FileService {
|
||||
log.info("caseId:" + caseId + ":fileinfo:" + JSON.toJSONString(file));
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件的fileid查询文件信息
|
||||
*/
|
||||
private OnFile getNewFileInofoByFileId(String fileId) {
|
||||
OnFile file = onFileMapper.selectById(fileId);
|
||||
if (file != null) {
|
||||
log.info("caseId:" + file.getCaseId() + ":fileinfo:" + JSON.toJSONString(file));
|
||||
} else {
|
||||
log.info("根据文件id未查询到文件信息");
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.oo.demo.entity.OnFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public interface OnFileService extends IService<OnFile> {
|
||||
/**
|
||||
* 保存onlyoffice文件
|
||||
*
|
||||
* @param bytes
|
||||
* @param fileType
|
||||
* @return
|
||||
@@ -17,14 +17,27 @@ public interface OnFileService extends IService<OnFile> {
|
||||
*/
|
||||
String saveFile(byte[] bytes, String fileType) throws Exception;
|
||||
|
||||
/**
|
||||
* 通过文件id更新onlyoffice文件
|
||||
*
|
||||
* @param bytes
|
||||
* @param fileType
|
||||
* @param fileId
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
String updateFileByFileId(byte[] bytes, String fileType, String fileId) throws Exception;
|
||||
|
||||
/**
|
||||
* 删除onlyoffice文件
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
void removeFile(String id);
|
||||
|
||||
/**
|
||||
* 下载onlyoffice文件
|
||||
*
|
||||
* @param id
|
||||
* @param isBrowser
|
||||
* @param response
|
||||
@@ -33,6 +46,7 @@ public interface OnFileService extends IService<OnFile> {
|
||||
|
||||
/**
|
||||
* 根据文件id查找文件信息
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -56,6 +56,14 @@ public class OnFileServiceImpl extends ServiceImpl<OnFileMapper, OnFile> impleme
|
||||
removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增文件信息到数据库
|
||||
*
|
||||
* @param bytes
|
||||
* @param fileType
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String saveFile(byte[] bytes, String fileType) throws Exception {
|
||||
|
||||
String fileId = IdUtil.simpleUUID();
|
||||
@@ -70,4 +78,33 @@ public class OnFileServiceImpl extends ServiceImpl<OnFileMapper, OnFile> impleme
|
||||
}
|
||||
return fileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文件信息到数据库
|
||||
*
|
||||
* @param bytes
|
||||
* @param fileType
|
||||
* @param fileId
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String updateFileByFileId(byte[] bytes, String fileType, String fileId) throws Exception {
|
||||
Boolean ishaveid = false;
|
||||
if (fileId != null && fileId != "") {
|
||||
ishaveid = true;
|
||||
}
|
||||
if (!ishaveid) {
|
||||
fileId = IdUtil.simpleUUID();
|
||||
}
|
||||
File file = new File(path + "/" + fileId + "." + fileType);
|
||||
File parentFile = file.getParentFile();
|
||||
if (!parentFile.exists()) {
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
try (FileOutputStream out = new FileOutputStream(path + "/" + fileId + "." + fileType)) {
|
||||
out.write(bytes);
|
||||
out.flush();
|
||||
}
|
||||
return fileId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
|
||||
@Override
|
||||
public OnFile handlerStatus(JSONObject jsonObject) throws Exception {
|
||||
OnFile file = new OnFile();
|
||||
String fileId = null;
|
||||
String fileId = jsonObject.getString("fileId");
|
||||
log.info("开始下载编辑器文件");
|
||||
int status = jsonObject.getIntValue("status");
|
||||
log.info("status[{}]:{}", status, jsonObject);
|
||||
@@ -202,10 +202,11 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
|
||||
if (caseId != null) {
|
||||
fileInfoMap.put("caseId", caseId);
|
||||
}
|
||||
saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
|
||||
//保存文件前进行自定义处理(该方法暂时没有实现业务逻辑)
|
||||
//saveFileProcessor.saveBeforeInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
|
||||
|
||||
// 保存文件
|
||||
Map<String, Object> map = saveFileProcessor.save(tempFile.get().getFileInfo(), fileByte, changes, key);
|
||||
Map<String, Object> map = saveFileProcessor.save(tempFile.get().getFileInfo(), fileByte, changes, fileId);
|
||||
log.info("文件信息1:" + JSON.toJSONString(tempFile.get().getFileInfo()));
|
||||
log.info("文件信息2:" + JSON.toJSONString(map));
|
||||
saveFileProcessor.saveAfterInitialization(tempFile.get().getFileInfo(), fileByte, fileExtension);
|
||||
@@ -310,7 +311,8 @@ public class OnlyServiceAPIImpl implements OnlyServiceAPI {
|
||||
log.info("文件变动信息文件url:" + changesurl);
|
||||
// 下载修改后文件
|
||||
byte[] fileByte = FileUtil.getFileByte(url);
|
||||
saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("key"));
|
||||
saveFileProcessor.save(tempFile.getFileInfo(), fileByte, null, jsonObject.getString("fileId"));
|
||||
log.info("zouzhele==================");
|
||||
}
|
||||
removeTempFile(jsonObject);
|
||||
String id = (String) cache.get("getID_" + jsonObject.getString("key"));
|
||||
|
||||
@@ -16,13 +16,14 @@ public interface SaveFileProcessor {
|
||||
void saveBeforeInitialization(Map<String, Object> map,byte[] bytes,String fileExtension) throws Exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* 存储onlyoffice文件信息
|
||||
* @param map 文件元信息
|
||||
* @param file 文件
|
||||
* @param changes 文件变动信息
|
||||
* @return 返回新的文件信息
|
||||
* @param fileId 文件id
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> save(Map<String, Object> map,byte[] file, byte[] changes,String key);
|
||||
Map<String, Object> save(Map<String, Object> map,byte[] file, byte[] changes,String fileId);
|
||||
|
||||
/**
|
||||
* 保存文件后进行自定义处理
|
||||
|
||||
Reference in New Issue
Block a user