feat: mac 档案与导出支持加载原图原视频

This commit is contained in:
Wxw-Gu committed 2026-12-01 10:40:58 +08:00
1 parent fccca5b459
commit b20e514c90
31 files changed
+1884 -111

No files matched your search

+204 -1
View File
@@ -2,7 +2,14 @@ import { execFile, execFileSync, spawn, type ChildProcess } from 'child_process'
import { createECDH, createHash } from 'node:crypto'
import { homedir } from 'node:os'
import { promisify } from 'node:util'
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
import {
chmodSync,
existsSync,
mkdirSync,
readFileSync,
renameSync,
writeFileSync
} from 'fs'
import { get, request as httpRequest } from 'http'
import { join } from 'path'
@@ -1253,6 +1260,202 @@ export class MacWechatRuntimeManager {
}
}
/**
* 调用原生运行时的 `POST /downloadOriginalImage`,让微信进程用原生下载器下载并解密原图。
*/
async downloadOriginalImage(payload: {
xml?: string
fileid?: string
aeskey?: string
md5?: string
file_type?: number
save_path: string
task_id?: string
timeout_ms?: number
}): Promise<{
success: boolean
task_id?: string
status?: string
file_path?: string
file_type?: string
file_size?: number
md5?: string
width?: number
height?: number
is_original?: boolean
byte_identical?: boolean
verification?: string
rendition?: string
error_code?: string
error?: string
}> {
if (process.platform !== 'darwin') {
return {
success: false,
status: 'failed',
error_code: 'PLATFORM_UNSUPPORTED',
error: '当前仅支持 macOS 微信原图下载'
}
}
if (this.lifecycle !== 'online') {
const snapshot = await this.ensureStarted({ allowManualRestart: true })
if (snapshot.lifecycle !== 'online' && !(await this.ping())) {
return {
success: false,
status: 'failed',
error_code: 'RUNTIME_OFFLINE',
error: snapshot.error || '微信原生运行时未在线,无法下载原图'
}
}
}
let binding = (await this.getBindingStatus()) || (await this.refreshBinding().catch(() => null))
if (!binding?.bound || !binding?.sessionAttached || !binding?.scriptLoaded) {
const bindResult = await this.bind()
if (!bindResult.ok) {
const fallbackBind = await this.request('POST', '/bindWeChat')
binding = await this.refreshBinding().catch(() => null)
if (
fallbackBind.status !== 200 ||
!binding?.bound ||
!binding?.sessionAttached ||
!binding?.scriptLoaded
) {
return {
success: false,
status: 'failed',
error_code: bindResult.state || 'BIND_FAILED',
error: bindResult.message || '绑定微信失败,无法下载原图'
}
}
} else {
binding = (await this.getBindingStatus()) || (await this.refreshBinding().catch(() => null))
}
}
if (binding && !binding.wechatRunning) {
return {
success: false,
status: 'failed',
error_code: 'WECHAT_NOT_RUNNING',
error: '微信未运行,请先启动并登录微信'
}
}
const timeoutMs =
typeof payload.timeout_ms === 'number' && payload.timeout_ms > 0
? payload.timeout_ms
: 180_000
const { status, body } = await this.request(
'POST',
'/downloadOriginalImage',
timeoutMs + 10_000,
JSON.stringify({ ...payload, timeout_ms: timeoutMs })
)
if (status === null || !body) {
return {
success: false,
status: 'failed',
error_code: 'HOST_UNREACHABLE',
error: '原图下载服务无响应,请检查微信发送组件状态'
}
}
try {
const parsed = JSON.parse(body) as {
success?: boolean
task_id?: string
status?: string
file_path?: string
file_type?: string
file_size?: number
md5?: string
width?: number
height?: number
is_original?: boolean
byte_identical?: boolean
verification?: string
rendition?: string
error_code?: string
error?: string | { code?: string; message?: string }
}
const errObj = parsed.error && typeof parsed.error === 'object' ? parsed.error : undefined
return {
...parsed,
success: Boolean(parsed.success),
error_code: parsed.error_code || errObj?.code,
error: typeof parsed.error === 'string' ? parsed.error : errObj?.message
}
} catch {
return {
success: false,
status: 'failed',
error_code: 'BAD_RESPONSE',
error: `媒体下载响应无法解析(HTTP ${status})`
}
}
}
/**
* 复用 `/downloadOriginalImage`(`file_type=4`)取视频,再 `rename` 成 `target_file_name`。
* `rename` 要求与 host 落盘目录同卷,跨卷抛 EXDEV,调用方需保证同盘。
*/
async downloadVideo(payload: {
fileid: string
aeskey: string
md5?: string
save_dir: string
target_file_name?: string
task_id?: string
timeout_ms?: number
}): Promise<{
success: boolean
task_id?: string
status?: string
file_path?: string
file_size?: number
md5?: string
error_code?: string
error?: string
}> {
mkdirSync(payload.save_dir, { recursive: true })
const result = await this.downloadOriginalImage({
fileid: payload.fileid,
aeskey: payload.aeskey,
...(payload.md5 ? { md5: payload.md5 } : {}),
file_type: 4,
save_path: payload.save_dir,
task_id: payload.task_id,
timeout_ms: payload.timeout_ms
})
if (!result.success || !result.file_path || !existsSync(result.file_path)) {
return {
success: false,
task_id: result.task_id,
status: result.status || 'failed',
error_code: result.error_code || 'DOWNLOAD_FAILED',
error: result.error || '视频下载失败'
}
}
let finalPath = result.file_path
const targetName = String(payload.target_file_name || '').trim()
if (targetName) {
const targetPath = join(payload.save_dir, targetName)
if (targetPath !== result.file_path) {
renameSync(result.file_path, targetPath)
finalPath = targetPath
}
}
return {
success: true,
task_id: result.task_id,
status: result.status || 'completed',
file_path: finalPath,
file_size: result.file_size,
md5: result.md5
}
}
/*
* 重新加载发送组件:优雅停掉当前 host 进程,再启动一个新实例。
* 用于:更新二进制后换新代码;或 host 卡死时的手动恢复。