Przeglądaj źródła

fix: 文件上传接口debug

sky 3 lat temu
rodzic
commit
ad610de73e

+ 47 - 0
src/main/java/cn/edu/nju/plagdemo/api/callbacks/OssCallback.java

@@ -0,0 +1,47 @@
+package cn.edu.nju.plagdemo.api.callbacks;
+
+import cn.edu.nju.plagdemo.model.FileStateEnum;
+import cn.edu.nju.plagdemo.model.form.plag.FileUploaderCallbackForm;
+import cn.edu.nju.plagdemo.model.po.plag.BaseFilePO;
+import cn.edu.nju.plagdemo.model.po.plag.DetectionFilePO;
+import cn.edu.nju.plagdemo.service.IBaseFileService;
+import cn.edu.nju.plagdemo.service.IDetectionFileService;
+import cn.edu.nju.plagdemo.utils.RestResult;
+import io.swagger.annotations.Api;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Api
+@Validated
+@RestController
+@RequestMapping("/ossCallback")
+public class OssCallback {
+
+    @Autowired
+    IDetectionFileService detectionFileService;
+
+    @Autowired
+    IBaseFileService baseFileService;
+
+    @PostMapping(value = "/uploadSuccess")
+    public RestResult<?> onFileUploadSuccess(
+            @RequestBody FileUploaderCallbackForm fileUploaderCallbackForm
+    ){
+        if(fileUploaderCallbackForm.getFileType().equals("file")) {
+            DetectionFilePO detectionFilePO = new DetectionFilePO();
+            detectionFilePO.setId(fileUploaderCallbackForm.getFileId());
+            detectionFilePO.setState(FileStateEnum.SUCCESSFUL.getState());
+            detectionFileService.saveOrUpdate(detectionFilePO);
+        } else if(fileUploaderCallbackForm.getFileType().equals("basefile")){
+            BaseFilePO baseFilePO = new BaseFilePO();
+            baseFilePO.setId(fileUploaderCallbackForm.getFileId());
+            baseFilePO.setState(FileStateEnum.SUCCESSFUL.getState());
+            baseFileService.saveOrUpdate(baseFilePO);
+        }
+        return RestResult.OK;
+    }
+}

+ 2 - 21
src/main/java/cn/edu/nju/plagdemo/api/impl/OssApiImpl.java

@@ -58,26 +58,7 @@ public class OssApiImpl implements IOssApi {
     // 以下Policy用于限制仅允许使用临时访问凭证向目标存储空间examplebucket上传文件。
     // 临时访问凭证最后获得的权限是步骤4设置的角色权限和该Policy设置权限的交集,即仅允许将文件上传至目标存储空间examplebucket下的exampledir目录。
     // 如果policy为空,则用户将获得该角色下所有权限。
-    String policy = "{\n" +
-            "    \"Version\": \"1\",\n" +
-            "    \"Statement\": [\n" +
-            "        {\n" +
-            "            \"Effect\": \"Allow\",\n" +
-            "            \"Action\": [\n" +
-            "                \"oss:GetObject\",\n" +
-            "                \"oss:GetObjectAcl\",\n" +
-            "                \"oss:DeleteObject\",\n" +
-            "                \"oss:PutObject\",\n" +
-            "                \"oss:PutObjectAcl\",\n" +
-            "                \"oss:ListObjects\"\n" +
-            "            ],\n" +
-            "            \"Resource\": [\n" +
-            "                \"acs:oss:*:*:plag-file-bucket/development/basefile/*\",\n" +
-            "                \"acs:oss:*:*:plag-file-bucket/development/file/*\"\n" +
-            "            ]\n" +
-            "        }\n" +
-            "    ]\n" +
-            "}";
+    String policy = null;
     // 设置临时访问凭证的有效时间为3600秒。
     Long durationSeconds = 3600L;
 
@@ -111,7 +92,7 @@ public class OssApiImpl implements IOssApi {
             stsToken = new OssTokenVO();
             stsToken.setSecurityToken(response.getCredentials().getSecurityToken());
             stsToken.setAccessKeyId(response.getCredentials().getAccessKeyId());
-            stsToken.setAccessKeySecret(response.getCredentials().getSecurityToken());
+            stsToken.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
             SimpleCache.put("sts_token", stsToken, (durationSeconds - 5) * 1000);
             return stsToken;
         } catch (ClientException e) {

+ 25 - 0
src/main/java/cn/edu/nju/plagdemo/config/CorsConfig.java

@@ -0,0 +1,25 @@
+package cn.edu.nju.plagdemo.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class CorsConfig implements WebMvcConfigurer {
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        //添加映射路径
+        registry.addMapping("/**")
+                //是否发送Cookie
+                .allowCredentials(true)
+                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
+                .allowedOriginPatterns("*")
+                //放行哪些请求方式
+                .allowedMethods("GET", "POST", "PUT", "DELETE")
+                //.allowedMethods("*") //或者放行全部
+                //放行哪些原始请求头部信息
+                .allowedHeaders("*")
+                //暴露哪些原始请求头部信息
+                .exposedHeaders("*");
+    }
+}

+ 1 - 1
src/main/java/cn/edu/nju/plagdemo/controller/DetectionFileController.java

@@ -24,7 +24,7 @@ public class DetectionFileController {
 
     @ApiOperation(value = "新增待检测文件", notes = "")
     @PostMapping(value = "/insert")
-    public RestResult<?> insert(
+    public RestResult<DetectionFileDTO> insert(
             @Validated @RequestBody DetectionFileForm detectionFileForm
     ){
         DetectionFileDTO detectionFileDTO = detectionFileService.save(detectionFileForm);

+ 9 - 0
src/main/java/cn/edu/nju/plagdemo/model/form/plag/FileUploaderCallbackForm.java

@@ -0,0 +1,9 @@
+package cn.edu.nju.plagdemo.model.form.plag;
+
+import lombok.Data;
+
+@Data
+public class FileUploaderCallbackForm {
+    String fileType;
+    Integer fileId;
+}

+ 1 - 0
src/main/resources/application.yml

@@ -16,6 +16,7 @@ spring:
 server:
   servlet:
     context-path: /
+  port: 8081
 springfox:
   documentation:
     enabled: true