toc.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import logger from "#logger";
  2. import { chapter_insert, searchChapterInfoForPath } from "#db";
  3. import { dirExists } from "#utils";
  4. import fs from "node:fs";
  5. import { calculateMD5 } from "./image.js";
  6. import cliProgress from 'cli-progress';
  7. export async function saveToc(epub, uploadPath, book_id, author_id) {
  8. // 初始化进度条
  9. const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
  10. progressBar.start(epub.toc.length, 0);
  11. await Promise.allSettled(epub.toc.map(async (elm, index) => {
  12. try {
  13. const match = elm.href.match(/\/(.*\.html).*/);
  14. let chapterInfo = { file_id: '' };
  15. let path = '';
  16. if (match) {
  17. path = match[1];
  18. chapterInfo = await searchChapterInfoForPath(path, book_id);
  19. }
  20. const params = {
  21. name: elm.title,
  22. book_id: book_id,
  23. author_id: author_id,
  24. content: JSON.stringify(elm),
  25. level: elm.level,
  26. order_index: elm.order,
  27. order_id: chapterInfo.file_id,
  28. old_path: elm.href,
  29. path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
  30. };
  31. // 更新进度条
  32. progressBar.update(index + 1);
  33. await chapter_insert(params);
  34. } catch (e) {
  35. logger.error(e);
  36. }
  37. }));
  38. // 更新进度条
  39. progressBar.update(epub.toc.length);
  40. // 停止进度条
  41. progressBar.stop();
  42. }