Jelajahi Sumber

在线解压epub

john 8 bulan lalu
induk
melakukan
09680b4549

+ 1 - 1
.vscode/launch.json

@@ -8,7 +8,7 @@
       "type": "node",
       "request": "launch",
       "name": "启动程序",
-      "program": "${workspaceFolder}/node_expores/app.js"
+      "program": "${workspaceFolder}/epub_node/app.js"
     }
   ]
 }

+ 1 - 1
epub_node/app.js

@@ -55,7 +55,6 @@ app.use(async function timeLog(req, res, next) {
   }
   next();
 });
-logger.error("error error error error error error error ");
 
 // 静态文件目录,添加前缀路径 /static
 // app.use("/static", express.static("public"));
@@ -77,5 +76,6 @@ app.use("/api/v1/epub", epub);
 // app.use("/api/v1/types", types);
 
 app.listen(port, () => {
+  console.log(`Example app listening on port ${port}`)
   logger.info(`Example app listening on port ${port}`)
 });

+ 7 - 7
epub_node/environment/index.js

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

+ 37 - 3
epub_node/router/epub/index.js

@@ -5,6 +5,7 @@ import path from "node:path";
 import fs from "node:fs";
 import express from "express";
 import { EPub } from "epub2";
+import { dirExists, isFileSync, isDir, waittime } from "#utils";
 
 import { saveImgs } from "./image.js";
 
@@ -50,6 +51,10 @@ router.get("/img/:fileId", async function (req, res) {
 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.");
@@ -58,9 +63,34 @@ router.put("/", async function (req, res) {
   sampleFile = req.files.file;
 
   const file_md5 = sampleFile.md5;
+  uploadPath = `./base_files/${file_md5}/`;
+  epubFilePath = uploadPath + sampleFile.name;
+  zipEpubExtract = uploadPath + "epub-extract/";
 
-  var epub = await EPub.createAsync(sampleFile.data, null, "");
-  // console.log('manifest__', epub.manifest);
+  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);
+  }
+
+  /* 是否需要解压文件 */
+  if (!(await isDir(zipEpubExtract))) {
+    epub = await EPub.createAsync(epubData, null, "");
+    dirExists(zipEpubExtract);
+    epub.zip.admZip.extractAllTo(zipEpubExtract);
+  } else {
+    epub = await EPub.createAsync(epubData, null, "");
+  }
 
   // Object.keys(epub.metadata).forEach((objKey) => {
   //   console.log(464646, objKey, epub.metadata[objKey]);
@@ -98,7 +128,11 @@ router.put("/", async function (req, res) {
   //   }
   // });
 
-  console.log(101, epub);
+  // epub.zip.names.forEach((elm) => {
+  //   console.log(102, elm);
+  // });
+
+  // console.log(101, epub);
 });
 
 export default router;

+ 16 - 8
epub_node/utils/dayTime.js

@@ -1,22 +1,30 @@
 import dayjs from "dayjs";
-import utc from 'dayjs/plugin/utc.js'; // 导入 UTC 插件
-import timezone from 'dayjs/plugin/timezone.js'; // 导入时区插件
+import utc from "dayjs/plugin/utc.js"; // 导入 UTC 插件
+import timezone from "dayjs/plugin/timezone.js"; // 导入时区插件
 dayjs.extend(utc); // 加载 UTC 插件
 dayjs.extend(timezone); // 加载时区插件
 
-export function shanghaiTime () {
+export function shanghaiTime() {
   // 获取当前时间
   const now = dayjs();
 
   // 设置为上海时区
-  const shanghaiTime = now.tz('Asia/Shanghai');
-  return shanghaiTime
+  const shanghaiTime = now.tz("Asia/Shanghai");
+  return shanghaiTime;
 }
 
-export function shanghaiTimeFormat (time, formatStr = 'YYYY-MM-DD HH:mm:ss') {
+export function shanghaiTimeFormat(time, formatStr = "YYYY-MM-DD HH:mm:ss") {
   // 获取当前时间
   const now = dayjs(time);
   // 设置为上海时区
-  const shanghaiTime = now.tz('Asia/Shanghai');
-  return shanghaiTime.format(formatStr)
+  const shanghaiTime = now.tz("Asia/Shanghai");
+  return shanghaiTime.format(formatStr);
+}
+
+export function waittime(time = 100) {
+  return new Promise((resolve) => {
+    setTimeout(() => {
+      resolve(0);
+    }, time);
+  });
 }

+ 33 - 11
epub_node/utils/files.js

@@ -12,30 +12,30 @@ export function getStat(path) {
       } else {
         resolve(stats);
       }
-    })
-  })
+    });
+  });
 }
 
 /**
-  * 创建路径
-  * @param {string} dir 路径
-  */
+ * 创建路径
+ * @param {string} dir 路径
+ */
 export function mkdir(dir) {
   return new Promise((resolve, reject) => {
-    fs.mkdir(dir, err => {
+    fs.mkdir(dir, (err) => {
       if (err) {
         resolve(false);
       } else {
         resolve(true);
       }
-    })
-  })
+    });
+  });
 }
 
 /**
-  * 路径是否存在,不存在则创建
-  * @param {string} dir 路径
-  */
+ * 路径是否存在,不存在则创建
+ * @param {string} dir 路径
+ */
 export async function dirExists(dir) {
   let isExists = await getStat(dir);
   //如果该路径且不是文件,返回true
@@ -55,3 +55,25 @@ export async function dirExists(dir) {
   }
   return mkdirStatus;
 }
+/**
+ * 路径是否存在
+ * @param {string} dir 路径
+ */
+export async function isDir(dir) {
+  let isExists = await getStat(dir);
+  //如果该路径且不是文件,返回true
+  if (isExists && isExists.isDirectory()) {
+    return true;
+  }
+  return false;
+}
+
+export function isFileSync(filePath) {
+  try {
+    const fullPath = path.resolve(filePath);
+    const stats = fs.statSync(fullPath);
+    return stats.isFile();
+  } catch (error) {
+    return false;
+  }
+}