Skip to content

Commit

Permalink
Catch exceptions on sd card directories constants creation. Methods t…
Browse files Browse the repository at this point in the history
…o get sd card directories. sd card directiories as constants deprecated warning
  • Loading branch information
Artur Chrusciel committed Dec 5, 2017
1 parent 821eeb0 commit 22fd32a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
18 changes: 17 additions & 1 deletion android.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,25 @@ function addCompleteDownload(config) {
return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
}

function getSDCardDir() {
if(Platform.OS === 'android')
return RNFetchBlob.getSDCardDir()
else
return Promise.reject('RNFetchBlob.android.getSDCardDir only supports Android.')
}

function getSDCardApplicationDir() {
if(Platform.OS === 'android')
return RNFetchBlob.getSDCardApplicationDir()
else
return Promise.reject('RNFetchBlob.android.getSDCardApplicationDir only supports Android.')
}


export default {
actionViewIntent,
getContentIntent,
addCompleteDownload
addCompleteDownload,
getSDCardDir,
getSDCardApplicationDir,
}
16 changes: 10 additions & 6 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public void run() {
RNFetchBlobFS.createFile(path, content, encode, callback);
}
});

}

@ReactMethod
Expand Down Expand Up @@ -136,7 +135,6 @@ public void run() {
RNFetchBlobFS.createFileASCII(path, dataArray, callback);
}
});

}

@ReactMethod
Expand Down Expand Up @@ -167,7 +165,6 @@ public void run() {
RNFetchBlobFS.cp(path, dest, callback);
}
});

}

@ReactMethod
Expand Down Expand Up @@ -228,7 +225,6 @@ public void run() {
RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
}
});

}

@ReactMethod
Expand Down Expand Up @@ -263,7 +259,6 @@ public void run() {
new RNFetchBlobFS(ctx).scanFile(p, m, callback);
}
});

}

@ReactMethod
Expand Down Expand Up @@ -324,7 +319,7 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
@ReactMethod
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
}
}

@ReactMethod
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
Expand Down Expand Up @@ -370,4 +365,13 @@ public void addCompleteDownload (ReadableMap config, Promise promise) {

}

@ReactMethod
public void getSDCardDir(Promise promise) {
RNFetchBlobFS.getSDCardDir(promise);
}

@ReactMethod
public void getSDCardApplicationDir(Promise promise) {
RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
}
}
28 changes: 27 additions & 1 deletion android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,38 @@ static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx)
state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
try {
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
} catch(Exception e) {
res.put("SDCardApplicationDir", "");
}
}
res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
return res;
}

static public void getSDCardDir(Promise promise) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
promise.resolve(Environment.getExternalStorageDirectory().getAbsolutePath());
} else {
promise.reject("RNFetchBlob.getSDCardDir", "External storage not mounted");
}

}

static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
final String path = ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath();
promise.resolve(path);
} catch (Exception e) {
promise.reject("RNFetchBlob.getSDCardApplicationDir", e.getLocalizedMessage());
}
} else {
promise.reject("RNFetchBlob.getSDCardApplicationDir", "External storage not mounted");
}
}

/**
* Static method that returns a temp file path
* @param ctx React Native application context
Expand Down
13 changes: 11 additions & 2 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ const dirs = {
MovieDir : RNFetchBlob.MovieDir,
DownloadDir : RNFetchBlob.DownloadDir,
DCIMDir : RNFetchBlob.DCIMDir,
SDCardDir : RNFetchBlob.SDCardDir,
SDCardApplicationDir : RNFetchBlob.SDCardApplicationDir,
get SDCardDir() {
console.warn('SDCardDir as a constant is deprecated and will be removed in feature release. ' +
'Use RNFetchBlob.android.getSDCardDir():Promise instead.');
return RNFetchBlob.SDCardDir;
},
get SDCardApplicationDir() {
console.warn('SDCardApplicationDir as a constant is deprecated and will be removed in feature release. ' +
'Use RNFetchBlob.android.getSDCardApplicationDir():Promise instead. ' +
'This variable can be empty on error in native code.');
return RNFetchBlob.SDCardApplicationDir;
},
MainBundleDir : RNFetchBlob.MainBundleDir,
LibraryDir : RNFetchBlob.LibraryDir
}
Expand Down

0 comments on commit 22fd32a

Please sign in to comment.