diff --git a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java index 575e0c345..066e59340 100644 --- a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java +++ b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java @@ -65,6 +65,12 @@ public RNFetchBlobBody(String taskId, RNFetchBlobReq.RequestType type, String ra requestType = type; this.rawBody = rawBody; mime = contentType; + if(rawBody != null) { + if(requestType == RNFetchBlobReq.RequestType.AsIs) + contentLength = rawBody.length(); + else + contentLength = caculateOctetContentLength(); + } } @Override @@ -96,7 +102,19 @@ public void writeTo(BufferedSink sink) throws IOException { buffer.flush(); } - private void caculateOctetContentLength() { + boolean clearRequestBody() { + try { + if (bodyCache != null && bodyCache.exists()) { + bodyCache.delete(); + } + } catch(Exception e) { + RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage()); + return false; + } + return true; + } + + private long caculateOctetContentLength() { long total = 0; // upload from storage if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) { @@ -106,31 +124,32 @@ private void caculateOctetContentLength() { if (RNFetchBlobFS.isAsset(orgPath)) { try { String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""); - contentLength = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength(); + total += RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength(); requestStream = RNFetchBlob.RCTContext.getAssets().open(assetName); } catch (IOException e) { -// e.printStackTrace(); + RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage()); } } else { File f = new File(RNFetchBlobFS.normalizePath(orgPath)); try { if(!f.exists()) f.createNewFile(); - contentLength = f.length(); + total += f.length(); requestStream = new FileInputStream(f); } catch (Exception e) { -// callback.invoke(e.getLocalizedMessage(), null); + RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +e.getLocalizedMessage()); } } } else { try { byte[] bytes = Base64.decode(rawBody, 0); - contentLength = bytes.length; requestStream = new ByteArrayInputStream(bytes); + total += requestStream.available(); } catch(Exception ex) { - Log.e("error", ex.getLocalizedMessage()); + RNFetchBlobUtils.emitWarningEvent("RNetchBlob error when counting content length: " +ex.getLocalizedMessage()); } } + return total; } /** @@ -173,7 +192,7 @@ private File createMultipartBodyCache() throws IOException { InputStream in = ctx.getAssets().open(assetName); pipeStreamToFileStream(in, os); } catch (IOException e) { - Log.e("RNFetchBlob", "Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() ); + RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() ); } } // data from normal files @@ -184,7 +203,7 @@ private File createMultipartBodyCache() throws IOException { pipeStreamToFileStream(fs, os); } else { - Log.e("RNFetchBlob", "Failed to create form data from path :" + orgPath + "file not exists."); + RNFetchBlobUtils.emitWarningEvent("RNFetchBlob Failed to create form data from path :" + orgPath + ", file not exists."); } } } @@ -284,7 +303,7 @@ private ArrayList countFormDataLength() { long length = ctx.getAssets().open(assetName).available(); total += length; } catch (IOException e) { - + RNFetchBlobUtils.emitWarningEvent(e.getLocalizedMessage()); } } // general files diff --git a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java index e158326f4..57746f06c 100644 --- a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java +++ b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URL; import java.nio.ByteBuffer; @@ -81,6 +82,7 @@ enum ResponseType { Callback callback; long contentLength; long downloadManagerId; + RNFetchBlobBody requestBody; RequestType requestType; ResponseType responseType; WritableMap respInfo; @@ -207,7 +209,10 @@ else if(this.options.fileCache == true) if(method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) { String cType = getHeaderIgnoreCases(mheaders, "Content-Type").toLowerCase(); - if(cType == null) { + if(rawRequestBodyArray != null) { + requestType = RequestType.Form; + } + else if(cType == null || cType.isEmpty()) { builder.header("Content-Type", "application/octet-stream"); requestType = RequestType.SingleFile; } @@ -235,29 +240,32 @@ else if(this.options.fileCache == true) // set request body switch (requestType) { case SingleFile: - builder.method(method, new RNFetchBlobBody( + requestBody = new RNFetchBlobBody( taskId, requestType, rawRequestBody, MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type")) - )); + ); + builder.method(method, requestBody); break; case AsIs: - builder.method(method, new RNFetchBlobBody( + requestBody = new RNFetchBlobBody( taskId, requestType, rawRequestBody, MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type")) - )); + ); + builder.method(method, requestBody); break; case Form: String boundary = "RNFetchBlob-" + taskId; - builder.method(method, new RNFetchBlobBody( + requestBody = new RNFetchBlobBody( taskId, requestType, rawRequestBodyArray, MediaType.parse("multipart/form-data; boundary="+ boundary) - )); + ); + builder.method(method, requestBody); break; case WithoutBody: @@ -301,8 +309,13 @@ public Response intercept(Chain chain) throws IOException { break; } return originalResponse.newBuilder().body(extended).build(); - } catch(Exception ex) { - RNFetchBlobUtils.emitWarningEvent(ex.getLocalizedMessage()); + } + catch (SocketException ex) { + timeout = true; + } + catch(Exception ex) { + RNFetchBlobUtils.emitWarningEvent("RNFetchBlob error when sending request : " + ex.getLocalizedMessage()); + } return chain.proceed(chain.request()); } @@ -337,7 +350,7 @@ public void onFailure(Call call, IOException e) { } else callback.invoke(e.getLocalizedMessage(), null, null); - removeTaskInfo(); + releaseTaskResource(); } @Override @@ -367,7 +380,7 @@ public void onResponse(Call call, Response response) throws IOException { } catch (Exception error) { error.printStackTrace(); - taskTable.remove(taskId); + releaseTaskResource(); callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause()); } } @@ -375,13 +388,15 @@ public void onResponse(Call call, Response response) throws IOException { /** * Remove cached information of the HTTP task */ - private void removeTaskInfo() { + private void releaseTaskResource() { if(taskTable.containsKey(taskId)) taskTable.remove(taskId); if(uploadProgressReport.containsKey(taskId)) uploadProgressReport.remove(taskId); if(progressReport.containsKey(taskId)) progressReport.remove(taskId); + if(requestBody != null) + requestBody.clearRequestBody(); } /** @@ -455,7 +470,7 @@ private void done(Response resp) { } if(!resp.isSuccessful()) resp.body().close(); - removeTaskInfo(); + releaseTaskResource(); } /**