Răsfoiți Sursa

优化断点续传

Michael Wang 3 ani în urmă
părinte
comite
ba348a418d
3 a modificat fișierele cu 36 adăugiri și 15 ștergeri
  1. 29 11
      src/features/ImageDownload/ImageDownload.vue
  2. 3 4
      src/lib/watermark.ts
  3. 4 0
      src/preload.ts

+ 29 - 11
src/features/ImageDownload/ImageDownload.vue

@@ -32,7 +32,7 @@ async function getImageDim(
     img.src = URL.createObjectURL(blob);
     img.onload = () => {
       URL.revokeObjectURL(img.src);
-      console.log(img.width);
+      // console.log(img.width);
       res([img.width, img.height]);
     };
   });
@@ -80,6 +80,19 @@ onMounted(async () => {
         for (const student of students) {
           for (const sheetUrl of student.sheetUrls) {
             try {
+              const index = student.sheetUrls.indexOf(sheetUrl);
+              student.index = index + 1;
+              student.examId = store.env.examId;
+              const filePath = window.electron.join(
+                config.dir,
+                mustache.render(config.template, student)
+              );
+              if (window.electron.existsSync(filePath) && config.append) {
+                console.log(filePath + " already exists");
+                finishedCount.value += 1;
+                continue;
+              }
+
               const imageRes = await httpApp.get(sheetUrl, {
                 responseType: "blob",
               });
@@ -89,16 +102,13 @@ onMounted(async () => {
               // console.log(imageRes.data);
               // console.log(await imageRes.data.arrayBuffer());
               // console.log(new Uint8Array(await imageRes.data.arrayBuffer()));
-              const index = student.sheetUrls.indexOf(sheetUrl);
-              student.index = index + 1;
-              student.examId = store.env.examId;
-              // console.log(mustache.render(config.template, student));
+
               await window.electron.addWatermark(
                 JSON.parse(JSON.stringify(store)),
                 await imageRes.data.arrayBuffer(),
                 width,
                 height,
-                [config.dir, mustache.render(config.template, student)],
+                filePath,
                 student,
                 index + 1,
                 config.trackMode
@@ -127,21 +137,29 @@ onMounted(async () => {
         p.examId = store.env.examId;
         for (let i = 0; i < p.urls.length; i++) {
           try {
+            const index = i + 1;
+            p.index = index;
+            const filePath = window.electron.join(
+              config.dir,
+              mustache.render(config.template, p)
+            );
+            if (window.electron.existsSync(filePath) && config.append) {
+              console.log(filePath + " already exists");
+              finishedCount.value += 1;
+              continue;
+            }
+
             const imageRes = await httpApp.get(p.urls[i], {
               responseType: "blob",
             });
 
             const [width, height] = await getImageDim(imageRes.data);
-
-            const index = i + 1;
-            p.index = index;
-            // console.log(mustache.render(config.template, student));
             await window.electron.addWatermark(
               JSON.parse(JSON.stringify(store)),
               await imageRes.data.arrayBuffer(),
               width,
               height,
-              [config.dir, mustache.render(config.template, p)],
+              filePath,
               p,
               index,
               config.trackMode

+ 3 - 4
src/lib/watermark.ts

@@ -12,7 +12,7 @@ export async function addWatermark(
   imageData: ArrayBuffer,
   imageWidth: number,
   imageHeight: number,
-  file: Array<string> | string,
+  file: string,
   student: Student,
   index: number,
   trackMode: string
@@ -23,7 +23,6 @@ export async function addWatermark(
   //   store.config.watermark.color,
   //   student.objectiveScore
   // );
-  file = Array.isArray(file) ? path.join(...file) : file;
   if (!gm) {
     gm =
       store.config.imagemagick != undefined
@@ -234,8 +233,8 @@ export async function addWatermark(
   }
 
   return new Promise((resolve, reject) => {
-    mkdirp.sync(path.dirname(file as string));
-    imgData.write(file as string, (error) => {
+    mkdirp.sync(path.dirname(file));
+    imgData.write(file, (error) => {
       if (error) {
         // logger.error("add watermark error: " + file);
         // logger.error(error);

+ 4 - 0
src/preload.ts

@@ -3,6 +3,8 @@ import { contextBridge, remote } from "electron";
 import config from "./lib/config";
 import { addWatermark } from "./lib/watermark";
 import { queryStudentCount, replaceStudents, replacePackage } from "./lib/sync";
+import { existsSync } from "fs";
+import { join } from "path";
 
 export const electronInWindow = {
   dialog: remote.dialog,
@@ -11,6 +13,8 @@ export const electronInWindow = {
   queryStudentCount,
   replaceStudents,
   replacePackage,
+  existsSync,
+  join,
 };
 
 contextBridge.exposeInMainWorld("electron", electronInWindow);