diff --git a/packages/core/tests/highway-utils.test.ts b/packages/core/tests/highway-utils.test.ts index bcbe5c2f..bd217ad9 100644 --- a/packages/core/tests/highway-utils.test.ts +++ b/packages/core/tests/highway-utils.test.ts @@ -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'); diff --git a/packages/onebot/src/modules/message-actions.ts b/packages/onebot/src/modules/message-actions.ts index fbd5db1d..f1674602 100644 --- a/packages/onebot/src/modules/message-actions.ts +++ b/packages/onebot/src/modules/message-actions.ts @@ -2,6 +2,7 @@ import { createLogger } from '@snowluma/common/logger'; import type { BridgeInterface } from '@snowluma/core/bridge-interface'; import type { ForwardNodePayload, FriendMessage, GroupMessage, MessageElement, MessageElementOf, QQEventVariant } from '@snowluma/protocol/events'; import { getVideoSourceSize, MAX_VIDEO_SIZE } from '@snowluma/protocol/highway/video-upload'; +import { guessFileNameFromUrl } from '@snowluma/protocol/highway/utils'; import type { MessageSendResult } from '../api-handler'; import { convertEvent, elementsToOneBotSegments, type ConverterContext } from '../event-converter'; import { segmentsToRawMessage } from '../helper/cq'; @@ -978,7 +979,7 @@ export async function sendPrivateMessage( const userUid = await ensureFileTargetUid(); for (const fileEl of allFileElements) { if (fileEl.url && !fileEl.fileId) { - const name = fileEl.fileName || fileEl.url.split('/').pop() || 'file'; + const name = fileEl.fileName || guessFileNameFromUrl(fileEl.url) || 'file'; // Upload and publish are separate here so the Action keeps the real // PbSendMsg receipt. Letting uploadPrivate publish internally discards // that receipt, which makes a reliable message_sent event impossible @@ -1109,7 +1110,7 @@ export async function sendGroupMessage( let fileId: string; if (fileEl.url && !fileEl.fileId) { // upload() already calls publish() internally — do NOT call publish() again. - const name = fileEl.fileName || fileEl.url.split('/').pop() || 'file'; + const name = fileEl.fileName || guessFileNameFromUrl(fileEl.url) || 'file'; const result = await ref.bridge.apis.groupFile.upload(groupId, fileEl.url, name, '/', true); fileId = result.fileId ?? ''; if (!fileId) throw new Error('group file auto-upload returned no file_id'); diff --git a/packages/onebot/tests/send-group-file.test.ts b/packages/onebot/tests/send-group-file.test.ts index 4e06d459..7af24910 100644 --- a/packages/onebot/tests/send-group-file.test.ts +++ b/packages/onebot/tests/send-group-file.test.ts @@ -295,4 +295,26 @@ describe('send_group_msg with {type:"file"} segment', () => { // publish must NOT be called — upload() already handles it internally expect(publish).not.toHaveBeenCalled(); }); + + it('percent-decodes a file:// URL basename when name is omitted (#354)', async () => { + const upload = vi.fn(async () => ({ fileId: 'auto-fid', fileHash: null })); + const bridge = fakeBridge({ + apis: { + message: { sendGroup: vi.fn() }, + groupFile: { upload, publish: vi.fn() }, + } as any, + resolveUserUid: vi.fn(), + } as any); + const ctx = makeCtx(bridge); + + await sendGroupMessage(ctx, 12345, [{ + type: 'file', + data: { file: 'file:///D:/test/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx' }, + }] as any, false); + + expect(upload).toHaveBeenCalledOnce(); + const [, src, name] = upload.mock.calls[0]!; + expect(src).toBe('file:///D:/test/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx'); + expect(name).toBe('测试文件.xlsx'); + }); }); diff --git a/packages/onebot/tests/send-private-c2c-file.test.ts b/packages/onebot/tests/send-private-c2c-file.test.ts index e3b2d6f8..63f217ef 100644 --- a/packages/onebot/tests/send-private-c2c-file.test.ts +++ b/packages/onebot/tests/send-private-c2c-file.test.ts @@ -336,4 +336,41 @@ describe('send_private_msg with {type:"file"} segment', () => { ); expect(sendC2cFile).toHaveBeenCalledOnce(); }); + + it('percent-decodes a file:// URL basename when name is omitted (#354)', async () => { + const uploadPrivate = vi.fn(async () => ({ fileId: 'auto-fid', fileHash: 'auto-hash' })); + const sendC2cFileMessage = vi.fn(async () => goodReceipt); + const bridge = fakeBridge({ + apis: { + message: { sendPrivate: vi.fn(), sendC2cFile: sendC2cFileMessage }, + groupFile: { uploadPrivate }, + } as any, + resolveUserUid: vi.fn(async () => 'u_peer'), + recallUploadedFile: vi.fn(() => ({ + fileId: 'auto-fid', + scope: 'private', + userId: 67890, + fileName: '测试文件.xlsx', + fileSize: 3, + fileMd5: new Uint8Array(16), + fileSha1: new Uint8Array(20), + fileHash: 'auto-hash', + rememberedAt: Date.now(), + })), + } as any); + const ctx = makeCtx(bridge); + + await sendPrivateMessage(ctx, 67890, [{ + type: 'file', + data: { file: 'file:///D:/test/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx' }, + }] as any, false); + + expect(uploadPrivate).toHaveBeenCalledWith( + 67890, + 'file:///D:/test/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.xlsx', + '测试文件.xlsx', + true, + false, + ); + }); }); diff --git a/packages/protocol/src/highway/utils.ts b/packages/protocol/src/highway/utils.ts index 9e785085..bb4a91b2 100644 --- a/packages/protocol/src/highway/utils.ts +++ b/packages/protocol/src/highway/utils.ts @@ -252,11 +252,17 @@ export async function loadBinarySource( return { bytes, fileName }; } -function guessFileNameFromUrl(url: string): string { +export function guessFileNameFromUrl(url: string): string { const queryPos = url.search(/[?#]/); const pathPart = queryPos >= 0 ? url.slice(0, queryPos) : url; const lastSlash = pathPart.lastIndexOf('/'); - return lastSlash >= 0 ? pathPart.slice(lastSlash + 1) : ''; + const raw = lastSlash >= 0 ? pathPart.slice(lastSlash + 1) : ''; + if (!raw) return ''; + try { + return decodeURIComponent(raw); + } catch { + return raw; + } } // --- Hashing ---