fix(onebot): percent-decode file URL basenames on upload (#354)

send_group_msg / send_private_msg with a file segment that only has
file:// (no name/filename) used url.split('/').pop() as the upload
name, so percent-encoded CJK paths uploaded as
%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx. Derive the fallback through
guessFileNameFromUrl, which decodeURIComponent's the path basename
(invalid encodings are left as-is). An explicit name still wins.

Fixes #354
This commit is contained in:
DrSmoothl committed 2026-08-13 11:02:02 +08:00
1 parent 5c2f620c54
commit 2f5065545a
5 files changed
+81 -5

No files matched your search

+11 -1
View File
@@ -3,7 +3,7 @@ import os from 'os';
import path from 'path';
import { pathToFileURL } from 'url';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { loadBinarySource, resolveLocalFilePath } from '@snowluma/protocol/highway/utils';
import { guessFileNameFromUrl, loadBinarySource, resolveLocalFilePath } from '@snowluma/protocol/highway/utils';
describe('highway source paths', () => {
it('normalizes file URLs with an extra leading slash on POSIX', () => {
@@ -12,6 +12,16 @@ describe('highway source paths', () => {
.toBe('/AstrBot/data/plugin/cache/BV-test.mp4');
});
it('percent-decodes a URL basename used as the upload filename (#354)', () => {
expect(guessFileNameFromUrl(
'file:///D:/test/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx',
)).toBe('测试文件.xlsx');
expect(guessFileNameFromUrl('https://cdn.example/a/%E4%B8%AD%E6%96%87.png?x=1'))
.toBe('中文.png');
expect(guessFileNameFromUrl('https://cdn.example/plain.bin')).toBe('plain.bin');
expect(guessFileNameFromUrl('https://cdn.example/%ZZ.xlsx')).toBe('%ZZ.xlsx');
});
it('loads encoded file URLs from the local filesystem', async () => {
const filePath = path.join(os.tmpdir(), `snowluma video ${process.pid}-${Date.now()}.txt`);
fs.writeFileSync(filePath, 'ok');