index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // 添加账本
  2. import {
  3. getFileBymd5,
  4. searchFileByPath,
  5. author_insert,
  6. get_author_info,
  7. clear_all_data,
  8. getChaptersByBookId,
  9. } from "#db";
  10. import logger from "#logger";
  11. import path from "node:path";
  12. import fs from "node:fs";
  13. import express from "express";
  14. import {EPub} from "epub2";
  15. import {v4 as uuidv4} from "uuid";
  16. import {dirExists, isFileSync, isDir, waittime} from "#utils";
  17. import {saveImgs, calculateMD5} from "./image.js";
  18. import {htmlParser, saveMateInfo} from "./txt.js";
  19. import {saveAllCSS} from "./style.js";
  20. import {saveAllFount} from "./font.js";
  21. import {saveToc, fetchAndBuildTree, fetchNextFile} from "./toc.js";
  22. const router = express.Router();
  23. // middleware that is specific to this router
  24. router.use(function timeLog(req, res, next) {
  25. next();
  26. });
  27. // define the home page route
  28. router.get("/", async function (req, res) {
  29. res.send("epub types");
  30. });
  31. // 获取所有的章节数据
  32. router.get("/chapter_all/:book_id", async function (req, res) {
  33. const book_id = req.params.book_id; // 获取 fileId 参数
  34. // res.send("epub types" + book_id);
  35. // console.log(393939, '开始查询')
  36. console.time('getChaptersByBookId')
  37. const chapter_all = await fetchAndBuildTree(book_id);
  38. // const chapter_all = await getChaptersByBookId(book_id);;
  39. // console.log(393939, chapter_all.length, chapter_all);
  40. fs.writeFileSync(`./chapter_${book_id}.json`, JSON.stringify(chapter_all));
  41. res.json(chapter_all)
  42. console.timeEnd('getChaptersByBookId')
  43. });
  44. // 获取下一页的数据
  45. router.get("/next_chapter/:file_id", async function (req, res) {
  46. const file_id = req.params.file_id; // 获取 fileId 参数
  47. // res.send("epub types" + book_id);
  48. console.log(393939, '开始查询')
  49. console.time('getChaptersByBookId')
  50. await fetchNextFile(file_id);
  51. res.json({code: 200})
  52. // // const chapter_all = await getChaptersByBookId(book_id);;
  53. // console.log(393939, chapter_all.length, chapter_all);
  54. // fs.writeFileSync(`./chapter_${book_id}.json`, JSON.stringify(chapter_all));
  55. console.timeEnd('getChaptersByBookId')
  56. });
  57. router.get("/clear", async function (req, res) {
  58. await clear_all_data();
  59. // 使用 fs.rmSync 删除非空目录及其内容
  60. fs.rmSync("./base_files", {recursive: true, force: true});
  61. res.send("epub types");
  62. });
  63. // define the about route
  64. router.get("/about", function (req, res) {
  65. res.send("About types");
  66. });
  67. router.get("/html/:fileId", async function (req, res) {
  68. const fileId = req.params.fileId; // 获取 fileId 参数
  69. logger.info(`Found ${fileId}`);
  70. const fileRow = await getFileBymd5(fileId);
  71. if (!fileRow) {
  72. return res.status(404).send("文件查询失败");
  73. }
  74. const uploadPath =
  75. "./base_files/" + fileRow.book_id + "/Text/" + fileId + ".html";
  76. const filePath = path.resolve(uploadPath);
  77. // 检查文件是否存在
  78. if (!fs.existsSync(filePath)) {
  79. return res.status(404).send("服务器中不存在该文件");
  80. }
  81. const htmlStr = fs.readFileSync(filePath, "utf8");
  82. const newHtmlStr = htmlStr.replace(/href="[^#"]*#/g, `href="${fileId}#`);
  83. // 返回文件
  84. res.setHeader("Content-Type", fileRow.mimetype);
  85. // res.sendFile(filePath);
  86. res.send(newHtmlStr);
  87. });
  88. router.get("/img/:fileId", async function (req, res) {
  89. const fileId = req.params.fileId; // 获取 fileId 参数
  90. logger.info(`Found ${fileId}`);
  91. const fileRow = await getFileBymd5(fileId);
  92. if (!fileRow) {
  93. return res.status(404).send("文件查询失败");
  94. }
  95. const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
  96. const filePath = path.resolve(uploadPath);
  97. // 检查文件是否存在
  98. if (!fs.existsSync(filePath)) {
  99. return res.status(404).send("服务器中不存在该文件");
  100. }
  101. // 返回文件
  102. res.setHeader("Content-Type", fileRow.mimetype);
  103. res.sendFile(filePath);
  104. });
  105. router.get("/css/:fileId", async function (req, res) {
  106. const fileId = req.params.fileId; // 获取 fileId 参数
  107. logger.info(`Found ${fileId}`);
  108. const fileRow = await getFileBymd5(fileId);
  109. if (!fileRow) {
  110. return res.status(404).send("文件查询失败");
  111. }
  112. const uploadPath =
  113. "./base_files/" + fileRow.book_id + "/style/" + fileId + ".css";
  114. const filePath = path.resolve(uploadPath);
  115. // 检查文件是否存在
  116. if (!fs.existsSync(filePath)) {
  117. return res.status(404).send("服务器中不存在该文件");
  118. }
  119. // 返回文件
  120. res.setHeader("Content-Type", fileRow.mimetype);
  121. res.sendFile(filePath);
  122. });
  123. // define the about route
  124. router.put("/", async function (req, res) {
  125. let sampleFile;
  126. let uploadPath;
  127. let epubData;
  128. let zipEpubExtract;
  129. let epubFilePath;
  130. let epub;
  131. if (!req.files || Object.keys(req.files).length === 0) {
  132. return res.status(400).send("No files were uploaded.");
  133. }
  134. sampleFile = req.files.file;
  135. let file_md5 = sampleFile.md5;
  136. uploadPath = `./base_files/${file_md5}/`;
  137. epubFilePath = uploadPath + sampleFile.md5 + ".epub";
  138. zipEpubExtract = uploadPath + "epub-extract/";
  139. dirExists(uploadPath);
  140. await waittime(200);
  141. const isFile = isFileSync(epubFilePath);
  142. // 移动上传文件至指定目录
  143. if (!isFile) {
  144. sampleFile.mv(epubFilePath, function (err) {
  145. console.log("移动上传文件至指定目录的反馈", err);
  146. });
  147. epubData = sampleFile.data;
  148. } else {
  149. epubData = fs.readFileSync(epubFilePath);
  150. file_md5 = await calculateMD5(epubFilePath);
  151. }
  152. /* 是否需要解压文件 */
  153. if (!(await isDir(zipEpubExtract))) {
  154. epub = await EPub.createAsync(epubData, null, "");
  155. dirExists(zipEpubExtract);
  156. epub.zip.admZip.extractAllTo(zipEpubExtract);
  157. } else {
  158. epub = await EPub.createAsync(epubData, null, "");
  159. }
  160. // 生成作者的数据
  161. let authorInfo = await get_author_info(epub.metadata.creator);
  162. let author_id = authorInfo.author_id;
  163. if (!authorInfo) {
  164. author_id = uuidv4();
  165. await author_insert({name: epub.metadata.creator, author_id: author_id});
  166. }
  167. /*
  168. 1、读取图片信息
  169. 2、替换文件中的图片内容
  170. 3、存储html数据
  171. 4、存储css数据
  172. 5、存储章节数据
  173. */
  174. res.send("书籍正在处理中,请稍后!");
  175. console.log("书籍正在处理中,请稍后!");
  176. await saveMateInfo(epub, uploadPath, file_md5, author_id);
  177. logger.info("书籍的基础数据处理完毕");
  178. console.log("书籍的基础数据处理完毕");
  179. await saveImgs(epub, uploadPath, file_md5, author_id);
  180. logger.info("书籍的图片数据处理完毕");
  181. console.log("书籍的图片数据处理完毕");
  182. await saveAllFount(epub, uploadPath, file_md5, author_id);
  183. logger.info("书籍的字体数据处理完毕");
  184. console.log("书籍的字体数据处理完毕");
  185. await saveAllCSS(epub, uploadPath, file_md5, author_id);
  186. logger.info("书籍的css数据处理完毕");
  187. console.log("书籍的css数据处理完毕");
  188. // 存储html数据
  189. await htmlParser(epub, zipEpubExtract, file_md5, author_id);
  190. logger.info("书籍的章节数据处理完毕");
  191. console.log("书籍的章节数据处理完毕");
  192. // 章节数据
  193. await saveToc(epub, zipEpubExtract, file_md5, author_id);
  194. logger.info("书籍的目录处理完毕");
  195. logger.info("书籍处理完毕");
  196. console.log("书籍的目录处理完毕");
  197. console.log("书籍处理完毕");
  198. });
  199. export default router;