incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bows...@apache.org
Subject [17/50] git commit: changes after review of macdonst, download returns FileEntry
Date Tue, 03 Jan 2012 18:57:09 GMT
changes after review of macdonst, download returns FileEntry


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

Branch: refs/heads/master
Commit: 75963c88d5923529f42af010d76c8c21bc79b863
Parents: 8904c67
Author: Alexander Keller <ak@aflx.de>
Authored: Tue Nov 29 08:39:57 2011 +0100
Committer: Alexander Keller <ak@aflx.de>
Committed: Tue Nov 29 08:39:57 2011 +0100

----------------------------------------------------------------------
 framework/src/com/phonegap/FileTransfer.java |   32 ++++++++++++++------
 1 files changed, 22 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/75963c88/framework/src/com/phonegap/FileTransfer.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/FileTransfer.java b/framework/src/com/phonegap/FileTransfer.java
index fabe14d..6bd6d4e 100644
--- a/framework/src/com/phonegap/FileTransfer.java
+++ b/framework/src/com/phonegap/FileTransfer.java
@@ -99,9 +99,9 @@ public class FileTransfer extends Plugin {
                 Log.d(LOG_TAG, "****** About to return a result from upload");
                 return new PluginResult(PluginResult.Status.OK, r.toJSONObject());
             } else if (action.equals("download")) {
-                String r = download(file, server);
+                JSONObject r = download(file, server);
                 Log.d(LOG_TAG, "****** About to return a result from download");
-                return new PluginResult(PluginResult.Status.OK, r);
+                return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry");
             } else {
                 return new PluginResult(PluginResult.Status.INVALID_ACTION);
             }
@@ -371,13 +371,16 @@ public class FileTransfer extends Plugin {
      *
      * @param server        URL of the server to receive the file
      * @param file      	Full path of the file on the file system
-     * @return String 		containing the path to the downloaded file
+     * @return JSONObject 	the downloaded file
      */
-    public String download(String filePath, String sourceUrl) throws IOException {
+    public JSONObject download(String filePath, String sourceUrl) throws IOException {
         try {
             File file = new File(filePath);
+
+            // create needed directories
             file.getParentFile().mkdirs();
 
+            // connect to server
             URL url = new URL(sourceUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod("GET");
@@ -388,23 +391,32 @@ public class FileTransfer extends Plugin {
 
             InputStream inputStream = connection.getInputStream();
             byte[] buffer = new byte[1024];
-            int len1 = 0;
+            int bytesRead = 0;
 
             FileOutputStream outputStream = new FileOutputStream(file);
 
-            while ( (len1 = inputStream.read(buffer)) > 0 ) {
-                outputStream.write(buffer,0, len1);
+            // write bytes to file
+            while ( (bytesRead = inputStream.read(buffer)) > 0 ) {
+                outputStream.write(buffer,0, bytesRead);
             }
 
             outputStream.close();
 
             Log.d(LOG_TAG, "Saved file: " + filePath);
-        } catch (IOException e) {
+
+            // create FileEntry object
+            JSONObject entry = new JSONObject();
+
+            entry.put("isFile", file.isFile());
+            entry.put("isDirectory", file.isDirectory());
+            entry.put("name", file.getName());
+            entry.put("fullPath", file.getAbsolutePath());
+
+            return entry;
+        } catch (Exception e) {
             Log.d(LOG_TAG, e.getMessage(), e);
             throw new IOException("Error while downloading");
         }
-
-        return filePath;
     }
 
     /**


Mime
View raw message