Răsfoiți Sursa

新增文件上传,所有文件上传后返回对应url

ting.yin 4 ani în urmă
părinte
comite
744602ff62

+ 3 - 3
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/file/enums/FileType.java

@@ -5,9 +5,9 @@ package cn.com.qmth.stmms.biz.file.enums;
  */
 public enum FileType {
 
-    SHEET("原图", "sheet/%d/%s/%s-%d.%s"), SLICE("裁切图", "slice/%d/%s/%s-%d.%s"), JSON("作答内容",
-            "json/%d/%s/%s.%s"), PACKAGE("签到表", "package/%d/%s/%d.%s"), PAPER("试卷", "paper/%d/%s.%s"), ANSWER("标答",
-            "answer/%d/%s.%s"), CARD("题卡", "card/%d/%s.%s");
+    SHEET("原图", "sheet/%d/%s/%s-%d.%s"), SLICE("裁切图", "slice/%d/%s/%s-%d.%s"), JSON("作答内容", "json/%d/%s/%s.%s"), PACKAGE(
+            "签到表", "package/%d/%s/%d.%s"), PAPER("试卷", "paper/%d/%s.%s"), ANSWER("标答", "answer/%d/%s.%s"), CARD("题卡",
+            "card/%d/%s.%s"), EXCHANGE("文件", "exchange/%s");
 
     private String name;
 

+ 5 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/file/service/FileService.java

@@ -31,6 +31,8 @@ public interface FileService {
 
     void uploadCard(InputStream ins, String md5, int examId, String subjectCode, FormatType type) throws Exception;
 
+    void uploadExchange(InputStream ins, String md5, String name) throws Exception;
+
     String getSheetUri(int examId, String examNumber, int index);
 
     List<String> getSheetUris(int examId, String examNumber, int start, int end);
@@ -53,6 +55,8 @@ public interface FileService {
 
     String getCardUri(int examId, String subjectCode, FormatType type);
 
+    String getExchangeUri(String name);
+
     boolean sheetExist(int examId, String examNumber, int index);
 
     boolean sliceExist(int examId, String secretNumber, int index);
@@ -60,4 +64,5 @@ public interface FileService {
     boolean jsonExist(int examId, String secretNumber);
 
     byte[] downloadJson(int examId, String secretNumber) throws Exception;
+
 }

+ 15 - 6
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/file/service/impl/FileServiceImpl.java

@@ -240,6 +240,21 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
         store.close();
     }
 
+    @Override
+    public byte[] downloadJson(int examId, String secretNumber) throws Exception {
+        return store.read(getJsonUri(examId, secretNumber));
+    }
+
+    @Override
+    public void uploadExchange(InputStream ins, String md5, String name) throws Exception {
+        store.write(getExchangeUri(name), ins, md5);
+    }
+
+    @Override
+    public String getExchangeUri(String name) {
+        return FileType.EXCHANGE.getPath(name);
+    }
+
     public static void main(String[] args) throws Exception {
         FileServiceImpl service = new FileServiceImpl();
         service.fileServer = "123";
@@ -255,10 +270,4 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
         service.downloadSheet(1, "16110003", 1);
         System.out.println("success");
     }
-
-    @Override
-    public byte[] downloadJson(int examId, String secretNumber) throws Exception {
-        return store.read(getJsonUri(examId, secretNumber));
-    }
-
 }

+ 7 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/BaseApiController.java

@@ -39,6 +39,13 @@ public class BaseApiController extends BaseController {
         return obj;
     }
 
+    protected JSONObject result(boolean success, String url) {
+        JSONObject obj = new JSONObject();
+        obj.accumulate("success", success);
+        obj.accumulate("url", url);
+        return obj;
+    }
+
     protected JSONObject result(String key, String value) {
         JSONObject obj = new JSONObject();
         obj.accumulate(key, value);

+ 33 - 7
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/FileController.java

@@ -145,7 +145,7 @@ public class FileController extends BaseApiController {
             log.error("sheet upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("sheet upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true, fileService.getFileServer().concat(fileService.getSheetUri(examId, examNumber, index)));
     }
 
     @RequestMapping(value = "/slice/upload", method = RequestMethod.POST)
@@ -164,7 +164,8 @@ public class FileController extends BaseApiController {
             log.error("slice upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("slice upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true,
+                fileService.getFileServer().concat(fileService.getSliceUri(examId, student.getSecretNumber(), index)));
     }
 
     @RequestMapping(value = "/json/upload", method = RequestMethod.POST)
@@ -186,7 +187,8 @@ public class FileController extends BaseApiController {
             log.error("json upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("json upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true,
+                fileService.getFileServer().concat(fileService.getJsonUri(examId, student.getSecretNumber())));
     }
 
     @RequestMapping(value = "/package/upload", method = RequestMethod.POST)
@@ -205,7 +207,7 @@ public class FileController extends BaseApiController {
             log.error("package upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("package upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true, fileService.getFileServer().concat(fileService.getPackageUri(examId, packageCode, index)));
     }
 
     @RequestMapping(value = "/paper/upload", method = RequestMethod.POST)
@@ -260,7 +262,7 @@ public class FileController extends BaseApiController {
             log.error("paper upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("paper upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true, fileService.getFileServer().concat(fileService.getPaperUri(examId, subjectCode, format)));
     }
 
     @RequestMapping(value = "/answer/upload", method = RequestMethod.POST)
@@ -289,7 +291,7 @@ public class FileController extends BaseApiController {
             log.error("answer upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("answer upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true, fileService.getFileServer().concat(fileService.getAnswerUri(examId, subjectCode, format)));
     }
 
     @RequestMapping(value = "/card/upload", method = RequestMethod.POST)
@@ -308,21 +310,38 @@ public class FileController extends BaseApiController {
         } else {
             validateFormatType(format, FormatType.JSON, FormatType.ZIP);
         }
+        String uri = "";
         try {
             if (subject != null) {
                 fileService.uploadCard(file.getInputStream(), md5, examId, subjectCode, format);
                 subject.setCardType(format);
                 subjectService.save(subject);
+                uri = fileService.getCardUri(examId, subjectCode, format);
             } else {
                 fileService.uploadCard(file.getInputStream(), md5, examId, format);
                 exam.setCardType(format);
                 examService.save(exam);
+                uri = fileService.getCardUri(examId, format);
             }
         } catch (Exception e) {
             log.error("card upload error", e);
             throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("card upload error: " + e.getMessage());
         }
-        return result(true);
+        return result(true, fileService.getFileServer().concat(uri));
+    }
+
+    @RequestMapping(value = "/exchange/upload", method = RequestMethod.POST)
+    @RoleRequire({ Role.SCHOOL_ADMIN, Role.SCHOOL_DEV, Role.SCANNER })
+    @ResponseBody
+    public JSONObject uploadExchange(HttpServletRequest request, @RequestParam String name, @RequestParam String md5,
+            @RequestParam MultipartFile file) {
+        try {
+            fileService.uploadExchange(file.getInputStream(), md5, name);
+        } catch (Exception e) {
+            log.error("exchange upload error", e);
+            throw ApiException.FILE_UPLOAD_ERROR.replaceMessage("exchange upload error: " + e.getMessage());
+        }
+        return result(true, fileService.getFileServer().concat(fileService.getExchangeUri(name)));
     }
 
     @RequestMapping(value = "/card/url")
@@ -390,4 +409,11 @@ public class FileController extends BaseApiController {
         }
         return array;
     }
+
+    @RequestMapping(value = "/exchange/url")
+    @RoleRequire({ Role.SCHOOL_ADMIN, Role.SCHOOL_DEV, Role.SCANNER })
+    @ResponseBody
+    public JSONObject exchangeUrl(HttpServletRequest request, @RequestParam String name) {
+        return result("url", fileService.getFileServer().concat(fileService.getExchangeUri(name)));
+    }
 }