From 77ed5af2a687bc6252ee8a9c451a88d056a4509b Mon Sep 17 00:00:00 2001 From: xiyuesaves Date: Sun, 16 Jun 2024 22:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=98=BB=E6=AD=A2=E6=92=A4?= =?UTF-8?q?=E5=9B=9E=E5=8A=9F=E8=83=BD=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=9B=BE=E7=89=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main_modules/downloadPic.js | 16 +++++++-- src/main_modules/processPic.js | 57 ++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/main_modules/downloadPic.js b/src/main_modules/downloadPic.js index 99788c66..a16cdb0e 100644 --- a/src/main_modules/downloadPic.js +++ b/src/main_modules/downloadPic.js @@ -5,8 +5,20 @@ */ function downloadPic(url) { return fetch(url) - .then((res) => res.arrayBuffer()) - .then((buf) => Buffer.from(buf)); + .then((res) => { + if (res.status === 200) { + return res.arrayBuffer(); + } else { + throw new Error(res.statusText); + } + }) + .then((buf) => { + if (buf.size !== 0) { + return new Uint8Array(buf); + } else { + throw new Error("请求返回无数据"); + } + }); } export { downloadPic }; diff --git a/src/main_modules/processPic.js b/src/main_modules/processPic.js index dd6361fb..16682a59 100644 --- a/src/main_modules/processPic.js +++ b/src/main_modules/processPic.js @@ -1,5 +1,5 @@ import { dirname } from "path"; -import { existsSync, mkdirSync, writeFileSync } from "fs"; +import { existsSync, mkdirSync, writeFileSync, statSync } from "fs"; import { downloadPic } from "./downloadPic.js"; import { config } from "./config.js"; import { getRkey } from "./getRkey.js"; @@ -8,28 +8,26 @@ const log = new Logs("图片处理模块"); // 获取图片链接 async function getPicUrl(picData, chatType) { - log(picData); - if (!picData.original) { - if (picData.originImageUrl) { - if (picData.originImageUrl.includes("&rkey=")) { - return `${config.global.IMAGE_HTTP_HOST_NT}${picData.originImageUrl}`; - } else { - const rkey = await getRkey(chatType); - return `${config.global.IMAGE_HTTP_HOST_NT}${picData.originImageUrl}${rkey}`; - } + let picURL = ""; + if (picData.original) { + if (picData.originImageUrl.includes("&rkey=")) { + return `${config.global.IMAGE_HTTP_HOST_NT}${picData.originImageUrl}`; } else { - return `${config.global.IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${picData.originImageMd5}/`; + const rkey = await getRkey(chatType); + picURL = `${config.global.IMAGE_HTTP_HOST_NT}${picData.originImageUrl}${rkey}`; } } else { - return `${config.global.IMAGE_HTTP_HOST}/gchatpic_new/0/0-0-${picData.md5HexStr.toUpperCase()}/`; + picURL = `${config.global.IMAGE_HTTP_HOST}${picData.originImageUrl}`; } + log("获取图片链接", picURL); + return picURL; } async function getPicArr(picData, chatType) { const rawUrl = await getPicUrl(picData, chatType); return [0, 198, 720].map((el) => { if (rawUrl.includes("gchatpic_new")) { - return `${rawUrl}${el}`; + return rawUrl.replace(/\/0(\?term=255&is_origin=0)?$/, `/${el}$1`); } else { return rawUrl.replace("&spec=0", `&spec=${el}`); } @@ -49,11 +47,17 @@ async function processPic(msgItem) { const pic = el.picElement; const chatType = msgItem.chatType === 2 ? "group_rkey" : "private_rkey"; const picUrls = await getPicArr(pic, chatType); - if (!existsSync(pic.sourcePath)) { - log("下载原图", picUrls); - const body = await downloadPic(picUrls[0]); - mkdirSync(dirname(pic.sourcePath), { recursive: true }); - writeFileSync(pic.sourcePath, body); + if (requireDownload(pic.sourcePath)) { + try { + log("下载原图", picUrls[0]); + const body = await downloadPic(picUrls[0]); + mkdirSync(dirname(pic.sourcePath), { recursive: true }); + writeFileSync(pic.sourcePath, body); + } catch (err) { + log("原图下载失败", picUrls[0], err?.message, err?.stack); + } + } else { + log("原图已存在", pic.sourcePath); } // 修复本地数据中的错误 if (pic?.thumbPath) { @@ -62,22 +66,22 @@ async function processPic(msgItem) { [198, pic.sourcePath.replace("Ori", "Thumb").replace(pic.md5HexStr, pic.md5HexStr + "_198")], [720, pic.sourcePath.replace("Ori", "Thumb").replace(pic.md5HexStr, pic.md5HexStr + "_720")], ]); - log("修复缩略图数据", pic.thumbPath); } + log("缩略图数据", pic.thumbPath); // log("缩略图", typeof pic?.thumbPath, pic?.thumbPath instanceof Map); if (pic?.thumbPath) { const toArray = Array.from(pic.thumbPath); log("开始下载缩略图", toArray); for (let i = 0; i < toArray.length; i++) { const picPath = toArray[i][1]; - if (!existsSync(picPath)) { + if (requireDownload(picPath)) { try { - log("尝试下载", picPath, picUrls[i]); + log("尝试下载缩略图", picPath, picUrls[i]); const body = await downloadPic(picUrls[i]); mkdirSync(dirname(picPath), { recursive: true }); writeFileSync(picPath, body); } catch (err) { - log("缩略图下载失败", picPath, err?.message); + log("缩略图下载失败", picPath, err?.message, err?.stack); } } else { log("缩略图已存在", picPath); @@ -89,4 +93,13 @@ async function processPic(msgItem) { } } +function requireDownload(path) { + if (existsSync(path)) { + const stats = statSync(path); + log("图片尺寸", stats.size, stats.size >= 100); + return stats.size < 100; + } + return true; +} + export { processPic };