fix onebot video file sending

This commit is contained in:
DrSmoothl committed 2026-05-11 23:26:45 +08:00
1 parent e917720d9d
commit 6f506ff52c
8 files changed
+145 -48

No files matched your search

+2 -1
View File
@@ -470,7 +470,7 @@ export class Bridge {
async sendPrivateMessage(userUin: number, elements: MessageElement[]): Promise<SendMessageReceipt> {
if (elements.length === 0) throw new Error('message is empty');
// Resolve UID for image upload (C++ resolves UID before building elems)
// Resolve UID for media upload and the final c2c routing head.
let userUid = '';
const hasMedia = elements.some(e => e.type === 'image' || e.type === 'record' || e.type === 'video');
if (hasMedia) {
@@ -485,6 +485,7 @@ export class Bridge {
routingHead: {
c2c: {
uin: userUin,
...(userUid ? { uid: userUid } : {}),
},
} as any,
contentHead: {
+2 -19
View File
@@ -21,7 +21,7 @@ import {
NTV2UploadRichMediaRespSchema,
EncodableMediaMsgInfoSchema,
} from '../proto/highway';
import { loadBinarySource, computeHashes } from './utils';
import { loadBinarySource, computeHashes, resolveLocalFilePath } from './utils';
import {
fetchHighwaySession,
uploadHighwayHttp,
@@ -49,23 +49,6 @@ interface PttPayload {
cleanups: Array<() => void>;
}
/**
* Decide whether `source` looks like a local filesystem path. The shape
* matches the heuristics used inside `loadBinarySource`: we only reuse the
* caller-supplied path verbatim for things the addon can read directly.
*/
function asLocalFilePath(source: string): string | null {
if (!source) return null;
if (source.startsWith('base64://')) return null;
if (source.startsWith('http://') || source.startsWith('https://')) return null;
let filePath = source;
if (filePath.startsWith('file://')) filePath = filePath.slice(7);
if (/^\/[a-zA-Z]:/.test(filePath)) filePath = filePath.slice(1);
return filePath;
}
async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPayload> {
const source = element.url || element.fileId || '';
if (!source) throw new Error('record source is empty');
@@ -87,7 +70,7 @@ async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPay
// Resolve the on-disk path the addon should read. Anything that isn't
// already a local file (base64, HTTP) gets staged into the temp dir.
let inputPath: string;
const local = asLocalFilePath(source);
const local = resolveLocalFilePath(source);
if (local && fs.existsSync(local)) {
inputPath = local;
} else {
+33 -7
View File
@@ -4,6 +4,7 @@
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
export interface LoadedBinary {
bytes: Uint8Array;
@@ -25,14 +26,42 @@ export interface ImageFormat {
// --- Binary source loading ---
export function resolveLocalFilePath(source: string): string | null {
if (!source) return null;
if (/^base64:\/\//i.test(source)) return null;
if (/^https?:\/\//i.test(source)) return null;
let filePath = source;
if (/^file:\/\//i.test(source)) {
try {
filePath = fileURLToPath(source);
} catch {
filePath = source.replace(/^file:\/+/i, '/');
try {
filePath = decodeURIComponent(filePath);
} catch {
// Keep the original fallback path when percent decoding fails.
}
}
if (process.platform !== 'win32' && filePath.startsWith('//')) {
filePath = filePath.replace(/^\/+/, '/');
}
}
// Windows-style paths can arrive from file:///C:/... as /C:/...
if (/^\/[a-zA-Z]:/.test(filePath)) filePath = filePath.slice(1);
return filePath;
}
export async function loadBinarySource(source: string, resourceName: string): Promise<LoadedBinary> {
if (!source) throw new Error(`${resourceName} source is empty`);
if (source.startsWith('base64://')) {
if (/^base64:\/\//i.test(source)) {
return { bytes: Buffer.from(source.slice(9), 'base64'), fileName: '' };
}
if (source.startsWith('http://') || source.startsWith('https://')) {
if (/^https?:\/\//i.test(source)) {
const resp = await fetch(source);
if (!resp.ok) throw new Error(`HTTP download failed: ${resp.status}`);
const bytes = new Uint8Array(await resp.arrayBuffer());
@@ -40,11 +69,8 @@ export async function loadBinarySource(source: string, resourceName: string): Pr
return { bytes, fileName };
}
// File path (strip file:// prefix)
let filePath = source;
if (filePath.startsWith('file://')) filePath = filePath.slice(7);
// Windows: strip leading / before drive letter
if (/^\/[a-zA-Z]:/.test(filePath)) filePath = filePath.slice(1);
const filePath = resolveLocalFilePath(source);
if (!filePath) throw new Error(`${resourceName} source is not a local file`);
const bytes = fs.readFileSync(filePath);
const fileName = path.basename(filePath);
@@ -26,6 +26,7 @@ import {
loadBinarySource,
computeHashes,
detectImageFormat,
resolveLocalFilePath,
} from './utils';
import {
fetchHighwaySession,
@@ -212,18 +213,6 @@ function computeVideoSha1Blocks(bytes: Uint8Array): Uint8Array[] {
return blocks;
}
function asLocalFilePath(source: string): string | null {
if (!source) return null;
if (source.startsWith('base64://')) return null;
if (source.startsWith('http://') || source.startsWith('https://')) return null;
let filePath = source;
if (filePath.startsWith('file://')) filePath = filePath.slice(7);
if (/^\/[a-zA-Z]:/.test(filePath)) filePath = filePath.slice(1);
return filePath;
}
function defaultVideoTempDir(): string {
return path.join(os.tmpdir(), 'snowluma-video');
}
@@ -232,7 +221,7 @@ function sourceExtension(fileName: string, source: string): string {
const fromName = path.extname(fileName);
if (fromName) return fromName;
const local = asLocalFilePath(source);
const local = resolveLocalFilePath(source);
const fromSource = local ? path.extname(local) : '';
return fromSource || '.mp4';
}
@@ -250,7 +239,7 @@ async function stageVideoSource(element: MessageElement, tempDir: string, cleanu
const source = element.url || element.fileId || '';
if (!source) throw new Error('video source is empty');
const local = asLocalFilePath(source);
const local = resolveLocalFilePath(source);
if (local && fs.existsSync(local)) {
return {
bytes: new Uint8Array(fs.readFileSync(local)),