Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit b636868

Browse files
committed
Add error handler to android file response handler
1 parent 38905e6 commit b636868

File tree

8 files changed

+50
-32
lines changed

8 files changed

+50
-32
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ public void fetchBlob(ReadableMap options, String taskId, String method, String
198198
AsyncHttpResponseHandler handler;
199199

200200
// create handler
201-
if(config.fileCache || config.path != null)
201+
if(config.fileCache || config.path != null) {
202202
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+
}
203209
else
204210
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
205211

@@ -287,8 +293,14 @@ public void fetchBlobForm(ReadableMap options, String taskId, String method, Str
287293
AsyncHttpResponseHandler handler;
288294

289295
// create handler
290-
if(config.fileCache || config.path != null)
296+
if(config.fileCache || config.path != null) {
291297
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+
}
292304
else
293305
handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback);
294306

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,23 +406,15 @@ static void stat(String path, Callback callback) {
406406
}
407407

408408
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);
426418
}
427419
}
428420

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFileHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
2323

24+
public boolean isValid;
2425
Callback onResponse;
2526
ReactContext mCtx;
2627
String mTaskId;
@@ -32,6 +33,10 @@ public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
3233
this.mTaskId = taskId;
3334
this.mConfig = config;
3435
this.mCtx = ctx;
36+
if(!new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, config)).isFile()) {
37+
this.isValid = false;
38+
}
39+
this.isValid = true;
3540
}
3641

3742
static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ - (id) init {
574574
return self;
575575
}
576576

577+
- (NSDictionary *)constantsToExport
578+
{
579+
return @{
580+
@"DocumentDir": [FetchBlobFS getDocumentDir],
581+
@"CacheDir" : [FetchBlobFS getCacheDir]
582+
};
583+
}
584+
577585
// Fetch blob data request
578586
RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
579587
taskId:(NSString *)taskId

test/react-native-testkit/lib/test-context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
let tests: Array<TestCase> = []
44
let RCTContext: ReactElement = null
55
let props:any = {}
6-
let timeout = 8000
6+
let timeout = 30000
77

88
export default class TestContext {
99

test/test-android.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ describe('MediaScanner tests ', (report, done) => {
7070
])
7171
})
7272
.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}/>)
7974
return RNFetchBlob
8075
.config({
8176
path : dirs.DCIMDir + '/beethoven-'+ Date.now() +'.mp3'
@@ -87,7 +82,10 @@ describe('MediaScanner tests ', (report, done) => {
8782
path : resp.path()
8883
}])
8984
.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}/>)
9189
done()
9290
})
9391
})

test/test-fs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
2222
let dirs = RNFetchBlob.fs.dirs
2323

2424
describe('Get storage folders', (report, done) => {
25+
console.log(dirs)
2526
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} />,
2730
<Assert key="check properties"
2831
expect={['DocumentDir', 'CacheDir']}
2932
comparer={Comparer.hasProperties}

test/test-init.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const { Assert, Comparer, Info, describe, prop } = RNTest
1818
// test environment variables
1919

2020
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')
2222
prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
2323
prop('styles', {
2424
image : {
@@ -52,7 +52,7 @@ describe('GET image from server', (report, done) => {
5252
})
5353

5454

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')
5858
require('./test-android')

0 commit comments

Comments
 (0)