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

Commit 9ec437b

Browse files
committed
Fix Android custom MIME type function
1 parent f493167 commit 9ec437b

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ static boolean isAsset(String path) {
787787
}
788788

789789
static String normalizePath(String path) {
790-
if(path.startsWith("bundle-assets://")) {
790+
if(path.startsWith(assetPrefix)) {
791791
return path;
792792
}
793793
else if (path.startsWith("content://")) {

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

+44-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.loopj.android.http.MySSLSocketFactory;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.security.KeyStore;
2325

2426
import cz.msebera.android.httpclient.HttpEntity;
@@ -111,6 +113,8 @@ public void run() {
111113

112114
req = new AsyncHttpClient();
113115

116+
req.setLoggingEnabled(false);
117+
114118
// use trusty SSL socket
115119
if(this.options.trusty) {
116120
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
@@ -196,16 +200,34 @@ void buildFormEntity(ReadableArray body) {
196200
String data = map.getString("data");
197201
// file field
198202
if(map.hasKey("filename")) {
203+
String mime = map.hasKey("type") ? map.getString("type") : ContentType.APPLICATION_OCTET_STREAM.getMimeType();
199204
String filename = map.getString("filename");
200205
// upload from storage
201206
if(data.startsWith(filePathPrefix)) {
202207
String orgPath = data.substring(filePathPrefix.length());
203-
File file = new File(RNFetchBlobFS.normalizePath(orgPath));
204-
form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename);
208+
orgPath = RNFetchBlobFS.normalizePath(orgPath);
209+
// path starts with content://
210+
if(RNFetchBlobFS.isAsset(orgPath)) {
211+
try {
212+
String assetName = orgPath.replace(RNFetchBlobFS.assetPrefix, "");
213+
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
214+
long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
215+
byte [] bytes = new byte[(int) length];
216+
in.read(bytes, 0, (int) length);
217+
in.close();
218+
form.addBinaryBody(name, bytes, ContentType.create(mime), filename);
219+
} catch (IOException e) {
220+
// e.printStackTrace();
221+
}
222+
}
223+
else {
224+
File file = new File(RNFetchBlobFS.normalizePath(orgPath));
225+
form.addBinaryBody(name, file, ContentType.create(mime), filename);
226+
}
205227
}
206228
// base64 embedded file content
207229
else {
208-
form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename);
230+
form.addBinaryBody(name, Base64.decode(data, 0), ContentType.create(mime), filename);
209231
}
210232
}
211233
// data field
@@ -228,8 +250,25 @@ void buildEntity(String body) {
228250
byte [] blob;
229251
// upload from storage
230252
if(body.startsWith(filePathPrefix)) {
231-
String filePath = body.substring(filePathPrefix.length());
232-
entity = new FileEntity(new File(RNFetchBlobFS.normalizePath(filePath)));
253+
String orgPath = body.substring(filePathPrefix.length());
254+
orgPath = RNFetchBlobFS.normalizePath(orgPath);
255+
// handle
256+
if(RNFetchBlobFS.isAsset(orgPath)) {
257+
try {
258+
String assetName = orgPath.replace(RNFetchBlobFS.assetPrefix, "");
259+
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
260+
long length = 0;
261+
length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
262+
byte [] bytes = new byte[(int) length];
263+
in.read(bytes, 0, (int) length);
264+
in.close();
265+
entity = new ByteArrayEntity(bytes);
266+
} catch (IOException e) {
267+
// e.printStackTrace();
268+
}
269+
}
270+
else
271+
entity = new FileEntity(new File(RNFetchBlobFS.normalizePath(orgPath)));
233272
}
234273
else {
235274
blob = Base64.decode(body, 0);

0 commit comments

Comments
 (0)