john 7 mesiacov pred
rodič
commit
63a9697f23

+ 32 - 3
epub_node/db/author.js

@@ -27,13 +27,42 @@ export async function get_author_info(name = "") {
       const values = [name];
       // 直接接收 execute 返回的内容
       connection.execute(sql, values, (error, row) => {
-        if(error) {
-          return resolve('')
+        if (error) {
+          return resolve("");
         }
-        return resolve(row.length > 0 ? row[0] : '')
+        return resolve(row.length > 0 ? row[0] : "");
       });
     } catch (err) {
       return resolve(false);
     }
   });
 }
+
+export async function clear_all_data(name = "") {
+  /* 
+    DELETE FROM epub_manage.book;
+    DELETE FROM epub_manage.author;
+    DELETE FROM epub_manage.category;
+    DELETE FROM epub_manage.style;
+    DELETE FROM epub_manage.style_link_book;
+    DELETE FROM epub_manage.chapter;
+    DELETE FROM epub_manage.files;
+    DELETE FROM epub_manage.book_link_file;
+ */
+  return new Promise(async (resolve, reject) => {
+    try {
+      // 直接接收 execute 返回的内容
+      await connection.execute("DELETE FROM epub_manage.book;");
+      await connection.execute("DELETE FROM epub_manage.author;");
+      await connection.execute("DELETE FROM epub_manage.category;");
+      await connection.execute("DELETE FROM epub_manage.style;");
+      await connection.execute("DELETE FROM epub_manage.style_link_book;");
+      await connection.execute("DELETE FROM epub_manage.chapter;");
+      await connection.execute("DELETE FROM epub_manage.files;");
+      await connection.execute("DELETE FROM epub_manage.book_link_file;");
+      return resolve("ok");
+    } catch (err) {
+      return resolve(false);
+    }
+  });
+}

+ 32 - 6
epub_node/db/chapter.js

@@ -1,6 +1,5 @@
 import connection from "./base.js";
 
-
 /*
 *  `name` VARCHAR(255) NOT NULL,  -- Chapter name with a maximum length of 255 characters
     `book_id` VARCHAR(100) NOT NULL,  -- Book ID with a maximum length of 100 characters
@@ -17,7 +16,7 @@ export async function chapter_insert({
                                          book_id = "",
                                          author_id = "",
                                          content = "",
-                                         level = '',
+                                         level = "",
                                          order_index = "",
                                          order_id = "",
                                          old_path = "",
@@ -26,19 +25,26 @@ export async function chapter_insert({
     // 假设最大长度为255,您可能需要根据实际数据库定义调整
     const maxLength = 255;
     
-    
     const sql = `
         INSERT INTO chapter (name, book_id, author_id, content, level, order_index, order_id, old_path, path)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
     `;
     const values = [
-        name, book_id, author_id, content, level, order_index, order_id, old_path, path,
+        name,
+        book_id,
+        author_id,
+        content,
+        level,
+        order_index,
+        order_id,
+        old_path,
+        path,
     ];
     
     return new Promise((resolve, reject) => {
         connection.execute(sql, values, (error, result) => {
             if (error) {
-                console.error('Database error:', error);
+                console.error("Database error:", error);
                 return reject(error); // 使用 reject 处理错误
             }
             resolve(result); // 返回查询结果
@@ -46,7 +52,6 @@ export async function chapter_insert({
     });
 }
 
-
 /*根据文件路径查询章节数据*/
 
 // select * from files where source_id like '%part0042%'
@@ -72,3 +77,24 @@ export async function searchChapterInfoForPath(path, book_id) {
     });
 }
 
+/*获取当前书籍的所有数据*/
+export async function searchChapterForBookId({book_id, level = 10}) {
+    console.log(8282, level);
+    return new Promise((resolve, reject) => {
+        const query = `
+            SELECT files.*, chapter.*
+            FROM files
+                     INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
+                     INNER JOIN chapter ON chapter.content LIKE CONCAT('%', files.source_id, '%')
+            WHERE book_link_file.book_id = '${book_id}'
+              AND files.mimetype = 'text/html' ${level === 10 ? '' : `AND chapter.level = '${level}'`};
+        `;
+        console.log(9292, query)
+        connection.query(query, [], (err, rows) => {
+            if (err) {
+                return reject(err);
+            }
+            resolve(rows);
+        });
+    });
+}

+ 14 - 7
epub_node/environment/index.js

@@ -1,5 +1,12 @@
 function dbInfo() {
   // 根据需要更新db的数据配置
+  return {
+    host: "124.221.51.4",
+    port: 3306,
+    user: "root",
+    password: "btm-2024-.",
+    database: "epub_manage",
+  };
   // return {
   //   host: "localhost",
   //   port: 3306,
@@ -7,13 +14,13 @@ function dbInfo() {
   //   password: "12345678",
   //   database: "epub_manage",
   // };
-  return {
-    host: "192.168.2.101",
-    port: 6806,
-    user: "root",
-    password: "admin",
-    database: "epub_manage",
-  };
+  // return {
+  //   host: "192.168.2.101",
+  //   port: 6806,
+  //   user: "root",
+  //   password: "admin",
+  //   database: "epub_manage",
+  // };
 }
 
 

+ 39 - 14
epub_node/router/epub/index.js

@@ -4,6 +4,8 @@ import {
     searchFileByPath,
     author_insert,
     get_author_info,
+    clear_all_data,
+    searchChapterForBookId,
 } from "#db";
 import logger from "#logger";
 import path from "node:path";
@@ -30,6 +32,21 @@ router.get("/", async function (req, res) {
     res.send("epub types");
 });
 
+// 获取所有的章节数据
+router.get("/chapter_all/:book_id", async function (req, res) {
+    const book_id = req.params.book_id; // 获取 fileId 参数
+    res.send("epub types" + book_id);
+    console.log(393939, '开始查询')
+    const chapter_all = await searchChapterForBookId({book_id, level: '1'});
+    console.log(393939, chapter_all.length, chapter_all[0]);
+});
+router.get("/clear", async function (req, res) {
+    await clear_all_data();
+    // 使用 fs.rmSync 删除非空目录及其内容
+    fs.rmSync("./base_files", {recursive: true, force: true});
+    res.send("epub types");
+});
+
 // define the about route
 router.get("/about", function (req, res) {
     res.send("About types");
@@ -156,28 +173,36 @@ router.put("/", async function (req, res) {
         await author_insert({name: epub.metadata.creator, author_id: author_id});
     }
     /*
-      1、读取图片信息
-      2、替换文件中的图片内容
-      3、存储html数据
-      4、存储css数据
-      5、存储章节数据
-     */
+        1、读取图片信息
+        2、替换文件中的图片内容
+        3、存储html数据
+        4、存储css数据
+        5、存储章节数据
+       */
     res.send("书籍正在处理中,请稍后!");
-    await saveMateInfo(epub, uploadPath, file_md5, author_id)
-    logger.info('书籍的基础数据处理完毕')
+    console.log("书籍正在处理中,请稍后!");
+    await saveMateInfo(epub, uploadPath, file_md5, author_id);
+    logger.info("书籍的基础数据处理完毕");
+    console.log("书籍的基础数据处理完毕");
     await saveImgs(epub, uploadPath, file_md5, author_id);
-    logger.info('书籍的图片数据处理完毕')
+    logger.info("书籍的图片数据处理完毕");
+    console.log("书籍的图片数据处理完毕");
     await saveAllFount(epub, uploadPath, file_md5, author_id);
-    logger.info('书籍的字体数据处理完毕')
+    logger.info("书籍的字体数据处理完毕");
+    console.log("书籍的字体数据处理完毕");
     await saveAllCSS(epub, uploadPath, file_md5, author_id);
-    logger.info('书籍的css数据处理完毕')
+    logger.info("书籍的css数据处理完毕");
+    console.log("书籍的css数据处理完毕");
     // 存储html数据
     await htmlParser(epub, zipEpubExtract, file_md5, author_id);
-    logger.info('书籍的章节数据处理完毕')
+    logger.info("书籍的章节数据处理完毕");
+    console.log("书籍的章节数据处理完毕");
     // 章节数据
     await saveToc(epub, zipEpubExtract, file_md5, author_id);
-    logger.info('书籍的目录处理完毕')
-    logger.info('书籍处理完毕')
+    logger.info("书籍的目录处理完毕");
+    logger.info("书籍处理完毕");
+    console.log("书籍的目录处理完毕");
+    console.log("书籍处理完毕");
 });
 
 export default router;

+ 20 - 28
epub_node/router/epub/toc.js

@@ -1,28 +1,27 @@
 import logger from "#logger";
-import { chapter_insert, searchChapterInfoForPath } from "#db";
-import { dirExists } from "#utils";
+import {chapter_insert, searchChapterInfoForPath} from "#db";
+import {dirExists} from "#utils";
 import fs from "node:fs";
-import { calculateMD5 } from "./image.js";
-import cliProgress from 'cli-progress';
+import {calculateMD5} from "./image.js";
 
+// ./base_files/5ae2d9158081faab184484ed1783e176
 export async function saveToc(epub, uploadPath, book_id, author_id) {
-    // 初始化进度条
-    const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
-    progressBar.start(epub.toc.length, 0);
-
-    await Promise.allSettled(epub.toc.map(async (elm, index) => {
+    await Promise.all(epub.toc.map(async (elm) => {
         try {
-            const match = elm.href.match(/\/(.*\.html).*/);
-            let chapterInfo = { file_id: '' };
-            let path = '';
-
+            const match = `${elm.href}`.match(/\/(.*\.html).*/)
+            let chapterInfo = {
+                file_id: ''
+            }
+            let path = ''
             if (match) {
                 path = match[1];
-                chapterInfo = await searchChapterInfoForPath(path, book_id);
+                chapterInfo = await searchChapterInfoForPath(path, book_id)
             }
-
+            logger.info(elm);
+            const  name = `${elm.title}`
+            elm.title = '';
             const params = {
-                name: elm.title,
+                name: name,
                 book_id: book_id,
                 author_id: author_id,
                 content: JSON.stringify(elm),
@@ -31,18 +30,11 @@ export async function saveToc(epub, uploadPath, book_id, author_id) {
                 order_id: chapterInfo.file_id,
                 old_path: elm.href,
                 path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
-            };
-            // 更新进度条
-            progressBar.update(index + 1);
-            await chapter_insert(params);
+            }
+            logger.info(params)
+            return await chapter_insert(params)
         } catch (e) {
-            logger.error(e);
+            logger.error(e)
         }
-    }));
-
-    // 更新进度条
-    progressBar.update(epub.toc.length);
-
-    // 停止进度条
-    progressBar.stop();
+    }))
 }

+ 145 - 139
epub_node/router/epub/txt.js

@@ -1,156 +1,162 @@
 import fs from "node:fs";
 import path from "node:path";
-import { dirExists, isFileSync, isDir, waittime } from "#utils";
+import {dirExists, isFileSync, isDir, waittime} from "#utils";
 import {
-  getFileBymd5,
-  searchFileByPath,
-  searchFileByName,
-  files_insert_link_epub,
-  files_insert,
-  book_mate_insert,
+    getFileBymd5,
+    searchFileByPath,
+    searchFileByName,
+    files_insert_link_epub,
+    files_insert,
+    book_mate_insert,
 } from "#db";
-import { calculateMD5 } from "./image.js";
-import cliProgress from "cli-progress";
-import logger from "#logger";
+import {calculateMD5} from "./image.js";
 
 const imageExtensions = [".png", ".jpg", ".jpeg", ".svg"];
 
 async function processFiles(elmData, file_md5) {
-  const rows = elmData.toString().split(/\n/);
-  const promises = rows.map(async (rowtext) => {
-    if (
-      rowtext.includes("<img ") &&
-      imageExtensions.some((ext) => rowtext.includes(ext))
-    ) {
-      const match = rowtext.match(/src=("|')(.*\/(.*\.[a-zA-Z]+))("|')/);
-      if (match) {
-        const [, , imgPath, imageSrc] = match;
-        const imgRow = await searchFileByPath(imageSrc);
-        if (imgRow) {
-          return (
-            rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`) +
-            "\n"
-          );
+    const rows = elmData.toString().split(/\n/);
+    const promises = rows.map(async (rowtext) => {
+        if (
+            rowtext.includes("<img ") &&
+            imageExtensions.some((ext) => rowtext.includes(ext))
+        ) {
+            // const match = rowtext.match(/.*(..\/Images\/(.*\.(jpg|png|jpeg|svg))).*/);
+            const match = rowtext.match(/src=("|')(.*\/(.*\.[a-zA-Z]+))("|')/)
+            if (match) {
+                const [, , imgPath, imageSrc] = match;
+                const imgRow = await searchFileByPath(imageSrc);
+                if (imgRow) {
+                    return (
+                        rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`) +
+                        "\n"
+                    );
+                }
+            }
+        } else if (rowtext.includes(".css")) {
+            const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
+            const [elmPath, elmName] = `${rowtext}`.match(/.*\/(.*\.css)/);
+            if (match) {
+                const [, cssPath, cssSrc] = match;
+                // const imgRow = await searchFileByPath(elmName, file_md5);
+                const imgNameRow =  await searchFileByName(elmName, file_md5);
+                if (imgNameRow) {
+                    return (
+                        rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
+                        "\n"
+                    );
+                }
+            }
+        } else if (rowtext.includes(".ttf")) {
+            // 使用正则表达式匹配路径和文件名
+            const match = rowtext.match(/.*\((.*\/?(.*ttf))\)./);
+            if (match) {
+                const [, cssPath, cssSrc] = match;
+                try {
+                    // 搜索数据库中是否存在该字体文件
+                    const imgRow = await searchFileByPath(cssSrc, file_md5);
+                    if (imgRow) {
+                        // 如果找到,替换路径为 API 端点
+                        console.log(57, rowtext);
+                        console.log(58, cssPath, cssSrc);
+                        console.log(59, `/api/v1/epub/css/${imgRow.file_id}`);
+                        
+                        return (
+                            rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
+                            "\n"
+                        );
+                    } else {
+                        console.warn(`Font file not found for path: ${cssSrc}`);
+                    }
+                } catch (error) {
+                    console.error("Error searching for font file:", error);
+                }
+            }
         }
-      }
-    } else if (rowtext.includes(".css")) {
-      const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
-      const [elmPath, elmName] = `${rowtext}`.match(/.*\/(.*\.css)/);
-      if (match) {
-        const [, cssPath, cssSrc] = match;
-        const imgNameRow = await searchFileByName(elmName, file_md5);
-        if (imgNameRow) {
-          return (
-            rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
-            "\n"
-          );
-        }
-      }
-    } else if (rowtext.includes(".ttf")) {
-      const match = rowtext.match(/.*\((.*\/?(.*ttf))\)./);
-      if (match) {
-        const [, cssPath, cssSrc] = match;
-        try {
-          const imgRow = await searchFileByPath(cssSrc, file_md5);
-          if (imgRow) {
-            return (
-              rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
-              "\n"
-            );
-          } else {
-            logger.warn(`Font file not found for path: ${cssSrc}`);
-          }
-        } catch (error) {
-            logger.error("Error searching for font file:", error);
-        }
-      }
-    }
-    return rowtext + "\n";
-  });
-
-  const results = await Promise.all(promises);
-  return results.join("");
+        
+        return rowtext + "\n";
+    });
+    
+    const results = await Promise.all(promises);
+    return results.join("");
 }
 
 export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
-  const needSetImage = epub.zip.names.filter(
-    (elm) => elm.endsWith(".html") || elm.endsWith(".css")
-  );
-
-  const needSetFont = epub.zip.names.filter((elm) => elm.endsWith(".ttf"));
-  const basePath = path.join("./base_files", file_md5, "Text");
-  const styleBasePath = path.join("./base_files", file_md5, "style");
-  dirExists(basePath);
-  dirExists(styleBasePath);
-
-  // 初始化进度条
-  const progressBar = new cliProgress.SingleBar(
-    {},
-    cliProgress.Presets.shades_classic
-  );
-  progressBar.start(needSetImage.length, 0);
-
-  await Promise.allSettled(
-    needSetImage.map(async (elm, index) => {
-      const filePath = path.join(zipEpubExtract, elm);
-      const elmData = fs.readFileSync(filePath);
-      const htmlStr = await processFiles(elmData, file_md5);
-      let file_path;
-      let source_id;
-      if (htmlStr) {
-        fs.writeFileSync(filePath, htmlStr);
-
-        const htmlMd5 = await calculateMD5(filePath);
-        const isCss = elm.endsWith(".css");
-        const newFilePath = path.join(
-          isCss ? styleBasePath : basePath,
-          `${htmlMd5}.${isCss ? "css" : "html"}`
-        );
-
-        for (const m_key in epub.manifest) {
-          const mElm = epub.manifest[m_key];
-          if (mElm.href.includes(elm) && !source_id) {
-            source_id = mElm.id;
-            file_path = mElm.href;
-            break;
-          }
-        }
-
-        const params = {
-          file_id: htmlMd5,
-          md5: htmlMd5,
-          mimetype: isCss ? "text/css" : "text/html",
-          size: Buffer.byteLength(htmlStr),
-          name: `${htmlMd5}.${isCss ? "css" : "html"}`,
-          path: file_path,
-          source_id: source_id,
-        };
-        await files_insert(params);
-        await Promise.allSettled([
-          files_insert_link_epub({
-            file_id: htmlMd5,
-            book_id: file_md5,
-            author_id,
-          }),
-          fs.promises.writeFile(newFilePath, htmlStr),
-          progressBar.update(index + 1)
-        ]);
-      }
-    })
-  );
-
-  // 停止进度条
-  progressBar.stop();
+    const needSetImage = epub.zip.names.filter(
+        (elm) => elm.endsWith(".html") || elm.endsWith(".css")
+    );
+    
+    const needSetFont = epub.zip.names.filter((elm) => elm.endsWith(".ttf"));
+    const basePath = path.join("./base_files", file_md5, "Text");
+    const styleBasePath = path.join("./base_files", file_md5, "style");
+    dirExists(basePath);
+    dirExists(styleBasePath);
+    
+    await Promise.all(
+        needSetImage.map(async (elm, elmIndex) => {
+            console.log('needSetImage', elmIndex)
+            const filePath = path.join(zipEpubExtract, elm);
+            const elmData = fs.readFileSync(filePath);
+            const htmlStr = await processFiles(elmData, file_md5);
+            let file_path;
+            let source_id;
+            
+            if (htmlStr) {
+                fs.writeFileSync(filePath, htmlStr);
+                
+                const htmlMd5 = await calculateMD5(filePath);
+                const isCss = elm.endsWith(".css");
+                const newFilePath = path.join(
+                    isCss ? styleBasePath : basePath,
+                    `${htmlMd5}.${isCss ? "css" : "html"}`
+                );
+                
+                Object.keys(epub.manifest).forEach(m_key => {
+                    const mElm = epub.manifest[m_key];
+                    if (mElm.href.indexOf(elm) > -1 && !source_id) {
+                        source_id = mElm.id;
+                        file_path = mElm.href
+                    }
+                })
+                
+                const params = {
+                    file_id: htmlMd5,
+                    md5: htmlMd5,
+                    mimetype: isCss ? "text/css" : "text/html",
+                    size: Buffer.byteLength(htmlStr),
+                    name: `${htmlMd5}.${isCss ? "css" : "html"}`,
+                    path: file_path,
+                    source_id: source_id,
+                };
+                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),
+                ]);
+            }
+        })
+    );
 }
 
 // saveMateInfo
 export async function saveMateInfo(epub, zipEpubExtract, file_md5, author_id) {
-  const params = {
-    book_name: epub.metadata.title,
-    book_id: file_md5,
-    book_md5: file_md5,
-    author_id: author_id,
-  };
-
-  await book_mate_insert(params);
+    // book_mate_insert
+    const params = {
+        book_name: epub.metadata.title,
+        book_id: file_md5,
+        book_md5: file_md5,
+        // language: "",
+        // date: "",
+        // creatorFileAs: "",
+        // UUID: "",
+        // ISBN: "",
+        author_id: author_id,
+        // category_id: "",
+        // Introduction: "",
+    };
+    
+    const res = await book_mate_insert(params);
 }