|
|
@@ -1,5 +1,6 @@
|
|
|
package com.github.jfcloud.excel.editor.docdeal.controller;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.io.file.FileNameUtil;
|
|
|
import cn.hutool.core.lang.Pair;
|
|
|
@@ -24,7 +25,6 @@ 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.client.RestTemplate;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@@ -38,8 +38,6 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
@Controller
|
|
|
public class FileController {
|
|
|
- @Autowired
|
|
|
- RestTemplate restTemplate;
|
|
|
@Autowired
|
|
|
OssTemplate ossTemplate;
|
|
|
@Autowired
|
|
|
@@ -108,73 +106,81 @@ public class FileController {
|
|
|
}
|
|
|
log.info("==> 下载文档: name={}, bucket={}", name, bucket);
|
|
|
|
|
|
- long size = ossTemplate.getSize(bucket, name);
|
|
|
-
|
|
|
try {
|
|
|
- if (size < 0) {
|
|
|
+ S3Object s3Object = ossTemplate.getObject(bucket, name);
|
|
|
+ if (s3Object == null) {
|
|
|
log.error("文档不存在 name={}, bucket={}", name, bucket);
|
|
|
- response.getWriter().println("Sorry, we cannot find the file, pls check the file name and try again. --" + System.currentTimeMillis());
|
|
|
+ response.getWriter().println(DateUtil.now() + " Sorry, we cannot find the file ( T_T )");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
response.setContentType("application/octet-stream");
|
|
|
+ response.setContentLength((int) s3Object.getObjectMetadata().getContentLength());
|
|
|
response.setHeader("Content-Disposition", "attachment;filename*=utf8''" + URLEncodeUtil.encode(name));
|
|
|
- response.setHeader("Content-Length", String.valueOf(size));
|
|
|
|
|
|
- S3Object s3Object = ossTemplate.getObject(bucket, name);
|
|
|
IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
|
|
|
} catch (IOException e) {
|
|
|
log.error("文档下载失败 name={}, error={}", name, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("/reviewExcel")
|
|
|
+ public String reviewExcel(PreviewVo vo, Model model) {
|
|
|
+ return reviewDocFile(vo, model);
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/review")
|
|
|
- public String reviewDocFile(@RequestParam(required = false) String bucket,
|
|
|
- @RequestParam(required = false) String title,
|
|
|
- @RequestParam(required = false) String url,
|
|
|
- @RequestParam(required = false) String name,
|
|
|
- @RequestParam String userId,
|
|
|
- @RequestParam String userName,
|
|
|
- Model model) {
|
|
|
- if (StrUtil.isNotBlank(url)) {
|
|
|
- url = url.replace("/admin/sys-file/", "");
|
|
|
- bucket = url.substring(0, url.indexOf('/'));
|
|
|
- name = url.substring(url.lastIndexOf('/') + 1);
|
|
|
+ public String reviewDocFile(PreviewVo vo, Model model) {
|
|
|
+ vo.setEdit(false);
|
|
|
+ log.info("==> 查看文档 vo: {}", vo);
|
|
|
+ if (!ossTemplate.exist(vo.getBucket(), vo.getName())) {
|
|
|
+ model.addAttribute("name", vo.getName());
|
|
|
+ return "/notFound";
|
|
|
}
|
|
|
|
|
|
- log.info("==> 预览文档 bucket: {}, name: {}", bucket, name);
|
|
|
-
|
|
|
- Pair<Document, DocumentEditParam> pair = docPair(bucket, title, name, userId, userName);
|
|
|
- Document document = pair.getKey();
|
|
|
- model.addAttribute("document", document);
|
|
|
+ Pair<Document, DocumentEditParam> pair = docInfo(vo);
|
|
|
+ model.addAttribute("document", pair.getKey());
|
|
|
model.addAttribute("documentEditParam", pair.getValue());
|
|
|
|
|
|
- if (document.getFileType().equals("xls") || document.getFileType().equals("xlsx")) {
|
|
|
+ String suffix = FileNameUtil.getSuffix(vo.getName());
|
|
|
+ if (suffix.equals("xls") || suffix.equals("xlsx")) {
|
|
|
return "/viewerExcel";
|
|
|
}
|
|
|
return "/viewer";
|
|
|
}
|
|
|
|
|
|
- private Pair<Document, DocumentEditParam> docPair(String bucket, String title, String name,
|
|
|
- String userId, String userName) {
|
|
|
+ @GetMapping("/edit")
|
|
|
+ public String editDocFile(PreviewVo vo, Model model) {
|
|
|
+ vo.setEdit(true);
|
|
|
+ log.info("==> 编辑文档 vo: {}", vo);
|
|
|
+ if (!ossTemplate.exist(vo.getBucket(), vo.getName())) {
|
|
|
+ model.addAttribute("name", vo.getName());
|
|
|
+ return "/notFound";
|
|
|
+ }
|
|
|
+ Pair<Document, DocumentEditParam> pair = docInfo(vo);
|
|
|
+ model.addAttribute("document", pair.getKey());
|
|
|
+ model.addAttribute("documentEditParam", pair.getValue());
|
|
|
+ return "/editor";
|
|
|
+ }
|
|
|
|
|
|
- title = StrUtil.isBlank(title) ? name : title;
|
|
|
- bucket = StrUtil.isBlank(bucket) ? ossProperties.getBucketName() : bucket;
|
|
|
- String downloadUrl = String.format(DocumentConstants.OFFICE_API_DOC_FILE_BUCKET, serverUrl, name, bucket) + "&_t=" + System.currentTimeMillis();
|
|
|
+ private Pair<Document, DocumentEditParam> docInfo(PreviewVo vo) {
|
|
|
+ String title = StrUtil.blankToDefault(vo.getTitle(), vo.getName());
|
|
|
+ String bucket = StrUtil.blankToDefault(vo.getBucket(), ossProperties.getBucketName());
|
|
|
+ String downloadUrl = String.format(DocumentConstants.OFFICE_API_DOC_FILE_BUCKET, serverUrl, vo.getName(), bucket) + "&_t=" + System.currentTimeMillis();
|
|
|
|
|
|
Document document = Document.builder()
|
|
|
.key(IdUtil.fastSimpleUUID())
|
|
|
.title(title)
|
|
|
- .fileType(FileNameUtil.getSuffix(name))
|
|
|
+ .fileType(FileNameUtil.getSuffix(vo.getName()))
|
|
|
.url(downloadUrl)
|
|
|
.storage(bucket)
|
|
|
- .len(ossTemplate.getSize(bucket, name))
|
|
|
+ .len(ossTemplate.getSize(bucket, vo.getName()))
|
|
|
.permissions(new JSONObject()
|
|
|
.fluentPut("chat", true)
|
|
|
.fluentPut("comment", true)
|
|
|
.fluentPut("copy", true)
|
|
|
.fluentPut("download", true)
|
|
|
- .fluentPut("edit", false)
|
|
|
+ .fluentPut("edit", vo.isEdit())
|
|
|
.fluentPut("fillForms", false)
|
|
|
.fluentPut("modifyContentControl", true)
|
|
|
.fluentPut("modifyFilter", true)
|
|
|
@@ -188,43 +194,14 @@ public class FileController {
|
|
|
|
|
|
//回调接口回传文档名称和存储桶
|
|
|
DocumentEditParam param = DocumentEditParam.builder()
|
|
|
- .callbackUrl(String.format(DocumentConstants.OFFICE_API_CALLBACK_BUCKET, serverUrl, name, bucket))
|
|
|
- .user(new DocumentEditParam.UserBean(userId, userName))
|
|
|
+ .callbackUrl(String.format(DocumentConstants.OFFICE_API_CALLBACK_BUCKET, serverUrl, vo.getName(), bucket))
|
|
|
+ .user(new DocumentEditParam.UserBean(vo.getUserId(), vo.getUserName()))
|
|
|
.build();
|
|
|
log.info("param: {}", JSON.toJSONString(param));
|
|
|
|
|
|
return Pair.of(document, param);
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/reviewExcel")
|
|
|
- public String reviewExcel(@RequestParam(required = false) String bucket,
|
|
|
- @RequestParam(required = false) String title,
|
|
|
- @RequestParam(required = false) String url,
|
|
|
- @RequestParam(required = false) String name,
|
|
|
- String userId,
|
|
|
- String userName,
|
|
|
- Model model) {
|
|
|
- return reviewDocFile(bucket, title, url, name, userName, userId, model);
|
|
|
- }
|
|
|
-
|
|
|
- @GetMapping("/edit")
|
|
|
- public String editDocFile(@RequestParam(required = false) String bucket,
|
|
|
- @RequestParam(required = false) String title,
|
|
|
- @RequestParam String name,
|
|
|
- String userName,
|
|
|
- String userId,
|
|
|
- Model model) {
|
|
|
- log.info("==> 编辑文档 bucket: {}, name: {}", bucket, name);
|
|
|
-
|
|
|
- Pair<Document, DocumentEditParam> pair = docPair(bucket, title, name, userId, userName);
|
|
|
- Document document = pair.getKey();
|
|
|
- document.getPermissions().clear();
|
|
|
- model.addAttribute("document", document);
|
|
|
- model.addAttribute("documentEditParam", pair.getValue());
|
|
|
- return "/editor";
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* Only Office 回调接口
|
|
|
* <a href="https://api.onlyoffice.com/zh-CN/docs/docs-api/usage-api/callback-handler/">回调参数</a>
|
|
|
@@ -240,42 +217,43 @@ public class FileController {
|
|
|
.stream()
|
|
|
.map(DocumentEditCallback.ActionsBean::getUserid)
|
|
|
.collect(Collectors.joining(", "));
|
|
|
+ String msg = String.format("bucket=%s name=%s user=%s", bucket, name, userIds);
|
|
|
|
|
|
try {
|
|
|
switch (param.getStatus()) {
|
|
|
case 0:
|
|
|
- log.info("找不到具有对应key的文档 user={}", userIds);
|
|
|
+ log.info("找不到具有对应key的文档 {}", msg);
|
|
|
break;
|
|
|
case 1:
|
|
|
- log.info("文档正在被编辑 user={}", userIds);
|
|
|
+ log.info("文档正在被编辑 {}", msg);
|
|
|
break;
|
|
|
case 2:
|
|
|
case 6:
|
|
|
if (param.getStatus() == 2) {
|
|
|
- log.info("文档已准备好保存 user={}", userIds);
|
|
|
+ log.info("文档已准备好保存 {}", msg);
|
|
|
} else {
|
|
|
- log.info("正在进行强制保存请求 user={}", userIds);
|
|
|
+ log.info("正在进行强制保存请求 {}", msg);
|
|
|
}
|
|
|
|
|
|
//更新oss文件
|
|
|
byte[] bytes = HttpUtil.downloadBytes(param.getUrl());
|
|
|
ossTemplate.putObject(bucket, name, new ByteArrayInputStream(bytes));
|
|
|
- log.info("文档已保存,bucket={} name={}", bucket, name);
|
|
|
+ log.info("文档已保存 {}", msg);
|
|
|
break;
|
|
|
case 3:
|
|
|
- log.info("文档保存时发生错误 user={}", userIds);
|
|
|
+ log.warn("文档保存时发生错误 {}", msg);
|
|
|
break;
|
|
|
case 4:
|
|
|
- log.info("文档关闭时没有更改 user={}", userIds);
|
|
|
+ log.info("文档关闭时没有更改 {}", msg);
|
|
|
break;
|
|
|
case 7:
|
|
|
- log.info("强制保存文档时发生错误 user={}", userIds);
|
|
|
+ log.error("强制保存文档时发生错误 {}", msg);
|
|
|
break;
|
|
|
default:
|
|
|
- log.info("未知状态 user={}", userIds);
|
|
|
+ log.warn("未知状态 {}", msg);
|
|
|
}
|
|
|
} catch (Exception ex) {
|
|
|
- log.error("文档保存时发生错误 user={} msg={}", userIds, ex.getMessage());
|
|
|
+ log.error("文档保存时发生错误 {} msg={}", msg, ex.getMessage());
|
|
|
return DocumentEditCallbackResponse.fail();
|
|
|
}
|
|
|
return DocumentEditCallbackResponse.success();
|