index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { saveImgs } from "./image.js";
  9. const router = express.Router();
  10. // middleware that is specific to this router
  11. router.use(function timeLog(req, res, next) {
  12. next();
  13. });
  14. // define the home page route
  15. router.get("/", async function (req, res) {
  16. res.send("epub types");
  17. });
  18. // define the about route
  19. router.get("/about", function (req, res) {
  20. res.send("About types");
  21. });
  22. router.get("/img/:fileId", async function (req, res) {
  23. const fileId = req.params.fileId; // 获取 fileId 参数
  24. logger.info(`Found ${fileId}`);
  25. const fileRow = await getFileBymd5(fileId);
  26. if (!fileRow) {
  27. return res.status(404).send("文件查询失败");
  28. }
  29. const uploadPath = "./base_files/" + fileId;
  30. console.log("uploadPath", uploadPath);
  31. const filePath = path.resolve(uploadPath);
  32. console.log("filePath", filePath);
  33. // 检查文件是否存在
  34. if (!fs.existsSync(filePath)) {
  35. return res.status(404).send("服务器中不存在该文件");
  36. }
  37. // 返回文件
  38. res.setHeader("Content-Type", fileRow.mimetype);
  39. res.sendFile(filePath);
  40. });
  41. // define the about route
  42. router.put("/", async function (req, res) {
  43. let sampleFile;
  44. let uploadPath;
  45. if (!req.files || Object.keys(req.files).length === 0) {
  46. return res.status(400).send("No files were uploaded.");
  47. }
  48. sampleFile = req.files.file;
  49. const file_md5 = sampleFile.md5;
  50. var epub = await EPub.createAsync(sampleFile.data, null, "");
  51. // console.log('manifest__', epub.manifest);
  52. // Object.keys(epub.metadata).forEach((objKey) => {
  53. // console.log(464646, objKey, epub.metadata[objKey]);
  54. // });
  55. // let imgs = epub.listImage();
  56. // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
  57. // console.log(`\ngetImage: cover\n`);
  58. // console.log(data);
  59. // console.log(mimeType)
  60. // });
  61. res.send("About types");
  62. /*
  63. 1、读取图片信息
  64. 2、替换文件中的图片内容
  65. 3、存储html数据
  66. */
  67. // await saveImgs(epub);
  68. // 存储html数据
  69. // console.log("\nSPINE:\n");
  70. // // console.log(epub.flow);
  71. // epub.flow.forEach((elm) => {
  72. // if (elm.href.indexOf("html") < 0) {
  73. // console.log(88, elm);
  74. // }
  75. // });
  76. // console.log("\nTOC:\n");
  77. // console.log(epub.toc);
  78. // epub.toc.forEach((elm) => {
  79. // if (elm.href.indexOf("html") < 0) {
  80. // console.log(88, elm);
  81. // }
  82. // });
  83. console.log(101, epub);
  84. });
  85. export default router;