Skip to content

Commit 1b5b314

Browse files
committed
fix: Node file checks
1 parent 49d82c6 commit 1b5b314

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/files/NodeFilesHandler.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,33 @@ export class NodeFilesHandler extends BaseFilesHandler {
2222
async prepareFormDataRequest(file: FilePathOrFileObject): Promise<FormDataRequest> {
2323
console.log('Preparing form data request for Node.js');
2424
try {
25-
const FormData = await import('form-data').then((m) => m.default || m);
25+
const FormData = await import('form-data').then(m => m.default || m);
2626
console.log('Successfully imported form-data module');
27-
27+
2828
const formData = new FormData();
2929
console.log('Created new FormData instance');
3030

3131
if (typeof file === 'string') {
32-
const fs = await import('fs').then((m) => m.default || m);
32+
const fs = await import('fs').then(m => m.default || m);
3333
if (!fs.existsSync(file)) {
3434
throw new Error(`File not found: ${file}`);
3535
}
3636
console.log(`Appending file from path: ${file}`);
3737
formData.append('file', fs.createReadStream(file), { filename: file.split('/').pop() });
38-
} else if (file instanceof File) {
39-
console.log('Converting ReadableStream to Node stream');
40-
const nodeStream = await this.convertReadableStream(file.stream());
41-
console.log('Appending file from File instance');
42-
formData.append('file', nodeStream, file.name);
38+
} else if (file && typeof file === 'object') {
39+
console.log('Processing file object:', file);
40+
if ('buffer' in file) {
41+
// Handle Node.js file-like object
42+
console.log('Appending file from buffer');
43+
formData.append('file', file.buffer, { filename: file.name, contentType: file.type });
44+
} else if ('stream' in file && typeof file.stream === 'function') {
45+
// Handle File object
46+
console.log('Converting and appending file from stream');
47+
const nodeStream = await this.convertReadableStream(file.stream());
48+
formData.append('file', nodeStream, { filename: file.name, contentType: file.type });
49+
} else {
50+
throw new Error(`Invalid file object structure: ${JSON.stringify(file)}`);
51+
}
4352
} else {
4453
throw new Error(`Unsupported file type for Node.js file upload flow: ${file}`);
4554
}

0 commit comments

Comments
 (0)