index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // 添加账本
  2. import {
  3. getFileBymd5,
  4. searchFileByPath,
  5. author_insert,
  6. get_author_info,
  7. } from "#db";
  8. import logger from "#logger";
  9. import path from "node:path";
  10. import fs from "node:fs";
  11. import express from "express";
  12. import {EPub} from "epub2";
  13. import {v4 as uuidv4} from "uuid";
  14. import {dirExists, isFileSync, isDir, waittime} from "#utils";
  15. import {saveImgs, calculateMD5} from "./image.js";
  16. import {htmlParser, saveMateInfo} from "./txt.js";
  17. import {saveAllCSS} from "./style.js";
  18. import {saveAllFount} from "./font.js";
  19. import {saveToc} from "./toc.js";
  20. const router = express.Router();
  21. // middleware that is specific to this router
  22. router.use(function timeLog(req, res, next) {
  23. next();
  24. });
  25. // define the home page route
  26. router.get("/", async function (req, res) {
  27. res.send("epub types");
  28. });
  29. // define the about route
  30. router.get("/about", function (req, res) {
  31. res.send("About types");
  32. });
  33. router.get("/html/:fileId", async function (req, res) {
  34. const fileId = req.params.fileId; // 获取 fileId 参数
  35. logger.info(`Found ${fileId}`);
  36. const fileRow = await getFileBymd5(fileId);
  37. if (!fileRow) {
  38. return res.status(404).send("文件查询失败");
  39. }
  40. const uploadPath =
  41. "./base_files/" + fileRow.book_id + "/Text/" + fileId + ".html";
  42. console.log(46, fileRow);
  43. const filePath = path.resolve(uploadPath);
  44. // 检查文件是否存在
  45. if (!fs.existsSync(filePath)) {
  46. return res.status(404).send("服务器中不存在该文件");
  47. }
  48. // 返回文件
  49. res.setHeader("Content-Type", fileRow.mimetype);
  50. res.sendFile(filePath);
  51. });
  52. router.get("/img/:fileId", async function (req, res) {
  53. const fileId = req.params.fileId; // 获取 fileId 参数
  54. logger.info(`Found ${fileId}`);
  55. const fileRow = await getFileBymd5(fileId);
  56. if (!fileRow) {
  57. return res.status(404).send("文件查询失败");
  58. }
  59. const uploadPath = "./base_files/" + fileRow.book_id + "/image/" + fileId;
  60. const filePath = path.resolve(uploadPath);
  61. // 检查文件是否存在
  62. if (!fs.existsSync(filePath)) {
  63. return res.status(404).send("服务器中不存在该文件");
  64. }
  65. // 返回文件
  66. res.setHeader("Content-Type", fileRow.mimetype);
  67. res.sendFile(filePath);
  68. });
  69. router.get("/css/:fileId", async function (req, res) {
  70. const fileId = req.params.fileId; // 获取 fileId 参数
  71. logger.info(`Found ${fileId}`);
  72. const fileRow = await getFileBymd5(fileId);
  73. if (!fileRow) {
  74. return res.status(404).send("文件查询失败");
  75. }
  76. const uploadPath =
  77. "./base_files/" + fileRow.book_id + "/style/" + fileId + ".css";
  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. // define the about route
  88. router.put("/", async function (req, res) {
  89. let sampleFile;
  90. let uploadPath;
  91. let epubData;
  92. let zipEpubExtract;
  93. let epubFilePath;
  94. let epub;
  95. if (!req.files || Object.keys(req.files).length === 0) {
  96. return res.status(400).send("No files were uploaded.");
  97. }
  98. sampleFile = req.files.file;
  99. let file_md5 = sampleFile.md5;
  100. uploadPath = `./base_files/${file_md5}/`;
  101. epubFilePath = uploadPath + sampleFile.md5 + ".epub";
  102. zipEpubExtract = uploadPath + "epub-extract/";
  103. dirExists(uploadPath);
  104. await waittime(200);
  105. const isFile = isFileSync(epubFilePath);
  106. // 移动上传文件至指定目录
  107. if (!isFile) {
  108. sampleFile.mv(epubFilePath, function (err) {
  109. console.log("移动上传文件至指定目录的反馈", err);
  110. });
  111. epubData = sampleFile.data;
  112. } else {
  113. epubData = fs.readFileSync(epubFilePath);
  114. file_md5 = await calculateMD5(epubFilePath);
  115. }
  116. /* 是否需要解压文件 */
  117. if (!(await isDir(zipEpubExtract))) {
  118. epub = await EPub.createAsync(epubData, null, "");
  119. dirExists(zipEpubExtract);
  120. epub.zip.admZip.extractAllTo(zipEpubExtract);
  121. } else {
  122. epub = await EPub.createAsync(epubData, null, "");
  123. }
  124. // 生成作者的数据
  125. let authorInfo = await get_author_info(epub.metadata.creator);
  126. let author_id = authorInfo.author_id;
  127. if (!authorInfo) {
  128. author_id = uuidv4();
  129. await author_insert({name: epub.metadata.creator, author_id: author_id});
  130. }
  131. /*
  132. 1、读取图片信息
  133. 2、替换文件中的图片内容
  134. 3、存储html数据
  135. 4、存储css数据
  136. 5、存储章节数据
  137. */
  138. /*await saveMateInfo(epub, uploadPath, file_md5, author_id)
  139. logger.info('书籍的基础数据处理完毕')
  140. await saveImgs(epub, uploadPath, file_md5, author_id);
  141. logger.info('书籍的图片数据处理完毕')
  142. await saveAllFount(epub, uploadPath, file_md5, author_id);
  143. logger.info('书籍的字体数据处理完毕')
  144. await saveAllCSS(epub, uploadPath, file_md5, author_id);
  145. logger.info('书籍的css数据处理完毕')
  146. // 存储html数据
  147. await htmlParser(epub, zipEpubExtract, file_md5, author_id);
  148. logger.info('书籍的章节数据处理完毕')*/
  149. // 章节数据
  150. await saveToc(epub, zipEpubExtract, file_md5, author_id);
  151. logger.info('书籍的目录处理完毕')
  152. logger.info('书籍处理完毕')
  153. res.send("书籍处理完毕");
  154. });
  155. export default router;