Browse Source

Fix Blob constructor FormData transform function

Ben Hsieh 8 years ago
parent
commit
fe67648845
2 changed files with 23 additions and 7 deletions
  1. 14
    7
      src/polyfill/Blob.js
  2. 9
    0
      src/polyfill/File.js

+ 14
- 7
src/polyfill/Blob.js View File

23
   type:string;
23
   type:string;
24
   size:number;
24
   size:number;
25
   isRNFetchBlobPolyfill:boolean = true;
25
   isRNFetchBlobPolyfill:boolean = true;
26
+  multipartBoundary:string = null;
26
 
27
 
27
   _ref:string = null;
28
   _ref:string = null;
28
   _blobCreated:boolean = false;
29
   _blobCreated:boolean = false;
84
     // process FormData
85
     // process FormData
85
     else if(data instanceof FormData) {
86
     else if(data instanceof FormData) {
86
       log.verbose('create Blob cache file from FormData', data)
87
       log.verbose('create Blob cache file from FormData', data)
87
-      let boundary = `--RNFetchBlob-${this.cacheName}-${Date.now()}\r\n`
88
+      let boundary = `RNFetchBlob-${this.cacheName}-${Date.now()}`
89
+      this.multipartBoundary = boundary
88
       let parts = data.getParts()
90
       let parts = data.getParts()
89
       let formArray = []
91
       let formArray = []
90
       for(let i in parts) {
92
       for(let i in parts) {
91
-        formArray.push(boundary)
93
+        formArray.push('\r\n--'+boundary+'\r\n')
92
         let part = parts[i]
94
         let part = parts[i]
93
         for(let j in part.headers) {
95
         for(let j in part.headers) {
94
-          formArray.push(j + part.headers[j] + ';\r\n')
96
+          formArray.push(j + ': ' +part.headers[j] + ';\r\n')
95
         }
97
         }
96
         formArray.push('\r\n')
98
         formArray.push('\r\n')
97
         if(part.isRNFetchBlobPolyfill)
99
         if(part.isRNFetchBlobPolyfill)
100
           formArray.push(part.string)
102
           formArray.push(part.string)
101
       }
103
       }
102
       log.verbose('FormData array', formArray)
104
       log.verbose('FormData array', formArray)
103
-      formArray.push(boundary + '--')
105
+      formArray.push('\r\n--'+boundary+'--\r\n')
104
       p = createMixedBlobData(this._ref, formArray)
106
       p = createMixedBlobData(this._ref, formArray)
105
     }
107
     }
106
     // if the data is a string starts with `RNFetchBlob-file://`, append the
108
     // if the data is a string starts with `RNFetchBlob-file://`, append the
125
       else
127
       else
126
         data = data.toString()
128
         data = data.toString()
127
       // create cache file
129
       // create cache file
130
+      this.type = String(this.type).replace(/;base64/ig, '')
128
       log.verbose('create Blob cache file from string', 'encode', encoding)
131
       log.verbose('create Blob cache file from string', 'encode', encoding)
129
       p = fs.writeFile(this._ref, data, encoding)
132
       p = fs.writeFile(this._ref, data, encoding)
130
-            .then((size) => Promise.resolve(size))
133
+            .then((size) => {
134
+              console.log('file bytes', size)
135
+              return Promise.resolve(size)
136
+            })
131
 
137
 
132
     }
138
     }
133
     // TODO : ArrayBuffer support
139
     // TODO : ArrayBuffer support
240
   let size = 0
246
   let size = 0
241
   for(let i in dataArray) {
247
   for(let i in dataArray) {
242
     let part = dataArray[i]
248
     let part = dataArray[i]
243
-    if(part instanceof Blob)
244
-      args.push([ref, part.getRNFetchBlobRef(), 'uri'])
249
+    if(part.isRNFetchBlobPolyfill) {
250
+      args.push([ref, part._ref, 'uri'])
251
+    }
245
     else if(typeof part === 'string')
252
     else if(typeof part === 'string')
246
       args.push([ref, part, 'utf8'])
253
       args.push([ref, part, 'utf8'])
247
     // TODO : ArrayBuffer
254
     // TODO : ArrayBuffer

+ 9
- 0
src/polyfill/File.js View File

9
 
9
 
10
   name : string = '';
10
   name : string = '';
11
 
11
 
12
+  static build(name:string, data:any, cType):Promise<File> {
13
+    this.name = name
14
+    return new Promise((resolve, reject) => {
15
+      new File(data, cType).onCreated((f) => {
16
+        resolve(f)
17
+      })
18
+    })
19
+  }
20
+
12
   constructor(data:any , cType:string) {
21
   constructor(data:any , cType:string) {
13
     super(data, cType)
22
     super(data, cType)
14
   }
23
   }