123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- // 添加账本
- import {
- getFileBymd5,
- searchFileByPath,
- author_insert,
- get_author_info,
- clear_all_data,
- getChaptersByBookId,
- } 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";
- import {htmlParser, saveMateInfo} from "./txt.js";
- import {saveAllCSS} from "./style.js";
- import {saveAllFount} from "./font.js";
- import {saveToc, fetchAndBuildTree, fetchNextFile} from "./toc.js";
- const router = express.Router();
- // middleware that is specific to this router
- router.use(function timeLog(req, res, next) {
- next();
- });
- // define the home page route
- router.get("/", async function (req, res) {
- res.send("epub types");
- });
- // 获取所有的章节数据
- router.get("/chapter_all/:book_id", async function (req, res) {
- const book_id = req.params.book_id; // 获取 fileId 参数
- // res.send("epub types" + book_id);
- // console.log(393939, '开始查询')
- console.time('getChaptersByBookId')
- const chapter_all = await fetchAndBuildTree(book_id);
- // const chapter_all = await getChaptersByBookId(book_id);;
- // console.log(393939, chapter_all.length, chapter_all);
- fs.writeFileSync(`./chapter_${book_id}.json`, JSON.stringify(chapter_all));
- res.json(chapter_all)
- console.timeEnd('getChaptersByBookId')
- });
- // 获取下一页的数据
- router.get("/next_chapter/:file_id", async function (req, res) {
- const file_id = req.params.file_id; // 获取 fileId 参数
- // res.send("epub types" + book_id);
- console.log(393939, '开始查询')
- console.time('getChaptersByBookId')
- await fetchNextFile(file_id);
- res.json({code: 200})
- // // const chapter_all = await getChaptersByBookId(book_id);;
- // console.log(393939, chapter_all.length, chapter_all);
- // fs.writeFileSync(`./chapter_${book_id}.json`, JSON.stringify(chapter_all));
-
- console.timeEnd('getChaptersByBookId')
- });
- router.get("/clear", async function (req, res) {
- await clear_all_data();
- // 使用 fs.rmSync 删除非空目录及其内容
- fs.rmSync("./base_files", {recursive: true, force: true});
- res.send("epub types");
- });
- // define the about route
- router.get("/about", function (req, res) {
- 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";
-
- const filePath = path.resolve(uploadPath);
- // 检查文件是否存在
- if (!fs.existsSync(filePath)) {
- return res.status(404).send("服务器中不存在该文件");
- }
- const htmlStr = fs.readFileSync(filePath, "utf8");
- const newHtmlStr = htmlStr.replace(/href="[^#"]*#/g, `href="${fileId}#`);
-
- // 返回文件
- res.setHeader("Content-Type", fileRow.mimetype);
- // res.sendFile(filePath);
- res.send(newHtmlStr);
- });
- 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);
- });
- 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);
- });
- // 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、存储章节数据
- */
- res.send("书籍正在处理中,请稍后!");
- console.log("书籍正在处理中,请稍后!");
- await saveMateInfo(epub, uploadPath, file_md5, author_id);
- logger.info("书籍的基础数据处理完毕");
- console.log("书籍的基础数据处理完毕");
- await saveImgs(epub, uploadPath, file_md5, author_id);
- logger.info("书籍的图片数据处理完毕");
- console.log("书籍的图片数据处理完毕");
- await saveAllFount(epub, uploadPath, file_md5, author_id);
- logger.info("书籍的字体数据处理完毕");
- console.log("书籍的字体数据处理完毕");
- await saveAllCSS(epub, uploadPath, file_md5, author_id);
- logger.info("书籍的css数据处理完毕");
- console.log("书籍的css数据处理完毕");
- // 存储html数据
- await htmlParser(epub, zipEpubExtract, file_md5, author_id);
- logger.info("书籍的章节数据处理完毕");
- console.log("书籍的章节数据处理完毕");
- // 章节数据
- await saveToc(epub, zipEpubExtract, file_md5, author_id);
- logger.info("书籍的目录处理完毕");
- logger.info("书籍处理完毕");
- console.log("书籍的目录处理完毕");
- console.log("书籍处理完毕");
- });
- export default router;
|