|
@@ -1,73 +1,66 @@
|
|
|
-import { dirExists } from "#utils";
|
|
|
+import logger from "#logger";
|
|
|
import { files_insert } from "#db";
|
|
|
-import crypto from "crypto";
|
|
|
+import { dirExists } from "#utils";
|
|
|
import fs from "node:fs";
|
|
|
-import logger from "#logger";
|
|
|
+import { calculateMD5 } from './image.js'
|
|
|
+
|
|
|
|
|
|
-export async function saveAllCSS(epub, uploadPath) {
|
|
|
+// ./base_files/5ae2d9158081faab184484ed1783e176
|
|
|
+export async function saveAllCSS( epub,uploadPath ) {
|
|
|
dirExists(uploadPath);
|
|
|
+ dirExists(`${ uploadPath }style/`);
|
|
|
|
|
|
// 获取原始数据源
|
|
|
- const getAllCss = epub.zip.names.filter(
|
|
|
- (elm) => elm.indexOf("css") > -1
|
|
|
- );
|
|
|
- console.log(14, getAllCss);
|
|
|
-/* if (getAllCss.length) {
|
|
|
- const imgRes = await Promise.allSettled(
|
|
|
- getAllCss.map((img) => {
|
|
|
- return fs.readFileSync()
|
|
|
- })
|
|
|
- );*/
|
|
|
- // // 过滤数据
|
|
|
- // let imgs = epub.listImage();
|
|
|
- // if (imgs.length) {
|
|
|
- // const imgRes = await Promise.allSettled(
|
|
|
- // imgs.map((img) => {
|
|
|
- // return epub.getImageAsync(img.id);
|
|
|
- // })
|
|
|
- // );
|
|
|
- // // 过滤数据
|
|
|
+ const getAllCss = epub.zip.names.filter(( elm ) => elm.indexOf("css") > -1);
|
|
|
+ const base_path = `${ uploadPath }epub-extract/`
|
|
|
+ if ( getAllCss.length ) {
|
|
|
+ const cssRes = await Promise.allSettled(getAllCss.map(( img ) => {
|
|
|
+ return fs.readFileSync(base_path + img,"utf8");
|
|
|
+ }));
|
|
|
+ const allCss_fulfilled = cssRes
|
|
|
+ .map(( img,index ) => {
|
|
|
+ const img_fulfilled = cssRes[index];
|
|
|
+ const cssPath = getAllCss[index];
|
|
|
+ if ( img_fulfilled.status === "fulfilled" ) {
|
|
|
+ // const file_md5 = await calculateMD5(base_path + cssPath)
|
|
|
+ return {
|
|
|
+ ...img,...img_fulfilled,index,path: base_path + cssPath,cssPath,// md5: file_md5,
|
|
|
+ css_data: img_fulfilled.value,mimeType: 'text/css',
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ })
|
|
|
+ .filter(( elm ) => elm);
|
|
|
|
|
|
- // const imgs_fulfilled = imgs
|
|
|
- // .map((img, index) => {
|
|
|
- // const img_fulfilled = imgRes[index];
|
|
|
- // if (img_fulfilled.status === "fulfilled") {
|
|
|
- // const [img_data, img_mimeType] = img_fulfilled.value;
|
|
|
- // return {
|
|
|
- // ...img,
|
|
|
- // index,
|
|
|
- // img_data,
|
|
|
- // img_mimeType,
|
|
|
- // };
|
|
|
- // }
|
|
|
- // return false;
|
|
|
- // })
|
|
|
- // .filter((elm) => elm);
|
|
|
+ await Promise.allSettled(allCss_fulfilled.map(async ( elm ) => {
|
|
|
+ const md5 = await calculateMD5(elm.path)
|
|
|
+ const [elmPath,elmName] = `${ elm.path }`.match(/.*\/(.*\.css)/)
|
|
|
|
|
|
- // await Promise.allSettled(
|
|
|
- // imgs_fulfilled.map(async (elm) => {
|
|
|
- // const img_md5 = await calculateMD5FromBuffer(elm.img_data);
|
|
|
+ //
|
|
|
+ // 移动文件
|
|
|
+ // './base_files/5ae2d9158081faab184484ed1783e176/epub-extract/OEBPS/flow0001.css'
|
|
|
+ // ./base_files/5ae2d9158081faab184484ed1783e176/style/flow0001.css
|
|
|
+ //
|
|
|
+
|
|
|
+ fs.writeFile(`${ uploadPath }style/${ elmName }`,elm.css_data,( err ) => {
|
|
|
+ if ( err ) {
|
|
|
+ logger.error("Error writing Img file:",err);
|
|
|
+ } else {
|
|
|
+ logger.info("Img data saved to " + md5);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- // const uploadPath = "./base_files/" + img_md5;
|
|
|
- // const params = {
|
|
|
- // file_id: img_md5,
|
|
|
- // md5: img_md5,
|
|
|
- // mimetype: elm.img_mimeType,
|
|
|
- // size: elm.img_data.length,
|
|
|
- // name: elm.id,
|
|
|
- // path: elm.href,
|
|
|
- // source_id: elm.id,
|
|
|
- // };
|
|
|
|
|
|
- // fs.writeFile(uploadPath, elm.img_data, (err) => {
|
|
|
- // if (err) {
|
|
|
- // logger.error("Error writing Img file:", err);
|
|
|
- // } else {
|
|
|
- // logger.info("Img data saved to " + img_md5);
|
|
|
- // }
|
|
|
- // });
|
|
|
- // return await files_insert(params);
|
|
|
- // })
|
|
|
- // );
|
|
|
- // }
|
|
|
+ const params = {
|
|
|
+ file_id: md5,
|
|
|
+ md5: md5,
|
|
|
+ mimetype: elm.mimeType,
|
|
|
+ size: elm.css_data.length,
|
|
|
+ name: elmName,
|
|
|
+ path: `${ uploadPath }style/${ elmName }`,
|
|
|
+ source_id: md5,
|
|
|
+ };
|
|
|
+ return await files_insert(params);
|
|
|
+ }));
|
|
|
+ }
|
|
|
}
|