|
| 1 | +package com.RNFetchBlob; |
| 2 | + |
| 3 | +import android.app.IntentService; |
| 4 | +import android.content.Intent; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.text.TextUtils; |
| 8 | + |
| 9 | +import com.facebook.react.modules.network.*; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.MalformedURLException; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +import okhttp3.Call; |
| 20 | +import okhttp3.Headers; |
| 21 | +import okhttp3.MediaType; |
| 22 | +import okhttp3.MultipartBody; |
| 23 | +import okhttp3.OkHttpClient; |
| 24 | +import okhttp3.Request; |
| 25 | +import okhttp3.RequestBody; |
| 26 | +import okhttp3.Response; |
| 27 | + |
| 28 | +/** |
| 29 | + * Created by pkwak on 11/15/16. |
| 30 | + * IntentService for POSTing multi-form data. Designed for long-uploads with file |
| 31 | + * (but can use data body). |
| 32 | + */ |
| 33 | + |
| 34 | +public class RNFetchBlobService extends IntentService implements ProgressListener { |
| 35 | + public static String RNFetchBlobServiceBroadcast = "RNFetchBlobServiceBroadcast"; |
| 36 | + |
| 37 | + public static String CategorySuccess = "CategorySuccess"; |
| 38 | + public static String CategoryFail = "CategoryFail"; |
| 39 | + public static String CategoryProgress = "CategoryProgress"; |
| 40 | + |
| 41 | + public static String BroadcastMsg = "BroadcastMsg"; |
| 42 | + public static String BroadcastProgressMap = "BroadcastProgressMap"; |
| 43 | + public static String BroadcastTaskId = "BroadcastTaskId"; |
| 44 | + |
| 45 | + public static String KeyWritten = "KeyWritten"; |
| 46 | + public static String KeyTotal = "KeyTotal"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates an IntentService. Invoked by your subclass's constructor. |
| 50 | + */ |
| 51 | + public RNFetchBlobService() { |
| 52 | + super("RNFetchBlobService"); |
| 53 | + } |
| 54 | + |
| 55 | + private String _taskId = null; |
| 56 | + @Override |
| 57 | + protected void onHandleIntent(final Intent intent) { |
| 58 | + |
| 59 | + Bundle bundle = intent.getExtras(); |
| 60 | + _taskId = bundle.getString("taskId"); |
| 61 | + String url = bundle.getString("url"); |
| 62 | + HashMap<String, String> mheaders = (HashMap<String, String>)bundle.getSerializable("mheaders"); |
| 63 | + ArrayList<Object> requestBodyArray = (ArrayList<Object>)bundle.getSerializable("requestBodyArray"); |
| 64 | + |
| 65 | + MultipartBody.Builder requestBuilder = new MultipartBody.Builder() |
| 66 | + .setType(MultipartBody.FORM); |
| 67 | + for(Object bodyPart : requestBodyArray) { |
| 68 | + if (bodyPart instanceof HashMap) { |
| 69 | + HashMap<String, String> bodyMap = (HashMap<String, String>)bodyPart; |
| 70 | + String name = bodyMap.get("name"); |
| 71 | + String type = bodyMap.get("type"); |
| 72 | + String filename = bodyMap.get("filename"); |
| 73 | + String data = bodyMap.get("data"); |
| 74 | + File file = null; |
| 75 | + MediaType mediaType = type != null |
| 76 | + ? MediaType.parse(type) |
| 77 | + : filename == null |
| 78 | + ? MediaType.parse("text/plain") |
| 79 | + : MediaType.parse("application/octet-stream"); |
| 80 | + if(filename != null && data.startsWith("RNFetchBlob-")) { |
| 81 | + try { |
| 82 | + String normalizedUri = RNFetchBlobFS.normalizePath(data.replace(RNFetchBlobConst.FILE_PREFIX, "")); |
| 83 | + file = new File(String.valueOf(Uri.parse(normalizedUri))); |
| 84 | + } catch (Exception e) { |
| 85 | + file = null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + String contentDisposition = "form-data" |
| 90 | + + (!TextUtils.isEmpty(name) ? "; name=" + name : "") |
| 91 | + + (!TextUtils.isEmpty(filename) ? "; filename=" + filename : ""); |
| 92 | + |
| 93 | + requestBuilder.addPart( |
| 94 | + Headers.of("Content-Disposition", contentDisposition), |
| 95 | + file != null |
| 96 | + ? RequestBody.create(mediaType, file) |
| 97 | + : RequestBody.create(mediaType, data) |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + RequestBody innerRequestBody = requestBuilder.build(); |
| 102 | + |
| 103 | + ProgressRequestBody requestBody = new ProgressRequestBody(innerRequestBody, this); |
| 104 | + |
| 105 | + final Request.Builder builder = new Request.Builder(); |
| 106 | + try { |
| 107 | + builder.url(new URL(url)); |
| 108 | + } catch (MalformedURLException e) { |
| 109 | + Intent broadcastIntent = new Intent(); |
| 110 | + broadcastIntent.setAction(RNFetchBlobServiceBroadcast); |
| 111 | + broadcastIntent.addCategory(CategoryFail); |
| 112 | + broadcastIntent.putExtra(BroadcastMsg, "Could not create URL : " + e.getMessage().getBytes()); |
| 113 | + sendBroadcast(broadcastIntent); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + builder.post(requestBody); |
| 118 | + for(String key : mheaders.keySet()) { |
| 119 | + builder.addHeader(key, mheaders.get(key)); |
| 120 | + } |
| 121 | + |
| 122 | + OkHttpClient client = new OkHttpClient.Builder() |
| 123 | + .writeTimeout(24, TimeUnit.HOURS) |
| 124 | + .readTimeout(24, TimeUnit.HOURS) |
| 125 | + .build(); |
| 126 | + |
| 127 | + final Call call = client.newCall(builder.build()); |
| 128 | + call.enqueue(new okhttp3.Callback() { |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onFailure(Call call, IOException e) { |
| 132 | + Intent broadcastIntent = new Intent(); |
| 133 | + broadcastIntent.setAction(RNFetchBlobServiceBroadcast); |
| 134 | + broadcastIntent.addCategory(CategoryFail); |
| 135 | + broadcastIntent.putExtra(BroadcastMsg, e.getMessage().getBytes()); |
| 136 | + broadcastIntent.putExtra(BroadcastTaskId, _taskId); |
| 137 | + sendBroadcast(broadcastIntent); |
| 138 | + call.cancel(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onResponse(Call call, Response response) throws IOException { |
| 143 | + // This is http-response success. Can be 2xx/4xx/5xx/etc. |
| 144 | + Intent broadcastIntent = new Intent(); |
| 145 | + broadcastIntent.setAction(RNFetchBlobServiceBroadcast); |
| 146 | + broadcastIntent.addCategory(CategorySuccess); |
| 147 | + broadcastIntent.putExtra(BroadcastMsg, response.body().bytes()); |
| 148 | + broadcastIntent.putExtra(BroadcastTaskId, _taskId); |
| 149 | + sendBroadcast(broadcastIntent); |
| 150 | + |
| 151 | + response.body().close(); |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + private int _progressPercent = 0; |
| 159 | + private long _lastProgressTime = 0; |
| 160 | + @Override |
| 161 | + public void onProgress(long bytesWritten, long contentLength, boolean done) { |
| 162 | + |
| 163 | + // no more than once per % |
| 164 | + int currentPercent = (int)((bytesWritten * 100) / contentLength); |
| 165 | + if (currentPercent <= _progressPercent) { |
| 166 | + return; |
| 167 | + } |
| 168 | + _progressPercent = currentPercent; |
| 169 | + |
| 170 | + // no more than twice a second. |
| 171 | + long now = System.currentTimeMillis(); |
| 172 | + if (_lastProgressTime + 500 > now) { |
| 173 | + return; |
| 174 | + } |
| 175 | + _lastProgressTime = now; |
| 176 | + |
| 177 | + Intent broadcastIntent = new Intent(); |
| 178 | + broadcastIntent.setAction(RNFetchBlobServiceBroadcast); |
| 179 | + broadcastIntent.addCategory(CategoryProgress); |
| 180 | + broadcastIntent.putExtra(BroadcastTaskId, _taskId); |
| 181 | + HashMap map = new HashMap(); |
| 182 | + map.put(KeyWritten, Long.valueOf(bytesWritten)); |
| 183 | + map.put(KeyTotal, Long.valueOf(contentLength)); |
| 184 | + broadcastIntent.putExtra(BroadcastProgressMap, map); |
| 185 | + sendBroadcast(broadcastIntent); |
| 186 | + } |
| 187 | +} |
0 commit comments