Browse Source

一些优化

john 8 months ago
parent
commit
91e2868f25

+ 9 - 6
epub_node/db/files.js

@@ -1,34 +1,37 @@
 import connection from "./base.js";
 
-/* 
+/*
 `file_id` VARCHAR(100) NOT NULL,  -- file_id 长度为 100
   `md5` VARCHAR(32) NOT NULL,  -- MD5 长度为 32
   `mimetype` VARCHAR(255) NOT NULL,  -- mimetype 长度为 255
   `size` INT NOT NULL,
   `name` VARCHAR(255) DEFAULT NULL,
   `path` VARCHAR(255) DEFAULT NULL,
-  
+
   */
 export async function files_insert({
   file_id = "",
+  source_id = '',
   md5 = "",
   mimetype = "",
   size = "",
   name = "",
   path = "",
 }) {
-  console.log(20, file_id, md5, mimetype, size, name, path)
   return new Promise(async (resolve, reject) => {
     try {
       const sql = `
-          INSERT INTO files (file_id, md5, mimetype, size, name, path)
-          VALUES (?, ?, ?, ?, ?, ?)
+          INSERT INTO files (file_id, source_id, md5, mimetype, size, name, path)
+VALUES (?, ?, ?, ?, ?, ?, ?)
+ON DUPLICATE KEY UPDATE file_id = file_id;
         `;
-      const values = [file_id, md5, mimetype, size, name, path];
+      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);
     }
   });

+ 1 - 0
epub_node/db/update.sql

@@ -0,0 +1 @@
+ALTER TABLE epub_manage.files ADD source_id varchar(100) NULL;

+ 70 - 54
epub_node/router/epub/image.js

@@ -2,103 +2,119 @@ 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 saveImgs( epub ) {
-  // const res = await Promise.allSettled(imgs.map(elm => {
-  // }))
+export async function saveImgs(epub) {
+  dirExists("./base_files/");
   let imgs = epub.listImage();
-  console.log(imgs[0]);
-  console.log(1121, imgs.length);
-  await epub.getImageAsync(imgs[2].id).then(async function ( [data,mimeType] ) {
-    console.log(`\ngetImage: cover\n`);
-    console.log(data);
-    const img_md5 = await calculateMD5FromBuffer(data);
-    console.log(img_md5);
-    console.log(mimeType);
-    console.log(1616,formatSize(data.length));
-    const params = {
-      file_id: img_md5,
-      md5: img_md5,
-      mimetype: mimeType,
-      size: data.length,
-      name: imgs[0].id,
-      path: imgs[0].href,
-    }
+  if (imgs.length) {
+    const imgRes = await Promise.allSettled(
+      imgs.map((img) => {
+        return epub.getImageAsync(img.id);
+      })
+    );
+    // 过滤数据
 
-    const uploadPath = "./base_files/" + img_md5;
-    dirExists("./base_files/");
+    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);
 
-    // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
-    fs.writeFile(uploadPath,data,( err ) => {
-      if ( err ) {
-        console.error("Error writing JSON file:",err);
-      } else {
-        console.log("JSON data saved to output.json");
-      }
-    });
+    await Promise.allSettled(
+      imgs_fulfilled.map(async (elm) => {
+        const img_md5 = await calculateMD5FromBuffer(elm.img_data);
 
-    await files_insert(params)
-  });
+        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);
+      })
+    );
+  }
 }
 
-function calculateMD5( filePath ) {
+function calculateMD5(filePath) {
   const hash = crypto.createHash("md5");
   const stream = fs.createReadStream(filePath);
 
-  stream.on("data",( chunk ) => {
+  stream.on("data", (chunk) => {
     hash.update(chunk);
   });
 
-  stream.on("end",() => {
-    console.log("MD5 hash:",hash.digest("hex"));
+  stream.on("end", () => {
+    console.log("MD5 hash:", hash.digest("hex"));
   });
 }
 
-export async function calculateMD5FromStream( fileStream ) {
-  return new Promise(( resolve,reject ) => {
+export async function calculateMD5FromStream(fileStream) {
+  return new Promise((resolve, reject) => {
     const hash = crypto.createHash("md5");
 
     // 错误处理
-    fileStream.on("error",( err ) => {
-      console.error("Error reading file stream:",err);
+    fileStream.on("error", (err) => {
+      console.error("Error reading file stream:", err);
       reject("生成失败!"); // 使用 reject 传递错误信息
     });
 
     // 监听 'data' 事件,更新 MD5 哈希
-    fileStream.on("data",( chunk ) => {
+    fileStream.on("data", (chunk) => {
       hash.update(chunk);
     });
 
     // 监听 'end' 事件,文件读取完毕后输出 MD5 值
-    fileStream.on("end",() => {
+    fileStream.on("end", () => {
       const md5 = hash.digest("hex");
-      console.log("MD5 Hash:",md5);
       resolve(md5); // 正常计算完 MD5 后 resolve
     });
   });
 }
 
-export function calculateMD5FromBuffer( buffer ) {
-  return new Promise(( resolve,reject ) => {
+export function calculateMD5FromBuffer(buffer) {
+  return new Promise((resolve, reject) => {
     try {
       // 使用 crypto 计算 MD5
       const hash = crypto.createHash("md5");
       hash.update(buffer); // 直接更新 hash
 
       const md5 = hash.digest("hex"); // 获取最终的 MD5 值
-      console.log("MD5 Hash:",md5);
       resolve(md5); // 返回 MD5
-    } catch ( err ) {
-      console.error("Error calculating MD5:",err);
+    } catch (err) {
+      console.error("Error calculating MD5:", err);
       reject("生成失败!");
     }
   });
 }
 
-const formatSize = ( bytes ) => {
-  if ( bytes < 1024 ) return `${ bytes } 字节`;
-  else if ( bytes < 1024 * 1024 ) return `${ ( bytes / 1024 ).toFixed(2) } KB`;
-  else if ( bytes < 1024 * 1024 * 1024 )
-    return `${ ( bytes / ( 1024 * 1024 ) ).toFixed(2) } MB`;
-  else return `${ ( bytes / ( 1024 * 1024 * 1024 ) ).toFixed(2) } GB`;
+const formatSize = (bytes) => {
+  if (bytes < 1024) return `${bytes} 字节`;
+  else if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
+  else if (bytes < 1024 * 1024 * 1024)
+    return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
+  else return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
 };

+ 22 - 6
epub_node/router/epub/index.js

@@ -46,7 +46,6 @@ router.get("/img/:fileId", async function (req, res) {
   res.sendFile(filePath);
 });
 
-
 // define the about route
 router.put("/", async function (req, res) {
   let sampleFile;
@@ -57,12 +56,10 @@ router.put("/", async function (req, res) {
   }
 
   sampleFile = req.files.file;
-  console.log(4545, sampleFile);
 
   const file_md5 = sampleFile.md5;
 
   var epub = await EPub.createAsync(sampleFile.data, null, "");
-  console.log(4545, epub.metadata);
   // console.log('manifest__', epub.manifest);
 
   // Object.keys(epub.metadata).forEach((objKey) => {
@@ -76,13 +73,32 @@ router.put("/", async function (req, res) {
   //   console.log(data);
   //   console.log(mimeType)
   // });
-  /* 
+  res.send("About types");
+  /*
     1、读取图片信息
     2、替换文件中的图片内容
     3、存储html数据
    */
-  await saveImgs(epub);
-  res.send("About types");
+  // await saveImgs(epub);
+  // 存储html数据
+  // console.log("\nSPINE:\n");
+  // // console.log(epub.flow);
+  // epub.flow.forEach((elm) => {
+  //   if (elm.href.indexOf("html") < 0) {
+  //     console.log(88, elm);
+  //   }
+  // });
+
+  // console.log("\nTOC:\n");
+  // console.log(epub.toc);
+
+  // epub.toc.forEach((elm) => {
+  //   if (elm.href.indexOf("html") < 0) {
+  //     console.log(88, elm);
+  //   }
+  // });
+
+  console.log(101, epub);
 });
 
 export default router;

+ 11 - 6
epub_node/utils/logger.js

@@ -1,21 +1,26 @@
-import winston from 'winston'
+import winston from "winston";
 
 const logger = winston.createLogger({
-  level: 'info',
+  level: "info",
   format: winston.format.json(),
-  defaultMeta: { service: 'user-service' },
+  defaultMeta: { service: "user-service" },
   transports: [
     //
     // - Write all logs with importance level of `error` or higher to `error.log`
     //   (i.e., error, fatal, but not other levels)
     //
-    new winston.transports.File({ filename: 'base_files/winston/error.log', level: 'error' }),
+    new winston.transports.File({
+      filename: "base_files/winston/error.log",
+      level: "error",
+    }),
     //
     // - Write all logs with importance level of `info` or higher to `combined.log`
     //   (i.e., fatal, error, warn, and info, but not trace)
     //
-    new winston.transports.File({ filename: 'base_files/winston/combined.log' }),
+    new winston.transports.File({
+      filename: "base_files/winston/combined.log",
+    }),
   ],
 });
 
-export default logger
+export default logger;