Browse Source

Ensure blob cache folder exists before Blob.slice #180

Ben Hsieh 8 years ago
parent
commit
66378fde79
1 changed files with 11 additions and 1 deletions
  1. 11
    1
      src/polyfill/Blob.js

+ 11
- 1
src/polyfill/Blob.js View File

229
     if(this._closed)
229
     if(this._closed)
230
       throw 'Blob has been released.'
230
       throw 'Blob has been released.'
231
     log.verbose('slice called', start, end, contentType)
231
     log.verbose('slice called', start, end, contentType)
232
+
233
+
232
     let resPath = blobCacheDir + getBlobName()
234
     let resPath = blobCacheDir + getBlobName()
233
     let pass = false
235
     let pass = false
234
     log.debug('fs.slice new blob will at', resPath)
236
     log.debug('fs.slice new blob will at', resPath)
235
     let result = new Blob(RNFetchBlob.wrap(resPath), { type : contentType }, true)
237
     let result = new Blob(RNFetchBlob.wrap(resPath), { type : contentType }, true)
236
-    fs.slice(this._ref, resPath, start, end).then((dest) => {
238
+    fs.exists(blobCacheDir)
239
+    .then((exist) => {
240
+      if(exist)
241
+        return Promise.resolve()
242
+      return fs.mkdir(blobCacheDir)
243
+    })
244
+    .then(() => fs.slice(this._ref, resPath, start, end))
245
+    .then((dest) => {
237
       log.debug('fs.slice done', dest)
246
       log.debug('fs.slice done', dest)
238
       result._invokeOnCreateEvent()
247
       result._invokeOnCreateEvent()
239
       pass = true
248
       pass = true
240
     })
249
     })
241
     .catch((err) => {
250
     .catch((err) => {
251
+      console.warn('Blob.slice failed:', err)
242
       pass = true
252
       pass = true
243
     })
253
     })
244
     log.debug('slice returning new Blob')
254
     log.debug('slice returning new Blob')