Bläddra i källkod

缓存图片,加快处理速度

Michael Wang 3 år sedan
förälder
incheckning
3897104aa1
1 ändrade filer med 47 tillägg och 6 borttagningar
  1. 47 6
      src/features/ImageDownload/ImageDownload.vue

+ 47 - 6
src/features/ImageDownload/ImageDownload.vue

@@ -38,6 +38,35 @@ async function getImageDim(
   });
 }
 
+// cache images
+async function cacheImages(urls: string) {
+  let allPromiseCount = 0;
+  let settledPromiseCount = 0;
+  const MAX_CONCURRENT = 6;
+
+  async function sleep() {
+    if (allPromiseCount - settledPromiseCount >= MAX_CONCURRENT) {
+      await new Promise((res) => setTimeout(res, 1000));
+      await sleep();
+    }
+  }
+  for (const url of urls) {
+    allPromiseCount++;
+    await sleep();
+    // console.log(url);
+    httpApp
+      .get(url, {
+        responseType: "blob",
+      })
+      .catch((e) => {
+        console.log(e, "cache error");
+      })
+      .finally(() => {
+        settledPromiseCount++;
+      });
+  }
+}
+
 const totalCount = ref(0);
 let finishedCount = ref(0);
 let errorCount = ref(0);
@@ -77,6 +106,12 @@ onMounted(async () => {
         );
         students = resStudents.data;
 
+        const urls = students.reduce(
+          (accumulator, stu) => accumulator.concat(stu.sheetUrls),
+          []
+        );
+        cacheImages(urls);
+
         for (const student of students) {
           for (const sheetUrl of student.sheetUrls) {
             try {
@@ -89,7 +124,9 @@ onMounted(async () => {
               );
               if (window.electron.existsSync(filePath) && config.append) {
                 console.log(filePath + " already exists");
-                finishedCount.value += 1;
+                // 执行到这里时,可能图片已经cache了
+                urls.splice(urls.indexOf(sheetUrl), 1);
+                // console.log(urls);
                 continue;
               }
 
@@ -113,8 +150,6 @@ onMounted(async () => {
                 index + 1,
                 config.trackMode
               );
-
-              finishedCount.value += 1;
             } catch (error) {
               errorCount.value += 1;
               if (config.failover) {
@@ -125,12 +160,19 @@ onMounted(async () => {
               }
             }
           }
+          // 下载完一个学生
+          finishedCount.value += 1;
         }
       }
     } else if (config.type === "2") {
       const res = await getPackages(store.env.examId, true, true);
       const array = res.data;
       totalCount.value = array.length;
+      const urls = array.reduce(
+        (accumulator, p) => accumulator.concat(p.urls),
+        []
+      );
+      cacheImages(urls);
 
       for (let i = 0; i < array.length; i++) {
         const p = array[i];
@@ -145,7 +187,7 @@ onMounted(async () => {
             );
             if (window.electron.existsSync(filePath) && config.append) {
               console.log(filePath + " already exists");
-              finishedCount.value += 1;
+              urls.splice(urls.indexOf(p.urls[i]), 1);
               continue;
             }
 
@@ -164,8 +206,6 @@ onMounted(async () => {
               index,
               config.trackMode
             );
-
-            finishedCount.value += 1;
           } catch (error) {
             errorCount.value += 1;
             if (config.failover) {
@@ -176,6 +216,7 @@ onMounted(async () => {
             }
           }
         }
+        finishedCount.value += 1;
       }
     }