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:47 UTC

[05/19] git commit: CB-5407: Move getFile into FS modules

CB-5407: Move getFile 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/0d0af8f1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/0d0af8f1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/0d0af8f1

Branch: refs/heads/dev
Commit: 0d0af8f116daa00983950ffe54a27d4ff76e91a6
Parents: 30988f7
Author: Ian Clelland <ic...@chromium.org>
Authored: Mon Nov 25 11:05:28 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500

----------------------------------------------------------------------
 src/android/ContentFilesystem.java |   5 ++
 src/android/FileUtils.java         |  81 +++++--------------------
 src/android/Filesystem.java        |   4 ++
 src/android/LocalFilesystem.java   | 101 ++++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/0d0af8f1/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index bef40a3..6b34641 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -49,5 +49,10 @@ public class ContentFilesystem implements Filesystem {
     	  throw new IOException();
       }
 	}
+	@Override
+	public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
+			String fileName, JSONObject options, boolean directory) throws IOException {
+		throw new IOException("Cannot create content url");
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/0d0af8f1/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
old mode 100755
new mode 100644
index 8c069b5..32e2a4a
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -418,11 +418,11 @@ public class FileUtils extends CordovaPlugin {
         	LocalFilesystemURL inputURL = new LocalFilesystemURL(decoded);
         	Filesystem fs = this.filesystemForURL(inputURL);
         	if (fs == null) {
-        		throw new MalformedURLException("Unrecognized filesystem URL");
+        		throw new MalformedURLException("No installed handlers for this URL");
         	}
         	return fs.getEntryForLocalURL(inputURL);
         } catch (IllegalArgumentException e) {
-        	throw new IOException();
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
     }   
     
@@ -804,7 +804,7 @@ public class FileUtils extends CordovaPlugin {
     /**
      * Creates or looks up a file.
      *
-     * @param dirPath base directory
+     * @param baseURLstr base directory
      * @param fileName file/directory to lookup or create
      * @param options specify whether to create or not
      * @param directory if true look up directory, if false look up file
@@ -815,72 +815,19 @@ public class FileUtils extends CordovaPlugin {
      * @throws EncodingException
      * @throws JSONException
      */
-    private JSONObject getFile(String dirPath, String fileName, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
-        boolean create = false;
-        boolean exclusive = false;
-        if (options != null) {
-            create = options.optBoolean("create");
-            if (create) {
-                exclusive = options.optBoolean("exclusive");
-            }
-        }
-
-        // Check for a ":" character in the file to line up with BB and iOS
-        if (fileName.contains(":")) {
-            throw new EncodingException("This file has a : in it's name");
-        }
-
-        File fp = createFileObject(dirPath, fileName);
-
-        if (create) {
-            if (exclusive && fp.exists()) {
-                throw new FileExistsException("create/exclusive fails: " + fp.getPath());
-            }
-            if (directory) {
-                fp.mkdir();
-            } else {
-                fp.createNewFile();
-            }
-            if (!fp.exists()) {
-                throw new FileExistsException("create fails: " + fp.getPath());
-            }
-        }
-        else {
-            if (!fp.exists()) {
-                throw new FileNotFoundException("path does not exist: " + fp.getPath());
-            }
-            if (directory) {
-                if (fp.isFile()) {
-                    throw new TypeMismatchException("path doesn't exist or is file: " + fp.getPath());
-                }
-            } else {
-                if (fp.isDirectory()) {
-                    throw new TypeMismatchException("path doesn't exist or is directory: " + fp.getPath());
-                }
-            }
+    private JSONObject getFile(String baseURLstr, String fileName, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
+        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.getFileForLocalURL(inputURL, fileName, options, directory);
+        
+        } catch (IllegalArgumentException e) {
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
 
-        // Return the directory
-        return getEntry(fp);
-    }
-
-    /**
-     * If the path starts with a '/' just return that file object. If not construct the file
-     * object from the path passed in and the file name.
-     *
-     * @param dirPath root directory
-     * @param fileName new file name
-     * @return
-     */
-    private File createFileObject(String dirPath, String fileName) {
-        File fp = null;
-        if (fileName.startsWith("/")) {
-            fp = new File(fileName);
-        } else {
-            dirPath = FileHelper.getRealPath(dirPath, cordova);
-            fp = new File(dirPath + File.separator + fileName);
-        }
-        return fp;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/0d0af8f1/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 1879ad0..19692ad 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -2,10 +2,14 @@ package org.apache.cordova.file;
 
 import java.io.IOException;
 
+import org.json.JSONException;
 import org.json.JSONObject;
 
 public interface Filesystem {
 
 	JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException;
 
+	JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, String fileName,
+			JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException;
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/0d0af8f1/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index c005867..563f8ed 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -8,6 +8,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.util.Base64;
+import android.net.Uri;
 
 public class LocalFilesystem implements Filesystem {
 
@@ -21,6 +22,33 @@ public class LocalFilesystem implements Filesystem {
 		this.cordova = cordova;
 	}
 
+	public String filesystemPathForURL(LocalFilesystemURL url) {
+	    String path = this.fsRoot + url.fullPath;
+	    if (path.endsWith("/")) {
+	      path = path.substring(0, path.length()-1);
+	    }
+	    return path;
+	}
+	
+    public static JSONObject makeEntryForPath(String path, int fsType, Boolean isDir) throws JSONException {
+        JSONObject entry = new JSONObject();
+
+        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);
+        entry.put("fullPath", path);
+        // The file system can't be specified, as it would lead to an infinite loop,
+        // but the filesystem type can
+        entry.put("filesystem", fsType);
+
+        return entry;
+    	
+    }
+
 	@Override
 	public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException {
       File fp = null;
@@ -47,4 +75,77 @@ public class LocalFilesystem implements Filesystem {
       }
 	}
 
+    /**
+     * If the path starts with a '/' just return that file object. If not construct the file
+     * object from the path passed in and the file name.
+     *
+     * @param dirPath root directory
+     * @param fileName new file name
+     * @return
+     */
+    private File createFileObject(String dirPath, String fileName) {
+        File fp = null;
+        if (fileName.startsWith("/")) {
+            fp = new File(this.fsRoot + fileName);
+        } else {
+            fp = new File(this.fsRoot + File.separator + dirPath + File.separator + fileName);
+        }
+        return fp;
+    }
+
+
+	@Override
+	public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
+			String fileName, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
+        boolean create = false;
+        boolean exclusive = false;
+
+        if (options != null) {
+            create = options.optBoolean("create");
+            if (create) {
+                exclusive = options.optBoolean("exclusive");
+            }
+        }
+
+        // Check for a ":" character in the file to line up with BB and iOS
+        if (fileName.contains(":")) {
+            throw new EncodingException("This file has a : in it's name");
+        }
+
+        LocalFilesystemURL requestedURL = new LocalFilesystemURL(Uri.withAppendedPath(inputURL.URL, fileName));
+        
+        File fp = new File(this.filesystemPathForURL(requestedURL));
+
+        if (create) {
+            if (exclusive && fp.exists()) {
+                throw new FileExistsException("create/exclusive fails");
+            }
+            if (directory) {
+                fp.mkdir();
+            } else {
+                fp.createNewFile();
+            }
+            if (!fp.exists()) {
+                throw new FileExistsException("create fails");
+            }
+        }
+        else {
+            if (!fp.exists()) {
+                throw new FileNotFoundException("path does not exist");
+            }
+            if (directory) {
+                if (fp.isFile()) {
+                    throw new TypeMismatchException("path doesn't exist or is file");
+                }
+            } else {
+                if (fp.isDirectory()) {
+                    throw new TypeMismatchException("path doesn't exist or is directory");
+                }
+            }
+        }
+
+        // Return the directory
+        return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemType, directory);
+	}
+
 }