toc.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. const match = `${elm.href}`.match(/\/(.*\.html).*/)
  10. let chapterInfo = {
  11. file_id: ''
  12. }
  13. let path = ''
  14. if (match) {
  15. path = match[1];
  16. chapterInfo = await searchChapterInfoForPath(path, book_id)
  17. }
  18. const params = {
  19. name: elm.title,
  20. book_id: book_id,
  21. author_id: author_id,
  22. content: JSON.stringify(elm),
  23. level: elm.level,
  24. order_index: elm.order,
  25. order_id: chapterInfo.file_id,
  26. old_path: elm.href,
  27. path: `./base_files/${book_id}/Text/${chapterInfo.file_id}.html`,
  28. }
  29. return await chapter_insert(params)
  30. }))
  31. }