|
@@ -10,10 +10,11 @@ import {
|
|
|
book_mate_insert,
|
|
|
} from "#db";
|
|
|
import {calculateMD5} from "./image.js";
|
|
|
+import cliProgress from 'cli-progress';
|
|
|
|
|
|
const imageExtensions = [".png", ".jpg", ".jpeg", ".svg"];
|
|
|
|
|
|
-async function processFiles(elmData, file_md5) {
|
|
|
+async function processFiles(elmData, file_md5, elmIndex) {
|
|
|
const rows = elmData.toString().split(/\n/);
|
|
|
const promises = rows.map(async (rowtext) => {
|
|
|
if (
|
|
@@ -34,15 +35,16 @@ async function processFiles(elmData, file_md5) {
|
|
|
}
|
|
|
} else if (rowtext.includes(".css")) {
|
|
|
const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
|
|
|
- const [elmPath, elmName] = `${rowtext}`.match(/.*\/(.*\.css)/);
|
|
|
- if (match) {
|
|
|
+ if (rowtext) {
|
|
|
+ const [elmPath , elmName] = `${rowtext}`.match(/.*\/?(.*\.css)/);
|
|
|
+
|
|
|
const [, cssPath, cssSrc] = match;
|
|
|
// const imgRow = await searchFileByPath(elmName, file_md5);
|
|
|
const imgNameRow = await searchFileByName(elmName, file_md5);
|
|
|
if (imgNameRow) {
|
|
|
return (
|
|
|
- rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
|
|
|
- "\n"
|
|
|
+ rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
|
|
|
+ "\n"
|
|
|
);
|
|
|
}
|
|
|
}
|
|
@@ -54,12 +56,7 @@ async function processFiles(elmData, file_md5) {
|
|
|
try {
|
|
|
// 搜索数据库中是否存在该字体文件
|
|
|
const imgRow = await searchFileByPath(cssSrc, file_md5);
|
|
|
- if (imgRow) {
|
|
|
- // 如果找到,替换路径为 API 端点
|
|
|
- console.log(57, rowtext);
|
|
|
- console.log(58, cssPath, cssSrc);
|
|
|
- console.log(59, `/api/v1/epub/css/${imgRow.file_id}`);
|
|
|
-
|
|
|
+ if (imgRow) {
|
|
|
return (
|
|
|
rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
|
|
|
"\n"
|
|
@@ -76,71 +73,79 @@ async function processFiles(elmData, file_md5) {
|
|
|
return rowtext + "\n";
|
|
|
});
|
|
|
|
|
|
- const results = await Promise.all(promises);
|
|
|
+ const results = await Promise.allSettled(promises);
|
|
|
return results.join("");
|
|
|
}
|
|
|
|
|
|
export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
|
|
|
const needSetImage = epub.zip.names.filter(
|
|
|
- (elm) => elm.endsWith(".html") || elm.endsWith(".css")
|
|
|
+ (elm) => elm.endsWith(".html") || elm.endsWith(".css")
|
|
|
);
|
|
|
-
|
|
|
- const needSetFont = epub.zip.names.filter((elm) => elm.endsWith(".ttf"));
|
|
|
+
|
|
|
const basePath = path.join("./base_files", file_md5, "Text");
|
|
|
const styleBasePath = path.join("./base_files", file_md5, "style");
|
|
|
dirExists(basePath);
|
|
|
dirExists(styleBasePath);
|
|
|
-
|
|
|
- await Promise.all(
|
|
|
- needSetImage.map(async (elm, elmIndex) => {
|
|
|
- console.log('needSetImage', elmIndex)
|
|
|
- const filePath = path.join(zipEpubExtract, elm);
|
|
|
- const elmData = fs.readFileSync(filePath);
|
|
|
- const htmlStr = await processFiles(elmData, file_md5);
|
|
|
- let file_path;
|
|
|
- let source_id;
|
|
|
-
|
|
|
- if (htmlStr) {
|
|
|
- fs.writeFileSync(filePath, htmlStr);
|
|
|
-
|
|
|
- const htmlMd5 = await calculateMD5(filePath);
|
|
|
- const isCss = elm.endsWith(".css");
|
|
|
- const newFilePath = path.join(
|
|
|
- isCss ? styleBasePath : basePath,
|
|
|
- `${htmlMd5}.${isCss ? "css" : "html"}`
|
|
|
- );
|
|
|
-
|
|
|
- Object.keys(epub.manifest).forEach(m_key => {
|
|
|
- const mElm = epub.manifest[m_key];
|
|
|
- if (mElm.href.indexOf(elm) > -1 && !source_id) {
|
|
|
- source_id = mElm.id;
|
|
|
- file_path = mElm.href
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- const params = {
|
|
|
+
|
|
|
+ // Initialize the progress bar
|
|
|
+ const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
|
|
|
+ progressBar.start(needSetImage.length, 0);
|
|
|
+
|
|
|
+ for (let elmIndex = 0; elmIndex < needSetImage.length; elmIndex++) {
|
|
|
+ const elm = needSetImage[elmIndex];
|
|
|
+ const filePath = path.join(zipEpubExtract, elm);
|
|
|
+ const elmData = fs.readFileSync(filePath);
|
|
|
+ const htmlStr = await processFiles(elmData, file_md5, elmIndex);
|
|
|
+ let file_path;
|
|
|
+ let source_id;
|
|
|
+ if (htmlStr) {
|
|
|
+ // console.log('needSetImage', elmIndex);
|
|
|
+ // fs.writeFileSync(filePath, htmlStr);
|
|
|
+
|
|
|
+ const htmlMd5 = await calculateMD5(filePath);
|
|
|
+ const isCss = elm.endsWith(".css");
|
|
|
+ const newFilePath = path.join(
|
|
|
+ isCss ? styleBasePath : basePath,
|
|
|
+ `${htmlMd5}.${isCss ? "css" : "html"}`
|
|
|
+ );
|
|
|
+
|
|
|
+ Object.keys(epub.manifest).forEach(m_key => {
|
|
|
+ const mElm = epub.manifest[m_key];
|
|
|
+ if (mElm.href.indexOf(elm) > -1 && !source_id) {
|
|
|
+ source_id = mElm.id;
|
|
|
+ file_path = mElm.href;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ file_id: htmlMd5,
|
|
|
+ md5: htmlMd5,
|
|
|
+ mimetype: isCss ? "text/css" : "text/html",
|
|
|
+ size: Buffer.byteLength(htmlStr),
|
|
|
+ name: `${htmlMd5}.${isCss ? "css" : "html"}`,
|
|
|
+ path: file_path,
|
|
|
+ source_id: source_id,
|
|
|
+ };
|
|
|
+ await files_insert(params);
|
|
|
+ await Promise.all([
|
|
|
+ files_insert_link_epub({
|
|
|
file_id: htmlMd5,
|
|
|
- md5: htmlMd5,
|
|
|
- mimetype: isCss ? "text/css" : "text/html",
|
|
|
- size: Buffer.byteLength(htmlStr),
|
|
|
- name: `${htmlMd5}.${isCss ? "css" : "html"}`,
|
|
|
- path: file_path,
|
|
|
- source_id: source_id,
|
|
|
- };
|
|
|
- await files_insert(params);
|
|
|
- await Promise.all([
|
|
|
- files_insert_link_epub({
|
|
|
- file_id: htmlMd5,
|
|
|
- book_id: file_md5,
|
|
|
- author_id,
|
|
|
- }),
|
|
|
- fs.promises.writeFile(newFilePath, htmlStr),
|
|
|
- ]);
|
|
|
- }
|
|
|
- })
|
|
|
- );
|
|
|
+ book_id: file_md5,
|
|
|
+ author_id,
|
|
|
+ }),
|
|
|
+ fs.promises.writeFile(newFilePath, htmlStr),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ // Update the progress bar
|
|
|
+ progressBar.update(elmIndex + 1);
|
|
|
+ }
|
|
|
+ // Update the progress bar
|
|
|
+ progressBar.update(needSetImage.length);
|
|
|
+ // Stop the progress bar
|
|
|
+ progressBar.stop();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// saveMateInfo
|
|
|
export async function saveMateInfo(epub, zipEpubExtract, file_md5, author_id) {
|
|
|
// book_mate_insert
|