Browse Source

small performance improve

Michael Wang 3 years ago
parent
commit
1d6add3383
3 changed files with 31 additions and 18 deletions
  1. 19 10
      src/features/ImageDownload/ImageDownload.vue
  2. 10 3
      src/lib/watermark.ts
  3. 2 5
      src/preload.ts

+ 19 - 10
src/features/ImageDownload/ImageDownload.vue

@@ -93,6 +93,7 @@ onMounted(async () => {
         subjectCodeIn: config.subjectCode ?? "",
       });
       totalCount.value = res.data;
+      let totalImageDownloadTime = 0;
 
       for (
         let pageNumber = 0;
@@ -127,11 +128,15 @@ onMounted(async () => {
               const index = student.sheetUrls.indexOf(sheetUrl);
               student.index = index + 1;
               student.examId = store.env.examId;
-              const filePath = window.electron.join(
+              // const filePath = window.electron.join(
+              //   config.dir,
+              //   mustache.render(config.template, student)
+              // );
+              const filePath = [
                 config.dir,
-                mustache.render(config.template, student)
-              );
-              if (config.append && window.electron.existsSync(filePath)) {
+                mustache.render(config.template, student),
+              ];
+              if (config.append && window.electron.existsImage(filePath)) {
                 console.log(filePath + " already exists");
                 // 执行到这里时,可能图片已经cache了
                 urls.splice(urls.indexOf(sheetUrl), 1);
@@ -140,12 +145,14 @@ onMounted(async () => {
               }
 
               console.debug("start ", sheetUrl);
+              const imageDownloadStartTime = Date.now();
               const imageRes = await httpApp.get(sheetUrl, {
                 responseType: "blob",
                 headers: {
                   "Cache-Control": "no-cache",
                 },
               });
+              totalImageDownloadTime += Date.now() - imageDownloadStartTime;
 
               const [width, height] = await getImageDim(imageRes.data);
               const arrayBuffer = await imageRes.data.arrayBuffer();
@@ -180,7 +187,12 @@ onMounted(async () => {
           finishedCount.value += 1;
         }
       }
-      console.log("all end ", Date.now());
+      console.log(
+        "all end at ",
+        Date.now(),
+        " totalImageDownloadTime: ",
+        totalImageDownloadTime
+      );
     } else if (config.type === "2") {
       await processPackage();
     }
@@ -217,11 +229,8 @@ async function processPackage() {
       try {
         const index = i + 1;
         p.index = index;
-        const filePath = window.electron.join(
-          config.dir,
-          mustache.render(config.template, p)
-        );
-        if (config.append && window.electron.existsSync(filePath)) {
+        const filePath = [config.dir, mustache.render(config.template, p)];
+        if (config.append && window.electron.existsImage(filePath)) {
           console.log(filePath + " already exists");
           urls.splice(urls.indexOf(p.urls[i]), 1);
           continue;

+ 10 - 3
src/lib/watermark.ts

@@ -12,18 +12,19 @@ export async function addWatermark(
   imageData: ArrayBuffer,
   imageWidth: number,
   imageHeight: number,
-  file: string,
+  filePath: string[],
   student: Student,
   index: number,
   trackMode: string,
   x = 0.01,
   y = 0.03
 ): Promise<boolean> {
+  const file = path.join(...filePath);
   if (
     index !== 1 &&
     (student.tags == undefined || student.tags[index] == undefined)
   ) {
-    return await saveImage(store, imageData, file);
+    return await saveImage(store, imageData, filePath);
   }
 
   if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
@@ -260,8 +261,10 @@ export async function addWatermark(
 export async function saveImage(
   store: Store,
   imageData: ArrayBuffer,
-  file: string
+  filePath: string[]
 ): Promise<boolean> {
+  const file = path.join(...filePath);
+
   if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
     console.log(file + " already exists");
     return true;
@@ -279,3 +282,7 @@ export async function saveImage(
     });
   });
 }
+
+export function existsImage(pathSepArray: string[]): boolean {
+  return fs.existsSync(path.join(...pathSepArray));
+}

+ 2 - 5
src/preload.ts

@@ -1,10 +1,8 @@
 import { contextBridge, remote } from "electron";
 
 import config from "./lib/config";
-import { saveImage, addWatermark } from "./lib/watermark";
+import { existsImage, saveImage, 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,
@@ -14,8 +12,7 @@ export const electronInWindow = {
   queryStudentCount,
   replaceStudents,
   replacePackage,
-  existsSync,
-  join,
+  existsImage,
 };
 
 contextBridge.exposeInMainWorld("electron", electronInWindow);