index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. console.log(4545, sampleFile);
  50. const file_md5 = sampleFile.md5;
  51. var epub = await EPub.createAsync(sampleFile.data, null, "");
  52. console.log(4545, epub.metadata);
  53. // console.log('manifest__', epub.manifest);
  54. // Object.keys(epub.metadata).forEach((objKey) => {
  55. // console.log(464646, objKey, epub.metadata[objKey]);
  56. // });
  57. // let imgs = epub.listImage();
  58. // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
  59. // console.log(`\ngetImage: cover\n`);
  60. // console.log(data);
  61. // console.log(mimeType)
  62. // });
  63. /*
  64. 1、读取图片信息
  65. 2、替换文件中的图片内容
  66. 3、存储html数据
  67. */
  68. await saveImgs(epub);
  69. res.send("About types");
  70. });
  71. export default router;