123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- // 添加账本
- import { getFileBymd5 } from "#db";
- import logger from "#logger";
- import path from "node:path";
- import fs from "node:fs";
- import express from "express";
- import { EPub } from "epub2";
- import { dirExists, isFileSync, isDir, waittime } from "#utils";
- import { saveImgs } from "./image.js";
- const router = express.Router();
- // middleware that is specific to this router
- router.use(function timeLog(req, res, next) {
- next();
- });
- // define the home page route
- router.get("/", async function (req, res) {
- res.send("epub types");
- });
- // define the about route
- router.get("/about", function (req, res) {
- res.send("About types");
- });
- router.get("/img/:fileId", async function (req, res) {
- const fileId = req.params.fileId; // 获取 fileId 参数
- logger.info(`Found ${fileId}`);
- const fileRow = await getFileBymd5(fileId);
- if (!fileRow) {
- return res.status(404).send("文件查询失败");
- }
- const uploadPath = "./base_files/" + fileId;
- console.log("uploadPath", uploadPath);
- const filePath = path.resolve(uploadPath);
- console.log("filePath", filePath);
- // 检查文件是否存在
- if (!fs.existsSync(filePath)) {
- return res.status(404).send("服务器中不存在该文件");
- }
- // 返回文件
- res.setHeader("Content-Type", fileRow.mimetype);
- res.sendFile(filePath);
- });
- // define the about route
- router.put("/", async function (req, res) {
- let sampleFile;
- let uploadPath;
- let epubData;
- let zipEpubExtract;
- let epubFilePath;
- let epub;
- if (!req.files || Object.keys(req.files).length === 0) {
- return res.status(400).send("No files were uploaded.");
- }
- sampleFile = req.files.file;
- const file_md5 = sampleFile.md5;
- uploadPath = `./base_files/${file_md5}/`;
- epubFilePath = uploadPath + sampleFile.name;
- zipEpubExtract = uploadPath + "epub-extract/";
- dirExists(uploadPath);
- await waittime(200);
- const isFile = isFileSync(epubFilePath);
- // 移动上传文件至指定目录
- if (!isFile) {
- sampleFile.mv(epubFilePath, function (err) {
- console.log("移动上传文件至指定目录的反馈", err);
- });
- epubData = sampleFile.data;
- } else {
- epubData = fs.readFileSync(epubFilePath);
- }
- /* 是否需要解压文件 */
- if (!(await isDir(zipEpubExtract))) {
- epub = await EPub.createAsync(epubData, null, "");
- dirExists(zipEpubExtract);
- epub.zip.admZip.extractAllTo(zipEpubExtract);
- } else {
- epub = await EPub.createAsync(epubData, null, "");
- }
- // Object.keys(epub.metadata).forEach((objKey) => {
- // console.log(464646, objKey, epub.metadata[objKey]);
- // });
- // let imgs = epub.listImage();
- // await epub.getImageAsync(imgs[0].id).then( function([data, mimeType]){
- // console.log(`\ngetImage: cover\n`);
- // console.log(data);
- // console.log(mimeType)
- // });
- res.send("About types");
- /*
- 1、读取图片信息
- 2、替换文件中的图片内容
- 3、存储html数据
- */
- // await saveImgs(epub);
- // 存储html数据
- // console.log("\nSPINE:\n");
- // // console.log(epub.flow);
- // epub.flow.forEach((elm) => {
- // if (elm.href.indexOf("html") < 0) {
- // console.log(88, elm);
- // }
- // });
- // console.log("\nTOC:\n");
- // console.log(epub.toc);
- // epub.toc.forEach((elm) => {
- // if (elm.href.indexOf("html") < 0) {
- // console.log(88, elm);
- // }
- // });
- // epub.zip.names.forEach((elm) => {
- // console.log(102, elm);
- // });
- // console.log(101, epub);
- });
- export default router;
|