Browse Source

Fix android download progress byte count #25

Ben Hsieh 8 years ago
parent
commit
49e1858a80

+ 6
- 7
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

1
 package com.RNFetchBlob.Response;
1
 package com.RNFetchBlob.Response;
2
 
2
 
3
-import com.RNFetchBlob.RNFetchBlob;
3
+import android.util.Log;
4
+
4
 import com.facebook.react.bridge.Arguments;
5
 import com.facebook.react.bridge.Arguments;
5
 import com.facebook.react.bridge.ReactApplicationContext;
6
 import com.facebook.react.bridge.ReactApplicationContext;
6
 import com.facebook.react.bridge.WritableMap;
7
 import com.facebook.react.bridge.WritableMap;
26
     String mTaskId;
27
     String mTaskId;
27
     ResponseBody originalBody;
28
     ResponseBody originalBody;
28
     String mPath;
29
     String mPath;
29
-    long bytesUploaded = 0;
30
+    long bytesDownloaded = 0;
30
     ReactApplicationContext rctContext;
31
     ReactApplicationContext rctContext;
31
     FileOutputStream ofStream;
32
     FileOutputStream ofStream;
32
 
33
 
62
     }
63
     }
63
 
64
 
64
     private class ProgressReportingSource implements Source {
65
     private class ProgressReportingSource implements Source {
65
-        int count = 0;
66
         @Override
66
         @Override
67
         public long read(Buffer sink, long byteCount) throws IOException {
67
         public long read(Buffer sink, long byteCount) throws IOException {
68
-            count++;
69
             byte [] bytes = new byte[(int) byteCount];
68
             byte [] bytes = new byte[(int) byteCount];
70
             long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
69
             long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
71
-            bytesUploaded += read > 0 ? read : 0;
70
+            bytesDownloaded += read > 0 ? read : 0;
71
+            Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
72
             if(read > 0) {
72
             if(read > 0) {
73
-                bytesUploaded += read;
74
                 ofStream.write(bytes, 0, (int) read);
73
                 ofStream.write(bytes, 0, (int) read);
75
                 WritableMap args = Arguments.createMap();
74
                 WritableMap args = Arguments.createMap();
76
                 args.putString("taskId", mTaskId);
75
                 args.putString("taskId", mTaskId);
77
-                args.putString("written", String.valueOf(bytesUploaded));
76
+                args.putString("written", String.valueOf(bytesDownloaded));
78
                 args.putString("total", String.valueOf(contentLength()));
77
                 args.putString("total", String.valueOf(contentLength()));
79
                 rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
78
                 rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
80
                         .emit("RNFetchBlobProgress", args);
79
                         .emit("RNFetchBlobProgress", args);

+ 27
- 48
test/test-0.7.0.js View File

45
     deb = Date.now()
45
     deb = Date.now()
46
     report(<Info uid="200" key="progress">
46
     report(<Info uid="200" key="progress">
47
       <Text>
47
       <Text>
48
-        {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`}
48
+        {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
49
       </Text>
49
       </Text>
50
     </Info>)
50
     </Info>)
51
   })
51
   })
52
   .then((res) => {
52
   .then((res) => {
53
     bigfile = res.path()
53
     bigfile = res.path()
54
+    return fs.stat(bigfile)
55
+  })
56
+  .then((stat) => {
57
+    report(<Info key="big file stat">
58
+      <Text>{JSON.stringify(stat)}</Text>
59
+    </Info>)
54
     done()
60
     done()
55
   })
61
   })
56
-  // .then((res) => {
57
-  //   bigfile = res.path()
58
-  //   try {
59
-  //   deb = Date.now()
60
-  //   let promise =  RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
61
-  //     Authorization : `Bearer ${DROPBOX_TOKEN}`,
62
-  //     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
63
-  //     'Content-Type' : 'application/octet-stream',
64
-  //   }, RNFetchBlob.wrap(res.path()))
65
-  //   promise.uploadProgress((now, total) => {
66
-  //     if(Date.now() - deb < 1000)
67
-  //       return
68
-  //     deb = Date.now()
69
-  //     if(begin2 === -1)
70
-  //       begin2 = Date.now()
71
-  //     let speed = Math.floor(now / (Date.now() - begin2))
72
-  //     report(<Info uid="100"  key="progress">
73
-  //       <Text>
74
-  //         {`upload ${now} / ${total} bytes (${speed} kb/s)`}
75
-  //         {` ${Math.floor((total-now)/speed/1000)} seconds left`}
76
-  //       </Text>
77
-  //     </Info>)
78
-  //   })
79
-  //   return promise
80
-  // } catch(err) { console.log(err) }
81
-  // })
82
-  // .then((res) => {
83
-  //   report(<Assert
84
-  //     key="upload should success without crashing app"
85
-  //     expect={filename}
86
-  //     actual={res.json().name}/>)
87
-  //   done()
88
-  // })
89
 })
62
 })
90
 
63
 
91
 describe('cancel task should work properly', (report, done) => {
64
 describe('cancel task should work properly', (report, done) => {
93
   let bytesWitten = 0
66
   let bytesWitten = 0
94
   let deb = Date.now()
67
   let deb = Date.now()
95
   let begin = -1
68
   let begin = -1
96
-  let promise =  RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
69
+  let task =  RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
97
     Authorization : `Bearer ${DROPBOX_TOKEN}`,
70
     Authorization : `Bearer ${DROPBOX_TOKEN}`,
98
     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
71
     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
99
     'Content-Type' : 'application/octet-stream',
72
     'Content-Type' : 'application/octet-stream',
100
   }, RNFetchBlob.wrap(bigfile))
73
   }, RNFetchBlob.wrap(bigfile))
101
-  promise.uploadProgress((now, total) => {
74
+
75
+  task.uploadProgress((now, total) => {
102
     bytesWitten = now
76
     bytesWitten = now
103
     if(Date.now() - deb < 1000)
77
     if(Date.now() - deb < 1000)
104
       return
78
       return
115
   })
89
   })
116
   let checkpoint1 = 0
90
   let checkpoint1 = 0
117
   Timer.setTimeout(() => {
91
   Timer.setTimeout(() => {
118
-    promise.cancel()
92
+    task.cancel()
119
   }, 5000)
93
   }, 5000)
120
   Timer.setTimeout(() => {
94
   Timer.setTimeout(() => {
121
     checkpoint1 = bytesWitten
95
     checkpoint1 = bytesWitten
124
     report(<Assert key="data should not write to stream after task is canceled"
98
     report(<Assert key="data should not write to stream after task is canceled"
125
       expect={checkpoint1}
99
       expect={checkpoint1}
126
       actual={bytesWitten}/>)
100
       actual={bytesWitten}/>)
127
-    done()
101
+    fs.unlink(bigfile).then(() => {
102
+      done()
103
+    })
128
   }, 10000)
104
   }, 10000)
129
-  promise.then((res) => {
130
-    report(
131
-      <Assert key="task not canceled"
132
-        expected={false}
105
+
106
+  task
107
+    .then((res) => {
108
+      report(
109
+        <Assert key="task not canceled"
110
+          expected={false}
111
+          actual={true}/>)
112
+    })
113
+    .catch((resp) => {
114
+      report(<Assert key="task cancelled rejection should be catachable"
115
+        expect={true}
133
         actual={true}/>)
116
         actual={true}/>)
134
-  })
135
-  promise.catch((resp) => {
136
-    report(<Assert key="task cancelled rejection should be catachable"
137
-      expect={true}
138
-      actual={true}/>)
139
-  })
117
+    })
118
+
140
 })
119
 })