1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import logger from "#logger";
- import { chapter_insert, searchChapterInfoForPath } from "#db";
- import cliProgress from 'cli-progress';
- export async function saveToc(epub, uploadPath, book_id, author_id) {
- // Initialize the progress bar
- const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
- progressBar.start(epub.toc.length, 0);
- for (const elm of epub.toc) {
- try {
- const match = `${elm.href}`.match(/\/(.*\.html).*/);
- let chapterInfo = {
- file_id: ''
- };
- let path = '';
- if (match) {
- path = match[1];
- chapterInfo = await searchChapterInfoForPath(path, book_id);
- }
- logger.info(elm);
- const name = `${elm.title}`;
- elm.title = '';
- const params = {
- name: name,
- 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`,
- };
- logger.info(params);
- await chapter_insert(params);
- } catch (e) {
- logger.error(e);
- } finally {
- // Update the progress bar
- progressBar.increment();
- }
- }
- // Stop the progress bar
- progressBar.stop();
- }
|