Преглед на файлове

fix: exportPDF cache oss url

ryanemax преди 2 месеца
родител
ревизия
e6542474d2
променени са 2 файла, в които са добавени 39 реда и са изтрити 7 реда
  1. 25 3
      server/cloud/tbook/func-tbook-export.js
  2. 14 4
      server/lib/docs/index.js

+ 25 - 3
server/cloud/tbook/func-tbook-export.js

@@ -1,4 +1,4 @@
-import { replaceDocx, createZip, uploadFileToOSS, docxToPdf, renderDocx, toBarCode } from "../../lib/docs";
+import { replaceDocx, createZip, uploadFileToOSS, docxToPdf, renderDocx, toBarCode, downloadUrl } from "../../lib/docs";
 // const Parse = global.Parse;
 
 const path = require("path")
@@ -202,8 +202,9 @@ export async function exportProcessReportDocs(processId,bookList) {
         plist.push(renderReportDocsByTextbook(textbook)); // 立即执行并返回 Promise
     }
     let presults = await Promise.all(plist);
+    console.log(presults)
     presults.forEach(result=>{
-        if(result?.filePath){
+        if(result?.filePath || result?.pdfPath){
             docsList.push(result)
         }
     })
@@ -418,13 +419,30 @@ function pushDistinctItem(list,item){
     return list
 }
 function renderReportDocsByTextbook(textbook){
+    
     console.log("renderReportDocsByTextbook")
     return new Promise(async (resolve)=>{
+  
         let json = textbook.toJSON();
         json.hasNoSign = false; // 未上传作者签名
         json.hasNoUnit = false; // 未上传单位资料
         json.hasNoNine = false; // 无需添加滞后九
-    let bookid = json.code || json?.objectId;
+        let bookid = json.code || json?.objectId;
+    
+        let exportPDF = textbook.get("exportPDF");
+        if(exportPDF){
+            // console.log(exportPDF)
+            let exportPath = await downloadUrl(exportPDF,{tempDir:path.join(tempDir,textbook.id),keepFileName:true});
+            // console.log(exportPath)
+            resolve({
+                code:bookid,
+                objectId:textbook.id,
+                title:json?.title,
+                pdfPath:exportPath,
+                urlPdf:exportPDF,
+            })
+            return
+        }
 
     // 默认单册为第一分册
     let book1Data = json?.childrens?.[0]
@@ -672,6 +690,10 @@ function renderReportDocsByTextbook(textbook){
             console.log("PDF CREATED:",filePath)
             if(pdfPath){
                 urlPdf = (await uploadFileToOSS(pdfPath,json?.objectId))?.url || null
+                if(urlPdf){
+                    textbook.set("exportPDF",urlPdf);
+                    textbook.save();
+                }
             }
         }catch(err){
             console.error(err)

+ 14 - 4
server/lib/docs/index.js

@@ -126,12 +126,22 @@ module.exports.createZip = createZip
 
 
 const download = require('download')
-async function downloadUrl(url,options) {
+export async function downloadUrl(url,options) {
+    if(!fs.existsSync(options?.tempDir)) fs.mkdirSync(options?.tempDir);
     // console.log(url)
     if(url?.startsWith("/")) {return url};
-    let md5 = crypto.createHash('md5');
-    let extname = path.extname(url)?.toLocaleLowerCase();
-    let filename = md5.update(url).digest('hex') + extname;
+   
+    // 保留文件名
+    let filename = ""
+    if(options?.keepFileName){
+        filename = decodeURIComponent(path.basename(url));
+    }else{
+        // 文件名转MD5
+        let md5 = crypto.createHash('md5');
+        let extname = path.extname(url)?.toLocaleLowerCase();
+        filename = md5.update(url).digest('hex') + extname;
+    }
+
     let filepath = path.join(options?.tempDir||tempDir,filename)
     // console.log(filename,filepath)
     try{