toc_old.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import logger from "#logger";
  2. import { chapter_insert, searchChapterInfoForPath } from "#db";
  3. import cliProgress from 'cli-progress';
  4. export async function saveToc(epub, uploadPath, book_id, author_id) {
  5. // Initialize the progress bar
  6. const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
  7. progressBar.start(epub.toc.length, 0);
  8. for (const elm of epub.toc) {
  9. try {
  10. const match = `${elm.href}`.match(/\/(.*\.html).*/);
  11. let chapterInfo = {
  12. file_id: ''
  13. };
  14. let path = '';
  15. if (match) {
  16. path = match[1];
  17. chapterInfo = await searchChapterInfoForPath(path, book_id);
  18. }
  19. logger.info(elm);
  20. const name = `${elm.title}`;
  21. elm.title = '';
  22. const params = {
  23. name: name,
  24. book_id: book_id,
  25. author_id: author_id,
  26. content: JSON.stringify(elm),
  27. level: elm.level,
  28. order_index: elm.order,
  29. order_id: chapterInfo.file_id,
  30. old_path: elm.href,
  31. path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
  32. };
  33. logger.info(params);
  34. await chapter_insert(params);
  35. } catch (e) {
  36. logger.error(e);
  37. } finally {
  38. // Update the progress bar
  39. progressBar.increment();
  40. }
  41. }
  42. // Stop the progress bar
  43. progressBar.stop();
  44. }