index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 添加账本
  2. import { getFileBymd5 } 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. const router = express.Router();
  11. // middleware that is specific to this router
  12. router.use(function timeLog(req, res, next) {
  13. next();
  14. });
  15. // define the home page route
  16. router.get("/", async function (req, res) {
  17. res.send("epub types");
  18. });
  19. // define the about route
  20. router.get("/about", function (req, res) {
  21. res.send("About types");
  22. });
  23. router.get("/img/:fileId", async function (req, res) {
  24. const fileId = req.params.fileId; // 获取 fileId 参数
  25. logger.info(`Found ${fileId}`);
  26. const fileRow = await getFileBymd5(fileId);
  27. if (!fileRow) {
  28. return res.status(404).send("文件查询失败");
  29. }
  30. const uploadPath = "./base_files/" + fileId;
  31. console.log("uploadPath", uploadPath);
  32. const filePath = path.resolve(uploadPath);
  33. console.log("filePath", filePath);
  34. // 检查文件是否存在
  35. if (!fs.existsSync(filePath)) {
  36. return res.status(404).send("服务器中不存在该文件");
  37. }
  38. // 返回文件
  39. res.setHeader("Content-Type", fileRow.mimetype);
  40. res.sendFile(filePath);
  41. });
  42. // define the about route
  43. router.put("/", async function (req, res) {
  44. let sampleFile;
  45. let uploadPath;
  46. let epubData;
  47. let zipEpubExtract;
  48. let epubFilePath;
  49. let epub;
  50. if (!req.files || Object.keys(req.files).length === 0) {
  51. return res.status(400).send("No files were uploaded.");
  52. }
  53. sampleFile = req.files.file;
  54. const file_md5 = sampleFile.md5;
  55. uploadPath = `./base_files/${file_md5}/`;
  56. epubFilePath = uploadPath + sampleFile.name;
  57. zipEpubExtract = uploadPath + "epub-extract/";
  58. dirExists(uploadPath);
  59. await waittime(200);
  60. const isFile = isFileSync(epubFilePath);
  61. // 移动上传文件至指定目录
  62. if (!isFile) {
  63. sampleFile.mv(epubFilePath, function (err) {
  64. console.log("移动上传文件至指定目录的反馈", err);
  65. });
  66. epubData = sampleFile.data;
  67. } else {
  68. epubData = fs.readFileSync(epubFilePath);
  69. }
  70. /* 是否需要解压文件 */
  71. if (!(await isDir(zipEpubExtract))) {
  72. epub = await EPub.createAsync(epubData, null, "");
  73. dirExists(zipEpubExtract);
  74. epub.zip.admZip.extractAllTo(zipEpubExtract);
  75. } else {
  76. epub = await EPub.createAsync(epubData, null, "");
  77. }
  78. // Object.keys(epub.metadata).forEach((objKey) => {
  79. // console.log(464646, objKey, epub.metadata[objKey]);
  80. // });
  81. // let imgs = epub.listImage();
  82. // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
  83. // console.log(`\ngetImage: cover\n`);
  84. // console.log(data);
  85. // console.log(mimeType)
  86. // });
  87. res.send("About types");
  88. /*
  89. 1、读取图片信息
  90. 2、替换文件中的图片内容
  91. 3、存储html数据
  92. */
  93. // await saveImgs(epub);
  94. // 存储html数据
  95. // console.log("\nSPINE:\n");
  96. // // console.log(epub.flow);
  97. // epub.flow.forEach((elm) => {
  98. // if (elm.href.indexOf("html") < 0) {
  99. // console.log(88, elm);
  100. // }
  101. // });
  102. // console.log("\nTOC:\n");
  103. // console.log(epub.toc);
  104. // epub.toc.forEach((elm) => {
  105. // if (elm.href.indexOf("html") < 0) {
  106. // console.log(88, elm);
  107. // }
  108. // });
  109. // epub.zip.names.forEach((elm) => {
  110. // console.log(102, elm);
  111. // });
  112. // console.log(101, epub);
  113. });
  114. export default router;