fix: 补充mac发送的类型

修复图片key
This commit is contained in:
Wxw-Gu committed 2026-11-18 17:42:17 +08:00
1 parent f16067d6d0
commit 5ca465f203
15 files changed
+338 -27

No files matched your search

@@ -55,7 +55,12 @@ export class ImageKeyConfigService {
return migrated.success ? this.getConfig() : legacy
}
const envAesKey = String(import.meta.env.VITE_IMAGE_AES_KEY || '').trim()
// 只有 dev 构建读 .env:生产包里带上开发者的 key,会让所有用户拿同一个人的密钥
// 去解自己的图,直接表现为「图片解密失败」。VITE_* 是编译期字面量,不加这道闸
// 就会被打进 app.asar。
const envAesKey = import.meta.env.DEV
? String(import.meta.env.VITE_IMAGE_AES_KEY || '').trim()
: ''
if (!settings.imageKeyFallbackDisabled && envAesKey) {
return this.buildResult({
source: 'environment',
@@ -188,7 +188,7 @@ export function deriveMacWechatSenderStatus({
}
const runtimeCapabilities = new Set(manifest.capabilities)
const supportsCapability = (name: 'text' | 'image' | 'voice'): boolean => {
const supportsCapability = (name: 'text' | 'image' | 'voice' | 'file'): boolean => {
if (manifest.capabilitiesByVersion) return versionCapabilities?.[name] === 'verified'
return runtimeCapabilities.has(name)
}
@@ -205,6 +205,10 @@ export function deriveMacWechatSenderStatus({
* 真正的发送结果仍以 Host 返回为准,不在 UI 层假装支持。
*/
const canSendVideo = bindingReady && binding?.canSendImage === true
const canSendFile =
bindingReady &&
supportsCapability('file') &&
versionCapabilities?.textMechanism === 'tm-message-source-send'
const sessionAttached = identityMatches && (binding?.sessionAttached ?? false)
const scriptLoaded = identityMatches && (binding?.scriptLoaded ?? false)
@@ -228,11 +232,12 @@ export function deriveMacWechatSenderStatus({
imageHookInstalled: identityMatches && (binding?.canSendImage ?? false),
imageHookReady: identityMatches && (binding?.canSendImage ?? false),
messageListenerReady: scriptLoaded,
canSend: canSendText || canSendImage || canSendVoice || canSendVideo,
canSend: canSendText || canSendImage || canSendVoice || canSendVideo || canSendFile,
canSendText,
canSendImage,
canSendVoice,
canSendVideo,
canSendFile,
message,
error: state === 'error' ? message : undefined
}
@@ -38,7 +38,8 @@ export class PersonalWechatCapabilityService {
text: Boolean(senderStatus.canSendText),
image: Boolean(senderStatus.canSendImage),
voice: Boolean(senderStatus.canSendVoice),
video: Boolean(senderStatus.canSendVideo)
video: Boolean(senderStatus.canSendVideo),
file: Boolean(senderStatus.canSendFile)
}
const status = this.mapState(senderStatus)
const ready = status === 'ready'
@@ -11,7 +11,7 @@ import {
writeFileSync
} from 'fs'
import { homedir, tmpdir } from 'os'
import { delimiter, dirname, extname, join, sep } from 'path'
import { delimiter, dirname, extname, join, sep, basename } from 'path'
import { resolveBundledFfmpegPath } from '../ffmpeg-path'
import { promisify } from 'util'
import type {
@@ -36,6 +36,7 @@ const MAX_IMAGE_BYTES = 20 * 1024 * 1024
const MAX_VOICE_BYTES = 20 * 1024 * 1024
/** 视频与扩展发送通道的上限保持一致。 */
const MAX_VIDEO_BYTES = 100 * 1024 * 1024
const MAX_FILE_BYTES = 100 * 1024 * 1024
// 语音编码由 native runtime 内的 SILK 实现完成,不再是外部编码器进程。
const VOICE_ENCODER_NAME = 'silk'
const VOICE_ENCODER_VERSION = 'tm-wechat-native'
@@ -398,6 +399,66 @@ export function stageMacWechatVideoForSend(
}
}
const MAC_FILE_STAGE_PREFIX = 'tm-file-'
const MAC_FILE_STAGE_RETENTION_MS = 10 * 60 * 1000
export function stageMacWechatFileForSend(
filePath: string,
wechatFilesRoot = WECHAT_FILES_ROOT
): { stagedPath: string; cleanup: () => void } {
const normalized = String(filePath || '').trim()
const containerDataRoot = dirname(dirname(wechatFilesRoot))
if (
containerDataRoot.includes('com.tencent.xinWeChat') &&
normalized.startsWith(`${containerDataRoot}${sep}`)
) {
return { stagedPath: normalized, cleanup: () => {} }
}
const documentsDir = dirname(wechatFilesRoot)
const stageDir = existsSync(documentsDir) ? documentsDir : wechatFilesRoot
mkdirSync(stageDir, { recursive: true })
const now = Date.now()
try {
for (const entry of readdirSync(stageDir)) {
if (!entry.startsWith(MAC_FILE_STAGE_PREFIX)) continue
const candidate = join(stageDir, entry)
try {
if (now - statSync(candidate).mtimeMs > MAC_FILE_STAGE_RETENTION_MS) {
unlinkSync(candidate)
}
} catch {
// Best-effort stale file cleanup.
}
}
} catch {
// Ignore directory listing errors.
}
const extension = extname(normalized).toLowerCase()
const safeExtension = /^\.[a-z0-9]{1,12}$/.test(extension) ? extension : '.bin'
const stagedPath = join(
stageDir,
`${MAC_FILE_STAGE_PREFIX}${randomBytes(6).toString('hex')}-${now}${safeExtension}`
)
copyFileSync(normalized, stagedPath)
return {
stagedPath,
cleanup: () => {
const timer = setTimeout(() => {
try {
unlinkSync(stagedPath)
} catch {
// Best-effort delayed cleanup after WeChat finishes asynchronous upload.
}
}, 300_000)
timer.unref?.()
}
}
}
/** Locate the current account's temporary image directory consumed by the image send path. */
export function findWechatImagePath(
root = WECHAT_FILES_ROOT,
@@ -521,6 +582,13 @@ export function buildWindowsWechatRequest(
}
}
if (request.type === 'file') {
return {
endpoint: '/SendMsg',
body: { toWxid: target, type: 'file', msg: String(request.filePath || '').trim() }
}
}
const fromId = String(request.fromId || '').trim()
const filePath = String(options.filePath || request.filePath || '').trim()
const durationMs = options.durationMs ?? request.durationMs
@@ -558,6 +626,7 @@ function windowsStatusBase(host = windowsHookHost() || ''): PersonalWechatSender
canSendImage: false,
canSendVoice: false,
canSendVideo: false,
canSendFile: false,
message: '正在检查 Windows 微信消息接口'
}
}
@@ -687,6 +756,7 @@ export class PersonalWechatSendService {
if (request.type === 'image') return this.sendMacImage(request)
if (request.type === 'voice') return this.sendMacVoice(request)
if (request.type === 'video') return this.sendMacVideo(request)
if (request.type === 'file') return this.sendMacFile(request)
}
return {
success: false,
@@ -907,6 +977,56 @@ export class PersonalWechatSendService {
}
}
private async sendMacFile(
request: Extract<PersonalWechatSendRequest, { type: 'file' }>
): Promise<PersonalWechatSendResult> {
await macWechatRuntimeManager.ensureStarted()
const status = await macWechatRuntimeManager.buildSenderStatus()
if (!status.canSendFile) {
return {
success: false,
status,
error: status.message || '当前微信发送组件不支持文件,请升级后重试'
}
}
const filePath = String(request.filePath || '').trim()
if (!filePath || !existsSync(filePath)) {
return { success: false, status, error: '请选择有效的文件' }
}
const size = statSync(filePath).size
if (size <= 0 || size > MAX_FILE_BYTES) {
return { success: false, status, error: '文件必须小于 100 MB' }
}
const fileName = String(request.fileName || '').trim() || basename(filePath)
let staged: { stagedPath: string; cleanup: () => void } | null = null
try {
staged = stageMacWechatFileForSend(filePath, this.wechatFilesRoot)
} catch (error) {
const latest = await macWechatRuntimeManager.buildSenderStatus()
return {
success: false,
status: latest,
error: error instanceof Error ? error.message : '无法将文件暂存到微信沙盒目录'
}
}
try {
const result = await this.postMacHost(
'/sendMsg',
{ to: request.to, type: 'file', path: staged.stagedPath, name: fileName },
180_000
)
const latest = await macWechatRuntimeManager.buildSenderStatus()
if (!result?.ok) {
return { success: false, status: latest, error: result?.message || '文件发送失败' }
}
return { success: true, status: latest }
} finally {
staged.cleanup()
}
}
private async sendMacImage(
request: Extract<PersonalWechatSendRequest, { type: 'image' }>
): Promise<PersonalWechatSendResult> {
@@ -1100,6 +1220,10 @@ export class PersonalWechatSendService {
}
request = { ...request, to, text }
} else {
if (request.type === 'file') {
const status = await this.getWindowsStatus()
return { success: false, status, error: 'Windows 端暂不支持文件发送' }
}
const filePath = String(request.filePath || '').trim()
const label = request.type === 'voice' ? '语音' : request.type === 'video' ? '视频' : '图片'
const limit =
+39 -5
View File
@@ -443,7 +443,8 @@ export class WechatActionGateway {
(request.content.type !== 'text' &&
request.content.type !== 'image' &&
request.content.type !== 'video' &&
request.content.type !== 'voice')
request.content.type !== 'voice' &&
request.content.type !== 'file')
) {
return
}
@@ -593,9 +594,18 @@ export function normalizeActionRequest(value: unknown): WechatActionRequest | nu
content: { type: 'text', text, ...(at ? { at } : {}) }
} as WechatActionRequest
}
if (contentType !== 'image' && contentType !== 'video' && contentType !== 'voice') return null
if (
contentType !== 'image' &&
contentType !== 'video' &&
contentType !== 'voice' &&
contentType !== 'file'
) {
return null
}
const filePath = String((content as { path?: unknown }).path || '').trim()
if (!filePath) return null
const contentName =
contentType === 'file' ? String((content as { name?: unknown }).name || '').trim() : ''
return {
...input,
origin,
@@ -612,7 +622,10 @@ export function normalizeActionRequest(value: unknown): WechatActionRequest | nu
? { name: String((recipient as { name?: unknown }).name).trim() }
: {})
},
content: { type: contentType, path: filePath }
content:
contentType === 'file'
? { type: 'file', path: filePath, ...(contentName ? { name: contentName } : {}) }
: { type: contentType, path: filePath }
} as WechatActionRequest
}
@@ -655,6 +668,14 @@ function toPersonalWechatSendRequest(request: WechatActionRequest): PersonalWech
if (request.content.type === 'video') {
return { ...base, type: 'video', filePath: request.content.path }
}
if (request.content.type === 'file') {
return {
...base,
type: 'file',
filePath: request.content.path,
...(request.content.name ? { fileName: request.content.name } : {})
}
}
const metadata = request.metadata && typeof request.metadata === 'object' ? request.metadata : {}
const fromId = typeof metadata.fromId === 'string' ? metadata.fromId.trim() : ''
const durationMs =
@@ -700,7 +721,12 @@ function contentAudit(
content: WechatActionContent
): Pick<WechatActionAuditRecord, 'contentPreview' | 'contentHash'> {
const raw = content.type === 'text' ? content.text : content.path
const preview = content.type === 'text' ? raw : path.basename(raw)
const preview =
content.type === 'text'
? raw
: content.type === 'file' && content.name
? content.name
: path.basename(raw)
return {
contentPreview: preview.slice(0, MAX_CONTENT_PREVIEW_LENGTH),
contentHash: createHash('sha256').update(raw).digest('hex')
@@ -708,7 +734,15 @@ function contentAudit(
}
function contentTypeLabel(type: WechatActionContent['type']): string {
return type === 'text' ? '文字' : type === 'image' ? '图片' : type === 'video' ? '视频' : '语音'
return type === 'text'
? '文字'
: type === 'image'
? '图片'
: type === 'video'
? '视频'
: type === 'file'
? '文件'
: '语音'
}
function normalizeAuditRecords(values: unknown[]): WechatActionAuditRecord[] {