index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 } 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. );
  34. // 发送文件
  35. res.sendFile(filePath, function (err) {
  36. if (err) {
  37. console.error("Error sending file:", err);
  38. res.status(500).send("File not found or other error occurred.");
  39. }
  40. });
  41. });
  42. router.get("/img/:fileId", async function (req, res) {
  43. const fileId = req.params.fileId; // 获取 fileId 参数
  44. logger.info(`Found ${fileId}`);
  45. const fileRow = await getFileBymd5(fileId);
  46. if (!fileRow) {
  47. return res.status(404).send("文件查询失败");
  48. }
  49. const uploadPath = "./base_files/" + fileId;
  50. const filePath = path.resolve(uploadPath);
  51. // 检查文件是否存在
  52. if (!fs.existsSync(filePath)) {
  53. return res.status(404).send("服务器中不存在该文件");
  54. }
  55. // 返回文件
  56. res.setHeader("Content-Type", fileRow.mimetype);
  57. res.sendFile(filePath);
  58. });
  59. // define the about route
  60. router.put("/", async function (req, res) {
  61. let sampleFile;
  62. let uploadPath;
  63. let epubData;
  64. let zipEpubExtract;
  65. let epubFilePath;
  66. let epub;
  67. if (!req.files || Object.keys(req.files).length === 0) {
  68. return res.status(400).send("No files were uploaded.");
  69. }
  70. sampleFile = req.files.file;
  71. const file_md5 = sampleFile.md5;
  72. uploadPath = `./base_files/${file_md5}/`;
  73. epubFilePath = uploadPath + sampleFile.name;
  74. zipEpubExtract = uploadPath + "epub-extract/";
  75. dirExists(uploadPath);
  76. await waittime(200);
  77. const isFile = isFileSync(epubFilePath);
  78. // 移动上传文件至指定目录
  79. if (!isFile) {
  80. sampleFile.mv(epubFilePath, function (err) {
  81. console.log("移动上传文件至指定目录的反馈", err);
  82. });
  83. epubData = sampleFile.data;
  84. } else {
  85. epubData = fs.readFileSync(epubFilePath);
  86. }
  87. /* 是否需要解压文件 */
  88. if (!(await isDir(zipEpubExtract))) {
  89. epub = await EPub.createAsync(epubData, null, "");
  90. dirExists(zipEpubExtract);
  91. epub.zip.admZip.extractAllTo(zipEpubExtract);
  92. } else {
  93. epub = await EPub.createAsync(epubData, null, "");
  94. }
  95. // console.log(epub);
  96. // Object.keys(epub.metadata).forEach((objKey) => {
  97. // console.log(464646, objKey, epub.metadata[objKey]);
  98. // });
  99. // let imgs = epub.listImage();
  100. // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
  101. // console.log(`\ngetImage: cover\n`);
  102. // console.log(data);
  103. // console.log(mimeType)
  104. // });
  105. res.send("About types");
  106. /*
  107. 1、读取图片信息
  108. 2、替换文件中的图片内容
  109. 3、存储html数据
  110. 4、存储css数据
  111. */
  112. // await saveImgs(epub, uploadPath);
  113. await saveAllCSS(epub, uploadPath)
  114. // 存储html数据
  115. // await htmlParser(epub, zipEpubExtract);
  116. // console.log("\nSPINE:\n");
  117. // // console.log(epub.flow);
  118. // epub.flow.forEach((elm) => {
  119. // if (elm.href.indexOf("html") < 0) {
  120. // console.log(88, elm);
  121. // }
  122. // });
  123. // console.log("\nTOC:\n");
  124. // console.log(epub.toc);
  125. // epub.toc.forEach((elm) => {
  126. // if (elm.href.indexOf("html") < 0) {
  127. // console.log(88, elm);
  128. // }
  129. // });
  130. });
  131. export default router;