index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // 添加账本
  2. import { getFileBymd5, searchFileByPath } from "#db";
  3. import logger from "#logger";
  4. import path from "node:path";
  5. import fs from "node:fs";
  6. import express from "express";
  7. import { EPub } from "epub2";
  8. import { dirExists, isFileSync, isDir, waittime } from "#utils";
  9. import { saveImgs, calculateMD5 } from "./image.js";
  10. import { htmlParser } from "./txt.js";
  11. import { saveAllCSS } from "./style.js";
  12. const router = express.Router();
  13. // middleware that is specific to this router
  14. router.use(function timeLog(req, res, next) {
  15. next();
  16. });
  17. // define the home page route
  18. router.get("/", async function (req, res) {
  19. res.send("epub types");
  20. });
  21. // define the about route
  22. router.get("/about", function (req, res) {
  23. res.send("About types");
  24. });
  25. router.get("/html", function (req, res) {
  26. //
  27. // res.sendFile('base_files/2a0b8153f3ede4bd43abb3b0e38ee857/epub-extract/OEBPS/Text/:::::::::::::::::::::*:*::**::*:.html')
  28. // res.send("About types");
  29. // 构建文件的绝对路径
  30. const filePath = path.join(
  31. process.cwd(),
  32. // "./base_files/2a0b8153f3ede4bd43abb3b0e38ee857/epub-extract/OEBPS/Text/:::::::::::::::::::::*:*::**::*:.html"
  33. "./base_files/5ae2d9158081faab184484ed1783e176/epub-extract/OEBPS/text00040.html"
  34. );
  35. // 发送文件
  36. res.sendFile(filePath, function (err) {
  37. if (err) {
  38. console.error("Error sending file:", err);
  39. res.status(500).send("File not found or other error occurred.");
  40. }
  41. });
  42. });
  43. router.get("/img/:fileId", async function (req, res) {
  44. const fileId = req.params.fileId; // 获取 fileId 参数
  45. logger.info(`Found ${fileId}`);
  46. const fileRow = await getFileBymd5(fileId);
  47. if (!fileRow) {
  48. return res.status(404).send("文件查询失败");
  49. }
  50. const uploadPath = "./base_files/" + fileId;
  51. const filePath = path.resolve(uploadPath);
  52. // 检查文件是否存在
  53. if (!fs.existsSync(filePath)) {
  54. return res.status(404).send("服务器中不存在该文件");
  55. }
  56. // 返回文件
  57. res.setHeader("Content-Type", fileRow.mimetype);
  58. res.sendFile(filePath);
  59. });
  60. router.get("/css/:fileId", async function (req, res) {
  61. const fileId = req.params.fileId; // 获取 fileId 参数
  62. logger.info(`Found ${fileId}`);
  63. const fileRow = await getFileBymd5(fileId);
  64. if (!fileRow) {
  65. return res.status(404).send("文件查询失败");
  66. }
  67. const filePath = path.resolve(fileRow.path);
  68. console.log(79, filePath)
  69. // 检查文件是否存在
  70. if (!fs.existsSync(filePath)) {
  71. return res.status(404).send("服务器中不存在该文件");
  72. }
  73. // 返回文件
  74. res.setHeader("Content-Type", fileRow.mimetype);
  75. res.sendFile(filePath);
  76. });
  77. // define the about route
  78. router.put("/", async function (req, res) {
  79. let sampleFile;
  80. let uploadPath;
  81. let epubData;
  82. let zipEpubExtract;
  83. let epubFilePath;
  84. let epub;
  85. if (!req.files || Object.keys(req.files).length === 0) {
  86. return res.status(400).send("No files were uploaded.");
  87. }
  88. sampleFile = req.files.file;
  89. let file_md5 = sampleFile.md5;
  90. uploadPath = `./base_files/${file_md5}/`;
  91. epubFilePath = uploadPath + sampleFile.md5 + '.epub';
  92. zipEpubExtract = uploadPath + "epub-extract/";
  93. dirExists(uploadPath);
  94. await waittime(200);
  95. const isFile = isFileSync(epubFilePath);
  96. // 移动上传文件至指定目录
  97. if (!isFile) {
  98. sampleFile.mv(epubFilePath, function (err) {
  99. console.log("移动上传文件至指定目录的反馈", err);
  100. });
  101. epubData = sampleFile.data;
  102. } else {
  103. epubData = fs.readFileSync(epubFilePath);
  104. file_md5 = await calculateMD5(epubFilePath)
  105. }
  106. /* 是否需要解压文件 */
  107. if (!(await isDir(zipEpubExtract))) {
  108. epub = await EPub.createAsync(epubData, null, "");
  109. dirExists(zipEpubExtract);
  110. epub.zip.admZip.extractAllTo(zipEpubExtract);
  111. } else {
  112. epub = await EPub.createAsync(epubData, null, "");
  113. }
  114. // console.log(epub);
  115. // Object.keys(epub.metadata).forEach((objKey) => {
  116. // console.log(464646, objKey, epub.metadata[objKey]);
  117. // });
  118. // let imgs = epub.listImage();
  119. // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
  120. // console.log(`\ngetImage: cover\n`);
  121. // console.log(data);
  122. // console.log(mimeType)
  123. // });
  124. res.send("About types");
  125. /*
  126. 1、读取图片信息
  127. 2、替换文件中的图片内容
  128. 3、存储html数据
  129. 4、存储css数据
  130. */
  131. await saveImgs(epub, uploadPath);
  132. await saveAllCSS(epub, uploadPath)
  133. // 存储html数据
  134. const test = await htmlParser(epub, zipEpubExtract, file_md5);
  135. // console.log("\nSPINE:\n");
  136. // // console.log(epub.flow);
  137. // epub.flow.forEach((elm) => {
  138. // if (elm.href.indexOf("html") < 0) {
  139. // console.log(88, elm);
  140. // }
  141. // });
  142. // console.log("\nTOC:\n");
  143. // console.log(epub.toc);
  144. // epub.toc.forEach((elm) => {
  145. // if (elm.href.indexOf("html") < 0) {
  146. // console.log(88, elm);
  147. // }
  148. // });
  149. });
  150. export default router;