Przeglądaj źródła

Fix android download progress byte count #25

Ben Hsieh 8 lat temu
rodzic
commit
49e1858a80

+ 6
- 7
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java Wyświetl plik

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

+ 27
- 48
test/test-0.7.0.js Wyświetl plik

@@ -45,47 +45,20 @@ describe('Upload and download large file', (report, done) => {
45 45
     deb = Date.now()
46 46
     report(<Info uid="200" key="progress">
47 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 49
       </Text>
50 50
     </Info>)
51 51
   })
52 52
   .then((res) => {
53 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 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 64
 describe('cancel task should work properly', (report, done) => {
@@ -93,12 +66,13 @@ describe('cancel task should work properly', (report, done) => {
93 66
   let bytesWitten = 0
94 67
   let deb = Date.now()
95 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 70
     Authorization : `Bearer ${DROPBOX_TOKEN}`,
98 71
     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
99 72
     'Content-Type' : 'application/octet-stream',
100 73
   }, RNFetchBlob.wrap(bigfile))
101
-  promise.uploadProgress((now, total) => {
74
+
75
+  task.uploadProgress((now, total) => {
102 76
     bytesWitten = now
103 77
     if(Date.now() - deb < 1000)
104 78
       return
@@ -115,7 +89,7 @@ describe('cancel task should work properly', (report, done) => {
115 89
   })
116 90
   let checkpoint1 = 0
117 91
   Timer.setTimeout(() => {
118
-    promise.cancel()
92
+    task.cancel()
119 93
   }, 5000)
120 94
   Timer.setTimeout(() => {
121 95
     checkpoint1 = bytesWitten
@@ -124,17 +98,22 @@ describe('cancel task should work properly', (report, done) => {
124 98
     report(<Assert key="data should not write to stream after task is canceled"
125 99
       expect={checkpoint1}
126 100
       actual={bytesWitten}/>)
127
-    done()
101
+    fs.unlink(bigfile).then(() => {
102
+      done()
103
+    })
128 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 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
 })