|
@@ -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) => {
|