Prechádzať zdrojové kódy

解决onlyoffice回调,存入mongo文件扩展名问题

wukai 1 rok pred
rodič
commit
40a1ec839d

+ 66 - 26
doc-biz/src/main/java/com/doc/biz/controller/OnlyOfficeController.java

@@ -92,10 +92,11 @@ public class OnlyOfficeController {
 
     /**
      * 保存文件及后续操作
+     *
      * @param id
      * @param request  请求参数
      * @param response 响应参数
-     * @param isTmpl 是否模板
+     * @param isTmpl   是否模板
      */
     public void onlyoffice(Long id, HttpServletRequest request, HttpServletResponse response, boolean isTmpl) throws Exception {
         PrintWriter writer = null;
@@ -140,39 +141,23 @@ public class OnlyOfficeController {
             String downloadUri = (String) jsonObj.get("url");
 
             try {
-                MultipartFile multipartFile;
                 String https = "https";
-                if (downloadUri.contains(https)) {
+                if (downloadUri.startsWith(https)) {
                     SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
                             NoopHostnameVerifier.INSTANCE);
-                    CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build();
-                    HttpGet httpget = new HttpGet(downloadUri);
-                    HttpResponse res = client.execute(httpget);
-                    HttpEntity entity = res.getEntity();
-                    try (InputStream is = entity.getContent()) {
-                        if (isTmpl) {
-                            DocTemplate tmpl = templateService.selectDocTemplateByTmplId(id);
-                            multipartFile = FileUtils.getMultipartFile(is, tmpl.getTmplName());
-                            processTmpl(tmpl, user, multipartFile);
-                        } else {
-                            DocInfo info = docInfoService.selectDocInfoByDocId(id);
-                            multipartFile = FileUtils.getMultipartFile(is, info.getFileName());
-                            process(info, user, multipartFile);
+                    try (CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build()) {
+                        HttpGet httpGet = new HttpGet(downloadUri);
+                        HttpResponse res = client.execute(httpGet);
+                        HttpEntity entity = res.getEntity();
+                        try (InputStream is = entity.getContent()) {
+                            processFile(is, isTmpl, id, user);
                         }
-                    } catch (Exception e) {
-                        throw e;
                     }
                 } else {
                     URL url = new URL(downloadUri);
                     HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
-                    if (isTmpl) {
-                        DocTemplate tmpl = templateService.selectDocTemplateByTmplId(id);
-                        multipartFile = FileUtils.getMultipartFile(connection.getInputStream(), tmpl.getTmplName());
-                        processTmpl(tmpl, user, multipartFile);
-                    } else {
-                        DocInfo info = docInfoService.selectDocInfoByDocId(id);
-                        multipartFile = FileUtils.getMultipartFile(connection.getInputStream(), info.getFileName());
-                        process(info, user, multipartFile);
+                    try (InputStream is = connection.getInputStream()) {
+                        processFile(is, isTmpl, id, user);
                     }
                     connection.disconnect();
                 }
@@ -190,6 +175,27 @@ public class OnlyOfficeController {
     }
 
     /**
+     * 处理文件
+     *
+     * @param is     文件输入流
+     * @param isTmpl 是否模板
+     * @param id     ID
+     * @param user   用户
+     * @throws Exception e
+     */
+    private void processFile(InputStream is, boolean isTmpl, Long id, SysUser user) throws Exception {
+        if (isTmpl) {
+            DocTemplate tmpl = templateService.selectDocTemplateByTmplId(id);
+            MultipartFile multipartFile = FileUtils.getMultipartFile(is, getFileNameWithExtension(tmpl));
+            processTmpl(tmpl, user, multipartFile);
+        } else {
+            DocInfo info = docInfoService.selectDocInfoByDocId(id);
+            MultipartFile multipartFile = FileUtils.getMultipartFile(is, getFileNameWithExtension(info));
+            process(info, user, multipartFile);
+        }
+    }
+
+    /**
      * 保存历史版本,处理新的文件信息
      *
      * @param info          文档信息
@@ -239,6 +245,40 @@ public class OnlyOfficeController {
     }
 
     /**
+     * 获取文件名
+     *
+     * @param tmpl 模板对象
+     * @return 文件名
+     */
+    private String getFileNameWithExtension(DocTemplate tmpl) {
+        String fileName = tmpl.getTmplName();
+        switch (tmpl.getTmplType()) {
+            case "word":
+                return fileName.endsWith(".docx") ? fileName : fileName + ".docx";
+            case "excel":
+                return fileName.endsWith(".xlsx") ? fileName : fileName + ".xlsx";
+            case "ppt":
+                return fileName.endsWith(".pptx") ? fileName : fileName + ".pptx";
+            default:
+                return fileName;
+        }
+    }
+
+    /**
+     * 获取文件名
+     *
+     * @param info 文件对象
+     * @return 文件名
+     */
+    private String getFileNameWithExtension(DocInfo info) {
+        String fileName = info.getFileName();
+        if (!fileName.endsWith(info.getFileType())) {
+            fileName += "." + info.getFileType();
+        }
+        return fileName;
+    }
+
+    /**
      * 插入操作日志
      *
      * @param body   请求参数