Ver código fonte

fix: merge plist

ryanemax 2 meses atrás
pai
commit
ac6805cfd0
1 arquivos alterados com 23 adições e 13 exclusões
  1. 23 13
      server/lib/docs/index.js

+ 23 - 13
server/lib/docs/index.js

@@ -268,26 +268,36 @@ const sizeOf = require("image-size");
  * @returns 
  */
 export async function mergePdfListReduce(pdfList,convertOpts){
+    // console.log("pdfList",pdfList)
     let mergeList = []
-    for (let index = 0; index < pdfList.length; index++) {
+    let plist = []
+    let length = pdfList.length
+    for (let index = 0; index < length; index++) {
         let file = pdfList.shift();
+        // console.log(file,index,length)
         if(!file) break;
         let files = [file,pdfList.shift(),pdfList.shift()]; // 每次合并三个
         files=files?.filter(item=>item);
-
-        if(files?.length==1){ // 单文件直接加载 自动获取后缀
-            let onefile = files[0]
-            if(!onefile?.ext){
-                onefile = {data:fs.readFileSync(onefile),ext:path.extname(files[0]).slice(1)}
+        // console.log(files)
+        plist.push(new Promise(async resolve=>{
+            if(files?.length==1){ // 单文件直接加载 自动获取后缀
+                let onefile = files[0]
+                if(!onefile?.ext){
+                    onefile = {data:fs.readFileSync(onefile),ext:path.extname(files[0]).slice(1)}
+                }
+                resolve(onefile);
+            }else{ // 多文件合并
+                convertOpts.files = files;
+                // console.log("多文件合并",convertOpts)
+                    let mergeBuffer = await LibreOffice.convert(convertOpts);
+                    resolve({data:mergeBuffer,ext:"pdf"})
             }
-            mergeList.push(onefile);
-        }else{ // 多文件合并
-            convertOpts.files = files;
-            // console.log("多文件合并",convertOpts)
-            let mergeBuffer = await LibreOffice.convert(convertOpts);
-            mergeList.push({data:mergeBuffer,ext:"pdf"});
-        }
+        }))
+    }
+    if(plist?.length){
+        mergeList = await Promise.all(plist);
     }
+    // console.log("mergeList",mergeList)
     if(mergeList?.length==1){
         return mergeList[0];
     }else{