Kaynağa Gözat

Fix Android Okhttp issue #93

Ben Hsieh 8 yıl önce
ebeveyn
işleme
bde574226b

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Dosyayı Görüntüle

@@ -450,8 +450,9 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
450 450
                 }
451 451
                 break;
452 452
         }
453
+        if(!resp.isSuccessful())
454
+            resp.body().close();
453 455
         removeTaskInfo();
454
-        resp.close();
455 456
     }
456 457
 
457 458
     /**

+ 0
- 9
src/scripts/prelink.js Dosyayı Görüntüle

@@ -45,15 +45,6 @@ if(VERSION < 0.28) {
45 45
   console.log('adding OkHttp3 dependency to pre 0.28 project .. ok')
46 46
 }
47 47
 
48
-if(VERSION > 0.30) {
49
-  let dest = process.cwd() + '/node_modules/react-native-fetch-blob/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java'
50
-  var main = fs.readFileSync(dest);
51
-  console.log('patching OkHttp3 3.4.1 API ..')
52
-  main = String(main).replace('// ${OKHTTP 3.4.1 ONLY STATEMENT}', "resp.close();");
53
-  fs.writeFileSync(dest, main);
54
-  console.log('patching OkHttp3 3.4.1 API .. ok')
55
-}
56
-
57 48
 // set file access permission for Android < 6.0
58 49
 fs.readFile(MANIFEST_PATH, function(err, data) {
59 50
 

+ 16
- 58
test/test-0.9.2.js Dosyayı Görüntüle

@@ -81,67 +81,25 @@ describe('slice test', (report, done) => {
81 81
     })
82 82
 })
83 83
 
84
+describe('Upload multipart/form-data', (report, done) => {
84 85
 
85
-describe('fs.slice test', (report, done) => {
86
+  RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
87
+      Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
88
+      'Content-Type' : 'multipart/form-data',
89
+    }, [
90
+      // { name : 'test-img', filename : 'test-img.png', data: image},
91
+      // { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
92
+      { name : 'field1', data : 'hello !!'},
93
+      { name : 'field2', data : 'hello2 !!'}
94
+    ])
95
+  .then((resp) => {
96
+    console.log(resp.json())
97
+    resp = resp.json()
86 98
 
87
-  let source = null
88
-  let parts = fs.dirs.DocumentDir + '/tmp-source-'
89
-  let dests = []
90
-  let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
91
-  let size = 0
92
-
93
-  window.fetch = new RNFetchBlob.polyfill.Fetch({
94
-    auto : true,
95
-    binaryContentTypes : ['image/', 'video/', 'audio/']
96
-  }).build()
97
-
98
-  fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
99
-  .then((res) => res.rawResp())
100
-  .then((res) => {
101
-    source = res.path()
102
-    return fs.stat(source)
103
-  })
104
-  // separate file into 4kb chunks
105
-  .then((stat) => {
106
-    size = stat.size
107
-    let promise = Promise.resolve()
108
-    let cursor = 0
109
-    while(cursor < size) {
110
-      promise = promise.then(function(start) {
111
-        console.log('slicing part ', start , start + 40960)
112
-        let offset = 0
113
-        return fs.slice(source, parts + start, start + offset, start + 40960)
114
-                .then((dest) => {
115
-                  console.log('slicing part ', start + offset, start + 40960, 'done')
116
-                  dests.push(dest)
117
-                  return Promise.resolve()
118
-                })
119
-      }.bind(this, cursor))
120
-      cursor += 40960
121
-    }
122
-    console.log('loop end')
123
-    return promise
124
-  })
125
-  // combine chunks and verify the result
126
-  .then(() => {
127
-    console.log('combinding files')
128
-    let p = Promise.resolve()
129
-    for(let d in dests) {
130
-      p = p.then(function(chunk){
131
-        return fs.appendFile(combined, chunk, 'uri').then((write) => {
132
-          console.log(write, 'bytes write')
133
-        })
134
-      }.bind(this, dests[d]))
135
-    }
136
-    return p.then(() => fs.stat(combined))
137
-  })
138
-  .then((stat) => {
139 99
     report(
140
-      <Assert key="verify file size" expect={size} actual={stat.size}/>,
141
-      <Info key="image viewer">
142
-        <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
143
-      </Info>)
100
+      <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
101
+      <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
102
+    )
144 103
     done()
145 104
   })
146
-
147 105
 })

+ 65
- 0
test/test-fs.js Dosyayı Görüntüle

@@ -407,6 +407,71 @@ describe('stat and lstat test', (report, done) => {
407 407
 
408 408
 })
409 409
 
410
+describe('fs.slice test', (report, done) => {
411
+
412
+  let source = null
413
+  let parts = fs.dirs.DocumentDir + '/tmp-source-'
414
+  let dests = []
415
+  let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
416
+  let size = 0
417
+
418
+  window.fetch = new RNFetchBlob.polyfill.Fetch({
419
+    auto : true,
420
+    binaryContentTypes : ['image/', 'video/', 'audio/']
421
+  }).build()
422
+
423
+  fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
424
+  .then((res) => res.rawResp())
425
+  .then((res) => {
426
+    source = res.path()
427
+    return fs.stat(source)
428
+  })
429
+  // separate file into 4kb chunks
430
+  .then((stat) => {
431
+    size = stat.size
432
+    let promise = Promise.resolve()
433
+    let cursor = 0
434
+    while(cursor < size) {
435
+      promise = promise.then(function(start) {
436
+        console.log('slicing part ', start , start + 40960)
437
+        let offset = 0
438
+        return fs.slice(source, parts + start, start + offset, start + 40960)
439
+                .then((dest) => {
440
+                  console.log('slicing part ', start + offset, start + 40960, 'done')
441
+                  dests.push(dest)
442
+                  return Promise.resolve()
443
+                })
444
+      }.bind(this, cursor))
445
+      cursor += 40960
446
+    }
447
+    console.log('loop end')
448
+    return promise
449
+  })
450
+  // combine chunks and verify the result
451
+  .then(() => {
452
+    console.log('combinding files')
453
+    let p = Promise.resolve()
454
+    for(let d in dests) {
455
+      p = p.then(function(chunk){
456
+        return fs.appendFile(combined, chunk, 'uri').then((write) => {
457
+          console.log(write, 'bytes write')
458
+        })
459
+      }.bind(this, dests[d]))
460
+    }
461
+    return p.then(() => fs.stat(combined))
462
+  })
463
+  .then((stat) => {
464
+    report(
465
+      <Assert key="verify file size" expect={size} actual={stat.size}/>,
466
+      <Info key="image viewer">
467
+        <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
468
+      </Info>)
469
+    done()
470
+  })
471
+
472
+})
473
+
474
+
410 475
 function getASCIIArray(str) {
411 476
   let r = []
412 477
   for(let i=0;i<str.length;i++) {