فهرست منبع

feat: 数据安全

zhangjie 7 ماه پیش
والد
کامیت
d997945ab3
2فایلهای تغییر یافته به همراه71 افزوده شده و 26 حذف شده
  1. 41 2
      src/modules/client/views/ScanPaper.vue
  2. 30 24
      src/plugins/imageOcr.js

+ 41 - 2
src/modules/client/views/ScanPaper.vue

@@ -303,8 +303,9 @@ export default {
     async evokeScanNotDelayOver() {
       await this.getScaningFile();
 
-      const scanCount = this.scanStageList.length - this.scanCount;
-      this.scanCount = this.scanStageList.length;
+      const scanCount =
+        this.scanStageList.length + this.errorStageList.length - this.scanCount;
+      this.scanCount = this.scanStageList.length + this.errorStageList.length;
 
       // 已扫描的数据
       const res = getPreUploadFiles(this.GLOBAL.input);
@@ -454,6 +455,7 @@ export default {
 
       this.clearViewPapers();
       logger.info(`04-1开始保存数据`);
+
       try {
         let datas = this.scanStageList
           .map((item) => {
@@ -466,7 +468,44 @@ export default {
           })
           .flat();
 
+        const stageValid = checkStageFileValid(
+          datas.map((item) => {
+            return {
+              frontFile: item.frontOriginImgPath,
+              versoFile: item.versoOriginImgPath,
+            };
+          })
+        );
+        if (!stageValid) {
+          logger.error(`04-1-1 stage 数据不完整,请重新扫描!`);
+          this.$notify.error({
+            title: "数据校验错误",
+            message: "stage数据不完整,请重新扫描!",
+            duration: 0,
+          });
+          return;
+        }
+
         datas = await batchSaveImages(datas, this.task.courseCode);
+
+        const outValid = checkStageFileValid(
+          datas.map((item) => {
+            return {
+              frontFile: item.frontOriginImgPath,
+              versoFile: item.versoOriginImgPath,
+            };
+          })
+        );
+        if (!outValid) {
+          logger.error(`04-1-2 out 数据不完整,请重新扫描!`);
+          this.$notify.error({
+            title: "数据校验错误",
+            message: "out数据不完整,请重新扫描!",
+            duration: 0,
+          });
+          return;
+        }
+
         await db.batchSaveUploadInfo(datas);
       } catch (err) {
         console.error(err);

+ 30 - 24
src/plugins/imageOcr.js

@@ -188,32 +188,38 @@ export function renamePreUploadJsonFile(dir) {
   });
 }
 
-export async function moveFile(src, dest, count = 1) {
-  let result = true;
-  await fs.promises.rename(src, dest).catch((error) => {
-    logger.error(
-      `loop-03-01 转存文件失败,rename:${count}:${
-        error.message || "rename error"
-      }`
-    );
-    result = false;
-  });
-  if (result) return;
+export async function moveFile(src, dest, maxRetries = 3) {
+  let success = false;
 
-  if (count < 3) {
-    count++;
-    await moveFile(src, dest, count);
-  } else {
+  for (let attempt = 1; attempt <= maxRetries; attempt++) {
     try {
-      await fs.promises.copyFile(src, dest);
-      await fs.promises.unlink(src);
+      await fs.promises.rename(src, dest);
+      logger.info(`文件${src}已成功转存`);
+      success = true;
+      break;
     } catch (error) {
       logger.error(
-        `loop-03-01 转存文件失败:copy:${error.message || "copy unlink error"}`
+        `loop-03-01 转存文件失败,rename尝试:${attempt},错误: ${
+          error.message || "rename error"
+        }`
       );
-      return Promise.reject(error);
     }
   }
+
+  if (success) return;
+
+  try {
+    await fs.promises.copyFile(src, dest);
+    await fs.promises.unlink(src);
+    logger.info(`文件${src}已成功转存`);
+  } catch (error) {
+    logger.error(
+      `loop-03-01 转存文件失败:最终尝试copy,错误: ${
+        error.message || "copy unlink error"
+      }`
+    );
+    throw error;
+  }
 }
 
 /**
@@ -357,7 +363,7 @@ export async function batchSaveImages(imageList, courseCode) {
   const outputDir = path.join(getOutputDir("origin"), courseCode);
   if (!fs.existsSync(outputDir)) makeDirSync(outputDir);
 
-  const { buildQueue } = usePQueue({ concurrency: 6 });
+  const { buildQueue } = usePQueue({ concurrency: 3 });
 
   const images = imageList
     .map((item, index) => {
@@ -379,12 +385,12 @@ export async function batchSaveImages(imageList, courseCode) {
   const tasks = images.map((item) => {
     return async () => {
       const outputOriginPath = path.join(outputDir, path.basename(item.url));
-      let result = true;
+      // let result = true;
       await moveFile(item.url, outputOriginPath).catch((error) => {
         console.error(`Error moving file: ${item.url}, ${error.message}`);
-        result = false;
+        // result = false;
       });
-      if (!result) return;
+      // if (!result) return;
       const urlKey = item.front ? "frontOriginImgPath" : "versoOriginImgPath";
       imageList[item.index][urlKey] = outputOriginPath;
     };
@@ -399,7 +405,7 @@ export async function moveInFileToStage(dir) {
   const ddir = dir || getInputDir();
   const stageDdir = getStageDir();
   const files = fs.readdirSync(ddir);
-  const { buildQueue } = usePQueue({ concurrency: 6 });
+  const { buildQueue } = usePQueue({ concurrency: 3 });
 
   const tasks = files.map((fileName) => {
     return async () => {