index.js 5.0 KB

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