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

[03/19] git commit: CB-5407: Move getmetadata methods into FS modules

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

Branch: refs/heads/dev
Commit: 06725c224d0e62b38d36edd9302a62937a4ae335
Parents: 85945d8
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Nov 27 14:36:19 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         | 50 +++++++++++----------------------
 src/android/Filesystem.java        |  3 ++
 src/android/LocalFilesystem.java   | 22 +++++++++++++++
 4 files changed, 46 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/06725c22/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 8fef685..17fddcb 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -71,5 +71,10 @@ public class ContentFilesystem implements Filesystem {
 		// TODO Auto-generated method stub
 		return null;
 	}
+	@Override
+	public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
+		// TODO Auto-generated method stub
+		return null;
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/06725c22/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 01ce90b..8e050c5 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -242,15 +242,16 @@ public class FileUtils extends CordovaPlugin {
         else if (action.equals("getMetadata")) {
             final String fname=args.getString(0);
             threadhelper( new FileOp( ){
-                public void run() throws FileNotFoundException, JSONException {
-                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getMetadata(fname)));
+                public void run() throws FileNotFoundException, JSONException, MalformedURLException {
+                    JSONObject obj = getFileMetadata(fname);
+                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj.getLong("lastModifiedDate")));
                 }
             }, callbackContext);
         }
         else if (action.equals("getFileMetadata")) {
             final String fname=args.getString(0);
             threadhelper( new FileOp( ){
-                public void run() throws FileNotFoundException, JSONException {
+                public void run() throws FileNotFoundException, JSONException, MalformedURLException {
                     JSONObject obj = getFileMetadata(fname);
                     callbackContext.success(obj);
                 }
@@ -891,45 +892,26 @@ public class FileUtils extends CordovaPlugin {
     }
 
     /**
-     * Look up metadata about this entry.
-     *
-     * @param filePath to entry
-     * @return a long
-     * @throws FileNotFoundException
-     */
-    private long getMetadata(String filePath) throws FileNotFoundException {
-        File file = createFileObject(filePath);
-
-        if (!file.exists()) {
-            throw new FileNotFoundException("Failed to find file in getMetadata");
-        }
-
-        return file.lastModified();
-    }
-
-    /**
      * Returns a File that represents the current state of the file that this FileEntry represents.
      *
      * @param filePath to entry
      * @return returns a JSONObject represent a W3C File object
      * @throws FileNotFoundException
      * @throws JSONException
+     * @throws MalformedURLException 
      */
-    private JSONObject getFileMetadata(String filePath) throws FileNotFoundException, JSONException {
-        File file = createFileObject(filePath);
-
-        if (!file.exists()) {
-            throw new FileNotFoundException("File: " + filePath + " does not exist.");
+    private JSONObject getFileMetadata(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.getFileMetadataForLocalURL(inputURL);
+        
+        } catch (IllegalArgumentException e) {
+        	throw new MalformedURLException("Unrecognized filesystem URL");
         }
-
-        JSONObject metadata = new JSONObject();
-        metadata.put("size", file.length());
-        metadata.put("type", FileHelper.getMimeType(filePath, cordova));
-        metadata.put("name", file.getName());
-        metadata.put("fullPath", filePath);
-        metadata.put("lastModifiedDate", file.lastModified());
-
-        return metadata;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/06725c22/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 4abd403..d627d10 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -3,6 +3,7 @@ package org.apache.cordova.file;
 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;
@@ -20,4 +21,6 @@ public interface Filesystem {
 
 	JSONArray readEntriesAtLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException;
 
+	JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException;
+
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/06725c22/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 7d3aacb..3d11cf2 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.apache.cordova.CordovaInterface;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -214,4 +215,25 @@ public class LocalFilesystem implements Filesystem {
 		return null;
 	}
 
+	@Override
+	public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
+        File file = new File(filesystemPathForURL(inputURL));
+
+        if (!file.exists()) {
+            throw new FileNotFoundException("File at " + inputURL.URL + " does not exist.");
+        }
+
+        JSONObject metadata = new JSONObject();
+        try {
+        	metadata.put("size", file.length());
+        	metadata.put("type", FileHelper.getMimeType(file.getAbsolutePath(), cordova));
+        	metadata.put("name", file.getName());
+        	metadata.put("fullPath", inputURL.fullPath);
+        	metadata.put("lastModifiedDate", file.lastModified());
+        } catch (JSONException e) {
+        	return null;
+        }
+        return metadata;
+	}
+
 }