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

Commit 8121dbf

Browse files
committed
Apply possible fix to fs.readFile and fs.readStream for #287
1 parent cafb7f2 commit 8121dbf

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,28 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
138138
* @param promise
139139
*/
140140
static public void readFile(String path, String encoding, final Promise promise ) {
141-
path = normalizePath(path);
141+
String resolved = normalizePath(path);
142+
if(resolved != null)
143+
path = resolved;
142144
try {
143145
byte[] bytes;
144146

145-
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
147+
if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
146148
String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
147149
long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
148150
bytes = new byte[(int) length];
149151
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
150152
in.read(bytes, 0, (int) length);
151153
in.close();
152154
}
155+
// issue 287
156+
else if(resolved == null) {
157+
InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
158+
int length = (int) in.available();
159+
bytes = new byte[length];
160+
in.read(bytes);
161+
in.close();
162+
}
153163
else {
154164
File f = new File(path);
155165
int length = (int) f.length();
@@ -225,17 +235,23 @@ static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
225235
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
226236
*/
227237
public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
228-
path = normalizePath(path);
238+
String resolved = normalizePath(path);
239+
if(resolved != null)
240+
path = resolved;
229241
try {
230242

231243
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
232244
if(bufferSize > 0)
233245
chunkSize = bufferSize;
234246

235247
InputStream fs;
236-
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
248+
if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
237249
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238250
}
251+
// fix issue 287
252+
else if(resolved == null) {
253+
fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
254+
}
239255
else {
240256
fs = new FileInputStream(new File(path));
241257
}

0 commit comments

Comments
 (0)