最新onlyoffice后端
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.oo.demo.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @Author: ymbgy
|
||||
* @Date: 2022-06-13 13:31
|
||||
*/
|
||||
@Configuration
|
||||
public class CrossOriginConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins("*")
|
||||
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
|
||||
.maxAge(3600)
|
||||
.allowCredentials(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.oo.demo.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
||||
/**
|
||||
* @BelongsProject: leaf-onlyoffice
|
||||
* @BelongsPackage: com.ideayp.leaf.config
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2022-12-01 17:37
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class MultipartResolverConfig {
|
||||
@Bean(name="multipartResolver")
|
||||
public MultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
resolver.setDefaultEncoding("UTF-8");
|
||||
resolver.setResolveLazily(true);
|
||||
resolver.setMaxInMemorySize(102400000);
|
||||
resolver.setMaxUploadSize(102400000);
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.oo.demo.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* MybatisPlusConfig.java
|
||||
* @Description 配置分页插件
|
||||
* @Date 2022/3/24 9:51
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
package com.oo.demo.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.oo.demo.entity.OnFile;
|
||||
import com.oo.demo.entity.Result;
|
||||
import com.oo.demo.service.FileService;
|
||||
import com.oo.demo.service.OnFileService;
|
||||
import com.oo.demo.service.TempUser;
|
||||
import com.oo.onlyoffice.api.OnlyServiceAPI;
|
||||
import com.oo.onlyoffice.dto.edit.FileUser;
|
||||
import com.oo.onlyoffice.tools.FileUtil;
|
||||
import com.oo.onlyoffice.tools.SecurityUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @BelongsProject: leaf-onlyoffice
|
||||
* @BelongsPackage: com.ideayp.leaf.onlyoffice.controller
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2022-11-08 18:11
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class OnlyOfficeController {
|
||||
|
||||
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
@Autowired
|
||||
private OnFileService onFileService;
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
@Autowired
|
||||
private OnlyServiceAPI onlyServiceAPI;
|
||||
@Value("${filepath}")
|
||||
private String filepath;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String filesView(Model model) {
|
||||
return "/index";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/file/all")
|
||||
@ResponseBody
|
||||
public Object file(@RequestParam Map<String, String> parameter) {
|
||||
int page = Integer.parseInt(parameter.get("page"));
|
||||
int limit = Integer.parseInt(parameter.get("limit"));
|
||||
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
// queryWrapper.notLike("id","MD");
|
||||
queryWrapper.orderByDesc("created_time");
|
||||
|
||||
Page<OnFile> p = new Page<>(page, limit);
|
||||
p = onFileService.page(p, queryWrapper);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("code", 0);
|
||||
map.put("msg", "SUCCESS");
|
||||
map.put("count", p.getTotal());
|
||||
map.put("data", p.getRecords());
|
||||
return map;
|
||||
}
|
||||
|
||||
@PostMapping("/files/upload")
|
||||
@ResponseBody
|
||||
public Object upload(@RequestParam("file") MultipartFile[] file) {
|
||||
List<OnFile> result = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (MultipartFile multipartFile : file) {
|
||||
CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
|
||||
|
||||
String filename = multipartFile.getOriginalFilename();
|
||||
// 获取文件后缀
|
||||
String suffix = filename.substring(filename.lastIndexOf(".") + 1);
|
||||
String fileId = "";
|
||||
try {
|
||||
fileId = onFileService.saveFile(commonsMultipartFile.getBytes(), suffix);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
OnFile onFile = new OnFile();
|
||||
onFile.setFileId(fileId);
|
||||
onFile.setFileName(filename);
|
||||
onFile.setVersion("1.0.0");
|
||||
onFile.setCreatedTime(sdf.format(new Date()));
|
||||
onFile.setFileSize(commonsMultipartFile.getSize());
|
||||
onFile.setFileType(suffix);
|
||||
onFile.setFilePath(filepath + "/" + fileId + "." + suffix);
|
||||
onFileService.save(onFile);
|
||||
result.add(onFile);
|
||||
i++;
|
||||
}
|
||||
if (file.length == i) {
|
||||
log.info("文件上传成功");
|
||||
return result;
|
||||
}
|
||||
return Result.error("文件上传失败");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 打开编辑
|
||||
*/
|
||||
@RequestMapping("/onlyOfficeConfig/{mode}/{id}")
|
||||
@ResponseBody
|
||||
public Object openDocument(@PathVariable(required = false) String mode, @PathVariable(required = false) String id, Model model) {//@RequestParam("url") String url,
|
||||
log.info("only office file:" + id);
|
||||
OnFile onFile = onFileService.getById(id);
|
||||
if (onFile == null) {
|
||||
return Result.error("文件不存在");
|
||||
}
|
||||
/**
|
||||
* 必要步骤
|
||||
*/
|
||||
FileUser user = new FileUser();
|
||||
user.setId(TempUser.getUserId());
|
||||
user.setName(TempUser.getUserName());
|
||||
SecurityUtils.setUserSession(user);
|
||||
/**
|
||||
* 必要步骤
|
||||
*/
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("fileId", onFile.getFileId());
|
||||
map.put("fileName", onFile.getFileName());
|
||||
map.put("fileType", onFile.getFileType());
|
||||
map.put("fileSize", onFile.getFileSize());
|
||||
map.put("version", onFile.getVersion());
|
||||
|
||||
Map config = onlyServiceAPI.openDocument(map, mode, false);
|
||||
|
||||
SecurityUtils.removeUserSession();
|
||||
|
||||
String s = JSON.toJSONString(config);
|
||||
log.info("only office config:" + s);
|
||||
return s;
|
||||
}
|
||||
// @RequestMapping("/onlyOffice/{mode}/{id}")
|
||||
// public ModelAndView openDocument(@PathVariable String mode,@PathVariable String id, Model model) {//@RequestParam("url") String url,
|
||||
// log.info("only office key:" + id);
|
||||
// OnFile onFile = onFileService.getById(id);
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 必要步骤
|
||||
// */
|
||||
// FileUser user = new FileUser();
|
||||
// user.setId(TempUser.getUserId());
|
||||
// user.setName(TempUser.getUserName());
|
||||
// SecurityUtils.setUserSession(user);
|
||||
// /**
|
||||
// * 必要步骤
|
||||
// */
|
||||
// Map<String,Object> map = new HashMap<>();
|
||||
// map.put("fileId",onFile.getFileId());
|
||||
// map.put("fileName",onFile.getFileName());
|
||||
// map.put("fileType",onFile.getFileType());
|
||||
// map.put("fileSize",onFile.getFileSize());
|
||||
// map.put("version",onFile.getVersion());
|
||||
//
|
||||
// Map config = onlyServiceAPI.openDocument(map, mode, false);
|
||||
//
|
||||
// SecurityUtils.removeUserSession();
|
||||
//
|
||||
// String s = JSON.toJSONString(config);
|
||||
// log.info("only office config:" + s);
|
||||
// model.addAllAttributes(config);
|
||||
// return new ModelAndView("onlyOffice");
|
||||
// }
|
||||
|
||||
/**
|
||||
* 文档编辑服务使用JavaScript API通知callbackUrl,向文档存储服务通知文档编辑的状态。
|
||||
* 文档编辑服务使用具有正文中的信息的POST请求。
|
||||
* https://api.onlyoffice.com/editors/callback
|
||||
* 当我们关闭编辑窗口后,十秒钟左右only office会将它存储的我们的编辑后的文件
|
||||
* 0 - 找不到具有密钥标识符的文档
|
||||
* 1 - 正在编辑文档
|
||||
* 2 - 文档已准备好保存
|
||||
* 3 - 发生文档保存错误
|
||||
* 4 - 不作任何更改就关闭文档
|
||||
* 6 - 正在编辑文档,但保存当前文档状态
|
||||
* 7 - 强制保存文档时发生错误
|
||||
*/
|
||||
@RequestMapping("/onlyOffice/save")
|
||||
@ResponseBody
|
||||
public Object saveFile(HttpServletRequest request, HttpServletResponse response) {
|
||||
PrintWriter writer = null;
|
||||
try {
|
||||
writer = response.getWriter();
|
||||
// 获取传输的json数据
|
||||
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
|
||||
String body = scanner.hasNext() ? scanner.next() : "";
|
||||
JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
log.info("{}", jsonObject);
|
||||
|
||||
Object result = fileService.documentSave(jsonObject);
|
||||
if (Objects.nonNull(writer)) {
|
||||
writer.write("{\"error\":0}");
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
writer.write("{\"error\":-1}");
|
||||
return Result.error("保存出错");
|
||||
}
|
||||
/*
|
||||
* status = 1,我们给onlyOffice的服务返回{"error":"0"}的信息。
|
||||
* 这样onlyOffice会认为回调接口是没问题的,这样就可以在线编辑文档了,否则的话会弹出窗口说明
|
||||
*/
|
||||
// if (Objects.nonNull(writer)) {
|
||||
// writer.write("{\"error\":0}");
|
||||
// }
|
||||
}
|
||||
|
||||
@PostMapping("/save/{id}")
|
||||
@ResponseBody
|
||||
public Object save(@PathVariable(required = false) String id) {
|
||||
OnFile onFile = onFileService.getById(id);
|
||||
if (onFile == null) {
|
||||
return Result.error("文件不存在,保存失败");
|
||||
}
|
||||
/**
|
||||
* 必要步骤
|
||||
*/
|
||||
try {
|
||||
FileUser user = new FileUser();
|
||||
user.setId(TempUser.getUserId());
|
||||
user.setName(TempUser.getUserName());
|
||||
SecurityUtils.setUserSession(user);
|
||||
String key = onlyServiceAPI.getKey(onFile.getFileId());
|
||||
String msg = onlyServiceAPI.save(key, TempUser.getUserId());
|
||||
SecurityUtils.removeUserSession();
|
||||
OnFile fileInfo = new OnFile();
|
||||
fileInfo.setFileId(id);
|
||||
fileInfo.setFileName(onFile.getFileName());
|
||||
fileInfo.setFileType(onFile.getFileType());
|
||||
fileInfo.setFilePath(filepath + "/" + id + "." + onFile.getFileType());
|
||||
return fileInfo;
|
||||
} catch (Exception e) {
|
||||
return Result.error("保存失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/converted")
|
||||
public void converted(String id, String suffix, HttpServletResponse response) {
|
||||
try {
|
||||
OnFile onFile = onFileService.getById(id);
|
||||
String title = onFile.getFileName().replace(onFile.getFileType(), suffix);
|
||||
String path = onlyServiceAPI.converted(onFile.getFileType(), onFile.getFileId(), suffix, title, null);
|
||||
|
||||
FileUtil.downloadAttachment(path, title, response);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/del")
|
||||
@ResponseBody
|
||||
public Object delFile(String id) {
|
||||
try {
|
||||
onFileService.removeFile(id);
|
||||
return Result.OK("删除成功", "fileTable");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Result.error("删除失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param isBrowser 用来判断是否是oo服务的回调下载 1.没有值 = oo服务回调下载。2.有值 = 页面手动下载
|
||||
* @param response
|
||||
*/
|
||||
@RequestMapping("/download/{id}")
|
||||
public void download(@PathVariable String id, String isBrowser, HttpServletResponse response) {
|
||||
try {
|
||||
onFileService.download(id, isBrowser, response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.oo.demo.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.oo.demo.entity.OnFile;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @since 2022-03-10
|
||||
*/
|
||||
@Mapper
|
||||
public interface OnFileMapper extends BaseMapper<OnFile> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.oo.demo.entity;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @BelongsProject: leaf-onlyoffice
|
||||
* @BelongsPackage: com.ideayp.leaf.entity
|
||||
* @Author: TongHui
|
||||
* @CreateTime: 2022-11-14 10:20
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
@TableName("on_file")
|
||||
public class OnFile {
|
||||
@TableId
|
||||
private String fileId;
|
||||
@TableField("`version`")
|
||||
private String version;
|
||||
private String createdTime;
|
||||
private String userName;
|
||||
private String userId;
|
||||
private String fileName;
|
||||
private String fileType;
|
||||
private String filePath;
|
||||
private Long fileSize;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.oo.demo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 接口返回数据格式
|
||||
* @author scott
|
||||
* @email jeecgos@163.com
|
||||
* @date 2019年1月19日
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 成功标志
|
||||
*/
|
||||
private boolean success = true;
|
||||
|
||||
/**
|
||||
* 返回处理消息
|
||||
*/
|
||||
private String message = "";
|
||||
|
||||
/**
|
||||
* 返回代码
|
||||
*/
|
||||
private Integer code = 200;
|
||||
|
||||
/**
|
||||
* 返回数据对象 data
|
||||
*/
|
||||
private T result;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private long timestamp = System.currentTimeMillis();
|
||||
|
||||
public Result() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容VUE3版token失效不跳转登录页面
|
||||
* @param code
|
||||
* @param message
|
||||
*/
|
||||
public Result(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Result<T> success(String message) {
|
||||
this.message = message;
|
||||
this.code = 200;
|
||||
this.success = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static<T> Result<T> OK() {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(200);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
public static<T> Result<T> OK(String msg) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(200);
|
||||
r.setMessage(msg);
|
||||
//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
|
||||
r.setResult((T) msg);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static<T> Result<T> OK(T data) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(200);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static<T> Result<T> OK(String msg, T data) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(200);
|
||||
r.setMessage(msg);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static<T> Result<T> error(String msg, T data) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setSuccess(false);
|
||||
r.setCode(500);
|
||||
r.setMessage(msg);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static<T> Result<T> error(String msg) {
|
||||
return error(500, msg);
|
||||
}
|
||||
|
||||
|
||||
public static<T> Result<T> error(int code, String msg) {
|
||||
Result<T> r = new Result<T>();
|
||||
r.setCode(code);
|
||||
r.setMessage(msg);
|
||||
r.setSuccess(false);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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