fix(cli): embed local images as data URI so each platform uploads via…#196
Open
jssyy wants to merge 1 commit into
Open
fix(cli): embed local images as data URI so each platform uploads via…#196jssyy wants to merge 1 commit into
jssyy wants to merge 1 commit into
Conversation
… its own publish() pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
… its own publish() pipeline# fix(cli): 同步本地图片改用 data URI,让各平台经自身 publish() 管线上传
背景 / 问题
用 CLI 同步带本地图片的文章时,部分平台图片上传失败:
图片上传响应解析失败→ 最终图片uri非法,请重新上传,同步失败。Origin/Referer头与 csrf)。复现:
wechatsync sync article.md -p toutiao # article.md 里是  本地图根因
CLI 的
sync命令对本地图片采用processLocalImages:把图片预上传到第一个目标平台当图床,走的是各适配器的裸uploadImage(blob)。问题:publish()的上下文——拿不到头条所需的页面内反爬签名(a_bogus/msToken),也没套上 B站publish()里withHeaderRules加的Origin/Referer,于是这些平台上传被拒。convertImagesToDataUri(源码注释标注为「推荐方式:让各平台适配器自己处理图片上传,确保图片存储在目标平台的图床」),但sync命令未启用它。改动
packages/cli/src/index.ts,sync命令处理本地图片处:把processLocalImages(预上传到首个平台)替换为convertImagesToDataUri(内嵌 data URI,交给各平台publish()自行上传)。if (localImages.length > 0) { - // 使用第一个目标平台作为图床 - const imageHost = platforms[0] - console.log(chalk.bold(`发现 ${localImages.length} 张本地图片,上传到 ${imageHost}...`)) - const imageResult = await processLocalImages(parsed.content, fileDir, bridge, imageHost) - if (imageResult.uploadedCount > 0) { - if (parsed.format === 'markdown') { - processedMarkdown = imageResult.content - processedHtml = markdownToHtml(imageResult.content) - } else { - processedHtml = imageResult.content - } - } - console.log(`图片上传完成: ${imageResult.uploadedCount} 成功, ${imageResult.failedCount} 失败`) + // 内嵌 data URI,交给各平台适配器自己的 publish() 上传到各自图床。 + // 这样每个平台都走自己 publish() 里的签名/Header 管线(头条 a_bogus/msToken、 + // B站 Origin/Referer+csrf 等),反爬平台也能传图,且各平台图片各存自己图床,避免跨站盗链。 + console.log(chalk.bold(`发现 ${localImages.length} 张本地图片,转为内嵌(各平台自传图床)...`)) + const imageResult = convertImagesToDataUri(parsed.content, fileDir) + if (imageResult.convertedCount > 0) { + if (parsed.format === 'markdown') { + processedMarkdown = imageResult.content + processedHtml = markdownToHtml(imageResult.content) + } else { + processedHtml = imageResult.content + } + } + console.log(`本地图片内嵌完成: ${imageResult.convertedCount} 成功, ${imageResult.failedCount} 跳过`) }(
convertImagesToDataUri与processLocalImages均为本文件已有函数;本改动仅切换调用,无新增依赖。)为什么这样修
把图片以 data URI 内嵌进文章内容后,每个平台的
publish()会在自己的processImages流程里把这些图上传到该平台自己的图床——也就是走和「浏览器扩展一键同步按钮」完全相同的、带平台签名/Header 的上传管线。因此:publish()时已套用withHeaderRules,无需额外补丁即可成功。测试
本地
pnpm --filter @wechatsync/cli build后,用本地 CLI 同步含 4 张本地图片的 Markdown:-p toutiao:✅ 图片成功上传到头条图床,草稿正常生成(此前失败)。-p zhihu:✅ 正常。publish()管线,图片走withHeaderRules路径上传。取舍
相关观察(可选,另一处独立缺口)
performImageUpload(packages/extension/src/mcp/client.ts)调用的裸adapter.uploadImage(blob)路径,对 B站而言缺少publish()里的withHeaderRules/csrf。若希望该独立路径也可用,可在BilibiliAdapter覆盖一个自带 header 规则与 csrf 的uploadImage(blob)。在本 PR 的 data URI 方案下,CLI 不再走该路径,故非必需。