Browse Source

Correct Blob.slice behavior #89

- Fix Blob constructor when mixed array contains undefined element
- Fix onCreate event invoke issue when Blob is created by Blob.slice
Ben Hsieh 8 years ago
parent
commit
e42498e3de
1 changed files with 35 additions and 38 deletions
  1. 35
    38
      src/polyfill/Blob.js

+ 35
- 38
src/polyfill/Blob.js View File

94
       this.multipartBoundary = boundary
94
       this.multipartBoundary = boundary
95
       let parts = data.getParts()
95
       let parts = data.getParts()
96
       let formArray = []
96
       let formArray = []
97
-      for(let i in parts) {
98
-        formArray.push('\r\n--'+boundary+'\r\n')
99
-        let part = parts[i]
100
-        for(let j in part.headers) {
101
-          formArray.push(j + ': ' +part.headers[j] + ';\r\n')
97
+      if(!parts) {
98
+        p = fs.writeFile(this._ref, '', 'utf8')
99
+      }
100
+      else {
101
+        for(let i in parts) {
102
+          formArray.push('\r\n--'+boundary+'\r\n')
103
+          let part = parts[i]
104
+          for(let j in part.headers) {
105
+            formArray.push(j + ': ' +part.headers[j] + ';\r\n')
106
+          }
107
+          formArray.push('\r\n')
108
+          if(part.isRNFetchBlobPolyfill)
109
+            formArray.push(part)
110
+          else
111
+            formArray.push(part.string)
102
         }
112
         }
103
-        formArray.push('\r\n')
104
-        if(part.isRNFetchBlobPolyfill)
105
-          formArray.push(part)
106
-        else
107
-          formArray.push(part.string)
113
+        log.verbose('FormData array', formArray)
114
+        formArray.push('\r\n--'+boundary+'--\r\n')
115
+        p = createMixedBlobData(this._ref, formArray)
108
       }
116
       }
109
-      log.verbose('FormData array', formArray)
110
-      formArray.push('\r\n--'+boundary+'--\r\n')
111
-      p = createMixedBlobData(this._ref, formArray)
112
     }
117
     }
113
     // if the data is a string starts with `RNFetchBlob-file://`, append the
118
     // if the data is a string starts with `RNFetchBlob-file://`, append the
114
     // Blob data from file path
119
     // Blob data from file path
205
    * @param  {string} contentType Optional, content type of new Blob object
210
    * @param  {string} contentType Optional, content type of new Blob object
206
    * @return {Blob}
211
    * @return {Blob}
207
    */
212
    */
208
-  slice(start:?number, end:?number, contentType:?string):Blob {
213
+  slice(start:?number, end:?number, contentType='':?string):Blob {
209
     if(this._closed)
214
     if(this._closed)
210
       throw 'Blob has been released.'
215
       throw 'Blob has been released.'
211
     log.verbose('slice called', start, end, contentType)
216
     log.verbose('slice called', start, end, contentType)
212
     let resPath = blobCacheDir + getBlobName()
217
     let resPath = blobCacheDir + getBlobName()
213
     let pass = false
218
     let pass = false
214
     log.debug('fs.slice new blob will at', resPath)
219
     log.debug('fs.slice new blob will at', resPath)
220
+    let result = new Blob(RNFetchBlob.wrap(resPath), { type : contentType })
215
     fs.slice(this._ref, resPath, start, end).then((dest) => {
221
     fs.slice(this._ref, resPath, start, end).then((dest) => {
216
       log.debug('fs.slice done', dest)
222
       log.debug('fs.slice done', dest)
223
+      result._invokeOnCreateEvent()
217
       pass = true
224
       pass = true
218
     })
225
     })
219
     .catch((err) => {
226
     .catch((err) => {
220
       pass = true
227
       pass = true
221
     })
228
     })
222
     log.debug('slice returning new Blob')
229
     log.debug('slice returning new Blob')
223
-    return new Blob(RNFetchBlob.wrap(resPath))
230
+
231
+    return result
224
   }
232
   }
225
 
233
 
226
   /**
234
   /**
283
   let size = 0
291
   let size = 0
284
   for(let i in dataArray) {
292
   for(let i in dataArray) {
285
     let part = dataArray[i]
293
     let part = dataArray[i]
294
+    if(!part)
295
+      continue
286
     if(part.isRNFetchBlobPolyfill) {
296
     if(part.isRNFetchBlobPolyfill) {
287
       args.push([ref, part._ref, 'uri'])
297
       args.push([ref, part._ref, 'uri'])
288
     }
298
     }
296
       args.push([ref, part, 'ascii'])
306
       args.push([ref, part, 'ascii'])
297
   }
307
   }
298
   // start write blob data
308
   // start write blob data
299
-  // return p.then(() => {
300
-    for(let i in args) {
301
-      p = p.then(function(written){
302
-        let arg = this
303
-        if(written)
304
-          size += written
305
-        log.verbose('mixed blob write', args[i], written)
306
-        return fs.appendFile(...arg)
307
-      }.bind(args[i]))
308
-    }
309
-    return p.then(() => Promise.resolve(size))
310
-    // let promises = args.map((p) => {
311
-    //   log.verbose('mixed blob write', ...p)
312
-    //   return fs.appendFile.call(this, ...p)
313
-    // })
314
-    // return Promise.all(promises).then((sizes) => {
315
-    //   log.verbose('blob write size', sizes)
316
-    //   for(let i in sizes) {
317
-    //     size += sizes[i]
318
-    //   }
319
-    //   return Promise.resolve(size)
320
-    // })
321
-  // })
309
+  for(let i in args) {
310
+    p = p.then(function(written){
311
+      let arg = this
312
+      if(written)
313
+        size += written
314
+      log.verbose('mixed blob write', args[i], written)
315
+      return fs.appendFile(...arg)
316
+    }.bind(args[i]))
317
+  }
318
+  return p.then(() => Promise.resolve(size))
322
 }
319
 }