Explorar el Código

[task-2605] 表单模板优化 / 【后端】修改相应接口参数

Li Yuan hace 1 año
padre
commit
83982ac090

+ 39 - 22
ibps-provider-root/modules/provider-business/src/main/java/com/onlyoffice/integration/controllers/FileController.java

@@ -22,8 +22,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
+import com.lc.ibps.api.base.constants.StateEnum;
 import com.lc.ibps.base.framework.table.ICommonDao;
 import com.lc.ibps.base.web.context.ContextUtil;
+import com.lc.ibps.cloud.entity.APIResult;
+import com.lc.ibps.cloud.utils.ResultUtil;
 import com.onlyoffice.integration.documentserver.callbacks.CallbackHandler;
 import com.onlyoffice.integration.documentserver.managers.callback.CallbackManager;
 import com.onlyoffice.integration.documentserver.managers.document.DocumentManager;
@@ -159,10 +162,11 @@ public class FileController {
     @PostMapping("/upload")
     @ResponseBody
     public String upload(@RequestParam("file") final MultipartFile file) {
+        APIResult<String> result = new APIResult<>();
         try {
             String userId = ContextUtil.getCurrentUserId();
             if(StringUtils.isEmpty(userId)){
-                return "{ \"error\": \"userId is empty\"}";
+                return buildErrorResult("userId is empty");
             }
 
             String fullFileName = file.getOriginalFilename();  // get file name
@@ -172,14 +176,14 @@ public class FileController {
 
             // check if the file size exceeds the maximum file size or is less than 0
             if (fileUtility.getMaxFileSize() < fileSize || fileSize <= 0) {
-                return "{ \"error\": \"File size is incorrect\"}";  // if so, write an error message to the response
+                return buildErrorResult("File size is incorrect");  // if so, write an error message to the response
             }
 
             // check if file extension is supported by the editor
             if (!fileUtility.getFileExts().contains(fileExtension)) {
 
                 // if not, write an error message to the response
-                return "{ \"error\": \"File type is not supported\"}";
+                return buildErrorResult("File type is not supported");
             }
             SimpleDateFormat df = new SimpleDateFormat("/yyyy/MMddHHmmss");//设置日期格式
             String date = df.format(new Date());
@@ -187,19 +191,17 @@ public class FileController {
 
             String fileNamePath = storageMutator.updateFile(FileSystems.getDefault().getPath(storagePathBuilder.getFileLocation(fullFileName)), bytes);  // update a file
             if (StringUtils.isBlank(fileNamePath)) {
-                throw new IOException("Could not update a file");  // if the file cannot be updated, an error occurs
+                return buildErrorResult("Could not update a file");  // if the file cannot be updated, an error occurs
             }
 
 //            fullFileName = fileUtility.getFileNameWithoutExtension(fileNamePath)
 //                    + "." + fileExtension;  // get full file name
-
-            return createUserMetadata(userService.getUser(), fullFileName);  // create user metadata and return it
+            result.setData(createUserMetadata(userService.getUser(), fullFileName));// create user metadata and return it
+            return objectMapper.writeValueAsString(result);
         } catch (Exception e) {
-            e.printStackTrace();
+            return buildErrorResult("Something went wrong when uploading the file.");
         }
 
-        // if the operation of file uploading is unsuccessful, an error occurs
-        return "{ \"error\": \"Something went wrong when uploading the file.\"}";
     }
 
     @PostMapping(path = "${url.converter}")
@@ -271,6 +273,7 @@ public class FileController {
     @PostMapping("/delete")
     @ResponseBody
     public String delete(@RequestBody final Converter body) {  // delete a file
+        APIResult<String> result = new APIResult<>();
         try {
             String fullFileName = body.getFileName();  // get full file name
 
@@ -279,11 +282,11 @@ public class FileController {
 
             // delete file history and return the status of this operation (true or false)
             boolean historySuccess = storageMutator.deleteFileHistory(fullFileName);
-
-            return "{ \"success\": \"" + (fileSuccess && historySuccess) + "\"}";
+            result.setData("{ \"success\": \"" + (fileSuccess && historySuccess) + "\"}");
+            return objectMapper.writeValueAsString(result);
         } catch (Exception e) {
             // if the operation of file deleting is unsuccessful, an error occurs
-            return "{ \"error\": \"" + e.getMessage() + "\"}";
+            return buildErrorResult(StateEnum.ERROR.getText());
         }
     }
 
@@ -333,6 +336,16 @@ public class FileController {
         }
     }
 
+    private String buildErrorResult(String message)  {
+        APIResult<String> result = new APIResult<>();
+        ResultUtil.setResult(result, StateEnum.ERROR.getCode(), message,"");
+        try {
+            return objectMapper.writeValueAsString(result);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     @GetMapping(path = "${url.editor}")
     @ResponseBody
     // process request to open the editor page
@@ -343,14 +356,16 @@ public class FileController {
                            @RequestParam(value = "directUrl", required = false, defaultValue = "false") final Boolean directUrl,
                            @RequestParam(value = "fileId", required = false) final String fileId) throws JsonProcessingException {
         String title = fileName;
+        APIResult<FileModel> result = new APIResult<>();
         if(StringUtils.isNotBlank(fileId)){
             String sql ="SELECT cun_fang_lu_jing_,biao_dan_ming_che FROM t_bdmbtxjl WHERE id_='%s'";
             Map<String, Object> objectMap = commonDao.queryOne(String.format(sql, fileId));
+            if(objectMap == null)  return buildErrorResult("Template ID is incorrect");
             fileName = (String)objectMap.get("cun_fang_lu_jing_");
-            if(StringUtils.isEmpty(fileName))  return "{\"error\":\"Template ID is incorrect\"}";
+            if(StringUtils.isEmpty(fileName))  return buildErrorResult("Template ID is incorrect");
             title = (String)objectMap.get("biao_dan_ming_che");
         }else if (StringUtils.isBlank(fileName)){
-            return "{\"error\":\"File Name is incorrect\"}";
+            return buildErrorResult("File Name is incorrect");
         }
 
 
@@ -379,8 +394,8 @@ public class FileController {
                         .title(title)
                         .build()
         );
-
-        return objectMapper.writeValueAsString(fileModel);
+        result.setData(fileModel);
+        return objectMapper.writeValueAsString(result);
     }
     @GetMapping("/create")
     @ResponseBody
@@ -388,18 +403,20 @@ public class FileController {
                          @RequestParam(value = "fileName", required = false)  String fileName,
                          @RequestParam(value = "templateId", required = false) final String templateId) throws JsonProcessingException {
         String title = fileName;
+        APIResult<FileModel> result = new APIResult<>();
         if(StringUtils.isNotBlank(templateId)){
             String sql ="SELECT cun_fang_lu_jing_,biao_dan_ming_che FROM t_bdmbpzb WHERE id_='%s'";
             Map<String, Object> objectMap = commonDao.queryOne(String.format(sql, templateId));
+            if(objectMap == null)  return buildErrorResult("Template ID is incorrect");
             templateName = (String)objectMap.get("cun_fang_lu_jing_");
-            if(StringUtils.isEmpty(templateName))  return "{\"error\":\"Template ID is incorrect\"}";
+            if(StringUtils.isEmpty(templateName))  return buildErrorResult("Template ID is incorrect");
             String fileExtension = fileUtility.getFileExtension(templateName).toString().toLowerCase();
             SimpleDateFormat df = new SimpleDateFormat("/yyyy/MMddHHmmss");//设置日期格式
             String date = df.format(new Date());
             title = (String)objectMap.get("biao_dan_ming_che");
             fileName = "/records/"+templateId+date+"."+fileExtension;
         }else if (StringUtils.isBlank(templateName) || StringUtils.isBlank(fileName)){
-            return "{\"error\":\"Template Name or File Name is incorrect\"}";
+            return buildErrorResult("Template Name or File Name is incorrect");
         }
 
         User user = userService.getUser();
@@ -408,13 +425,13 @@ public class FileController {
 
             InputStream stream = resource.getInputStream();
             if (Integer.parseInt(filesizeMax) < stream.available() || stream.available() <= 0) {
-                return "{\"error\":\"File size is incorrect\"}";
+                return buildErrorResult("File size is incorrect");
             }
             storageMutator.createFile(FileSystems.getDefault().getPath(storagePathBuilder.getFileLocation(fileName)), stream);
             createUserMetadata(user, fileName);
         } catch (Exception e) {
             e.printStackTrace();
-            return "{ \"error\" : 1, \"message\" : \"" + e.getMessage() + "\"}";
+            return buildErrorResult(StateEnum.ERROR.getText());
         }
 
         com.onlyoffice.integration.documentserver.models.enums.Action action = com.onlyoffice.integration.documentserver.models.enums.Action.edit;
@@ -433,8 +450,8 @@ public class FileController {
                         .title(title)
                         .build()
         );
-
-        return objectMapper.writeValueAsString(fileModel);
+        result.setData(fileModel);
+        return objectMapper.writeValueAsString(result);
     }
 
 

+ 2 - 2
ibps-provider-root/modules/provider-business/src/main/java/com/onlyoffice/integration/documentserver/storage/LocalFileStorage.java

@@ -192,9 +192,9 @@ public class LocalFileStorage implements FileStorageMutator, FileStoragePathBuil
 
         /* delete the specified history file without extension; for directories,
          recursively delete any nested directories or files as well */
-        boolean historyWithoutExtDeleted = FileSystemUtils.deleteRecursively(fileHistoryPathWithoutExt.toFile());
+        FileSystemUtils.deleteRecursively(fileHistoryPathWithoutExt.toFile());
 
-        return historyDeleted || historyWithoutExtDeleted;
+        return historyDeleted ;
     }
 
     // update a file