Pārlūkot izejas kodu

整合文件资源

john 7 mēneši atpakaļ
vecāks
revīzija
65543e1c52

+ 0 - 2
epub_node/db/files.js

@@ -60,8 +60,6 @@ export async function files_insert_link_epub({
           console.log(606060, err);
           return resolve(false);
         }
-        console.log(606060, row);
-
         return resolve(row);
       });
     } catch (err) {

+ 6 - 5
epub_node/router/epub/image.js

@@ -45,9 +45,6 @@ export async function saveImgs(epub, uploadPath, book_id, author_id) {
           path: elm.href,
           source_id: elm.id,
         };
-
-        console.log(49, imgUploadPath);
-
         fs.writeFile(imgUploadPath, elm.img_data, (err) => {
           if (err) {
             logger.error("Error writing Img file:", err);
@@ -55,8 +52,12 @@ export async function saveImgs(epub, uploadPath, book_id, author_id) {
             logger.info("Img data saved to " + img_md5);
           }
         });
-        await files_insert_link_epub({ file_id: img_md5, book_id, author_id });
-        return await files_insert(params);
+        await files_insert(params);
+        return await files_insert_link_epub({
+          file_id: img_md5,
+          book_id,
+          author_id,
+        });
       })
     );
   }

+ 32 - 21
epub_node/router/epub/index.js

@@ -27,40 +27,50 @@ router.use(function timeLog(req, res, next) {
 router.get("/", async function (req, res) {
   res.send("epub types");
 });
+
 // define the about route
 router.get("/about", function (req, res) {
   res.send("About types");
 });
-router.get("/html", function (req, res) {
-  //
-  // res.sendFile('base_files/2a0b8153f3ede4bd43abb3b0e38ee857/epub-extract/OEBPS/Text/:::::::::::::::::::::*:*::**::*:.html')
-  // res.send("About types");
-  // 构建文件的绝对路径
-  const filePath = path.join(
-    process.cwd(),
-    // "./base_files/2a0b8153f3ede4bd43abb3b0e38ee857/epub-extract/OEBPS/Text/:::::::::::::::::::::*:*::**::*:.html"
-    "./base_files/5ae2d9158081faab184484ed1783e176/epub-extract/OEBPS/text00040.html"
-  );
-
-  // 发送文件
-  res.sendFile(filePath, function (err) {
-    if (err) {
-      console.error("Error sending file:", err);
-      res.status(500).send("File not found or other error occurred.");
-    }
-  });
+
+router.get("/html/:fileId", async function (req, res) {
+  const fileId = req.params.fileId; // 获取 fileId 参数
+  logger.info(`Found ${fileId}`);
+  const fileRow = await getFileBymd5(fileId);
+
+  if (!fileRow) {
+    return res.status(404).send("文件查询失败");
+  }
+  const uploadPath =
+    "./base_files/" + fileRow.book_id + "/Text/" + fileId + ".html";
+  console.log(46, fileRow);
+
+  const filePath = path.resolve(uploadPath);
+  // 检查文件是否存在
+  if (!fs.existsSync(filePath)) {
+    return res.status(404).send("服务器中不存在该文件");
+  }
+
+  // 返回文件
+  res.setHeader("Content-Type", fileRow.mimetype);
+  res.sendFile(filePath);
 });
 
 router.get("/img/:fileId", async function (req, res) {
   const fileId = req.params.fileId; // 获取 fileId 参数
   logger.info(`Found ${fileId}`);
+  console.log(6262626, fileId);
+
   const fileRow = await getFileBymd5(fileId);
+  console.log(6565, fileRow);
 
   if (!fileRow) {
     return res.status(404).send("文件查询失败");
   }
   const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
   const filePath = path.resolve(uploadPath);
+  console.log(727272, filePath, fs.existsSync(filePath));
+
   // 检查文件是否存在
   if (!fs.existsSync(filePath)) {
     return res.status(404).send("服务器中不存在该文件");
@@ -79,8 +89,10 @@ router.get("/css/:fileId", async function (req, res) {
   if (!fileRow) {
     return res.status(404).send("文件查询失败");
   }
+  const uploadPath =
+    "./base_files/" + fileRow.book_id + "/style/" + fileId + ".css";
 
-  const filePath = path.resolve(fileRow.path);
+  const filePath = path.resolve(uploadPath);
   // 检查文件是否存在
   if (!fs.existsSync(filePath)) {
     return res.status(404).send("服务器中不存在该文件");
@@ -156,9 +168,8 @@ router.put("/", async function (req, res) {
    */
   await saveImgs(epub, uploadPath, file_md5, author_id);
   await saveAllCSS(epub, uploadPath, file_md5, author_id);
-
   // 存储html数据
-  const test = await htmlParser(epub, zipEpubExtract, file_md5);
+  await htmlParser(epub, zipEpubExtract, file_md5, author_id);
 
   // console.log("\nSPINE:\n");
   // // console.log(epub.flow);

+ 6 - 2
epub_node/router/epub/style.js

@@ -66,8 +66,12 @@ export async function saveAllCSS(epub, uploadPath, book_id, author_id) {
           path: `${uploadPath}style/${md5}.css`,
           source_id: md5,
         };
-        await files_insert_link_epub({ file_id: md5, book_id, author_id });
-        return await files_insert(params);
+        await files_insert(params);
+        return await files_insert_link_epub({
+          file_id: md5,
+          book_id,
+          author_id,
+        });
       })
     );
   }

+ 69 - 50
epub_node/router/epub/txt.js

@@ -1,74 +1,93 @@
 import fs from "node:fs";
-import { getFileBymd5, searchFileByPath } from "#db";
+import path from "node:path";
+import { dirExists, isFileSync, isDir, waittime } from "#utils";
+import {
+  getFileBymd5,
+  searchFileByPath,
+  files_insert_link_epub,
+  files_insert,
+} from "#db";
+import { calculateMD5 } from "./image.js";
 
-import * as cheerio from "cheerio";
+const imageExtensions = [".png", ".jpg", ".jpeg"];
 
-// 置换文件中的图片路径
-async function processFiles(elmDate, file_md5) {
-  const rows = elmDate.toString().split(/\n/);
-  let htmlStr = "";
-  for (const rowtext of rows) {
+async function processFiles(elmData, file_md5) {
+  const rows = elmData.toString().split(/\n/);
+  const promises = rows.map(async (rowtext) => {
     if (
       rowtext.includes("Images") &&
-      (rowtext.includes(".png") ||
-        rowtext.includes(".jpg") ||
-        rowtext.includes(".jpeg"))
+      imageExtensions.some((ext) => rowtext.includes(ext))
     ) {
-      const match = rowtext.match(/.*(..\/Images\/(.*(jpg|png|jpeg))).*/);
+      const match = rowtext.match(/.*(..\/Images\/(.*\.(jpg|png|jpeg))).*/);
       if (match) {
-        const [imgText, imgPath, imageSrc] = match;
+        const [, imgPath, imageSrc] = match;
         const imgRow = await searchFileByPath(imageSrc);
         if (imgRow) {
-          const text = rowtext.replace(
-            imgPath,
-            `/api/v1/epub/img/${imgRow.file_id}`
+          return (
+            rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`) +
+            "\n"
           );
-          htmlStr += text + "\n";
-        } else {
-          htmlStr += rowtext + "\n";
         }
-      } else {
-        htmlStr += rowtext + "\n";
       }
-      return;
-    }
-    if (rowtext.includes(".css")) {
+    } else if (rowtext.includes(".css")) {
       const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
       if (match) {
-        const [cssText, cssPath, cssSrc] = match;
+        const [, cssPath, cssSrc] = match;
         const imgRow = await searchFileByPath(cssSrc, file_md5);
         if (imgRow) {
-          const text = rowtext.replace(
-            cssPath,
-            `/api/v1/epub/css/${imgRow.file_id}`
+          return (
+            rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
+            "\n"
           );
-          htmlStr += text + "\n";
-        } else {
-          htmlStr += rowtext + "\n";
         }
-      } else {
-        htmlStr += rowtext + "\n";
       }
-    } else {
-      htmlStr += rowtext + "\n";
     }
-  }
-  return Promise.resolve(htmlStr);
+    return rowtext + "\n";
+  });
+
+  const results = await Promise.all(promises);
+  return results.join("");
 }
 
-export async function htmlParser(epub, zipEpubExtract, file_md5) {
-  // 获取原始数据源
-  const needSetImge = epub.zip.names.filter(
-    (elm) => elm.indexOf(".html") > -1 || elm.indexOf(".css") > -1
+export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
+  const needSetImage = epub.zip.names.filter(
+    (elm) => elm.endsWith(".html") || elm.endsWith(".css")
+  );
+
+  const basePath = path.join("./base_files", file_md5, "Text");
+  dirExists(basePath);
+
+  await Promise.all(
+    needSetImage.map(async (elm) => {
+      const filePath = path.join(zipEpubExtract, elm);
+      const elmData = fs.readFileSync(filePath);
+      const htmlStr = await processFiles(elmData, file_md5);
+
+      if (htmlStr) {
+        fs.writeFileSync(filePath, htmlStr);
+
+        const htmlMd5 = await calculateMD5(filePath);
+        const newFilePath = path.join(basePath, `${htmlMd5}.html`);
+
+        const params = {
+          file_id: htmlMd5,
+          md5: htmlMd5,
+          mimetype: "text/html",
+          size: Buffer.byteLength(htmlStr),
+          name: `${htmlMd5}.html`,
+          path: newFilePath,
+          source_id: elm,
+        };
+        await files_insert(params);
+        await Promise.all([
+          files_insert_link_epub({
+            file_id: htmlMd5,
+            book_id: file_md5,
+            author_id,
+          }),
+          fs.promises.writeFile(newFilePath, htmlStr),
+        ]);
+      }
+    })
   );
-  for (let i = 0; i < needSetImge.length; i++) {
-    // 执行当前层的异步操作
-    const elm = needSetImge[i];
-    const elmDate = fs.readFileSync(zipEpubExtract + elm);
-    let htmlStr = await processFiles(elmDate, file_md5);
-    if(htmlStr) {
-      // 修改源数据
-      fs.writeFileSync(zipEpubExtract + elm, htmlStr);
-    }
-  }
 }