|
@@ -10,57 +10,56 @@ import {
|
|
book_mate_insert,
|
|
book_mate_insert,
|
|
} from "#db";
|
|
} from "#db";
|
|
import {calculateMD5} from "./image.js";
|
|
import {calculateMD5} from "./image.js";
|
|
-import cliProgress from 'cli-progress';
|
|
|
|
|
|
|
|
-const imageExtensions = [".png", ".jpg", ".jpeg", ".svg"];
|
|
|
|
|
|
+const imageExtensions = [".png", ".jpg", ".jpeg", ".svg", ".gif"];
|
|
|
|
|
|
-async function processFiles(elmData, file_md5, elmIndex) {
|
|
|
|
|
|
+async function processFiles(elmData, file_md5) {
|
|
const rows = elmData.toString().split(/\n/);
|
|
const rows = elmData.toString().split(/\n/);
|
|
- const promises = rows.map(async (rowtext) => {
|
|
|
|
|
|
+ const results = [];
|
|
|
|
+
|
|
|
|
+ for (const rowtext of rows) {
|
|
if (
|
|
if (
|
|
rowtext.includes("<img ") &&
|
|
rowtext.includes("<img ") &&
|
|
imageExtensions.some((ext) => rowtext.includes(ext))
|
|
imageExtensions.some((ext) => rowtext.includes(ext))
|
|
) {
|
|
) {
|
|
- // const match = rowtext.match(/.*(..\/Images\/(.*\.(jpg|png|jpeg|svg))).*/);
|
|
|
|
- const match = rowtext.match(/src=("|')(.*\/(.*\.[a-zA-Z]+))("|')/)
|
|
|
|
|
|
+ const match = rowtext.match(/src=("|')(.*\/(.*\.[a-zA-Z]+))("|')/);
|
|
if (match) {
|
|
if (match) {
|
|
const [, , imgPath, imageSrc] = match;
|
|
const [, , imgPath, imageSrc] = match;
|
|
const imgRow = await searchFileByPath(imageSrc);
|
|
const imgRow = await searchFileByPath(imageSrc);
|
|
if (imgRow) {
|
|
if (imgRow) {
|
|
- return (
|
|
|
|
|
|
+ results.push(
|
|
rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`) +
|
|
rowtext.replace(imgPath, `/api/v1/epub/img/${imgRow.file_id}`) +
|
|
"\n"
|
|
"\n"
|
|
);
|
|
);
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if (rowtext.includes(".css")) {
|
|
} else if (rowtext.includes(".css")) {
|
|
const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
|
|
const match = rowtext.match(/.*="(.*\/?(.*\.css))/);
|
|
- if (rowtext) {
|
|
|
|
- const [elmPath , elmName] = `${rowtext}`.match(/.*\/?(.*\.css)/);
|
|
|
|
-
|
|
|
|
|
|
+ const [elmPath, elmName] = `${rowtext}`.match(/.*\/(.*\.css)/);
|
|
|
|
+ if (match) {
|
|
const [, cssPath, cssSrc] = match;
|
|
const [, cssPath, cssSrc] = match;
|
|
- // const imgRow = await searchFileByPath(elmName, file_md5);
|
|
|
|
- const imgNameRow = await searchFileByName(elmName, file_md5);
|
|
|
|
|
|
+ const imgNameRow = await searchFileByName(elmName, file_md5);
|
|
if (imgNameRow) {
|
|
if (imgNameRow) {
|
|
- return (
|
|
|
|
- rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
|
|
|
|
- "\n"
|
|
|
|
|
|
+ results.push(
|
|
|
|
+ rowtext.replace(cssPath, `/api/v1/epub/css/${imgNameRow.file_id}`) +
|
|
|
|
+ "\n"
|
|
);
|
|
);
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if (rowtext.includes(".ttf")) {
|
|
} else if (rowtext.includes(".ttf")) {
|
|
- // 使用正则表达式匹配路径和文件名
|
|
|
|
const match = rowtext.match(/.*\((.*\/?(.*ttf))\)./);
|
|
const match = rowtext.match(/.*\((.*\/?(.*ttf))\)./);
|
|
if (match) {
|
|
if (match) {
|
|
const [, cssPath, cssSrc] = match;
|
|
const [, cssPath, cssSrc] = match;
|
|
try {
|
|
try {
|
|
- // 搜索数据库中是否存在该字体文件
|
|
|
|
const imgRow = await searchFileByPath(cssSrc, file_md5);
|
|
const imgRow = await searchFileByPath(cssSrc, file_md5);
|
|
- if (imgRow) {
|
|
|
|
- return (
|
|
|
|
|
|
+ if (imgRow) {
|
|
|
|
+ results.push(
|
|
rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
|
|
rowtext.replace(cssPath, `/api/v1/epub/css/${imgRow.file_id}`) +
|
|
"\n"
|
|
"\n"
|
|
);
|
|
);
|
|
|
|
+ continue;
|
|
} else {
|
|
} else {
|
|
console.warn(`Font file not found for path: ${cssSrc}`);
|
|
console.warn(`Font file not found for path: ${cssSrc}`);
|
|
}
|
|
}
|
|
@@ -70,53 +69,49 @@ async function processFiles(elmData, file_md5, elmIndex) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- return rowtext + "\n";
|
|
|
|
- });
|
|
|
|
|
|
+ results.push(rowtext + "\n");
|
|
|
|
+ }
|
|
|
|
|
|
- const results = await Promise.allSettled(promises);
|
|
|
|
return results.join("");
|
|
return results.join("");
|
|
}
|
|
}
|
|
|
|
|
|
export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
|
|
export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
|
|
const needSetImage = epub.zip.names.filter(
|
|
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 basePath = path.join("./base_files", file_md5, "Text");
|
|
const styleBasePath = path.join("./base_files", file_md5, "style");
|
|
const styleBasePath = path.join("./base_files", file_md5, "style");
|
|
dirExists(basePath);
|
|
dirExists(basePath);
|
|
dirExists(styleBasePath);
|
|
dirExists(styleBasePath);
|
|
-
|
|
|
|
- // 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];
|
|
|
|
|
|
+
|
|
|
|
+ for (const elm of needSetImage) {
|
|
|
|
+ console.log('Processing:', elm);
|
|
const filePath = path.join(zipEpubExtract, elm);
|
|
const filePath = path.join(zipEpubExtract, elm);
|
|
const elmData = fs.readFileSync(filePath);
|
|
const elmData = fs.readFileSync(filePath);
|
|
- const htmlStr = await processFiles(elmData, file_md5, elmIndex);
|
|
|
|
|
|
+ const htmlStr = await processFiles(elmData, file_md5);
|
|
let file_path;
|
|
let file_path;
|
|
let source_id;
|
|
let source_id;
|
|
|
|
+
|
|
if (htmlStr) {
|
|
if (htmlStr) {
|
|
- // console.log('needSetImage', elmIndex);
|
|
|
|
- // fs.writeFileSync(filePath, htmlStr);
|
|
|
|
-
|
|
|
|
|
|
+ fs.writeFileSync(filePath, htmlStr);
|
|
|
|
+
|
|
const htmlMd5 = await calculateMD5(filePath);
|
|
const htmlMd5 = await calculateMD5(filePath);
|
|
const isCss = elm.endsWith(".css");
|
|
const isCss = elm.endsWith(".css");
|
|
const newFilePath = path.join(
|
|
const newFilePath = path.join(
|
|
- isCss ? styleBasePath : basePath,
|
|
|
|
- `${htmlMd5}.${isCss ? "css" : "html"}`
|
|
|
|
|
|
+ isCss ? styleBasePath : basePath,
|
|
|
|
+ `${htmlMd5}.${isCss ? "css" : "html"}`
|
|
);
|
|
);
|
|
-
|
|
|
|
- Object.keys(epub.manifest).forEach(m_key => {
|
|
|
|
|
|
+
|
|
|
|
+ for (const m_key of Object.keys(epub.manifest)) {
|
|
const mElm = epub.manifest[m_key];
|
|
const mElm = epub.manifest[m_key];
|
|
if (mElm.href.indexOf(elm) > -1 && !source_id) {
|
|
if (mElm.href.indexOf(elm) > -1 && !source_id) {
|
|
source_id = mElm.id;
|
|
source_id = mElm.id;
|
|
file_path = mElm.href;
|
|
file_path = mElm.href;
|
|
}
|
|
}
|
|
- });
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
const params = {
|
|
const params = {
|
|
file_id: htmlMd5,
|
|
file_id: htmlMd5,
|
|
md5: htmlMd5,
|
|
md5: htmlMd5,
|
|
@@ -127,40 +122,23 @@ export async function htmlParser(epub, zipEpubExtract, file_md5, author_id) {
|
|
source_id: source_id,
|
|
source_id: source_id,
|
|
};
|
|
};
|
|
await files_insert(params);
|
|
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),
|
|
|
|
- ]);
|
|
|
|
|
|
+ await files_insert_link_epub({
|
|
|
|
+ file_id: htmlMd5,
|
|
|
|
+ book_id: file_md5,
|
|
|
|
+ author_id,
|
|
|
|
+ });
|
|
|
|
+ await 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
|
|
// saveMateInfo
|
|
export async function saveMateInfo(epub, zipEpubExtract, file_md5, author_id) {
|
|
export async function saveMateInfo(epub, zipEpubExtract, file_md5, author_id) {
|
|
- // book_mate_insert
|
|
|
|
const params = {
|
|
const params = {
|
|
book_name: epub.metadata.title,
|
|
book_name: epub.metadata.title,
|
|
book_id: file_md5,
|
|
book_id: file_md5,
|
|
book_md5: file_md5,
|
|
book_md5: file_md5,
|
|
- // language: "",
|
|
|
|
- // date: "",
|
|
|
|
- // creatorFileAs: "",
|
|
|
|
- // UUID: "",
|
|
|
|
- // ISBN: "",
|
|
|
|
author_id: author_id,
|
|
author_id: author_id,
|
|
- // category_id: "",
|
|
|
|
- // Introduction: "",
|
|
|
|
};
|
|
};
|
|
|
|
|
|
const res = await book_mate_insert(params);
|
|
const res = await book_mate_insert(params);
|