Просмотр исходного кода

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

Li Yuan 1 год назад
Родитель
Сommit
9f240b3b9d

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

@@ -71,7 +71,6 @@ import java.nio.file.Paths;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
-@CrossOrigin("*")
 @RequestMapping("/onlyOffice")
 @Controller
 public class FileController {
@@ -162,6 +161,9 @@ public class FileController {
     public String upload(@RequestParam("file") final MultipartFile file) {
         try {
             String userId = ContextUtil.getCurrentUserId();
+            if(StringUtils.isEmpty(userId)){
+                return "{ \"error\": \"userId is empty\"}";
+            }
 
             String fullFileName = file.getOriginalFilename();  // get file name
             String fileExtension = fileUtility.getFileExtension(fullFileName);  // get file extension
@@ -179,14 +181,17 @@ public class FileController {
                 // if not, write an error message to the response
                 return "{ \"error\": \"File type is not supported\"}";
             }
+            SimpleDateFormat df = new SimpleDateFormat("/yyyy/MMddHHmmss");//设置日期格式
+            String date = df.format(new Date());
+            fullFileName = "/template/"+userId+date+"."+fileExtension;
 
-            String fileNamePath = storageMutator.updateFile(fullFileName, bytes);  // update a file
+            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
             }
 
-            fullFileName = fileUtility.getFileNameWithoutExtension(fileNamePath)
-                    + "." + fileExtension;  // get full file name
+//            fullFileName = fileUtility.getFileNameWithoutExtension(fileNamePath)
+//                    + "." + fileExtension;  // get full file name
 
             return createUserMetadata(userService.getUser(), fullFileName);  // create user metadata and return it
         } catch (Exception e) {

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

@@ -32,7 +32,7 @@ public interface FileStorageMutator {
     boolean createFile(Path path, InputStream stream);  // create a new file if it does not exist
     boolean deleteFile(String fileName) throws UnsupportedEncodingException;  // delete a file
     boolean deleteFileHistory(String fileName) throws UnsupportedEncodingException;  // delete file history
-    String updateFile(String fileName, byte[] bytes);  // update a file
+    String updateFile(Path fileName, byte[] bytes);  // update a file
     boolean writeToFile(String pathName, String payload);  // write the payload to the file
     boolean moveFile(Path source, Path destination);  // move a file to the specified destination
     Resource loadFileAsResource(String fileName);  // load file as a resource

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

@@ -32,7 +32,6 @@ import org.springframework.core.io.UrlResource;
 import org.springframework.stereotype.Component;
 import org.springframework.util.FileSystemUtils;
 
-import javax.servlet.http.HttpServletRequest;
 import java.io.*;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
@@ -199,10 +198,9 @@ public class LocalFileStorage implements FileStorageMutator, FileStoragePathBuil
     }
 
     // update a file
-    public String updateFile(final String fileName, final byte[] bytes) {
-        Path path = fileUtility
-                .generateFilepath(getStorageLocation(), fileName);  // generate the path to the specified file
+    public String updateFile(final Path path, final byte[] bytes) {
         try {
+            Files.createDirectories(path.getParent());
             Files.write(path, bytes);  // write new information in the bytes format to the file
             return path.getFileName().toString();
         } catch (IOException e) {