浏览代码

Fix Blob constructor create from Blob bug #44

Ben Hsieh 8 年前
父节点
当前提交
aaa2b1b74d
共有 3 个文件被更改,包括 67 次插入17 次删除
  1. 22
    7
      src/polyfill/Blob.js
  2. 35
    0
      test/test-firebase.js
  3. 10
    10
      test/test-init.js

+ 22
- 7
src/polyfill/Blob.js 查看文件

@@ -55,17 +55,31 @@ export default class Blob {
55 55
     let p = null
56 56
     if(data instanceof Blob) {
57 57
       log.verbose('create Blob cache file from Blob object')
58
-      this._ref = data.getRNFetchBlobRef()
59
-      p = fs.stat(String(this._ref).replace('RNFetchBlob-file://'))
60
-            .then((stat) =>  Promise.resolve(stat.size))
58
+      let size = 0
59
+      this._ref = String(data.getRNFetchBlobRef())
60
+      let orgPath = this._ref
61
+      p = fs.exists(orgPath)
62
+            .then((exist) =>  {
63
+              if(exist)
64
+                return fs.writeFile(orgPath, data, 'uri')
65
+                         .then((size) => Promise.resolve(size))
66
+                         .catch((err) => {
67
+                           throw `RNFetchBlob Blob file creation error, ${err}`
68
+                         })
69
+              else
70
+                throw `could not create Blob from path ${orgPath}, file not exists`
71
+            })
61 72
     }
62 73
     // if the data is a string starts with `RNFetchBlob-file://`, append the
63 74
     // Blob data from file path
64 75
     else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
65
-      log.verbose('create Blob cache file from file path')
66
-      this._ref = data
67
-      p = fs.stat(String(this._ref).replace('RNFetchBlob-file://'))
68
-            .then((stat) =>  Promise.resolve(stat.size))
76
+      log.verbose('create Blob cache file from file path', data)
77
+      this._ref = String(data).replace('RNFetchBlob-file://', '')
78
+      let orgPath = this._ref
79
+      p = fs.stat(orgPath)
80
+            .then((stat) =>  {
81
+                return Promise.resolve(stat.size)
82
+            })
69 83
     }
70 84
     // content from variable need create file
71 85
     else if(typeof data === 'string') {
@@ -197,6 +211,7 @@ function createMixedBlobData(ref, dataArray) {
197 211
   }
198 212
   return p.then(() => {
199 213
     let promises = args.map((p) => {
214
+      log.verbose('mixed blob write', ...p)
200 215
       return fs.appendFile.call(this, ...p)
201 216
     })
202 217
     return Promise.all(promises).then((sizes) => {

+ 35
- 0
test/test-firebase.js 查看文件

@@ -98,3 +98,38 @@ describe('download firebase storage item', (report, done) => {
98 98
     done()
99 99
   })
100 100
 })
101
+
102
+let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
103
+
104
+describe('upload using file path', (report, done) => {
105
+  RNFetchBlob
106
+    .config({ fileCache : true, appendExt : 'jpg' })
107
+    .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
108
+    .then((resp) => {
109
+      report(<Info key="test image">
110
+        <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
111
+      </Info>)
112
+      let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
113
+      blob.onCreated(() => {
114
+        firebase.storage().ref('rnfbtest')
115
+          .child(tier2FileName)
116
+          .put(blob, { contentType : 'image/jpg' })
117
+          .then(() => {
118
+            report(<Assert key="upload finished" />)
119
+            done()
120
+          })
121
+      })
122
+    })
123
+})
124
+
125
+describe('verify uploaded file', (report, done) => {
126
+  firebase.storage().ref('rnfbtest/' + tier2FileName)
127
+    .getDownloadURL()
128
+    .then((url) => {
129
+      console.log(url)
130
+      report(<Info key="image viewer">
131
+        <Image style={styles.image} source={{uri : url}}/>
132
+      </Info>)
133
+      done()
134
+    })
135
+})

+ 10
- 10
test/test-init.js 查看文件

@@ -58,14 +58,14 @@ describe('GET image from server', (report, done) => {
58 58
 })
59 59
 
60 60
 
61
-require('./test-0.1.x-0.4.x')
62
-require('./test-0.5.1')
63
-require('./test-0.5.2')
64
-require('./test-0.6.0')
65
-require('./test-0.6.2')
66
-require('./test-0.6.3')
67
-require('./test-0.7.0')
68
-require('./test-0.8.0')
69
-require('./test-fs')
61
+// require('./test-0.1.x-0.4.x')
62
+// require('./test-0.5.1')
63
+// require('./test-0.5.2')
64
+// require('./test-0.6.0')
65
+// require('./test-0.6.2')
66
+// require('./test-0.6.3')
67
+// require('./test-0.7.0')
68
+// require('./test-0.8.0')
69
+// require('./test-fs')
70 70
 require('./test-firebase')
71
-require('./test-android')
71
+// require('./test-android')