-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmedia.ts
More file actions
412 lines (364 loc) · 15 KB
/
media.ts
File metadata and controls
412 lines (364 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Media handling: AES encrypt/decrypt, CDN download/upload, voice synthesis.
*/
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import {
DIR,
CHANNEL_VERSION,
CDN_DOWNLOAD_URL,
CDN_UPLOAD_URL,
MEDIA_DIR,
log,
logError,
} from "./config.js";
import type { MessageItem, DownloadedMedia } from "./types.js";
import {
MSG_TYPE_BOT,
MSG_STATE_FINISH,
MSG_ITEM_TEXT,
UPLOAD_MEDIA_TYPE,
} from "./types.js";
import { apiFetch, generateClientId } from "./ilink-api.js";
// ── AES Helpers ──────────────────────────────────────────────────────────────
export function decryptAes128Ecb(ciphertext: Buffer, key: Buffer): Buffer {
const decipher = crypto.createDecipheriv("aes-128-ecb", key, null);
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
}
export function encryptAesEcb(plaintext: Buffer, key: Buffer): Buffer {
const cipher = crypto.createCipheriv("aes-128-ecb", key, null);
return Buffer.concat([cipher.update(plaintext), cipher.final()]);
}
export function parseAesKey(hexOrBase64: string): Buffer {
// Direct hex key (32 chars = 16 bytes)
if (/^[0-9a-fA-F]{32}$/.test(hexOrBase64)) {
return Buffer.from(hexOrBase64, "hex");
}
// Base64 encoded - decode first
const decoded = Buffer.from(hexOrBase64, "base64");
// Check if the decoded content is a hex string (base64(hex) pattern)
const decodedStr = decoded.toString("utf-8");
if (/^[0-9a-fA-F]{32}$/.test(decodedStr)) {
return Buffer.from(decodedStr, "hex");
}
// Raw binary key
return decoded.subarray(0, 16);
}
export function aesEcbPaddedSize(plaintextSize: number): number {
return Math.ceil((plaintextSize + 1) / 16) * 16;
}
// ── CDN Download ─────────────────────────────────────────────────────────────
export async function cdnDownload(encryptedQueryParam: string): Promise<Buffer> {
const url = `${CDN_DOWNLOAD_URL}?encrypted_query_param=${encodeURIComponent(encryptedQueryParam)}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 30_000);
try {
const res = await fetch(url, { signal: controller.signal });
clearTimeout(timer);
if (!res.ok) throw new Error(`CDN download failed: ${res.status}`);
return Buffer.from(await res.arrayBuffer());
} catch (err) {
clearTimeout(timer);
throw err;
}
}
// ── Image Extension Detection ────────────────────────────────────────────────
export function detectImageExt(buf: Buffer): string {
if (buf[0] === 0xff && buf[1] === 0xd8) return "jpg";
if (buf[0] === 0x89 && buf[1] === 0x50) return "png";
if (buf[0] === 0x47 && buf[1] === 0x49) return "gif";
if (buf[0] === 0x52 && buf[1] === 0x49) return "webp";
return "jpg";
}
// ── Media Download ───────────────────────────────────────────────────────────
export async function downloadMediaItem(item: MessageItem): Promise<DownloadedMedia | null> {
try {
const ts = Date.now();
// Image
if (item.type === 2 && item.image_item) {
const queryParam = item.image_item.media?.encrypt_query_param;
const keyStr = item.image_item.aeskey || item.image_item.media?.aes_key;
if (!queryParam || !keyStr) return null;
const encrypted = await cdnDownload(queryParam);
const key = parseAesKey(keyStr);
const decrypted = decryptAes128Ecb(encrypted, key);
const ext = detectImageExt(decrypted);
const fileName = `img_${ts}.${ext}`;
const filePath = path.join(MEDIA_DIR, fileName);
fs.writeFileSync(filePath, decrypted);
return { type: "image", filePath, fileName };
}
// File
if (item.type === 4 && item.file_item) {
const queryParam = item.file_item.media?.encrypt_query_param;
const keyStr = item.file_item.media?.aes_key;
if (!queryParam || !keyStr) return null;
const encrypted = await cdnDownload(queryParam);
const key = parseAesKey(keyStr);
const decrypted = decryptAes128Ecb(encrypted, key);
const origName = item.file_item.file_name || `file_${ts}`;
const fileName = `${ts}_${origName}`;
const filePath = path.join(MEDIA_DIR, fileName);
fs.writeFileSync(filePath, decrypted);
return { type: "file", filePath, fileName: origName };
}
// Video — download + ffmpeg frame extraction + audio extraction
if (item.type === 5 && item.video_item) {
const queryParam = item.video_item.media?.encrypt_query_param;
const keyStr = item.video_item.media?.aes_key;
if (!queryParam || !keyStr) return null;
const encrypted = await cdnDownload(queryParam);
const key = parseAesKey(keyStr);
const decrypted = decryptAes128Ecb(encrypted, key);
const fileName = `video_${ts}.mp4`;
const filePath = path.join(MEDIA_DIR, fileName);
fs.writeFileSync(filePath, decrypted);
const framesDir = path.join(MEDIA_DIR, `video_${ts}_frames`);
const framePaths: string[] = [];
try {
fs.mkdirSync(framesDir, { recursive: true });
const { execFileSync } = await import("node:child_process");
let duration = 0;
try {
const out = execFileSync(
"ffprobe",
["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", filePath],
{ encoding: "utf-8", timeout: 10_000 },
).trim();
duration = parseFloat(out) || 0;
} catch {}
if (duration > 0) {
const frameCount = Math.min(4, Math.max(1, Math.floor(duration)));
const interval = duration / (frameCount + 1);
for (let i = 1; i <= frameCount; i++) {
const seekTo = (interval * i).toFixed(2);
const fp = path.join(framesDir, `frame_${i}.jpg`);
try {
execFileSync(
"ffmpeg",
["-y", "-ss", seekTo, "-i", filePath, "-vframes", "1", "-q:v", "2", fp],
{ timeout: 15_000, stdio: "pipe" },
);
framePaths.push(fp);
} catch {}
}
log(`📎 视频抽帧: ${framePaths.length} 帧 → ${framesDir}`);
}
// Extract audio
const audioFile = path.join(MEDIA_DIR, `video_${ts}_audio.wav`);
try {
execFileSync(
"ffmpeg",
["-y", "-i", filePath, "-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1", audioFile],
{ timeout: 15_000, stdio: "pipe" },
);
log(`📎 视频音频: ${audioFile}`);
} catch {}
} catch (err) {
logError(`视频处理失败: ${String(err)}`);
}
return { type: "video", filePath, fileName };
}
// Voice (download audio file)
if (item.type === 3 && item.voice_item?.media) {
const queryParam = item.voice_item.media.encrypt_query_param;
const keyStr = item.voice_item.media.aes_key;
if (!queryParam || !keyStr) return null;
const encrypted = await cdnDownload(queryParam);
const key = parseAesKey(keyStr);
const decrypted = decryptAes128Ecb(encrypted, key);
const fileName = `voice_${ts}.silk`;
const filePath = path.join(MEDIA_DIR, fileName);
fs.writeFileSync(filePath, decrypted);
return { type: "voice", filePath, fileName };
}
} catch (err) {
logError(`媒体下载失败: ${String(err)}`);
// Tier 2: log failed media item
try {
const entry = { ts: new Date().toISOString(), type: "media_failed", item_type: item.type, error: String(err), item };
fs.appendFileSync(path.join(DIR, "unhandled.jsonl"), JSON.stringify(entry) + "\n", "utf-8");
} catch {}
}
return null;
}
// ── MIME Type Detection ──────────────────────────────────────────────────────
export function getMimeType(filePath: string): string {
const ext = path.extname(filePath).toLowerCase();
const mimes: Record<string, string> = {
".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png",
".gif": "image/gif", ".webp": "image/webp", ".bmp": "image/bmp",
".mp4": "video/mp4", ".mov": "video/quicktime", ".avi": "video/x-msvideo",
".pdf": "application/pdf", ".doc": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".zip": "application/zip", ".txt": "text/plain", ".md": "text/markdown",
};
return mimes[ext] || "application/octet-stream";
}
// ── Media Upload + Send ──────────────────────────────────────────────────────
export async function uploadAndSendMedia(
baseUrl: string,
token: string,
to: string,
filePath: string,
contextToken: string,
): Promise<void> {
const plaintext = fs.readFileSync(filePath);
const rawsize = plaintext.length;
const rawfilemd5 = crypto.createHash("md5").update(plaintext).digest("hex");
const filesize = aesEcbPaddedSize(rawsize);
const filekey = crypto.randomBytes(16).toString("hex");
const aeskey = crypto.randomBytes(16);
// Determine upload media type and message item type
const mime = getMimeType(filePath);
let uploadMediaType: number;
let itemType: number;
if (mime.startsWith("image/")) {
uploadMediaType = UPLOAD_MEDIA_TYPE.IMAGE;
itemType = 2; // MSG_ITEM_IMAGE
} else if (mime.startsWith("video/")) {
uploadMediaType = UPLOAD_MEDIA_TYPE.VIDEO;
itemType = 5; // MSG_ITEM_VIDEO
} else {
uploadMediaType = UPLOAD_MEDIA_TYPE.FILE;
itemType = 4; // MSG_ITEM_FILE
}
log(`📤 上传: ${path.basename(filePath)} (${rawsize} bytes, type=${itemType})`);
// Step 1: Get upload URL
const uploadUrlResp = await apiFetch({
baseUrl,
endpoint: "ilink/bot/getuploadurl",
body: JSON.stringify({
filekey,
media_type: uploadMediaType,
to_user_id: to,
rawsize,
rawfilemd5,
filesize,
no_need_thumb: true,
aeskey: aeskey.toString("hex"),
base_info: { channel_version: CHANNEL_VERSION },
}),
token,
timeoutMs: 15_000,
});
const uploadUrlData = JSON.parse(uploadUrlResp.text) as { upload_param?: string };
const uploadParam = uploadUrlData.upload_param;
if (!uploadParam) {
throw new Error("getuploadurl 未返回 upload_param");
}
// Step 2: Encrypt and upload to CDN
const ciphertext = encryptAesEcb(plaintext, aeskey);
const cdnUrl = `${CDN_UPLOAD_URL}?encrypted_query_param=${encodeURIComponent(uploadParam)}&filekey=${encodeURIComponent(filekey)}`;
const cdnRes = await fetch(cdnUrl, {
method: "POST",
headers: { "Content-Type": "application/octet-stream" },
body: new Uint8Array(ciphertext),
});
if (!cdnRes.ok) {
throw new Error(`CDN 上传失败: ${cdnRes.status}`);
}
const downloadParam = cdnRes.headers.get("x-encrypted-param");
if (!downloadParam) {
throw new Error("CDN 响应缺少 x-encrypted-param header");
}
// Step 3: Send message with media reference
const aesKeyBase64 = Buffer.from(aeskey.toString("hex")).toString("base64");
const mediaRef = {
encrypt_query_param: downloadParam,
aes_key: aesKeyBase64,
encrypt_type: 1,
};
let mediaItem: Record<string, any>;
if (itemType === 2) {
mediaItem = { type: 2, image_item: { media: mediaRef, mid_size: filesize } };
} else if (itemType === 5) {
mediaItem = { type: 5, video_item: { media: mediaRef, video_size: filesize } };
} else {
mediaItem = {
type: 4,
file_item: { media: mediaRef, file_name: path.basename(filePath), len: String(rawsize) },
};
}
await apiFetch({
baseUrl,
endpoint: "ilink/bot/sendmessage",
body: JSON.stringify({
msg: {
from_user_id: "",
to_user_id: to,
client_id: generateClientId(),
message_type: MSG_TYPE_BOT,
message_state: MSG_STATE_FINISH,
item_list: [mediaItem],
context_token: contextToken,
},
base_info: { channel_version: CHANNEL_VERSION },
}),
token,
timeoutMs: 15_000,
});
log(`📤 发送成功: ${path.basename(filePath)}`);
}
// ── Content Extraction ───────────────────────────────────────────────────────
export async function extractContent(msg: { item_list?: MessageItem[] }): Promise<string> {
if (!msg.item_list?.length) return "";
const parts: string[] = [];
for (const item of msg.item_list) {
// Text
if (item.type === MSG_ITEM_TEXT && item.text_item?.text) {
let text = item.text_item.text;
const refText = item.ref_msg?.title || item.ref_msg?.message_item?.text_item?.text;
if (refText) {
text = `[引用: ${refText}]\n${text}`;
}
parts.push(text);
continue;
}
// Voice with ASR text
if (item.type === 3 && item.voice_item?.text) {
parts.push(`[语音] ${item.voice_item.text}`);
continue;
}
// Media: try to download
if (item.type === 2 || item.type === 3 || item.type === 4 || item.type === 5) {
const media = await downloadMediaItem(item);
if (media) {
const labels: Record<string, string> = {
image: "图片",
voice: "语音文件",
file: "文件",
video: "视频",
};
parts.push(`[${labels[media.type]}] 已保存到 ${media.filePath}`);
log(`📎 ${labels[media.type]}: ${media.filePath}`);
// For videos, list extracted frames so Claude can read them
if (media.type === "video") {
const framesDir = media.filePath.replace(".mp4", "_frames");
try {
const frames = fs.readdirSync(framesDir).filter((f: string) => f.endsWith(".jpg")).sort();
if (frames.length > 0) {
parts.push(`[视频关键帧] 共 ${frames.length} 帧:`);
for (const frame of frames) {
parts.push(` ${path.join(framesDir, frame)}`);
}
}
} catch {}
// Check for extracted audio
const audioFile = media.filePath.replace(".mp4", "_audio.wav");
if (fs.existsSync(audioFile)) {
parts.push(`[视频音频] ${audioFile}`);
}
}
} else {
const typeNames: Record<number, string> = { 2: "图片", 3: "语音", 4: "文件", 5: "视频" };
parts.push(`[${typeNames[item.type] || "未知媒体"}] (无法下载)`);
}
continue;
}
}
return parts.join("\n");
}