image.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { dirExists } from "#utils";
  2. import { files_insert } from "#db";
  3. import crypto from "crypto";
  4. import fs from "node:fs";
  5. export async function saveImgs( epub ) {
  6. // const res = await Promise.allSettled(imgs.map(elm => {
  7. // }))
  8. let imgs = epub.listImage();
  9. console.log(imgs[0]);
  10. console.log(1121, imgs.length);
  11. await epub.getImageAsync(imgs[2].id).then(async function ( [data,mimeType] ) {
  12. console.log(`\ngetImage: cover\n`);
  13. console.log(data);
  14. const img_md5 = await calculateMD5FromBuffer(data);
  15. console.log(img_md5);
  16. console.log(mimeType);
  17. console.log(1616,formatSize(data.length));
  18. const params = {
  19. file_id: img_md5,
  20. md5: img_md5,
  21. mimetype: mimeType,
  22. size: data.length,
  23. name: imgs[0].id,
  24. path: imgs[0].href,
  25. }
  26. const uploadPath = "./base_files/" + img_md5;
  27. dirExists("./base_files/");
  28. // 如果需要将 JSON 保存为文件,可以使用 fs.writeFile()
  29. fs.writeFile(uploadPath,data,( err ) => {
  30. if ( err ) {
  31. console.error("Error writing JSON file:",err);
  32. } else {
  33. console.log("JSON data saved to output.json");
  34. }
  35. });
  36. await files_insert(params)
  37. });
  38. }
  39. function calculateMD5( filePath ) {
  40. const hash = crypto.createHash("md5");
  41. const stream = fs.createReadStream(filePath);
  42. stream.on("data",( chunk ) => {
  43. hash.update(chunk);
  44. });
  45. stream.on("end",() => {
  46. console.log("MD5 hash:",hash.digest("hex"));
  47. });
  48. }
  49. export async function calculateMD5FromStream( fileStream ) {
  50. return new Promise(( resolve,reject ) => {
  51. const hash = crypto.createHash("md5");
  52. // 错误处理
  53. fileStream.on("error",( err ) => {
  54. console.error("Error reading file stream:",err);
  55. reject("生成失败!"); // 使用 reject 传递错误信息
  56. });
  57. // 监听 'data' 事件,更新 MD5 哈希
  58. fileStream.on("data",( chunk ) => {
  59. hash.update(chunk);
  60. });
  61. // 监听 'end' 事件,文件读取完毕后输出 MD5 值
  62. fileStream.on("end",() => {
  63. const md5 = hash.digest("hex");
  64. console.log("MD5 Hash:",md5);
  65. resolve(md5); // 正常计算完 MD5 后 resolve
  66. });
  67. });
  68. }
  69. export function calculateMD5FromBuffer( buffer ) {
  70. return new Promise(( resolve,reject ) => {
  71. try {
  72. // 使用 crypto 计算 MD5
  73. const hash = crypto.createHash("md5");
  74. hash.update(buffer); // 直接更新 hash
  75. const md5 = hash.digest("hex"); // 获取最终的 MD5 值
  76. console.log("MD5 Hash:",md5);
  77. resolve(md5); // 返回 MD5
  78. } catch ( err ) {
  79. console.error("Error calculating MD5:",err);
  80. reject("生成失败!");
  81. }
  82. });
  83. }
  84. const formatSize = ( bytes ) => {
  85. if ( bytes < 1024 ) return `${ bytes } 字节`;
  86. else if ( bytes < 1024 * 1024 ) return `${ ( bytes / 1024 ).toFixed(2) } KB`;
  87. else if ( bytes < 1024 * 1024 * 1024 )
  88. return `${ ( bytes / ( 1024 * 1024 ) ).toFixed(2) } MB`;
  89. else return `${ ( bytes / ( 1024 * 1024 * 1024 ) ).toFixed(2) } GB`;
  90. };