|
@@ -5,6 +5,7 @@ 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";
|
|
|
|
|
@@ -50,6 +51,10 @@ router.get("/img/:fileId", async function (req, res) {
|
|
|
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.");
|
|
@@ -58,9 +63,34 @@ router.put("/", async function (req, res) {
|
|
|
sampleFile = req.files.file;
|
|
|
|
|
|
const file_md5 = sampleFile.md5;
|
|
|
+ uploadPath = `./base_files/${file_md5}/`;
|
|
|
+ epubFilePath = uploadPath + sampleFile.name;
|
|
|
+ zipEpubExtract = uploadPath + "epub-extract/";
|
|
|
|
|
|
- var epub = await EPub.createAsync(sampleFile.data, null, "");
|
|
|
- // console.log('manifest__', epub.manifest);
|
|
|
+ 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]);
|
|
@@ -98,7 +128,11 @@ router.put("/", async function (req, res) {
|
|
|
// }
|
|
|
// });
|
|
|
|
|
|
- console.log(101, epub);
|
|
|
+ // epub.zip.names.forEach((elm) => {
|
|
|
+ // console.log(102, elm);
|
|
|
+ // });
|
|
|
+
|
|
|
+ // console.log(101, epub);
|
|
|
});
|
|
|
|
|
|
export default router;
|