Просмотр исходного кода

Fix Android custom MIME type function

Ben Hsieh 8 лет назад
Родитель
Сommit
9ec437b886

+ 1
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Просмотреть файл

@@ -787,7 +787,7 @@ public class RNFetchBlobFS {
787 787
     }
788 788
 
789 789
     static String normalizePath(String path) {
790
-        if(path.startsWith("bundle-assets://")) {
790
+        if(path.startsWith(assetPrefix)) {
791 791
             return path;
792 792
         }
793 793
         else if (path.startsWith("content://")) {

+ 44
- 5
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Просмотреть файл

@@ -19,6 +19,8 @@ import com.loopj.android.http.Base64;
19 19
 import com.loopj.android.http.MySSLSocketFactory;
20 20
 
21 21
 import java.io.File;
22
+import java.io.IOException;
23
+import java.io.InputStream;
22 24
 import java.security.KeyStore;
23 25
 
24 26
 import cz.msebera.android.httpclient.HttpEntity;
@@ -111,6 +113,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
111 113
 
112 114
             req = new AsyncHttpClient();
113 115
 
116
+            req.setLoggingEnabled(false);
117
+
114 118
             // use trusty SSL socket
115 119
             if(this.options.trusty) {
116 120
                 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
@@ -196,16 +200,34 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
196 200
                 String data = map.getString("data");
197 201
                 // file field
198 202
                 if(map.hasKey("filename")) {
203
+                    String mime = map.hasKey("type") ? map.getString("type") : ContentType.APPLICATION_OCTET_STREAM.getMimeType();
199 204
                     String filename = map.getString("filename");
200 205
                     // upload from storage
201 206
                     if(data.startsWith(filePathPrefix)) {
202 207
                         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
+                        }
205 227
                     }
206 228
                     // base64 embedded file content
207 229
                     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);
209 231
                     }
210 232
                 }
211 233
                 // data field
@@ -228,8 +250,25 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
228 250
             byte [] blob;
229 251
             // upload from storage
230 252
             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)));
233 272
             }
234 273
             else {
235 274
                 blob = Base64.decode(body, 0);