john 7 ماه پیش
والد
کامیت
875124e58f
8فایلهای تغییر یافته به همراه179 افزوده شده و 95 حذف شده
  1. BIN
      .DS_Store
  2. 39 0
      epub_node/db/author.js
  3. 45 10
      epub_node/db/files.js
  4. 2 1
      epub_node/db/index.js
  5. 12 9
      epub_node/router/epub/image.js
  6. 24 26
      epub_node/router/epub/index.js
  7. 51 43
      epub_node/router/epub/style.js
  8. 6 6
      epub_node/router/epub/txt.js

BIN
.DS_Store


+ 39 - 0
epub_node/db/author.js

@@ -0,0 +1,39 @@
+import connection from "./base.js";
+
+export async function author_insert({ name = "", author_id = "" }) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+          INSERT INTO author (name, author_id)
+VALUES (?, ?)
+ON DUPLICATE KEY UPDATE author_id = author_id;
+        `;
+      const values = [name, author_id];
+      // 直接接收 execute 返回的内容
+      const result = await connection.execute(sql, values);
+      return resolve(result);
+    } catch (err) {
+      return resolve(false);
+    }
+  });
+}
+
+export async function get_author_info(name = "") {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+          SELECT * FROM author WHERE name = ?
+        `;
+      const values = [name];
+      // 直接接收 execute 返回的内容
+      connection.execute(sql, values, (error, row) => {
+        if(error) {
+          return resolve('')
+        }
+        return resolve(row.length > 0 ? row[0] : '')
+      });
+    } catch (err) {
+      return resolve(false);
+    }
+  });
+}

+ 45 - 10
epub_node/db/files.js

@@ -22,16 +22,49 @@ export async function files_insert({
     try {
       const sql = `
           INSERT INTO files (file_id, source_id, md5, mimetype, size, name, path)
-VALUES (?, ?, ?, ?, ?, ?, ?)
-ON DUPLICATE KEY UPDATE file_id = file_id;
+          VALUES (?, ?, ?, ?, ?, ?, ?)
+          ON DUPLICATE KEY UPDATE file_id = file_id;
         `;
       const values = [file_id, source_id, md5, mimetype, size, name, path];
       // 直接接收 execute 返回的内容
       const result = await connection.execute(sql, values);
       return resolve(result);
     } catch (err) {
-      console.log(34, err);
+      return resolve(false);
+    }
+  });
+}
+
+export async function files_insert_link_epub({
+  file_id = "",
+  book_id = "",
+  author_id = "",
+}) {
+  console.log("files_insert_link_epub", {
+    file_id,
+    book_id,
+    author_id,
+  });
 
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+          INSERT INTO book_link_file (file_id, book_id, author_id)
+          VALUES (?, ?, ?);
+        `;
+
+      const values = [file_id, book_id, author_id];
+      // 直接接收 execute 返回的内容
+      connection.execute(sql, values, (err, row) => {
+        if (err) {
+          console.log(606060, err);
+          return resolve(false);
+        }
+        console.log(606060, row);
+
+        return resolve(row);
+      });
+    } catch (err) {
       return resolve(false);
     }
   });
@@ -41,13 +74,16 @@ ON DUPLICATE KEY UPDATE file_id = file_id;
 export function getFileBymd5(md5Str) {
   return new Promise((resolve, reject) => {
     connection.query(
-      `SELECT * FROM files WHERE md5 = ?`,
+      `SELECT files.*, book_link_file.book_id AS book_id
+       FROM files
+       JOIN book_link_file ON files.file_id = book_link_file.file_id
+       WHERE files.file_id = ?`,
       [md5Str],
       (err, rows) => {
         if (err) {
-          resolve(false); // 如果存在记录,则返回 true,否则返回 false
+          resolve(false); // 如果出现错误,返回 false
         } else {
-          resolve(rows[0]); // 如果存在记录,则返回 true,否则返回 false
+          resolve(rows.length > 0 ? rows[0] : false); // 如果存在记录,返回第一条记录,否则返回 false
         }
       }
     );
@@ -57,14 +93,13 @@ export function getFileBymd5(md5Str) {
 // 查询图片信息
 export function searchFileByPath(imgPath, file_md5) {
   return new Promise((resolve, reject) => {
-    console.log(606060, `SELECT * FROM files WHERE path like ? ${file_md5 ? 'and path like ?' : ''};`)
-    console.log(606060, [`%${imgPath}%`, `%${file_md5}%`])
     connection.query(
-      `SELECT * FROM files WHERE path like ? ${file_md5 ? 'and path like ?' : ''};`,
+      `SELECT * FROM files WHERE path like ? ${
+        file_md5 ? "and path like ?" : ""
+      };`,
       [`%${imgPath}%`, `%${file_md5}%`],
       (err, rows) => {
         if (err) {
-          console.log(62626262, err);
           resolve(false); // 如果存在记录,则返回 true,否则返回 false
         } else {
           resolve(rows.length > 0 ? rows[0] : false); // 如果存在记录,则返回 true,否则返回 false

+ 2 - 1
epub_node/db/index.js

@@ -1,2 +1,3 @@
 
-export * from './files.js'
+export * from './files.js'
+export * from './author.js'

+ 12 - 9
epub_node/router/epub/image.js

@@ -1,11 +1,11 @@
 import { dirExists } from "#utils";
-import { files_insert } from "#db";
+import { files_insert, files_insert_link_epub } from "#db";
 import crypto from "crypto";
 import fs from "node:fs";
 import logger from "#logger";
 
-export async function saveImgs(epub) {
-  dirExists("./base_files/");
+export async function saveImgs(epub, uploadPath, book_id, author_id) {
+  dirExists(uploadPath + "image/");
   let imgs = epub.listImage();
   if (imgs.length) {
     const imgRes = await Promise.allSettled(
@@ -35,7 +35,7 @@ export async function saveImgs(epub) {
       imgs_fulfilled.map(async (elm) => {
         const img_md5 = await calculateMD5FromBuffer(elm.img_data);
 
-        const uploadPath = "./base_files/" + img_md5;
+        const imgUploadPath = uploadPath + "image/" + img_md5;
         const params = {
           file_id: img_md5,
           md5: img_md5,
@@ -46,13 +46,16 @@ export async function saveImgs(epub) {
           source_id: elm.id,
         };
 
-        fs.writeFile(uploadPath, elm.img_data, (err) => {
+        console.log(49, imgUploadPath);
+
+        fs.writeFile(imgUploadPath, elm.img_data, (err) => {
           if (err) {
             logger.error("Error writing Img file:", err);
           } else {
             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);
       })
     );
@@ -71,11 +74,11 @@ export function calculateMD5(filePath) {
 
       stream.on("end", () => {
         resolve(hash.digest("hex"));
-      });  
-    } catch ( err ) {
-      resolve('');
+      });
+    } catch (err) {
+      resolve("");
     }
-  })
+  });
 }
 
 export async function calculateMD5FromStream(fileStream) {

+ 24 - 26
epub_node/router/epub/index.js

@@ -1,10 +1,16 @@
 // 添加账本
-import { getFileBymd5, searchFileByPath } from "#db";
+import {
+  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 { saveImgs, calculateMD5 } from "./image.js";
@@ -53,8 +59,7 @@ router.get("/img/:fileId", async function (req, res) {
   if (!fileRow) {
     return res.status(404).send("文件查询失败");
   }
-
-  const uploadPath = "./base_files/" + fileId;
+  const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
   const filePath = path.resolve(uploadPath);
   // 检查文件是否存在
   if (!fs.existsSync(filePath)) {
@@ -66,7 +71,6 @@ router.get("/img/:fileId", async function (req, res) {
   res.sendFile(filePath);
 });
 
-
 router.get("/css/:fileId", async function (req, res) {
   const fileId = req.params.fileId; // 获取 fileId 参数
   logger.info(`Found ${fileId}`);
@@ -77,7 +81,6 @@ router.get("/css/:fileId", async function (req, res) {
   }
 
   const filePath = path.resolve(fileRow.path);
-  console.log(79, filePath)
   // 检查文件是否存在
   if (!fs.existsSync(filePath)) {
     return res.status(404).send("服务器中不存在该文件");
@@ -105,7 +108,7 @@ router.put("/", async function (req, res) {
 
   let file_md5 = sampleFile.md5;
   uploadPath = `./base_files/${file_md5}/`;
-  epubFilePath = uploadPath + sampleFile.md5 + '.epub';
+  epubFilePath = uploadPath + sampleFile.md5 + ".epub";
   zipEpubExtract = uploadPath + "epub-extract/";
 
   dirExists(uploadPath);
@@ -122,7 +125,7 @@ router.put("/", async function (req, res) {
     epubData = sampleFile.data;
   } else {
     epubData = fs.readFileSync(epubFilePath);
-    file_md5 = await calculateMD5(epubFilePath)
+    file_md5 = await calculateMD5(epubFilePath);
   }
 
   /* 是否需要解压文件 */
@@ -134,34 +137,29 @@ router.put("/", async function (req, res) {
     epub = await EPub.createAsync(epubData, null, "");
   }
 
-  // console.log(epub);
-
-  // Object.keys(epub.metadata).forEach((objKey) => {
-  //   console.log(464646, objKey, epub.metadata[objKey]);
-  // });
-  // let imgs = epub.listImage();
-  // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
-
-  //   console.log(`\ngetImage: cover\n`);
-
-  //   console.log(data);
-  //   console.log(mimeType)
-  // });
-
   res.send("About types");
+  console.log(137, typeof epub.metadata.creator, epub.metadata.creator);
+  // 作者
+  // const author_id = await author_insert()
+
+  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数据
    */
-  await saveImgs(epub, uploadPath);
-  await saveAllCSS(epub, uploadPath)
+  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);
-  
-  
+  const test = await htmlParser(epub, zipEpubExtract, file_md5);
+
   // console.log("\nSPINE:\n");
   // // console.log(epub.flow);
   // epub.flow.forEach((elm) => {

+ 51 - 43
epub_node/router/epub/style.js

@@ -1,66 +1,74 @@
 import logger from "#logger";
-import { files_insert } from "#db";
+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 ) {
+export async function saveAllCSS(epub, uploadPath, book_id, author_id) {
   dirExists(uploadPath);
-  dirExists(`${ uploadPath }style/`);
+  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 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 ) => {
+      .map((img, index) => {
         const img_fulfilled = cssRes[index];
         const cssPath = getAllCss[index];
-        if ( img_fulfilled.status === "fulfilled" ) {
+        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',
+            ...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);
+      .filter((elm) => elm);
 
-    await Promise.allSettled(allCss_fulfilled.map(async ( elm ) => {
-      const md5 = await calculateMD5(elm.path)
-      const [elmPath,elmName] = `${ elm.path }`.match(/.*\/(.*\.css)/)
+    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/${ elmName }`,elm.css_data,( err ) => {
-        if ( err ) {
-          logger.error("Error writing Img file:",err);
-        } else {
-          logger.info("Img data saved to " + md5);
-        }
-      });
+        //
+        // 移动文件
+        // './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/${ elmName }`,
-        source_id: md5,
-      };
-      return await files_insert(params);
-    }));
+        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_link_epub({ file_id: md5, book_id, author_id });
+        return await files_insert(params);
+      })
+    );
   }
 }

+ 6 - 6
epub_node/router/epub/txt.js

@@ -15,11 +15,9 @@ async function processFiles(elmDate, file_md5) {
         rowtext.includes(".jpeg"))
     ) {
       const match = rowtext.match(/.*(..\/Images\/(.*(jpg|png|jpeg))).*/);
-      console.log(18, match)
       if (match) {
         const [imgText, imgPath, imageSrc] = match;
         const imgRow = await searchFileByPath(imageSrc);
-        console.log(18, imgRow)
         if (imgRow) {
           const text = rowtext.replace(
             imgPath,
@@ -32,7 +30,7 @@ async function processFiles(elmDate, file_md5) {
       } else {
         htmlStr += rowtext + "\n";
       }
-      return 
+      return;
     }
     if (rowtext.includes(".css")) {
       const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
@@ -61,14 +59,16 @@ async function processFiles(elmDate, file_md5) {
 export async function htmlParser(epub, zipEpubExtract, file_md5) {
   // 获取原始数据源
   const needSetImge = epub.zip.names.filter(
-    (elm) => elm.indexOf("html") > -1 || elm.indexOf("css") > -1
+    (elm) => elm.indexOf(".html") > -1 || elm.indexOf(".css") > -1
   );
   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);
-    // 修改源数据
-    fs.writeFileSync(zipEpubExtract + elm, htmlStr);
+    if(htmlStr) {
+      // 修改源数据
+      fs.writeFileSync(zipEpubExtract + elm, htmlStr);
+    }
   }
 }