@@ -22,24 +22,33 @@ export class NodeFilesHandler extends BaseFilesHandler {
22
22
async prepareFormDataRequest ( file : FilePathOrFileObject ) : Promise < FormDataRequest > {
23
23
console . log ( 'Preparing form data request for Node.js' ) ;
24
24
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 ) ;
26
26
console . log ( 'Successfully imported form-data module' ) ;
27
-
27
+
28
28
const formData = new FormData ( ) ;
29
29
console . log ( 'Created new FormData instance' ) ;
30
30
31
31
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 ) ;
33
33
if ( ! fs . existsSync ( file ) ) {
34
34
throw new Error ( `File not found: ${ file } ` ) ;
35
35
}
36
36
console . log ( `Appending file from path: ${ file } ` ) ;
37
37
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
+ }
43
52
} else {
44
53
throw new Error ( `Unsupported file type for Node.js file upload flow: ${ file } ` ) ;
45
54
}
0 commit comments