You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/02/12 16:56:39 UTC

android commit: [CB-2095] Delete file on FileTransfer.download fail

Updated Branches:
  refs/heads/master 1246a81d3 -> dd86d7a5e


[CB-2095] Delete file on FileTransfer.download fail


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/dd86d7a5
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/dd86d7a5
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/dd86d7a5

Branch: refs/heads/master
Commit: dd86d7a5edb817b7be00c31c61db125de4184d2d
Parents: 1246a81
Author: Andrew Grieve <ag...@chromium.org>
Authored: Tue Feb 12 10:55:33 2013 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Feb 12 10:55:33 2013 -0500

----------------------------------------------------------------------
 framework/src/org/apache/cordova/FileTransfer.java |   30 +++++++++++---
 1 files changed, 23 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/dd86d7a5/framework/src/org/apache/cordova/FileTransfer.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java
index edf417f..623baf8 100644
--- a/framework/src/org/apache/cordova/FileTransfer.java
+++ b/framework/src/org/apache/cordova/FileTransfer.java
@@ -77,6 +77,7 @@ public class FileTransfer extends CordovaPlugin {
     private static final class RequestContext {
         String source;
         String target;
+        File targetFile;
         CallbackContext callbackContext;
         InputStream currentInputStream;
         OutputStream currentOutputStream;
@@ -626,11 +627,13 @@ public class FileTransfer extends CordovaPlugin {
                 URLConnection connection = null;
                 HostnameVerifier oldHostnameVerifier = null;
                 SSLSocketFactory oldSocketFactory = null;
+                File file = null;
+                PluginResult result = null;
 
                 try {
-
+                    file = getFileFromPath(target);
+                    context.targetFile = file;
                     // create needed directories
-                    File file = getFileFromPath(target);
                     file.getParentFile().mkdirs();
         
                     // connect to server
@@ -718,22 +721,22 @@ public class FileTransfer extends CordovaPlugin {
                     FileUtils fileUtil = new FileUtils();
                     JSONObject fileEntry = fileUtil.getEntry(file);
                     
-                    context.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileEntry));
+                    result = new PluginResult(PluginResult.Status.OK, fileEntry);
                 } catch (FileNotFoundException e) {
                     JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection);
                     Log.e(LOG_TAG, error.toString(), e);
-                    context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
+                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                 } catch (IOException e) {
                     JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
                     Log.e(LOG_TAG, error.toString(), e);
-                    context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
+                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                 } catch (JSONException e) {
                     Log.e(LOG_TAG, e.getMessage(), e);
-                    context.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
+                    result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
                 } catch (Throwable e) {
                     JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
                     Log.e(LOG_TAG, error.toString(), e);
-                    context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
+                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                 } finally {
                     synchronized (activeRequests) {
                         activeRequests.remove(objectId);
@@ -747,6 +750,15 @@ public class FileTransfer extends CordovaPlugin {
                             https.setSSLSocketFactory(oldSocketFactory);
                         }
                     }
+
+                    if (result == null) {
+                        result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, target, connection));
+                    }
+                    // Remove incomplete download.
+                    if (result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) {
+                        file.delete();
+                    }
+                    context.sendPluginResult(result);
                 }
             }
         });
@@ -809,6 +821,10 @@ public class FileTransfer extends CordovaPlugin {
             context = activeRequests.remove(objectId);
         }
         if (context != null) {
+            File file = context.targetFile;
+            if (file != null) {
+                file.delete();
+            }
             // Trigger the abort callback immediately to minimize latency between it and abort() being called.
             JSONObject error = createFileTransferError(ABORTED_ERR, context.source, context.target, -1);
             synchronized (context) {