Skip to content

Commit 20d1be4

Browse files
authored
Merge pull request #45 from ataillefer/0.10.9
Bug fix: Support Android content URIs for upload
2 parents fe56a52 + cd1eb1e commit 20d1be4

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.InputStream;
1919
import java.util.ArrayList;
2020

21+
import android.net.Uri;
2122
import okhttp3.MediaType;
2223
import okhttp3.RequestBody;
2324
import okio.BufferedSink;
@@ -68,7 +69,7 @@ RNFetchBlobBody setBody(String body) {
6869
try {
6970
switch (requestType) {
7071
case SingleFile:
71-
requestStream = getReuqestStream();
72+
requestStream = getRequestStream();
7273
contentLength = requestStream.available();
7374
break;
7475
case AsIs:
@@ -135,7 +136,7 @@ boolean clearRequestBody() {
135136
return true;
136137
}
137138

138-
private InputStream getReuqestStream() throws Exception {
139+
private InputStream getRequestStream() throws Exception {
139140

140141
// upload from storage
141142
if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
@@ -159,6 +160,13 @@ private InputStream getReuqestStream() throws Exception {
159160
throw new Exception("error when getting request stream: " +e.getLocalizedMessage());
160161
}
161162
}
163+
} else if (rawBody.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
164+
String contentURI = rawBody.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
165+
try {
166+
return RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(contentURI));
167+
} catch (Exception e) {
168+
throw new Exception("error when getting request stream for content URI: " + contentURI, e);
169+
}
162170
}
163171
// base 64 encoded
164172
else {
@@ -224,6 +232,20 @@ private File createMultipartBodyCache() throws IOException {
224232
RNFetchBlobUtils.emitWarningEvent("Failed to create form data from path :" + orgPath + ", file not exists.");
225233
}
226234
}
235+
} else if (data.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
236+
String contentURI = data.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
237+
InputStream is = null;
238+
try {
239+
is = ctx.getContentResolver().openInputStream(Uri.parse(contentURI));
240+
pipeStreamToFileStream(is, os);
241+
} catch(Exception e) {
242+
RNFetchBlobUtils.emitWarningEvent(
243+
"Failed to create form data from content URI:" + contentURI + ", " + e.getLocalizedMessage());
244+
} finally {
245+
if (is != null) {
246+
is.close();
247+
}
248+
}
227249
}
228250
// base64 embedded file content
229251
else {
@@ -289,7 +311,7 @@ private void pipeStreamToFileStream(InputStream is, FileOutputStream os) throws
289311
* Compute approximate content length for form data
290312
* @return ArrayList<FormField>
291313
*/
292-
private ArrayList<FormField> countFormDataLength() {
314+
private ArrayList<FormField> countFormDataLength() throws IOException {
293315
long total = 0;
294316
ArrayList<FormField> list = new ArrayList<>();
295317
ReactApplicationContext ctx = RNFetchBlob.RCTContext;
@@ -320,6 +342,21 @@ else if (field.filename != null) {
320342
File file = new File(RNFetchBlobFS.normalizePath(orgPath));
321343
total += file.length();
322344
}
345+
} else if (data.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
346+
String contentURI = data.substring(RNFetchBlobConst.CONTENT_PREFIX.length());
347+
InputStream is = null;
348+
try {
349+
is = ctx.getContentResolver().openInputStream(Uri.parse(contentURI));
350+
long length = is.available();
351+
total += length;
352+
} catch(Exception e) {
353+
RNFetchBlobUtils.emitWarningEvent(
354+
"Failed to estimate form data length from content URI:" + contentURI + ", " + e.getLocalizedMessage());
355+
} finally {
356+
if (is != null) {
357+
is.close();
358+
}
359+
}
323360
}
324361
// base64 embedded file content
325362
else {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class RNFetchBlobConst {
77
public static final String EVENT_HTTP_STATE = "RNFetchBlobState";
88
public static final String EVENT_MESSAGE = "RNFetchBlobMessage";
99
public static final String FILE_PREFIX = "RNFetchBlob-file://";
10+
public static final String CONTENT_PREFIX = "RNFetchBlob-content://";
1011
public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
1112
public static final String FILE_PREFIX_CONTENT = "content://";
1213
public static final String DATA_ENCODE_URI = "uri";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ else if(cType.isEmpty()) {
266266
requestType = RequestType.SingleFile;
267267
}
268268
if(rawRequestBody != null) {
269-
if(rawRequestBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
269+
if (rawRequestBody.startsWith(RNFetchBlobConst.FILE_PREFIX)
270+
|| rawRequestBody.startsWith(RNFetchBlobConst.CONTENT_PREFIX)) {
270271
requestType = RequestType.SingleFile;
271272
}
272273
else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
7979
}
8080

8181
function wrap(path:string):string {
82-
return 'RNFetchBlob-file://' + path
82+
const prefix = path.startsWith('content://') ? 'RNFetchBlob-content://' : 'RNFetchBlob-file://'
83+
return prefix + path
8384
}
8485

8586
/**

0 commit comments

Comments
 (0)