index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // 添加账本
  2. import {
  3. getFileBymd5,
  4. searchFileByPath,
  5. author_insert,
  6. get_author_info,
  7. clear_all_data,
  8. searchChapterForBookId,
  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} from "./toc_old.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('searchChapterForBookId')
  37. const chapter_all = await searchChapterForBookId({book_id, level: '1'});
  38. console.timeEnd('searchChapterForBookId')
  39. console.log(393939, chapter_all.length, chapter_all[0]);
  40. });
  41. router.get("/clear", async function (req, res) {
  42. await clear_all_data();
  43. // 使用 fs.rmSync 删除非空目录及其内容
  44. fs.rmSync("./base_files", {recursive: true, force: true});
  45. res.send("epub types");
  46. });
  47. // define the about route
  48. router.get("/about", function (req, res) {
  49. res.send("About types");
  50. });
  51. router.get("/html/:fileId", async function (req, res) {
  52. const fileId = req.params.fileId; // 获取 fileId 参数
  53. logger.info(`Found ${fileId}`);
  54. const fileRow = await getFileBymd5(fileId);
  55. if (!fileRow) {
  56. return res.status(404).send("文件查询失败");
  57. }
  58. const uploadPath =
  59. "./base_files/" + fileRow.book_id + "/Text/" + fileId + ".html";
  60. console.log(46, fileRow);
  61. const filePath = path.resolve(uploadPath);
  62. // 检查文件是否存在
  63. if (!fs.existsSync(filePath)) {
  64. return res.status(404).send("服务器中不存在该文件");
  65. }
  66. // 返回文件
  67. res.setHeader("Content-Type", fileRow.mimetype);
  68. res.sendFile(filePath);
  69. });
  70. router.get("/img/:fileId", async function (req, res) {
  71. const fileId = req.params.fileId; // 获取 fileId 参数
  72. logger.info(`Found ${fileId}`);
  73. const fileRow = await getFileBymd5(fileId);
  74. if (!fileRow) {
  75. return res.status(404).send("文件查询失败");
  76. }
  77. const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
  78. const filePath = path.resolve(uploadPath);
  79. // 检查文件是否存在
  80. if (!fs.existsSync(filePath)) {
  81. return res.status(404).send("服务器中不存在该文件");
  82. }
  83. // 返回文件
  84. res.setHeader("Content-Type", fileRow.mimetype);
  85. res.sendFile(filePath);
  86. });
  87. router.get("/css/:fileId", async function (req, res) {
  88. const fileId = req.params.fileId; // 获取 fileId 参数
  89. logger.info(`Found ${fileId}`);
  90. const fileRow = await getFileBymd5(fileId);
  91. if (!fileRow) {
  92. return res.status(404).send("文件查询失败");
  93. }
  94. const uploadPath =
  95. "./base_files/" + fileRow.book_id + "/style/" + fileId + ".css";
  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. // define the about route
  106. router.put("/", async function (req, res) {
  107. let sampleFile;
  108. let uploadPath;
  109. let epubData;
  110. let zipEpubExtract;
  111. let epubFilePath;
  112. let epub;
  113. if (!req.files || Object.keys(req.files).length === 0) {
  114. return res.status(400).send("No files were uploaded.");
  115. }
  116. sampleFile = req.files.file;
  117. let file_md5 = sampleFile.md5;
  118. uploadPath = `./base_files/${file_md5}/`;
  119. epubFilePath = uploadPath + sampleFile.md5 + ".epub";
  120. zipEpubExtract = uploadPath + "epub-extract/";
  121. dirExists(uploadPath);
  122. await waittime(200);
  123. const isFile = isFileSync(epubFilePath);
  124. // 移动上传文件至指定目录
  125. if (!isFile) {
  126. sampleFile.mv(epubFilePath, function (err) {
  127. console.log("移动上传文件至指定目录的反馈", err);
  128. });
  129. epubData = sampleFile.data;
  130. } else {
  131. epubData = fs.readFileSync(epubFilePath);
  132. file_md5 = await calculateMD5(epubFilePath);
  133. }
  134. /* 是否需要解压文件 */
  135. if (!(await isDir(zipEpubExtract))) {
  136. epub = await EPub.createAsync(epubData, null, "");
  137. dirExists(zipEpubExtract);
  138. epub.zip.admZip.extractAllTo(zipEpubExtract);
  139. } else {
  140. epub = await EPub.createAsync(epubData, null, "");
  141. }
  142. // 生成作者的数据
  143. let authorInfo = await get_author_info(epub.metadata.creator);
  144. let author_id = authorInfo.author_id;
  145. if (!authorInfo) {
  146. author_id = uuidv4();
  147. await author_insert({name: epub.metadata.creator, author_id: author_id});
  148. }
  149. /*
  150. 1、读取图片信息
  151. 2、替换文件中的图片内容
  152. 3、存储html数据
  153. 4、存储css数据
  154. 5、存储章节数据
  155. */
  156. res.send("书籍正在处理中,请稍后!");
  157. console.log("书籍正在处理中,请稍后!");
  158. await saveMateInfo(epub, uploadPath, file_md5, author_id);
  159. logger.info("书籍的基础数据处理完毕");
  160. console.log("书籍的基础数据处理完毕");
  161. await saveImgs(epub, uploadPath, file_md5, author_id);
  162. logger.info("书籍的图片数据处理完毕");
  163. console.log("书籍的图片数据处理完毕");
  164. await saveAllFount(epub, uploadPath, file_md5, author_id);
  165. logger.info("书籍的字体数据处理完毕");
  166. console.log("书籍的字体数据处理完毕");
  167. await saveAllCSS(epub, uploadPath, file_md5, author_id);
  168. logger.info("书籍的css数据处理完毕");
  169. console.log("书籍的css数据处理完毕");
  170. // 存储html数据
  171. await htmlParser(epub, zipEpubExtract, file_md5, author_id);
  172. logger.info("书籍的章节数据处理完毕");
  173. console.log("书籍的章节数据处理完毕");
  174. // 章节数据
  175. await saveToc(epub, zipEpubExtract, file_md5, author_id);
  176. logger.info("书籍的目录处理完毕");
  177. logger.info("书籍处理完毕");
  178. console.log("书籍的目录处理完毕");
  179. console.log("书籍处理完毕");
  180. });
  181. export default router;