index.js 4.9 KB

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