Skip to content

Commit

Permalink
修复阻止撤回功能图片下载模块无法正确下载图片的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyuesaves committed Jun 16, 2024
1 parent 77a9952 commit 77ed5af
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
16 changes: 14 additions & 2 deletions src/main_modules/downloadPic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
57 changes: 35 additions & 22 deletions src/main_modules/processPic.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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}`);
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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 };

0 comments on commit 77ed5af

Please sign in to comment.