john 8 月之前
父節點
當前提交
a363adb71a
共有 3 個文件被更改,包括 96 次插入9 次删除
  1. 5 4
      epub_node/router/epub/index.js
  2. 66 0
      epub_node/router/epub/style.js
  3. 25 5
      epub_node/router/epub/txt.js

+ 5 - 4
epub_node/router/epub/index.js

@@ -9,6 +9,7 @@ import { dirExists, isFileSync, isDir, waittime } from "#utils";
 
 import { saveImgs } from "./image.js";
 import { htmlParser } from "./txt.js";
+import { saveAllCSS } from "./style.js";
 
 const router = express.Router();
 
@@ -53,9 +54,7 @@ router.get("/img/:fileId", async function (req, res) {
   }
 
   const uploadPath = "./base_files/" + fileId;
-  console.log("uploadPath", uploadPath);
   const filePath = path.resolve(uploadPath);
-  console.log("filePath", filePath);
   // 检查文件是否存在
   if (!fs.existsSync(filePath)) {
     return res.status(404).send("服务器中不存在该文件");
@@ -130,11 +129,13 @@ router.put("/", async function (req, res) {
     1、读取图片信息
     2、替换文件中的图片内容
     3、存储html数据
+    4、存储css数据
    */
-  // await saveImgs(epub);
+  // await saveImgs(epub, uploadPath);
+  await saveAllCSS(epub, uploadPath)
 
   // 存储html数据
-  await htmlParser(epub, zipEpubExtract);
+  // await htmlParser(epub, zipEpubExtract);
   // console.log("\nSPINE:\n");
   // // console.log(epub.flow);
   // epub.flow.forEach((elm) => {

+ 66 - 0
epub_node/router/epub/style.js

@@ -0,0 +1,66 @@
+import { dirExists } from "#utils";
+import { files_insert } from "#db";
+import crypto from "crypto";
+import fs from "node:fs";
+import logger from "#logger";
+
+export async function saveAllCSS(epub, uploadPath) {
+  dirExists(uploadPath);
+
+  // 获取原始数据源
+  const getAllCss = epub.zip.names.filter(
+    (elm) => elm.indexOf("css") > -1
+  );
+  console.log(14, getAllCss);
+  //   let imgs = epub.listImage();
+  //   if (imgs.length) {
+  //     const imgRes = await Promise.allSettled(
+  //       imgs.map((img) => {
+  //         return epub.getImageAsync(img.id);
+  //       })
+  //     );
+  //     // 过滤数据
+
+  //     const imgs_fulfilled = imgs
+  //       .map((img, index) => {
+  //         const img_fulfilled = imgRes[index];
+  //         if (img_fulfilled.status === "fulfilled") {
+  //           const [img_data, img_mimeType] = img_fulfilled.value;
+  //           return {
+  //             ...img,
+  //             index,
+  //             img_data,
+  //             img_mimeType,
+  //           };
+  //         }
+  //         return false;
+  //       })
+  //       .filter((elm) => elm);
+
+  //     await Promise.allSettled(
+  //       imgs_fulfilled.map(async (elm) => {
+  //         const img_md5 = await calculateMD5FromBuffer(elm.img_data);
+
+  //         const uploadPath = "./base_files/" + img_md5;
+  //         const params = {
+  //           file_id: img_md5,
+  //           md5: img_md5,
+  //           mimetype: elm.img_mimeType,
+  //           size: elm.img_data.length,
+  //           name: elm.id,
+  //           path: elm.href,
+  //           source_id: elm.id,
+  //         };
+
+  //         fs.writeFile(uploadPath, elm.img_data, (err) => {
+  //           if (err) {
+  //             logger.error("Error writing Img file:", err);
+  //           } else {
+  //             logger.info("Img data saved to " + img_md5);
+  //           }
+  //         });
+  //         return await files_insert(params);
+  //       })
+  //     );
+  //   }
+}

+ 25 - 5
epub_node/router/epub/txt.js

@@ -5,8 +5,8 @@ import * as cheerio from "cheerio";
 // 置换文件中的图片路径
 async function processFiles(elmDate) {
   const rows = elmDate.toString().split(/\n/);
-  let htmlStr = ''
-  for (const rowtext of rows) {    
+  let htmlStr = "";
+  for (const rowtext of rows) {
     if (
       rowtext.includes("Images") &&
       (rowtext.includes(".png") ||
@@ -18,7 +18,27 @@ async function processFiles(elmDate) {
         const [imgText, imgPath, imageSrc] = match;
         const imgRow = await searchFileByPath(imageSrc);
         if (imgRow) {
-          const text = rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`);
+          const text = rowtext.replace(
+            imgPath,
+            `/api/v1/epub/img/${imgRow.file_id}`
+          );
+          htmlStr += text + "\n";
+        } else {
+          htmlStr += rowtext + "\n";
+        }
+      } else {
+        htmlStr += rowtext + "\n";
+      }
+    } else if (rowtext.includes(".css")) {
+      const match = rowtext.match(/.*"(.*\/(.*\.css))/);
+      if (match) {
+        const [cssText, cssPath, cssSrc] = match;
+        const imgRow = await searchFileByPath(imageSrc);
+        if (imgRow) {
+          const text = rowtext.replace(
+            imgPath,
+            `/api/v1/epub/css/${imgRow.file_id}`
+          );
           htmlStr += text + "\n";
         } else {
           htmlStr += rowtext + "\n";
@@ -30,7 +50,7 @@ async function processFiles(elmDate) {
       htmlStr += rowtext + "\n";
     }
   }
-  return Promise.resolve(htmlStr)
+  return Promise.resolve(htmlStr);
 }
 
 export async function htmlParser(epub, zipEpubExtract) {
@@ -44,7 +64,7 @@ export async function htmlParser(epub, zipEpubExtract) {
     const elmDate = fs.readFileSync(zipEpubExtract + elm);
     let htmlStr = await processFiles(elmDate);
     // 修改源数据
-    fs.writeFileSync(zipEpubExtract + elm, htmlStr)
+    fs.writeFileSync(zipEpubExtract + elm, htmlStr);
     console.log(48, zipEpubExtract + elm);
   }
 }