Pārlūkot izejas kodu

处理默认资源

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

+ 1 - 1
epub_node/db/DB.sql

@@ -69,7 +69,7 @@ CREATE TABLE `style_link_book` (
 -- epub_manage.chapter definition
 CREATE TABLE `chapter` (
     `id` INT NOT NULL AUTO_INCREMENT,
-    `name` VARCHAR(255) NOT NULL,  -- Chapter name with a maximum length of 255 characters
+    `name` text 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
     `author_id` VARCHAR(100) NOT NULL,  -- Author ID with a maximum length of 100 characters
     `content` LONGTEXT DEFAULT NULL,  -- Chapter content

+ 4 - 0
epub_node/db/chapter.js

@@ -23,6 +23,10 @@ export async function chapter_insert({
                                          old_path = "",
                                          path = "",
                                      }) {
+    // 假设最大长度为255,您可能需要根据实际数据库定义调整
+    const maxLength = 255;
+    
+    
     const sql = `
         INSERT INTO chapter (name, book_id, author_id, content, level, order_index, order_id, old_path, path)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);

+ 147 - 147
epub_node/router/epub/index.js

@@ -1,183 +1,183 @@
 // 添加账本
 import {
-  getFileBymd5,
-  searchFileByPath,
-  author_insert,
-  get_author_info,
+    getFileBymd5,
+    searchFileByPath,
+    author_insert,
+    get_author_info,
 } from "#db";
 import logger from "#logger";
 import path from "node:path";
 import fs from "node:fs";
 import express from "express";
-import { EPub } from "epub2";
-import { v4 as uuidv4 } from "uuid";
-import { dirExists, isFileSync, isDir, waittime } from "#utils";
+import {EPub} from "epub2";
+import {v4 as uuidv4} from "uuid";
+import {dirExists, isFileSync, isDir, waittime} from "#utils";
 
-import { saveImgs, calculateMD5 } from "./image.js";
-import { htmlParser, saveMateInfo } from "./txt.js";
-import { saveAllCSS } from "./style.js";
-import { saveAllFount } from "./font.js";
-import { saveToc } from "./toc.js";
+import {saveImgs, calculateMD5} from "./image.js";
+import {htmlParser, saveMateInfo} from "./txt.js";
+import {saveAllCSS} from "./style.js";
+import {saveAllFount} from "./font.js";
+import {saveToc} from "./toc.js";
 
 const router = express.Router();
 
 // middleware that is specific to this router
 router.use(function timeLog(req, res, next) {
-  next();
+    next();
 });
 // define the home page route
 router.get("/", async function (req, res) {
-  res.send("epub types");
+    res.send("epub types");
 });
 
 // define the about route
 router.get("/about", function (req, res) {
-  res.send("About types");
+    res.send("About types");
 });
 
 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);
+    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}`);
-
-  const fileRow = await getFileBymd5(fileId);
-
-  if (!fileRow) {
-    return res.status(404).send("文件查询失败");
-  }
-  const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
-  const filePath = path.resolve(uploadPath);
-
-  // 检查文件是否存在
-  if (!fs.existsSync(filePath)) {
-    return res.status(404).send("服务器中不存在该文件");
-  }
-
-  // 返回文件
-  res.setHeader("Content-Type", fileRow.mimetype);
-  res.sendFile(filePath);
+    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 + "/image/" + fileId;
+    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("/css/: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 + "/style/" + fileId + ".css";
-
-  const filePath = path.resolve(uploadPath);
-  // 检查文件是否存在
-  if (!fs.existsSync(filePath)) {
-    return res.status(404).send("服务器中不存在该文件");
-  }
-
-  // 返回文件
-  res.setHeader("Content-Type", fileRow.mimetype);
-  res.sendFile(filePath);
+    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 + "/style/" + fileId + ".css";
+    
+    const filePath = path.resolve(uploadPath);
+    // 检查文件是否存在
+    if (!fs.existsSync(filePath)) {
+        return res.status(404).send("服务器中不存在该文件");
+    }
+    
+    // 返回文件
+    res.setHeader("Content-Type", fileRow.mimetype);
+    res.sendFile(filePath);
 });
 
 // define the about route
 router.put("/", async function (req, res) {
-  let sampleFile;
-  let uploadPath;
-  let epubData;
-  let zipEpubExtract;
-  let epubFilePath;
-  let epub;
-
-  if (!req.files || Object.keys(req.files).length === 0) {
-    return res.status(400).send("No files were uploaded.");
-  }
-
-  sampleFile = req.files.file;
-
-  let file_md5 = sampleFile.md5;
-  uploadPath = `./base_files/${file_md5}/`;
-  epubFilePath = uploadPath + sampleFile.md5 + ".epub";
-  zipEpubExtract = uploadPath + "epub-extract/";
-
-  dirExists(uploadPath);
-
-  await waittime(200);
-
-  const isFile = isFileSync(epubFilePath);
-
-  // 移动上传文件至指定目录
-  if (!isFile) {
-    sampleFile.mv(epubFilePath, function (err) {
-      console.log("移动上传文件至指定目录的反馈", err);
-    });
-    epubData = sampleFile.data;
-  } else {
-    epubData = fs.readFileSync(epubFilePath);
-    file_md5 = await calculateMD5(epubFilePath);
-  }
-
-  /* 是否需要解压文件 */
-  if (!(await isDir(zipEpubExtract))) {
-    epub = await EPub.createAsync(epubData, null, "");
-    dirExists(zipEpubExtract);
-    epub.zip.admZip.extractAllTo(zipEpubExtract);
-  } else {
-    epub = await EPub.createAsync(epubData, null, "");
-  }
-  
-  // 生成作者的数据
-  let authorInfo = await get_author_info(epub.metadata.creator);
-  let author_id = authorInfo.author_id;
-  if (!authorInfo) {
-    author_id = uuidv4();
-    await author_insert({ name: epub.metadata.creator, author_id: author_id });
-  }
-  /*
-    1、读取图片信息
-    2、替换文件中的图片内容
-    3、存储html数据
-    4、存储css数据
-    5、存储章节数据
-   */
-  await saveMateInfo(epub, uploadPath, file_md5, author_id)
-  logger.info('书籍的基础数据处理完毕')
-  await saveImgs(epub, uploadPath, file_md5, author_id);
-  logger.info('书籍的图片数据处理完毕')
-  await saveAllFount(epub, uploadPath, file_md5, author_id);
-  logger.info('书籍的字体数据处理完毕')
-  await saveAllCSS(epub, uploadPath, file_md5, author_id);
-  logger.info('书籍的css数据处理完毕')
-  // 存储html数据
-  await htmlParser(epub, zipEpubExtract, file_md5, author_id);
-  logger.info('书籍的章节数据处理完毕')
-  // 章节数据
-  await saveToc(epub, zipEpubExtract, file_md5, author_id);
-  logger.info('书籍的目录处理完毕')
-  logger.info('书籍处理完毕')
-  res.send("书籍处理完毕");
+    let sampleFile;
+    let uploadPath;
+    let epubData;
+    let zipEpubExtract;
+    let epubFilePath;
+    let epub;
+    
+    if (!req.files || Object.keys(req.files).length === 0) {
+        return res.status(400).send("No files were uploaded.");
+    }
+    
+    sampleFile = req.files.file;
+    
+    let file_md5 = sampleFile.md5;
+    uploadPath = `./base_files/${file_md5}/`;
+    epubFilePath = uploadPath + sampleFile.md5 + ".epub";
+    zipEpubExtract = uploadPath + "epub-extract/";
+    
+    dirExists(uploadPath);
+    
+    await waittime(200);
+    
+    const isFile = isFileSync(epubFilePath);
+    
+    // 移动上传文件至指定目录
+    if (!isFile) {
+        sampleFile.mv(epubFilePath, function (err) {
+            console.log("移动上传文件至指定目录的反馈", err);
+        });
+        epubData = sampleFile.data;
+    } else {
+        epubData = fs.readFileSync(epubFilePath);
+        file_md5 = await calculateMD5(epubFilePath);
+    }
+    
+    /* 是否需要解压文件 */
+    if (!(await isDir(zipEpubExtract))) {
+        epub = await EPub.createAsync(epubData, null, "");
+        dirExists(zipEpubExtract);
+        epub.zip.admZip.extractAllTo(zipEpubExtract);
+    } else {
+        epub = await EPub.createAsync(epubData, null, "");
+    }
+    
+    // 生成作者的数据
+    let authorInfo = await get_author_info(epub.metadata.creator);
+    let author_id = authorInfo.author_id;
+    if (!authorInfo) {
+        author_id = uuidv4();
+        await author_insert({name: epub.metadata.creator, author_id: author_id});
+    }
+    /*
+      1、读取图片信息
+      2、替换文件中的图片内容
+      3、存储html数据
+      4、存储css数据
+      5、存储章节数据
+     */
+    /*await saveMateInfo(epub, uploadPath, file_md5, author_id)
+    logger.info('书籍的基础数据处理完毕')
+    await saveImgs(epub, uploadPath, file_md5, author_id);
+    logger.info('书籍的图片数据处理完毕')
+    await saveAllFount(epub, uploadPath, file_md5, author_id);
+    logger.info('书籍的字体数据处理完毕')
+    await saveAllCSS(epub, uploadPath, file_md5, author_id);
+    logger.info('书籍的css数据处理完毕')
+    // 存储html数据
+    await htmlParser(epub, zipEpubExtract, file_md5, author_id);
+    logger.info('书籍的章节数据处理完毕')*/
+    // 章节数据
+    await saveToc(epub, zipEpubExtract, file_md5, author_id);
+    logger.info('书籍的目录处理完毕')
+    logger.info('书籍处理完毕')
+    res.send("书籍处理完毕");
 });
 
 export default router;

+ 82 - 72
epub_node/router/epub/style.js

@@ -1,78 +1,88 @@
 import logger from "#logger";
-import { files_insert, files_insert_link_epub } from "#db";
-import { dirExists } from "#utils";
+import {files_insert, files_insert_link_epub} from "#db";
+import {dirExists} from "#utils";
 import fs from "node:fs";
-import { calculateMD5 } from "./image.js";
+import {calculateMD5} from "./image.js";
 
 // ./base_files/5ae2d9158081faab184484ed1783e176
 export async function saveAllCSS(epub, uploadPath, book_id, author_id) {
-  dirExists(uploadPath);
-  dirExists(`${uploadPath}style/`);
-
-  // 获取原始数据源
-  const getAllCss = epub.zip.names.filter((elm) => elm.indexOf("css") > -1);
-  const base_path = `${uploadPath}epub-extract/`;
-  if (getAllCss.length) {
-    const cssRes = await Promise.allSettled(
-      getAllCss.map((img) => {
-        return fs.readFileSync(base_path + img, "utf8");
-      })
-    );
-    const allCss_fulfilled = cssRes
-      .map((img, index) => {
-        const img_fulfilled = cssRes[index];
-        const cssPath = getAllCss[index];
-        if (img_fulfilled.status === "fulfilled") {
-          // const file_md5 = await calculateMD5(base_path + cssPath)
-          return {
-            ...img,
-            ...img_fulfilled,
-            index,
-            path: base_path + cssPath,
-            cssPath, // md5: file_md5,
-            css_data: img_fulfilled.value,
-            mimeType: "text/css",
-          };
-        }
-        return false;
-      })
-      .filter((elm) => elm);
-
-    await Promise.allSettled(
-      allCss_fulfilled.map(async (elm) => {
-        const md5 = await calculateMD5(elm.path);
-        const [elmPath, elmName] = `${elm.path}`.match(/.*\/(.*\.css)/);
-
-        //
-        // 移动文件
-        // './base_files/5ae2d9158081faab184484ed1783e176/epub-extract/OEBPS/flow0001.css'
-        // ./base_files/5ae2d9158081faab184484ed1783e176/style/flow0001.css
-        //
-
-        fs.writeFile(`${uploadPath}style/${md5}.css`, elm.css_data, (err) => {
-          if (err) {
-            logger.error("Error writing Img file:", err);
-          } else {
-            logger.info("Img data saved to " + md5);
-          }
-        });
-
-        const params = {
-          file_id: md5,
-          md5: md5,
-          mimetype: elm.mimeType,
-          size: elm.css_data.length,
-          name: elmName,
-          path: `${uploadPath}style/${md5}.css`,
-          source_id: md5,
-        };
-        await files_insert(params);
-        return await files_insert_link_epub({
-          file_id: md5,
-          book_id,
-          author_id,
-        });
-      })
-    );
-  }
+    dirExists(uploadPath);
+    dirExists(`${uploadPath}style/`);
+    
+    // 获取原始数据源
+    const getAllCss = epub.zip.names.filter((elm) => elm.indexOf("css") > -1);
+    const base_path = `${uploadPath}epub-extract/`;
+    if (getAllCss.length) {
+        const cssRes = await Promise.allSettled(
+            getAllCss.map((img) => {
+                return fs.readFileSync(base_path + img, "utf8");
+            })
+        );
+        const allCss_fulfilled = cssRes
+            .map((img, index) => {
+                const img_fulfilled = cssRes[index];
+                const cssPath = getAllCss[index];
+                if (img_fulfilled.status === "fulfilled") {
+                    // const file_md5 = await calculateMD5(base_path + cssPath)
+                    return {
+                        ...img,
+                        ...img_fulfilled,
+                        index,
+                        path: base_path + cssPath,
+                        cssPath, // md5: file_md5,
+                        css_data: img_fulfilled.value,
+                        mimeType: "text/css",
+                    };
+                }
+                return false;
+            })
+            .filter((elm) => elm);
+        
+        await Promise.allSettled(
+            allCss_fulfilled.map(async (elm) => {
+                const md5 = await calculateMD5(elm.path);
+                let file_path;
+                let source_id;
+                const [elmPath, elmName] = `${elm.path}`.match(/.*\/(.*\.css)/);
+                
+                //
+                // 移动文件
+                // './base_files/5ae2d9158081faab184484ed1783e176/epub-extract/OEBPS/flow0001.css'
+                // ./base_files/5ae2d9158081faab184484ed1783e176/style/flow0001.css
+                //
+                
+                fs.writeFile(`${uploadPath}style/${md5}.css`, elm.css_data, (err) => {
+                    if (err) {
+                        logger.error("Error writing Img file:", err);
+                    } else {
+                        logger.info("Img data saved to " + md5);
+                    }
+                });
+                Object.keys(epub.manifest).forEach(m_key => {
+                    const mElm = epub.manifest[m_key];
+                    if (mElm.href.indexOf(elmName) > -1 && !source_id) {
+                        source_id = mElm.id;
+                        file_path = mElm.href
+                    }
+                })
+                
+                const params = {
+                    file_id: md5,
+                    md5: md5,
+                    mimetype: elm.mimeType,
+                    size: elm.css_data.length,
+                    name: elmName,
+                    // path: `${uploadPath}style/${md5}.css`,
+                    path: file_path,
+                    source_id: source_id,
+                };
+                await files_insert(params);
+                return await files_insert_link_epub({
+                    file_id: md5,
+                    book_id,
+                    author_id,
+                });
+            })
+        );
+    }
 }

+ 26 - 21
epub_node/router/epub/toc.js

@@ -7,27 +7,32 @@ import {calculateMD5} from "./image.js";
 // ./base_files/5ae2d9158081faab184484ed1783e176
 export async function saveToc(epub, uploadPath, book_id, author_id) {
     await Promise.all(epub.toc.map(async (elm) => {
-        const match = `${elm.href}`.match(/\/(.*\.html).*/)
-        let chapterInfo = {
-            file_id: ''
+        try {
+            const match = `${elm.href}`.match(/\/(.*\.html).*/)
+            let chapterInfo = {
+                file_id: ''
+            }
+            let path = ''
+            if (match) {
+                path = match[1];
+                chapterInfo = await searchChapterInfoForPath(path, book_id)
+            }
+            logger.info(elm)
+            const params = {
+                name: elm.title,
+                book_id: book_id,
+                author_id: author_id,
+                content: JSON.stringify(elm),
+                level: elm.level,
+                order_index: elm.order,
+                order_id: chapterInfo.file_id,
+                old_path: elm.href,
+                path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
+            }
+            logger.info(params)
+            return await chapter_insert(params)
+        } catch (e) {
+            logger.error(e)
         }
-        let path = ''
-        if (match) {
-            path = match[1];
-            chapterInfo = await searchChapterInfoForPath(path, book_id)
-        }
-        
-        const params = {
-            name: elm.title,
-            book_id: book_id,
-            author_id: author_id,
-            content: JSON.stringify(elm),
-            level: elm.level,
-            order_index: elm.order,
-            order_id: chapterInfo.file_id,
-            old_path: elm.href,
-            path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
-        }
-        return await chapter_insert(params)
     }))
 }

+ 12 - 2
epub_node/router/epub/txt.js

@@ -96,6 +96,8 @@ export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
             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);
@@ -107,14 +109,22 @@ export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
                     `${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: newFilePath,
-                    source_id: elm,
+                    path: file_path,
+                    source_id: source_id,
                 };
                 await files_insert(params);
                 await Promise.all([