Quellcode durchsuchen

Add new attribute to fs.readStream #118

Introduce new argument which can limit readStream event frequency
Ben Hsieh vor 8 Jahren
Ursprung
Commit
016e3c49f7

+ 2
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Datei anzeigen

@@ -207,13 +207,13 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
207 207
      * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
208 208
      * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
209 209
      */
210
-    public void readStream(final String path, final String encoding, final int bufferSize, final String streamId) {
210
+    public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
211 211
         final ReactApplicationContext ctx = this.getReactApplicationContext();
212 212
         threadPool.execute(new Runnable() {
213 213
             @Override
214 214
             public void run() {
215 215
                 RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
216
-                fs.readStream(path, encoding, bufferSize, streamId);
216
+                fs.readStream(path, encoding, bufferSize, tick, streamId);
217 217
             }
218 218
         });
219 219
     }

+ 8
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Datei anzeigen

@@ -11,6 +11,7 @@ import android.net.Uri;
11 11
 import android.os.AsyncTask;
12 12
 import android.os.Environment;
13 13
 import android.os.Looper;
14
+import android.os.SystemClock;
14 15
 import android.provider.MediaStore;
15 16
 import android.util.Base64;
16 17
 
@@ -229,7 +230,7 @@ public class RNFetchBlobFS {
229 230
      * @param encoding  File stream decoder, should be one of `base64`, `utf8`, `ascii`
230 231
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
231 232
      */
232
-    public void readStream(String path, String encoding, int bufferSize, final String streamId) {
233
+    public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
233 234
         path = normalizePath(path);
234 235
         try {
235 236
 
@@ -253,6 +254,8 @@ public class RNFetchBlobFS {
253 254
                 while ((cursor = fs.read(buffer)) != -1) {
254 255
                     String chunk = new String(buffer, 0, cursor, "UTF-8");
255 256
                     emitStreamEvent(streamId, "data", chunk);
257
+                    if(tick > 0)
258
+                        SystemClock.sleep(tick);
256 259
                 }
257 260
             } else if (encoding.equalsIgnoreCase("ascii")) {
258 261
                 while ((cursor = fs.read(buffer)) != -1) {
@@ -262,6 +265,8 @@ public class RNFetchBlobFS {
262 265
                         chunk.pushInt((int)buffer[i]);
263 266
                     }
264 267
                     emitStreamEvent(streamId, "data", chunk);
268
+                    if(tick > 0)
269
+                        SystemClock.sleep(tick);
265 270
                 }
266 271
             } else if (encoding.equalsIgnoreCase("base64")) {
267 272
                 while ((cursor = fs.read(buffer)) != -1) {
@@ -274,6 +279,8 @@ public class RNFetchBlobFS {
274 279
                     }
275 280
                     else
276 281
                         emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
282
+                    if(tick > 0)
283
+                        SystemClock.sleep(tick);
277 284
                 }
278 285
             } else {
279 286
                 String msg = "unrecognized encoding `" + encoding + "`";

+ 16
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Datei anzeigen

@@ -17,6 +17,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
17 17
 import com.facebook.react.bridge.ReadableArray;
18 18
 import com.facebook.react.bridge.ReadableMap;
19 19
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
20
+import com.facebook.react.bridge.WritableArray;
20 21
 import com.facebook.react.bridge.WritableMap;
21 22
 import com.facebook.react.modules.core.DeviceEventManagerModule;
22 23
 
@@ -32,6 +33,7 @@ import java.nio.ByteBuffer;
32 33
 import java.nio.charset.CharacterCodingException;
33 34
 import java.nio.charset.Charset;
34 35
 import java.nio.charset.CharsetEncoder;
36
+import java.util.ArrayList;
35 37
 import java.util.HashMap;
36 38
 import java.util.concurrent.TimeUnit;
37 39
 
@@ -87,6 +89,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
87 89
     ResponseType responseType;
88 90
     WritableMap respInfo;
89 91
     boolean timeout = false;
92
+    ArrayList<String> redirects = new ArrayList<>();
90 93
 
91 94
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
92 95
         this.method = method.toUpperCase();
@@ -277,7 +280,13 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
277 280
             }
278 281
 
279 282
             final Request req = builder.build();
280
-
283
+            clientBuilder.addNetworkInterceptor(new Interceptor() {
284
+                @Override
285
+                public Response intercept(Chain chain) throws IOException {
286
+                        redirects.add(chain.request().url().toString());
287
+                        return chain.proceed(chain.request());
288
+                    }
289
+            });
281 290
             // Add request interceptor for upload progress event
282 291
             clientBuilder.addInterceptor(new Interceptor() {
283 292
                 @Override
@@ -330,6 +339,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
330 339
             clientBuilder.retryOnConnectionFailure(false);
331 340
             clientBuilder.followRedirects(true);
332 341
 
342
+
333 343
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
334 344
             Call call =  client.newCall(req);
335 345
             taskTable.put(taskId, call);
@@ -508,6 +518,11 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
508 518
         for(int i =0;i< resp.headers().size();i++) {
509 519
             headers.putString(resp.headers().name(i), resp.headers().value(i));
510 520
         }
521
+        WritableArray redirectList = Arguments.createArray();
522
+        for(String r : redirects) {
523
+                redirectList.pushString(r);
524
+        }
525
+        info.putArray("redirects", redirectList);
511 526
         info.putMap("headers", headers);
512 527
         Headers h = resp.headers();
513 528
         if(isBlobResp) {

+ 4
- 1
src/fs.js Datei anzeigen

@@ -113,8 +113,11 @@ function readStream(
113 113
   path : string,
114 114
   encoding : 'utf8' | 'ascii' | 'base64',
115 115
   bufferSize? : ?number
116
+  tick? : ?number
116 117
 ):Promise<RNFetchBlobReadStream> {
117
-  return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize))
118
+  if(!tick)
119
+    tick = -1
120
+  return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize, tick))
118 121
 }
119 122
 
120 123
 /**

+ 2
- 2
src/ios/RNFetchBlob/RNFetchBlob.m Datei anzeigen

@@ -374,7 +374,7 @@ RCT_EXPORT_METHOD(readFile:(NSString *)path encoding:(NSString *)encoding resolv
374 374
 })
375 375
 
376 376
 #pragma mark - fs.readStream
377
-RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize streamId:(NSString *)streamId
377
+RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId
378 378
 {
379 379
 
380 380
 //    RNFetchBlobFS *fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
@@ -386,7 +386,7 @@ RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding
386 386
     }
387 387
     
388 388
 //    [fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
389
-    [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize streamId:streamId bridgeRef:_bridge];
389
+    [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
390 390
 })
391 391
 
392 392
 #pragma mark - fs.getEnvionmentDirs

+ 1
- 1
src/ios/RNFetchBlobFS.h Datei anzeigen

@@ -64,7 +64,7 @@
64 64
      rejecter:(RCTPromiseRejectBlock)reject;
65 65
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
66 66
 + (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest;
67
-+ (void) readStream:(NSString *)uri encoding:(NSString * )encoding bufferSize:(int)bufferSize streamId:(NSString *)streamId bridgeRef:(RCTBridge *)bridgeRef;
67
++ (void) readStream:(NSString *)uri encoding:(NSString * )encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId bridgeRef:(RCTBridge *)bridgeRef;
68 68
 
69 69
 // constructor
70 70
 - (id) init;

+ 1
- 0
src/ios/RNFetchBlobFS.m Datei anzeigen

@@ -122,6 +122,7 @@ NSMutableDictionary *fileStreams = nil;
122 122
 + (void) readStream:(NSString *)uri
123 123
            encoding:(NSString * )encoding
124 124
          bufferSize:(int)bufferSize
125
+               tick:(int)tick
125 126
            streamId:(NSString *)streamId
126 127
           bridgeRef:(RCTBridge *)bridgeRef
127 128
 {