@@ -35,8 +35,52 @@ class PubNubFile {
3535 const contents = file . data ;
3636 fileMimeType = file . mimeType ;
3737 fileName = file . name ;
38- fileData = new File ( [ contents ] , fileName , { type : fileMimeType } ) ;
39- contentLength = fileData . size ;
38+ // create a ReadableFile wrapper for ArrayBuffer data
39+ if ( contents instanceof ArrayBuffer || ArrayBuffer . isView ( contents ) ) {
40+ // Create ReadableFile wrapper for ArrayBuffer
41+ const arrayBuffer = contents instanceof ArrayBuffer ? contents : contents . buffer ;
42+ contentLength = arrayBuffer . byteLength ;
43+ fileData = {
44+ arrayBuffer : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return arrayBuffer ; } ) ,
45+ blob : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
46+ throw new Error ( 'toBlob() is not supported in React Native environment. Use toArrayBuffer() instead.' ) ;
47+ } ) ,
48+ text : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
49+ const decoder = new TextDecoder ( ) ;
50+ return decoder . decode ( arrayBuffer ) ;
51+ } ) ,
52+ } ;
53+ }
54+ else if ( typeof contents === 'string' ) {
55+ // Handle string data
56+ const encoder = new TextEncoder ( ) ;
57+ const arrayBuffer = encoder . encode ( contents ) . buffer ;
58+ contentLength = arrayBuffer . byteLength ;
59+ fileData = {
60+ arrayBuffer : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return arrayBuffer ; } ) ,
61+ blob : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return new Blob ( [ contents ] , { type : fileMimeType } ) ; } ) ,
62+ text : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return contents ; } ) ,
63+ } ;
64+ }
65+ else if ( contents instanceof Blob ) {
66+ // Handle Blob data
67+ contentLength = contents . size ;
68+ fileData = {
69+ arrayBuffer : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return yield contents . arrayBuffer ( ) ; } ) ,
70+ blob : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return contents ; } ) ,
71+ text : ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) { return yield contents . text ( ) ; } ) ,
72+ } ;
73+ }
74+ else {
75+ // Fallback: Try to use File constructor (may still fail in some environments)
76+ try {
77+ fileData = new File ( [ contents ] , fileName , { type : fileMimeType } ) ;
78+ contentLength = fileData . size ;
79+ }
80+ catch ( error ) {
81+ throw new Error ( `Unable to create file from provided data type. ArrayBuffer, Blob, or string expected.Error: ${ error } ` ) ;
82+ }
83+ }
4084 }
4185 else if ( 'uri' in file ) {
4286 fileMimeType = file . mimeType ;
0 commit comments