18
18
import java .io .InputStream ;
19
19
import java .util .ArrayList ;
20
20
21
+ import android .net .Uri ;
21
22
import okhttp3 .MediaType ;
22
23
import okhttp3 .RequestBody ;
23
24
import okio .BufferedSink ;
@@ -68,7 +69,7 @@ RNFetchBlobBody setBody(String body) {
68
69
try {
69
70
switch (requestType ) {
70
71
case SingleFile :
71
- requestStream = getReuqestStream ();
72
+ requestStream = getRequestStream ();
72
73
contentLength = requestStream .available ();
73
74
break ;
74
75
case AsIs :
@@ -135,7 +136,7 @@ boolean clearRequestBody() {
135
136
return true ;
136
137
}
137
138
138
- private InputStream getReuqestStream () throws Exception {
139
+ private InputStream getRequestStream () throws Exception {
139
140
140
141
// upload from storage
141
142
if (rawBody .startsWith (RNFetchBlobConst .FILE_PREFIX )) {
@@ -159,6 +160,13 @@ private InputStream getReuqestStream() throws Exception {
159
160
throw new Exception ("error when getting request stream: " +e .getLocalizedMessage ());
160
161
}
161
162
}
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
+ }
162
170
}
163
171
// base 64 encoded
164
172
else {
@@ -224,6 +232,20 @@ private File createMultipartBodyCache() throws IOException {
224
232
RNFetchBlobUtils .emitWarningEvent ("Failed to create form data from path :" + orgPath + ", file not exists." );
225
233
}
226
234
}
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
+ }
227
249
}
228
250
// base64 embedded file content
229
251
else {
@@ -289,7 +311,7 @@ private void pipeStreamToFileStream(InputStream is, FileOutputStream os) throws
289
311
* Compute approximate content length for form data
290
312
* @return ArrayList<FormField>
291
313
*/
292
- private ArrayList <FormField > countFormDataLength () {
314
+ private ArrayList <FormField > countFormDataLength () throws IOException {
293
315
long total = 0 ;
294
316
ArrayList <FormField > list = new ArrayList <>();
295
317
ReactApplicationContext ctx = RNFetchBlob .RCTContext ;
@@ -320,6 +342,21 @@ else if (field.filename != null) {
320
342
File file = new File (RNFetchBlobFS .normalizePath (orgPath ));
321
343
total += file .length ();
322
344
}
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
+ }
323
360
}
324
361
// base64 embedded file content
325
362
else {
0 commit comments