toc.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // ./base_files/5ae2d9158081faab184484ed1783e176
  7. export async function saveToc(epub, uploadPath, book_id, author_id) {
  8. await Promise.all(epub.toc.map(async (elm) => {
  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. return await chapter_insert(params)
  35. } catch (e) {
  36. logger.error(e)
  37. }
  38. }))
  39. }