toc.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 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. logger.info(params)
  32. return await chapter_insert(params)
  33. } catch (e) {
  34. logger.error(e)
  35. }
  36. }))
  37. }