Переглянути джерело

加入音频和图片处理

wangliang 1 місяць тому
батько
коміт
72ce8a5b21

+ 103 - 2
server/src/main/java/com/qmth/jkserver/service/impl/JointFlowSimulationServiceImpl.java

@@ -1,5 +1,7 @@
 package com.qmth.jkserver.service.impl;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.jkserver.constant.SystemConstant;
 import com.qmth.jkserver.core.exception.JkServerException;
@@ -12,15 +14,23 @@ import com.qmth.jkserver.params.callApi.SaveSubjectParams;
 import com.qmth.jkserver.params.callApi.fileUpload.JsonFileParams;
 import com.qmth.jkserver.params.callApi.fileUpload.PaperFileParams;
 import com.qmth.jkserver.service.*;
+import com.qmth.jkserver.util.Base64Util;
+import com.qmth.jkserver.util.FileStoreUtil;
+import com.qmth.jkserver.util.JacksonUtil;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateFormatUtils;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.context.annotation.Scope;
 import org.springframework.context.annotation.ScopedProxyMode;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.LinkedMultiValueMap;
 
 import javax.annotation.Resource;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
@@ -61,6 +71,9 @@ public class JointFlowSimulationServiceImpl implements JointFlowSimulationServic
     @Resource
     CommonCacheService commonCacheService;
 
+    @Resource
+    FileStoreUtil fileStoreUtil;
+
     @Override
     public Map<String, Object> saveYunExam(Map<String, Object> map) throws IOException, IllegalAccessException {
         ExamTask examTask = (ExamTask) map.get(SystemConstant.TASK);
@@ -151,6 +164,9 @@ public class JointFlowSimulationServiceImpl implements JointFlowSimulationServic
                     throw new JkServerException("没有找到试卷编号为[" + yunSubjectCode + "]的试卷文件");
                 }
                 File paperFile = paperFileList.get(0);
+                //解析试卷
+                Map<String, Object> jsonMap = this.disposeQuestionUrl(paperFile);
+                IOUtils.write(JacksonUtil.parseJson(jsonMap).getBytes(SystemConstant.CHARSET_NAME), new FileOutputStream(paperFile));
                 // 上传试卷结构和答案
                 this.savePaperAndAnswer(map, yunExamId, yunSubjectCode, paperFile);
 
@@ -175,7 +191,7 @@ public class JointFlowSimulationServiceImpl implements JointFlowSimulationServic
                                             !e.getIsAbsent() &&
                                             paperId.equals(e.getPaperId()) &&
                                             Objects.equals(examStudent.getBatchId(), key)
-//                                            && (e.getTaskSyncResult() == null || !e.getTaskSyncResult())
+                                            && (e.getTaskSyncResult() == null || !e.getTaskSyncResult())
                                     ) {
                                         return true;
                                     } else {
@@ -269,12 +285,12 @@ public class JointFlowSimulationServiceImpl implements JointFlowSimulationServic
                         continue;
                     }
                     map = this.saveStudentJson(map, yunExamId, examNumber, thisJsonList.get(0));
+                    examStudentAnswer.setTaskSyncResult(true);
                 }
                 // 考生创建完毕同时有作答的也生成完毕了 考生上传成功 任务完成数++
                 AtomicInteger completeProgress = new AtomicInteger(examTask.getCompleteProgress());
                 completeProgress.incrementAndGet();
                 examTask.setCompleteProgress(completeProgress.get());
-
 //                examStudentAnswer.setTaskSyncResult(true);
 //                examStudentAnswer.setExamTask(examTask);
 //                mongoTemplate.save(examStudentAnswer);
@@ -446,4 +462,89 @@ public class JointFlowSimulationServiceImpl implements JointFlowSimulationServic
             return Arrays.asList(Objects.requireNonNull(tempList.get(0).listFiles()));
         }
     }
+
+    /**
+     * 解析试卷
+     *
+     * @param paperFile
+     * @return
+     * @throws Exception
+     */
+    public Map<String, Object> disposeQuestionUrl(File paperFile) throws Exception {
+        Map<String, Object> jsonMap = new LinkedHashMap<>();
+        byte[] bytes = IOUtils.toByteArray(new FileInputStream(paperFile));
+        jsonMap = JSONArray.parseObject(new String(bytes, SystemConstant.CHARSET_NAME), Map.class);
+        if (!CollectionUtils.isEmpty(jsonMap)) {
+            JSONArray structdetails = (JSONArray) jsonMap.get("details");
+            if (Objects.nonNull(structdetails) && structdetails.size() > 0) {
+                for (int i = 0; i < structdetails.size(); i++) {
+                    JSONArray structdetailquestions = structdetails.getJSONObject(i).getJSONArray("questions");
+                    if (Objects.nonNull(structdetailquestions) && structdetailquestions.size() > 0) {
+                        for (int j = 0; j < structdetailquestions.size(); j++) {
+                            JSONObject structquestion = structdetailquestions.getJSONObject(j);
+                            JSONObject body = structquestion.getJSONObject(SystemConstant.BODY);
+                            this.resolveImageAndAudio(paperFile, body);
+                            JSONArray options = structquestion.getJSONArray("options");
+                            if (Objects.nonNull(options) && options.size() > 0) {
+                                for (int k = 0; k < options.size(); k++) {
+                                    JSONObject option = options.getJSONObject(k);
+                                    if (Objects.nonNull(option)) {
+                                        JSONObject optionBody = option.getJSONObject(SystemConstant.BODY);
+                                        this.resolveImageAndAudio(paperFile, optionBody);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return jsonMap;
+    }
+
+    /**
+     * base64图片和音频
+     *
+     * @param paperFile
+     * @param body
+     */
+    public void resolveImageAndAudio(File paperFile, JSONObject body) {
+        if (Objects.nonNull(body)) {
+            JSONArray sections = body.getJSONArray("sections");
+            if (Objects.nonNull(sections) && sections.size() > 0) {
+                for (int k = 0; k < sections.size(); k++) {
+                    JSONObject block = sections.getJSONObject(k);
+                    if (block != null) {
+                        JSONArray blocks = block.getJSONArray("blocks");
+                        if (Objects.nonNull(blocks) && blocks.size() > 0) {
+                            for (int m = 0; m < blocks.size(); m++) {
+                                JSONObject blockInfo = blocks.getJSONObject(m);
+                                String type = blockInfo.getString(SystemConstant.TYPE);
+                                String value = blockInfo.getString("value");
+                                File attachmentDir = new File(paperFile.getParentFile().getParentFile(), "attachment" + File.separator + value);
+                                if (("image".equals(type) || "audio".equals(type)) && StringUtils.isNotBlank(value) && !value.toLowerCase()
+                                        .startsWith("https://") && !value.toLowerCase().startsWith("http://")) {
+                                    if ("image".equals(type)) {
+                                        if (value.contains("data:image")) {
+                                            blockInfo.put("value", value);
+                                        } else {
+                                            String base64Str = Base64Util.imageToBase64(attachmentDir);
+                                            blockInfo.put("value", base64Str);
+                                        }
+                                    } else if ("audio".equals(type)) {
+                                        if (value.contains("data:audio")) {
+                                            blockInfo.put("value", value);
+                                        } else {
+                                            String base64Str = Base64Util.audioToBase64(attachmentDir);
+                                            blockInfo.put("value", base64Str);
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
 }

+ 36 - 0
server/src/main/java/com/qmth/jkserver/util/Base64Util.java

@@ -3,6 +3,8 @@ package com.qmth.jkserver.util;
 import com.qmth.jkserver.constant.SystemConstant;
 import com.qmth.jkserver.signature.Constants;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.util.Base64;
 
 /**
@@ -21,4 +23,38 @@ public class Base64Util implements Constants {
     public static byte[] decode(String input) {
         return Base64.getDecoder().decode(input.getBytes(SystemConstant.CHARSET));
     }
+
+    /**
+     * 文件File类型转BASE64
+     *
+     * @param file
+     * @return
+     */
+    public static String imageToBase64(File file) {
+        return "data:image/png;base64," + encode(fileToByte(file));
+    }
+
+    public static String audioToBase64(File file) {
+        return "data:audio/mp3;base64," + encode(fileToByte(file));
+    }
+
+    /**
+     * 文件File类型转byte[]
+     *
+     * @param file
+     * @return
+     */
+    private static byte[] fileToByte(File file) {
+        byte[] fileBytes = null;
+        FileInputStream fis;
+        try {
+            fis = new FileInputStream(file);
+            fileBytes = new byte[(int) file.length()];
+            fis.read(fileBytes);
+            fis.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return fileBytes;
+    }
 }