index.js 7.7 KB

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