cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [1/4] git commit: CB-6928: Handle 304 status code
Date Tue, 08 Jul 2014 18:45:06 GMT
Repository: cordova-plugin-file-transfer
Updated Branches:
  refs/heads/master 4e5f10c3a -> 78453b5d1


CB-6928: Handle 304 status code


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/commit/b88584c5
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/tree/b88584c5
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/diff/b88584c5

Branch: refs/heads/master
Commit: b88584c5a3f874f975942ca5c9509526d63825af
Parents: 22f1ea5
Author: Javier Puerto <javier.puerto@becompany.ch>
Authored: Thu Jun 19 02:20:51 2014 +0200
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Tue Jul 8 14:14:12 2014 -0400

----------------------------------------------------------------------
 src/android/FileTransfer.java | 142 ++++++++++++++++++++-----------------
 www/FileTransferError.js      |   1 +
 2 files changed, 79 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/b88584c5/src/android/FileTransfer.java
----------------------------------------------------------------------
diff --git a/src/android/FileTransfer.java b/src/android/FileTransfer.java
index 2b1a2f0..c757c99 100644
--- a/src/android/FileTransfer.java
+++ b/src/android/FileTransfer.java
@@ -76,6 +76,7 @@ public class FileTransfer extends CordovaPlugin {
     public static int INVALID_URL_ERR = 2;
     public static int CONNECTION_ERR = 3;
     public static int ABORTED_ERR = 4;
+    public static int NOT_MODIFIED_ERR = 5;
 
     private static HashMap<String, RequestContext> activeRequests = new HashMap<String,
RequestContext>();
     private static final int MAX_BUFFER_SIZE = 16 * 1024;
@@ -712,6 +713,7 @@ public class FileTransfer extends CordovaPlugin {
                 File file = null;
                 PluginResult result = null;
                 TrackingInputStream inputStream = null;
+                boolean cached = false;
 
                 OutputStream outputStream = null;
                 try {
@@ -763,79 +765,91 @@ public class FileTransfer extends CordovaPlugin {
                         }
         
                         connection.connect();
-    
-                        if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip"))
{
-                            // Only trust content-length header if we understand
-                            // the encoding -- identity or gzip
-                            if (connection.getContentLength() != -1) {
-                                progress.setLengthComputable(true);
-                                progress.setTotal(connection.getContentLength());
+                        if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
{
+                            cached = true;
+                            connection.disconnect();
+                            Log.d(LOG_TAG, "Resource not modified: " + source);
+                            JSONObject error = createFileTransferError(NOT_MODIFIED_ERR,
source, target, connection, null);
+                            result = new PluginResult(PluginResult.Status.ERROR, error);
+                        } else {
+                            if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip"))
{
+                                // Only trust content-length header if we understand
+                                // the encoding -- identity or gzip
+                                if (connection.getContentLength() != -1) {
+                                    progress.setLengthComputable(true);
+                                    progress.setTotal(connection.getContentLength());
+                                }
                             }
+                            inputStream = getInputStream(connection);
                         }
-                        inputStream = getInputStream(connection);
                     }
-                    
-                    try {
-                        synchronized (context) {
-                            if (context.aborted) {
-                                return;
+
+                    if (!cached) {
+                        try {
+                            synchronized (context) {
+                                if (context.aborted) {
+                                    return;
+                                }
+                                context.connection = connection;
                             }
-                            context.connection = connection;
-                        }
-                        
-                        // write bytes to file
-                        byte[] buffer = new byte[MAX_BUFFER_SIZE];
-                        int bytesRead = 0;
-                        outputStream = resourceApi.openOutputStream(targetUri);
-                        while ((bytesRead = inputStream.read(buffer)) > 0) {
-                            outputStream.write(buffer, 0, bytesRead);
-                            // Send a progress event.
-                            progress.setLoaded(inputStream.getTotalRawBytesRead());
-                            PluginResult progressResult = new PluginResult(PluginResult.Status.OK,
progress.toJSONObject());
-                            progressResult.setKeepCallback(true);
-                            context.sendPluginResult(progressResult);
-                        }
-                    } finally {
-                        synchronized (context) {
-                            context.connection = null;
+
+                            // write bytes to file
+                            byte[] buffer = new byte[MAX_BUFFER_SIZE];
+                            int bytesRead = 0;
+                            outputStream = resourceApi.openOutputStream(targetUri);
+                            while ((bytesRead = inputStream.read(buffer)) > 0) {
+                                outputStream.write(buffer, 0, bytesRead);
+                                // Send a progress event.
+                                progress.setLoaded(inputStream.getTotalRawBytesRead());
+                                PluginResult progressResult = new PluginResult(PluginResult.Status.OK,
progress.toJSONObject());
+                                progressResult.setKeepCallback(true);
+                                context.sendPluginResult(progressResult);
+                            }
+                        } finally {
+                            synchronized (context) {
+                                context.connection = null;
+                            }
+                            safeClose(inputStream);
+                            safeClose(outputStream);
                         }
-                        safeClose(inputStream);
-                        safeClose(outputStream);
-                    }
-    
-                    Log.d(LOG_TAG, "Saved file: " + target);
-    
-                    // create FileEntry object
-                    Class webViewClass = webView.getClass();
-                    PluginManager pm = null;
-                    try {
-                        Method gpm = webViewClass.getMethod("getPluginManager");
-                        pm = (PluginManager) gpm.invoke(webView);
-                    } catch (NoSuchMethodException e) {
-                    } catch (IllegalAccessException e) {
-                    } catch (InvocationTargetException e) {
-                    }
-                    if (pm == null) {
+
+                        Log.d(LOG_TAG, "Saved file: " + target);
+
+
+                        // create FileEntry object
+                        Class webViewClass = webView.getClass();
+                        PluginManager pm = null;
                         try {
-                            Field pmf = webViewClass.getField("pluginManager");
-                            pm = (PluginManager)pmf.get(webView);
-                        } catch (NoSuchFieldException e) {
+                            Method gpm = webViewClass.getMethod("getPluginManager");
+                            pm = (PluginManager) gpm.invoke(webView);
+                        } catch (NoSuchMethodException e) {
                         } catch (IllegalAccessException e) {
+                        } catch (InvocationTargetException e) {
                         }
-                    }
-                    FileUtils filePlugin = (FileUtils) pm.getPlugin("File");
-                    if (filePlugin != null) {
-                        JSONObject fileEntry = filePlugin.getEntryForFile(file);
-                        if (fileEntry != null) {
-                            result = new PluginResult(PluginResult.Status.OK, fileEntry);
+                        if (pm == null) {
+                            try {
+                                Field pmf = webViewClass.getField("pluginManager");
+                                pm = (PluginManager)pmf.get(webView);
+                            } catch (NoSuchFieldException e) {
+                            } catch (IllegalAccessException e) {
+                            }
+                        }
+                        file = resourceApi.mapUriToFile(targetUri);
+                        context.targetFile = file;
+                        FileUtils filePlugin = (FileUtils) pm.getPlugin("File");
+                        if (filePlugin != null) {
+                            JSONObject fileEntry = filePlugin.getEntryForFile(file);
+                            if (fileEntry != null) {
+                                result = new PluginResult(PluginResult.Status.OK, fileEntry);
+                            } else {
+                                JSONObject error = createFileTransferError(CONNECTION_ERR,
source, target, connection, null);
+                                Log.e(LOG_TAG, "File plugin cannot represent download path");
+                                result = new PluginResult(PluginResult.Status.IO_EXCEPTION,
error);
+                            }
                         } else {
-                            JSONObject error = createFileTransferError(CONNECTION_ERR, source,
target, connection, null);
-                            Log.e(LOG_TAG, "File plugin cannot represent download path");
-                            result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
+                            Log.e(LOG_TAG, "File plugin not found; cannot save downloaded
file");
+                            result = new PluginResult(PluginResult.Status.ERROR, "File plugin
not found; cannot save downloaded file");
                         }
-                    } else {
-                        Log.e(LOG_TAG, "File plugin not found; cannot save downloaded file");
-                        result = new PluginResult(PluginResult.Status.ERROR, "File plugin
not found; cannot save downloaded file");
                     }
                     
                 } catch (FileNotFoundException e) {
@@ -871,7 +885,7 @@ public class FileTransfer extends CordovaPlugin {
                         result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR,
source, target, connection, null));
                     }
                     // Remove incomplete download.
-                    if (result.getStatus() != PluginResult.Status.OK.ordinal() &&
file != null) {
+                    if (!cached && result.getStatus() != PluginResult.Status.OK.ordinal()
&& file != null) {
                         file.delete();
                     }
                     context.sendPluginResult(result);

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/b88584c5/www/FileTransferError.js
----------------------------------------------------------------------
diff --git a/www/FileTransferError.js b/www/FileTransferError.js
index 1394f59..7121882 100644
--- a/www/FileTransferError.js
+++ b/www/FileTransferError.js
@@ -35,5 +35,6 @@ FileTransferError.FILE_NOT_FOUND_ERR = 1;
 FileTransferError.INVALID_URL_ERR = 2;
 FileTransferError.CONNECTION_ERR = 3;
 FileTransferError.ABORT_ERR = 4;
+FileTransferError.NOT_MODIFIED_ERR = 5;
 
 module.exports = FileTransferError;


Mime
View raw message