最新onlyoffice后端
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.oo.demo.service;
|
||||
|
||||
import com.oo.demo.entity.OnFile;
|
||||
import com.oo.onlyoffice.config.OnlyProperties;
|
||||
import com.oo.onlyoffice.core.SaveFileProcessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @BelongsProject: onlyoffice-demo
|
||||
* @BelongsPackage: com.oo.demo.service
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2023-08-01 16:00
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class DemoService implements SaveFileProcessor {
|
||||
|
||||
@Autowired
|
||||
private OnlyProperties onlyProperties;
|
||||
@Autowired
|
||||
private OnFileService onFileService;
|
||||
|
||||
|
||||
@Override
|
||||
public void saveBeforeInitialization(Map<String, Object> map, byte[] bytes, String fileExtension) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> save(Map<String, Object> map, byte[] file, byte[] changes, String key){
|
||||
String fileId = "";
|
||||
try {
|
||||
fileId = onFileService.saveFile(file,map.get("fileType").toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String version = map.get("version").toString();
|
||||
|
||||
String[] split = version.split("\\.");
|
||||
version = split[0]+"."+split[1]+"."+ (Integer.valueOf(split[2])+1);
|
||||
|
||||
OnFile onFile = new OnFile();
|
||||
onFile.setFileId(fileId);
|
||||
onFile.setFileName(map.get("fileName").toString());
|
||||
onFile.setVersion(version);
|
||||
onFile.setFileSize((long) file.length);
|
||||
onFile.setFileType(map.get("fileType").toString());
|
||||
onFileService.save(onFile);
|
||||
|
||||
map.put("version", version);
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveAfterInitialization(Map<String, Object> map, byte[] bytes, String fileExtension) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.oo.demo.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.oo.onlyoffice.api.OnlyServiceAPI;
|
||||
import com.oo.onlyoffice.dto.edit.FileUser;
|
||||
import com.oo.onlyoffice.tools.SecurityUtils;
|
||||
import com.oo.demo.entity.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
//import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @BelongsProject: onlyoffice-demo
|
||||
* @BelongsPackage: com.oo.demo.service
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2023-08-02 09:57
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FileService {
|
||||
|
||||
@Autowired
|
||||
private OnlyServiceAPI onlyServiceAPI;
|
||||
@Autowired
|
||||
private DemoService demoService;
|
||||
|
||||
|
||||
/**
|
||||
* 文档服务器 保存文件回调
|
||||
*
|
||||
* @param jsonObject
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Object documentSave(JSONObject jsonObject) {
|
||||
JSONObject result = new JSONObject();
|
||||
String key = "";
|
||||
try {
|
||||
int status = jsonObject.getIntValue("status");
|
||||
log.info("status[{}]", status);
|
||||
if (6 == status) {
|
||||
log.info("开始保存文件");
|
||||
|
||||
JSONArray jsonArray = jsonObject.getJSONObject("history").getJSONArray("changes");
|
||||
JSONObject object = jsonArray.getJSONObject(0);
|
||||
String userId = object.getJSONObject("user").getString("id");
|
||||
FileUser fileUser = new FileUser();
|
||||
fileUser.setId(userId);
|
||||
SecurityUtils.setUserSession(fileUser);
|
||||
|
||||
key = jsonObject.getString("key");
|
||||
|
||||
//文件id
|
||||
String id = onlyServiceAPI.getFileId(key);
|
||||
|
||||
|
||||
//判断是否是最后一人进行保存
|
||||
int users = onlyServiceAPI.getUserNum(key);
|
||||
if (users > 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//历史版本最大个数 获取当前文件的历史版本数量
|
||||
Integer histNum = onlyServiceAPI.getHistNum();
|
||||
/**
|
||||
* 如果有需要保存历史记录 可以进行相关操作
|
||||
*/
|
||||
if (null != histNum) {
|
||||
|
||||
}
|
||||
|
||||
//处理文件的保存
|
||||
onlyServiceAPI.handlerStatus(jsonObject);
|
||||
|
||||
|
||||
log.info("保存文件结束");
|
||||
SecurityUtils.removeUserSession();
|
||||
result.put("key", key);
|
||||
result.put("fileId", id);
|
||||
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);
|
||||
}
|
||||
result.put("key", key);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.oo.demo.service;
|
||||
|
||||
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> {
|
||||
|
||||
String saveFile(byte[] bytes, String fileType) throws Exception;
|
||||
|
||||
void removeFile(String id);
|
||||
|
||||
void download(String id,String isBrowser, HttpServletResponse response);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.oo.demo.service;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.oo.demo.dao.OnFileMapper;
|
||||
import com.oo.demo.entity.OnFile;
|
||||
import com.oo.onlyoffice.tools.FileUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Files;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @since 2022-03-10
|
||||
*/
|
||||
@Service
|
||||
public class OnFileServiceImpl extends ServiceImpl<OnFileMapper, OnFile> implements OnFileService {
|
||||
@Value("${filepath}")
|
||||
private String path;
|
||||
|
||||
@Override
|
||||
public void download(String id, String isBrowser, HttpServletResponse response) {
|
||||
OnFile byId = baseMapper.selectById(id);
|
||||
|
||||
//判断是否有值
|
||||
if (StrUtil.isNotEmpty(isBrowser)) {
|
||||
FileUtil.downloadAttachment(path + "/" + id + "." + byId.getFileType(), byId.getFileName(), response);
|
||||
} else {
|
||||
FileUtil.downloadInline(path + "/" + id + "." + byId.getFileType(), byId.getFileName(), response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFile(String id) {
|
||||
OnFile byId = baseMapper.selectById(id);
|
||||
System.out.println(path);
|
||||
File file = new File(path + "/" + id + "." + byId.getFileType());
|
||||
System.out.println("删除文件");
|
||||
FileUtils.deleteQuietly(file);
|
||||
|
||||
removeById(id);
|
||||
}
|
||||
|
||||
public String saveFile(byte[] bytes, String fileType) throws Exception {
|
||||
|
||||
String 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.oo.demo.service;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @BelongsProject: onlyoffice-demo
|
||||
* @BelongsPackage: com.oo.demo.service
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2023-08-05 17:14
|
||||
* @Description: TODO 用户信息
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class TempUser {
|
||||
private static Logger logger = LoggerFactory.getLogger(TempUser.class);
|
||||
private static List<Map<String,String>> cache1 = new ArrayList<>();
|
||||
private static Integer index;
|
||||
|
||||
static {
|
||||
for (int i = 0; i < 1; i++) {
|
||||
Map<String,String> map = new HashMap<>();
|
||||
map.put("userId", "1");
|
||||
map.put("userName","OnlyOfficeUserName"+map.get("userId"));
|
||||
cache1.add(map);
|
||||
}
|
||||
logger.info("用户初始完毕");
|
||||
Random random = new Random();
|
||||
// index = random.nextInt(10);
|
||||
index = 0;
|
||||
logger.info("获取用户完毕"+index);
|
||||
}
|
||||
public static String getUserName(){
|
||||
return cache1.get(index).get("userName");
|
||||
}
|
||||
public static String getUserId(){
|
||||
return cache1.get(index).get("userId");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user