Selaa lähdekoodia

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 vuotta sitten
vanhempi
commit
e42498e3de
1 muutettua tiedostoa jossa 35 lisäystä ja 38 poistoa
  1. 35
    38
      src/polyfill/Blob.js

+ 35
- 38
src/polyfill/Blob.js Näytä tiedosto

@@ -94,21 +94,26 @@ export default class Blob extends EventTarget {
94 94
       this.multipartBoundary = boundary
95 95
       let parts = data.getParts()
96 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 118
     // if the data is a string starts with `RNFetchBlob-file://`, append the
114 119
     // Blob data from file path
@@ -205,22 +210,25 @@ export default class Blob extends EventTarget {
205 210
    * @param  {string} contentType Optional, content type of new Blob object
206 211
    * @return {Blob}
207 212
    */
208
-  slice(start:?number, end:?number, contentType:?string):Blob {
213
+  slice(start:?number, end:?number, contentType='':?string):Blob {
209 214
     if(this._closed)
210 215
       throw 'Blob has been released.'
211 216
     log.verbose('slice called', start, end, contentType)
212 217
     let resPath = blobCacheDir + getBlobName()
213 218
     let pass = false
214 219
     log.debug('fs.slice new blob will at', resPath)
220
+    let result = new Blob(RNFetchBlob.wrap(resPath), { type : contentType })
215 221
     fs.slice(this._ref, resPath, start, end).then((dest) => {
216 222
       log.debug('fs.slice done', dest)
223
+      result._invokeOnCreateEvent()
217 224
       pass = true
218 225
     })
219 226
     .catch((err) => {
220 227
       pass = true
221 228
     })
222 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,6 +291,8 @@ function createMixedBlobData(ref, dataArray) {
283 291
   let size = 0
284 292
   for(let i in dataArray) {
285 293
     let part = dataArray[i]
294
+    if(!part)
295
+      continue
286 296
     if(part.isRNFetchBlobPolyfill) {
287 297
       args.push([ref, part._ref, 'uri'])
288 298
     }
@@ -296,27 +306,14 @@ function createMixedBlobData(ref, dataArray) {
296 306
       args.push([ref, part, 'ascii'])
297 307
   }
298 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
 }