123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import logger from "#logger";
- import { files_insert, files_insert_link_epub } from "#db";
- import { dirExists } from "#utils";
- import fs from "node:fs";
- import { calculateMD5 } from "./image.js";
- import cliProgress from 'cli-progress';
- export async function saveAllFount(epub, uploadPath, book_id, author_id) {
- dirExists(uploadPath);
- dirExists(`${uploadPath}font/`);
- // 获取原始数据源
- const getAllCss = epub.zip.names.filter((elm) => elm.includes("ttf"));
- const base_path = `${uploadPath}epub-extract/`;
- if (getAllCss.length) {
- const cssRes = await Promise.allSettled(
- getAllCss.map((img) => {
- return fs.promises.readFile(base_path + img, "utf8");
- })
- );
- const allCss_fulfilled = cssRes
- .map((img_fulfilled, index) => {
- const cssPath = getAllCss[index];
- if (img_fulfilled.status === "fulfilled") {
- return {
- index,
- path: base_path + cssPath,
- cssPath,
- css_data: img_fulfilled.value,
- mimeType: "font/ttf",
- };
- }
- return null;
- })
- .filter(Boolean);
- // Initialize the progress bar
- const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
- progressBar.start(allCss_fulfilled.length, 0);
- await Promise.allSettled(
- allCss_fulfilled.map(async (elm, index) => {
- try {
- const md5 = await calculateMD5(elm.path);
- const elmName = elm.cssPath.split('/').pop();
- // Write the font data to a file
- await fs.promises.writeFile(`${uploadPath}font/${md5}.ttf`, elm.css_data);
- logger.info("Font data saved to " + md5);
- const params = {
- file_id: md5,
- md5: md5,
- mimetype: elm.mimeType,
- size: elm.css_data.length,
- name: elmName,
- path: `${uploadPath}font/${md5}.ttf`,
- source_id: md5,
- };
- await files_insert(params);
- await files_insert_link_epub({
- file_id: md5,
- book_id,
- author_id,
- });
- // Update the progress bar
- progressBar.update(index + 1);
- } catch (err) {
- logger.error("Error processing font:", err);
- }
- })
- );
- // Update the progress bar
- progressBar.update(allCss_fulfilled.length);
- // Stop the progress bar
- progressBar.stop();
- }
- }
|