You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2013/12/13 17:18:48 UTC

[06/19] git commit: CB-5407: Move readdir methods into FS modules

CB-5407: Move readdir methods into FS modules


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

Branch: refs/heads/dev
Commit: 85945d84b96c4005997955306ae8bb8331d2bff3
Parents: 919f99f
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Nov 27 14:35:55 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500

----------------------------------------------------------------------
 src/android/ContentFilesystem.java |  7 +++++++
 src/android/FileUtils.java         | 33 ++++++++++++-----------------
 src/android/Filesystem.java        |  4 ++++
 src/android/LocalFilesystem.java   | 37 +++++++++++++++++++++++++++++++--
 4 files changed, 59 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/85945d84/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 7325ac6..8fef685 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -5,6 +5,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 
 import org.apache.cordova.CordovaInterface;
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -64,5 +65,11 @@ public class ContentFilesystem implements Filesystem {
 			throws NoModificationAllowedException {
 		throw new NoModificationAllowedException("Cannot remove content url");
 	}
+	@Override
+	public JSONArray readEntriesAtLocalURL(LocalFilesystemURL inputURL)
+			throws FileNotFoundException {
+		// TODO Auto-generated method stub
+		return null;
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/85945d84/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index a373d50..01ce90b 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -337,7 +337,7 @@ public class FileUtils extends CordovaPlugin {
         else if (action.equals("readEntries")) {
             final String fname=args.getString(0);
             threadhelper( new FileOp( ){
-                public void run() throws FileNotFoundException, JSONException {
+                public void run() throws FileNotFoundException, JSONException, MalformedURLException {
                     JSONArray entries = readEntries(fname);
                     callbackContext.success(entries);
                 }
@@ -433,27 +433,20 @@ public class FileUtils extends CordovaPlugin {
      * @return a JSONArray containing JSONObjects that represent Entry objects.
      * @throws FileNotFoundException if the directory is not found.
      * @throws JSONException
+     * @throws MalformedURLException 
      */
-    private JSONArray readEntries(String fileName) throws FileNotFoundException, JSONException {
-        File fp = createFileObject(fileName);
-
-        if (!fp.exists()) {
-            // The directory we are listing doesn't exist so we should fail.
-            throw new FileNotFoundException();
-        }
-
-        JSONArray entries = new JSONArray();
-
-        if (fp.isDirectory()) {
-            File[] files = fp.listFiles();
-            for (int i = 0; i < files.length; i++) {
-                if (files[i].canRead()) {
-                    entries.put(getEntry(files[i]));
-                }
-            }
+    private JSONArray readEntries(String baseURLstr) throws FileNotFoundException, JSONException, MalformedURLException {
+        try {
+        	LocalFilesystemURL inputURL = new LocalFilesystemURL(baseURLstr);
+        	Filesystem fs = this.filesystemForURL(inputURL);
+        	if (fs == null) {
+        		throw new MalformedURLException("No installed handlers for this URL");
+        	}
+        	return fs.readEntriesAtLocalURL(inputURL);
+        
+        } catch (IllegalArgumentException e) {
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
-
-        return entries;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/85945d84/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index a4730ba..4abd403 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -1,7 +1,9 @@
 package org.apache.cordova.file;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -16,4 +18,6 @@ public interface Filesystem {
 
 	boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException, NoModificationAllowedException;
 
+	JSONArray readEntriesAtLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException;
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/85945d84/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index aca9d15..7d3aacb 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -4,6 +4,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -36,7 +37,6 @@ public class LocalFilesystem implements Filesystem {
         int end = path.endsWith("/") ? 1 : 0;
         String[] parts = path.substring(0,path.length()-end).split("/");
         String name = parts[parts.length-1];
-        Log.d("FILEDEBUG", name);
         entry.put("isFile", !isDir);
         entry.put("isDirectory", isDir);
         entry.put("name", name);
@@ -65,7 +65,7 @@ public class LocalFilesystem implements Filesystem {
     	  entry.put("isFile", fp.isFile());
     	  entry.put("isDirectory", fp.isDirectory());
     	  entry.put("name", fp.getName());
-    	  entry.put("fullPath", "file://" + fp.getAbsolutePath());
+    	  entry.put("fullPath", inputURL.fullPath);
     	  // The file system can't be specified, as it would lead to an infinite loop.
     	  // But we can specify the type of FS, and the rest can be reconstructed in JS.
     	  entry.put("filesystem", inputURL.filesystemType);
@@ -181,4 +181,37 @@ public class LocalFilesystem implements Filesystem {
         }
 	}
 
+	@Override
+	public JSONArray readEntriesAtLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
+        File fp = new File(filesystemPathForURL(inputURL));
+
+        if (!fp.exists()) {
+            // The directory we are listing doesn't exist so we should fail.
+            throw new FileNotFoundException();
+        }
+
+        JSONArray entries = new JSONArray();
+
+        if (fp.isDirectory()) {
+            File[] files = fp.listFiles();
+            for (int i = 0; i < files.length; i++) {
+                if (files[i].canRead()) {
+                    try {
+						entries.put(makeEntryForPath(fullPathForFilesystemPath(files[i].getAbsolutePath()), inputURL.filesystemType, files[i].isDirectory()));
+					} catch (JSONException e) {
+					}
+                }
+            }
+        }
+
+        return entries;
+	}
+
+	private String fullPathForFilesystemPath(String absolutePath) {
+		if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) {
+			return absolutePath.substring(this.fsRoot.length());
+		}
+		return null;
+	}
+
 }