fix: 图片/文件读取改用 base64,修复 atob Latin1 错误

- 主进程新增 fs:readFileBase64 IPC,读 Buffer 直接返回 base64
- 图片读取从 atob 转换改为直接使用 readFileBase64
- .metona 备份导入同理修复
- 根因:fs:readFile 用 utf-8 编码读取二进制文件,UTF-8 多字节序列超出 Latin1 范围导致 atob 失败
This commit is contained in:
thzxx
2026-04-08 16:46:22 +08:00
parent 43f45cc09c
commit 21d774fab4
5 changed files with 18 additions and 8 deletions
+5 -6
View File
@@ -136,20 +136,19 @@ async function handleImagePaths(paths: string[]): Promise<void> {
for (const filePath of paths) {
const name = filePath.split(/[/\\]/).pop() || filePath;
try {
const result = await bridge.fs.readFile(filePath);
const result = await bridge.fs.readFileBase64(filePath);
if (!result.success) {
appendSystemMessage(`❌ 读取 ${name} 失败: ${result.error}`);
continue;
}
// IPC 返回的 content 是 UTF-8 字符串,转为 base64
const binary = Uint8Array.from(atob(result.content as string), c => c.charCodeAt(0));
const base64 = btoa(String.fromCharCode(...binary));
if (binary.length > maxSize) {
const size = result.size ?? 0;
if (size > maxSize) {
appendSystemMessage(`⚠️ 图片 ${name} 超过 20MB 限制`);
continue;
}
const base64 = result.content!;
pendingImages.push({ name, base64 });
logInfo(`图片已添加: ${name}`, formatSize(binary.length));
logInfo(`图片已添加: ${name}`, formatSize(size));
} catch (err) {
logError('图片读取失败', (err as Error).message);
appendSystemMessage(`❌ 读取 ${name} 失败`);