123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import logger from "#logger";
- import { chapter_insert, searchChapterInfoForPath } from "#db";
- import { dirExists } from "#utils";
- import fs from "node:fs";
- import { calculateMD5 } from "./image.js";
- import cliProgress from 'cli-progress';
- export async function saveToc(epub, uploadPath, book_id, author_id) {
- // 初始化进度条
- const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
- progressBar.start(epub.toc.length, 0);
- await Promise.allSettled(epub.toc.map(async (elm, index) => {
- try {
- const match = elm.href.match(/\/(.*\.html).*/);
- let chapterInfo = { file_id: '' };
- let path = '';
- if (match) {
- path = match[1];
- chapterInfo = await searchChapterInfoForPath(path, book_id);
- }
- const params = {
- name: elm.title,
- book_id: book_id,
- author_id: author_id,
- content: JSON.stringify(elm),
- level: elm.level,
- order_index: elm.order,
- order_id: chapterInfo.file_id,
- old_path: elm.href,
- path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
- };
- // 更新进度条
- progressBar.update(index + 1);
- await chapter_insert(params);
- } catch (e) {
- logger.error(e);
- }
- }));
- // 更新进度条
- progressBar.update(epub.toc.length);
- // 停止进度条
- progressBar.stop();
- }
|