Browse Source

Add error handler to android file response handler

Ben Hsieh 8 years ago
parent
commit
b6368681bd

+ 14
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

198
             AsyncHttpResponseHandler handler;
198
             AsyncHttpResponseHandler handler;
199
 
199
 
200
             // create handler
200
             // create handler
201
-            if(config.fileCache || config.path != null)
201
+            if(config.fileCache || config.path != null) {
202
                 handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
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
             else
209
             else
204
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
210
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
205
 
211
 
287
             AsyncHttpResponseHandler handler;
293
             AsyncHttpResponseHandler handler;
288
 
294
 
289
             // create handler
295
             // create handler
290
-            if(config.fileCache || config.path != null)
296
+            if(config.fileCache || config.path != null) {
291
                 handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback);
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
             else
304
             else
293
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
305
                 handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
294
 
306
 

+ 9
- 17
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

406
     }
406
     }
407
 
407
 
408
     void scanFile(String [] path, String[] mimes, final Callback callback) {
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 View File

21
  */
21
  */
22
 public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
22
 public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
23
 
23
 
24
+    public boolean isValid;
24
     Callback onResponse;
25
     Callback onResponse;
25
     ReactContext mCtx;
26
     ReactContext mCtx;
26
     String mTaskId;
27
     String mTaskId;
32
         this.mTaskId = taskId;
33
         this.mTaskId = taskId;
33
         this.mConfig = config;
34
         this.mConfig = config;
34
         this.mCtx = ctx;
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
     static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
42
     static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {

+ 8
- 0
src/ios/RNFetchBlob/RNFetchBlob.m View File

574
     return self;
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
 // Fetch blob data request
585
 // Fetch blob data request
578
 RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
586
 RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
579
                   taskId:(NSString *)taskId
587
                   taskId:(NSString *)taskId

+ 1
- 1
test/react-native-testkit/lib/test-context.js View File

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

+ 5
- 7
test/test-android.js View File

70
     ])
70
     ])
71
   })
71
   })
72
   .then(() => {
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
     return RNFetchBlob
74
     return RNFetchBlob
80
             .config({
75
             .config({
81
               path : dirs.DCIMDir + '/beethoven-'+ Date.now() +'.mp3'
76
               path : dirs.DCIMDir + '/beethoven-'+ Date.now() +'.mp3'
87
       path : resp.path()
82
       path : resp.path()
88
     }])
83
     }])
89
     .then(() => {
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
       done()
89
       done()
92
     })
90
     })
93
   })
91
   })

+ 4
- 1
test/test-fs.js View File

22
 let dirs = RNFetchBlob.fs.dirs
22
 let dirs = RNFetchBlob.fs.dirs
23
 
23
 
24
 describe('Get storage folders', (report, done) => {
24
 describe('Get storage folders', (report, done) => {
25
+  console.log(dirs)
25
   report(
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
     <Assert key="check properties"
30
     <Assert key="check properties"
28
       expect={['DocumentDir', 'CacheDir']}
31
       expect={['DocumentDir', 'CacheDir']}
29
       comparer={Comparer.hasProperties}
32
       comparer={Comparer.hasProperties}

+ 4
- 4
test/test-init.js View File

18
 // test environment variables
18
 // test environment variables
19
 
19
 
20
 prop('FILENAME', `${Platform.OS}-0.5.0-${Date.now()}.png`)
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
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
22
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23
 prop('styles', {
23
 prop('styles', {
24
   image : {
24
   image : {
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
 require('./test-android')
58
 require('./test-android')