Parcourir la source

fix:为文件上传接口添加等待时间

lalala il y a 1 an
Parent
commit
56cf31693b

+ 3 - 0
application.yaml

@@ -28,6 +28,9 @@ spring:
       max-file-size: 500MB
       # 所有上传文件最大大小
       max-request-size: 500MB
+  mvc:
+    async:
+      request-timeout: 300000
 
 
 # MyBatis配置

+ 17 - 5
src/main/java/com/njuzr/eaibackend/controller/FileController.java

@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author: Leonezhurui
@@ -27,11 +29,21 @@ public class FileController {
 
 
     @PostMapping
-    public MyResponse upload(@RequestParam("file") MultipartFile file) throws IOException {
-        String filePath = "uploads/" + file.getOriginalFilename();
-        String url = ossUtil.uploadFile(file.getInputStream(), filePath);
-        log.info("url:" + url);
-        return MyResponse.success(url);
+    public CompletableFuture<MyResponse> upload(@RequestParam("file") MultipartFile file) {
+        return CompletableFuture.supplyAsync(() -> {
+            try {
+                String filePath = "uploads/" + file.getOriginalFilename();
+                String url = ossUtil.uploadFile(file.getInputStream(), filePath);
+                log.info("url:" + url);
+                return MyResponse.success(url);
+            } catch (IOException e) {
+                throw new RuntimeException("文件上传失败", e);
+            }
+        }).completeOnTimeout(
+                MyResponse.error(400,"文件上传超时,请稍后再试"),
+                300,  // 超时时间(秒)
+                TimeUnit.SECONDS
+        );
     }
 
     @GetMapping

+ 1 - 2
src/main/java/com/njuzr/eaibackend/service/impl/MediaAnalysisServiceImpl.java

@@ -341,6 +341,7 @@ public class MediaAnalysisServiceImpl implements MediaAnalysisService {
         }
     }
 
+    @Override
     public VideoAnalysisVo getVideoSentiment(Long userId, Long assignmentId) {
         String requestKey = userId + "-" + assignmentId + "-video-sentiment";
         log.info("{} 开始获取视频情感分析, userId: {}, assignmentId: {}",
@@ -367,7 +368,6 @@ public class MediaAnalysisServiceImpl implements MediaAnalysisService {
                     VIDEO_SENTIMENT_HEADER, userId, assignmentId);
             throw new MyException(HttpStatus.BAD_REQUEST.value(), "不存在相应记录");
         }
-
         if (dialogue.getVideoAnalysis() != null && dialogue.getVideoAnalysis().getNewAudioUrl() != null) {
             log.debug("{} 返回已存在的视频分析结果", VIDEO_SENTIMENT_HEADER);
             return dialogue.getVideoAnalysis();
@@ -403,7 +403,6 @@ public class MediaAnalysisServiceImpl implements MediaAnalysisService {
 
     private VideoAnalysisVo sendRequestToDeepface(List<String> audioSegment, SpeakingAIDialogue dialogue) throws IOException {
         log.debug("{} 开始发送视频分析请求, 音频片段数量: {}", VIDEO_SENTIMENT_HEADER, audioSegment.size());
-
         JSONObject json = new JSONObject();
         json.put("video_urls", audioSegment);
         RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), json.toString());