Przeglądaj źródła

Add error handler to android file response handler

Ben Hsieh 8 lat temu
rodzic
commit
b6368681bd

+ 14
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Wyświetl plik

@@ -198,8 +198,14 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
198 198
             AsyncHttpResponseHandler handler;
199 199
 
200 200
             // create handler
201
-            if(config.fileCache || config.path != null)
201
+            if(config.fileCache || config.path != null) {
202 202
                 handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
203
+                // if path format invalid, throw error
204
+                if (!((RNFetchBlobFileHandler)handler).isValid) {
205
+                    callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path  +"` is not a valid path.");
206
+                    return;
207
+                }
208
+            }
203 209
             else
204 210
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
205 211
 
@@ -287,8 +293,14 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
287 293
             AsyncHttpResponseHandler handler;
288 294
 
289 295
             // create handler
290
-            if(config.fileCache || config.path != null)
296
+            if(config.fileCache || config.path != null) {
291 297
                 handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
298
+                // if path format invalid, throw error
299
+                if (!((RNFetchBlobFileHandler)handler).isValid) {
300
+                    callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path  +"` is not a valid path.");
301
+                    return;
302
+                }
303
+            }
292 304
             else
293 305
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
294 306
 

+ 9
- 17
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Wyświetl plik

@@ -406,23 +406,15 @@ public class RNFetchBlobFS {
406 406
     }
407 407
 
408 408
     void scanFile(String [] path, String[] mimes, final Callback callback) {
409
-//        try {
410
-//            MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
411
-//                @Override
412
-//                public void onScanCompleted(String s, Uri uri) {
413
-//                    callback.invoke(null, true);
414
-//                }
415
-//            });
416
-//        } catch(Exception err) {
417
-//            callback.invoke(err.getLocalizedMessage(), null);
418
-//        }
419
-        for(String p : path) {
420
-            File file = new File(p);
421
-            Uri uri = Uri.fromFile(file);
422
-            Intent scanFileIntent = new Intent(
423
-                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
424
-            mCtx.sendBroadcast(scanFileIntent);
425
-            callback.invoke(null, true);
409
+        try {
410
+            MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
411
+                @Override
412
+                public void onScanCompleted(String s, Uri uri) {
413
+                    callback.invoke(null, true);
414
+                }
415
+            });
416
+        } catch(Exception err) {
417
+            callback.invoke(err.getLocalizedMessage(), null);
426 418
         }
427 419
     }
428 420
 

+ 5
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFileHandler.java Wyświetl plik

@@ -21,6 +21,7 @@ import cz.msebera.android.httpclient.Header;
21 21
  */
22 22
 public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
23 23
 
24
+    public boolean isValid;
24 25
     Callback onResponse;
25 26
     ReactContext mCtx;
26 27
     String mTaskId;
@@ -32,6 +33,10 @@ public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
32 33
         this.mTaskId = taskId;
33 34
         this.mConfig = config;
34 35
         this.mCtx = ctx;
36
+        if(!new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, config)).isFile()) {
37
+            this.isValid = false;
38
+        }
39
+        this.isValid = true;
35 40
     }
36 41
 
37 42
     static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {

+ 8
- 0
src/ios/RNFetchBlob/RNFetchBlob.m Wyświetl plik

@@ -574,6 +574,14 @@ RCT_EXPORT_MODULE();
574 574
     return self;
575 575
 }
576 576
 
577
+- (NSDictionary *)constantsToExport
578
+{
579
+    return @{
580
+             @"DocumentDir": [FetchBlobFS getDocumentDir],
581
+             @"CacheDir" : [FetchBlobFS getCacheDir]
582
+            };
583
+}
584
+
577 585
 // Fetch blob data request
578 586
 RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
579 587
                   taskId:(NSString *)taskId

+ 1
- 1
test/react-native-testkit/lib/test-context.js Wyświetl plik

@@ -3,7 +3,7 @@
3 3
 let tests: Array<TestCase> = []
4 4
 let RCTContext: ReactElement = null
5 5
 let props:any = {}
6
-let timeout = 8000
6
+let timeout = 30000
7 7
 
8 8
 export default class TestContext {
9 9
 

+ 5
- 7
test/test-android.js Wyświetl plik

@@ -70,12 +70,7 @@ describe('MediaScanner tests ', (report, done) => {
70 70
     ])
71 71
   })
72 72
   .then(() => {
73
-    report(<Assert key="scan success" expect={true} actual={true}/>)
74
-    console.log(dirs)
75
-    for(let i in dirs) {
76
-      console.log(i)
77
-    }
78
-    console.log(dirs.DCIMDir)
73
+    report(<Assert key={`scan image success, there should be a new file in Picture app named "${filename}"`} expect={true} actual={true}/>)
79 74
     return RNFetchBlob
80 75
             .config({
81 76
               path : dirs.DCIMDir + '/beethoven-'+ Date.now() +'.mp3'
@@ -87,7 +82,10 @@ describe('MediaScanner tests ', (report, done) => {
87 82
       path : resp.path()
88 83
     }])
89 84
     .then(() => {
90
-      report(<Assert key="scan mp3 file success" expect={true} actual={true}/>)
85
+      report(<Assert
86
+        key={`scan mp3 file success, there exist a new file named "beethoven-${Date.now()}.mp3" in Music app`}
87
+        expect={true}
88
+        actual={true}/>)
91 89
       done()
92 90
     })
93 91
   })

+ 4
- 1
test/test-fs.js Wyświetl plik

@@ -22,8 +22,11 @@ let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
22 22
 let dirs = RNFetchBlob.fs.dirs
23 23
 
24 24
 describe('Get storage folders', (report, done) => {
25
+  console.log(dirs)
25 26
   report(
26
-    <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
27
+    <Assert key="system folders should exists"
28
+      expect={dirs}
29
+      comparer={Comparer.exists} />,
27 30
     <Assert key="check properties"
28 31
       expect={['DocumentDir', 'CacheDir']}
29 32
       comparer={Comparer.hasProperties}

+ 4
- 4
test/test-init.js Wyświetl plik

@@ -18,7 +18,7 @@ const { Assert, Comparer, Info, describe, prop } = RNTest
18 18
 // test environment variables
19 19
 
20 20
 prop('FILENAME', `${Platform.OS}-0.5.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.0.14:8123')
21
+prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22 22
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23 23
 prop('styles', {
24 24
   image : {
@@ -52,7 +52,7 @@ describe('GET image from server', (report, done) => {
52 52
 })
53 53
 
54 54
 
55
-// require('./test-fs')
56
-// require('./test-0.1.x-0.4.x')
57
-// require('./test-0.5.x')
55
+require('./test-fs')
56
+require('./test-0.1.x-0.4.x')
57
+require('./test-0.5.x')
58 58
 require('./test-android')