feat(flash-transfer): let create_flash_task set per-file names
upload_file_stream now returns file_name with the original name.
create_flash_task accepts { file, name } so a streamed path can
keep the real filename on the fileset.
Fixes #361
This commit is contained in:
8 files changed
+170
-28
No files matched your search
@@ -2209,3 +2209,46 @@ describe('extended-actions / get_fileset_id', () => {
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 100 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('extended-actions / create_flash_task (#361)', () => {
|
||||
it('accepts a path string', async () => {
|
||||
const createFlashTask = vi.fn(async () => ({ filesetId: 'fs-1' }));
|
||||
const bridge = fakeBridge({ apis: { flashTransfer: { createFlashTask } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('create_flash_task', { files: '/tmp/a.pdf' });
|
||||
expect(res).toMatchObject({ status: 'ok', data: { fileset_id: 'fs-1', task_id: 'fs-1' } });
|
||||
expect(createFlashTask).toHaveBeenCalledWith([{ file: '/tmp/a.pdf' }], undefined, undefined);
|
||||
});
|
||||
|
||||
it('accepts { file, name } and forwards the per-file name', async () => {
|
||||
const createFlashTask = vi.fn(async () => ({ filesetId: 'fs-2' }));
|
||||
const bridge = fakeBridge({ apis: { flashTransfer: { createFlashTask } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('create_flash_task', {
|
||||
files: { file: '/tmp/uuid__a.pdf', name: 'a.pdf' },
|
||||
name: '卡片',
|
||||
});
|
||||
expect(res.status).toBe('ok');
|
||||
expect(createFlashTask).toHaveBeenCalledWith(
|
||||
[{ file: '/tmp/uuid__a.pdf', name: 'a.pdf' }],
|
||||
'卡片',
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects an empty files value', async () => {
|
||||
const createFlashTask = vi.fn(async () => ({ filesetId: 'fs' }));
|
||||
const bridge = fakeBridge({ apis: { flashTransfer: { createFlashTask } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('create_flash_task', { files: [] });
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 1400, wording: 'files must not be empty' });
|
||||
expect(createFlashTask).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects a non-string per-file name', async () => {
|
||||
const createFlashTask = vi.fn(async () => ({ filesetId: 'fs' }));
|
||||
const bridge = fakeBridge({ apis: { flashTransfer: { createFlashTask } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('create_flash_task', {
|
||||
files: { file: '/tmp/a.pdf', name: 1 },
|
||||
});
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 1400, wording: 'files[].name must be a string' });
|
||||
expect(createFlashTask).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -42,7 +42,10 @@ describe('upload_file_stream', () => {
|
||||
|
||||
r = await handleUpload(mk({ stream_id: id, total_chunks: 3, is_complete: true, expected_sha256: sha }));
|
||||
const data = r.data as Record<string, unknown>;
|
||||
expect(data).toMatchObject({ type: 'response', status: 'file_complete', total_chunks: 3, file_size: full.length, sha256: sha });
|
||||
expect(data).toMatchObject({
|
||||
type: 'response', status: 'file_complete', total_chunks: 3,
|
||||
file_size: full.length, sha256: sha, file_name: `upload_${id}`,
|
||||
});
|
||||
expect(fs.readFileSync(String(data.file_path), 'utf8')).toBe(full);
|
||||
// state dropped after completion, chunk dir gone
|
||||
expect(uploads.has(id)).toBe(false);
|
||||
@@ -97,6 +100,17 @@ describe('upload_file_stream', () => {
|
||||
await expect(handleUpload(mk({ stream_id: 'nope', verify_only: true }))).rejects.toThrow(/not found/i);
|
||||
});
|
||||
|
||||
it('returns the original filename alongside the prefixed file_path (#361)', async () => {
|
||||
const id = 'up-named';
|
||||
await handleUpload(mk({
|
||||
stream_id: id, total_chunks: 1, chunk_index: 0, chunk_data: b64('x'), filename: 'report.pdf',
|
||||
}));
|
||||
const r = await handleUpload(mk({ stream_id: id, total_chunks: 1, is_complete: true }));
|
||||
const data = r.data as Record<string, unknown>;
|
||||
expect(data.file_name).toBe('report.pdf');
|
||||
expect(String(data.file_path)).toBe(path.join(STREAM_UPLOAD_DIR, `${id}__report.pdf`));
|
||||
});
|
||||
|
||||
it('strips path separators from filename (no temp-dir escape)', async () => {
|
||||
const id = 'up-escape';
|
||||
await handleUpload(mk({ stream_id: id, total_chunks: 1, chunk_index: 0, chunk_data: b64('x'), filename: '../../evil.sh' }));
|
||||
|
||||
Reference in new issue
Block a user