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:43 UTC
[01/19] git commit: CB-5532 Fix
Updated Branches:
refs/heads/dev 95677c491 -> a2b90737e
CB-5532 Fix
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/7872b9ce
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/7872b9ce
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/7872b9ce
Branch: refs/heads/dev
Commit: 7872b9ce466679638cf38b1968da39f360347759
Parents: 95677c4
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Dec 11 09:57:32 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 10:55:50 2013 -0500
----------------------------------------------------------------------
www/FileWriter.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/7872b9ce/www/FileWriter.js
----------------------------------------------------------------------
diff --git a/www/FileWriter.js b/www/FileWriter.js
index 7b3a007..786e994 100644
--- a/www/FileWriter.js
+++ b/www/FileWriter.js
@@ -118,7 +118,7 @@ FileWriter.prototype.write = function(data) {
// Mark data type for safer transport over the binary bridge
isBinary = supportsBinary && (data instanceof ArrayBuffer);
- if (isBinary) {
+ if (isBinary && ['windowsphone', 'windows8'].indexOf(cordova.platformId) >= 0) {
// create a plain array, using the keys from the Uint8Array view so that we can serialize it
data = Array.apply(null, new Uint8Array(data));
}
[05/19] git commit: CB-5407: Move getFile into FS modules
Posted by ia...@apache.org.
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);
+ }
+
}
[15/19] git commit: iOS: Add method for testing
backwards-compatiblity of filetransfer plugin
Posted by ia...@apache.org.
iOS: Add method for testing backwards-compatiblity of filetransfer plugin
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/4c447801
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/4c447801
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/4c447801
Branch: refs/heads/dev
Commit: 4c447801fa18406da48e6b451abf47bfae03016a
Parents: 6f52e3b
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Dec 11 10:46:38 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:36 2013 -0500
----------------------------------------------------------------------
src/ios/CDVFile.h | 4 ++++
src/ios/CDVFile.m | 29 ++++++++++++++++++++++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4c447801/src/ios/CDVFile.h
----------------------------------------------------------------------
diff --git a/src/ios/CDVFile.h b/src/ios/CDVFile.h
index 8891174..1f2c812 100644
--- a/src/ios/CDVFile.h
+++ b/src/ios/CDVFile.h
@@ -104,6 +104,7 @@ typedef int CDVFileSystemType;
- (NSNumber*)checkFreeDiskSpace:(NSString*)appPath;
- (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystem:(int)fsType isDirectory:(BOOL)isDir;
- (NSDictionary *)makeEntryForURL:(NSURL *)URL;
+- (CDVFilesystemURL *)fileSystemURLforLocalPath:(NSString *)localPath;
- (NSObject<CDVFileSystem> *)filesystemForURL:(CDVFilesystemURL *)localURL;
@@ -134,6 +135,9 @@ typedef int CDVFileSystemType;
- (NSString*)getMimeTypeFromPath:(NSString*)fullPath;
- (NSDictionary *)getDirectoryEntry:(NSString *)target isDirectory:(BOOL)bDirRequest;
+/* Internal methods for testing */
+- (void)_getLocalFilesystemPath:(CDVInvokedUrlCommand*)command;
+
@property (nonatomic, strong) NSString* appDocsPath;
@property (nonatomic, strong) NSString* appLibraryPath;
@property (nonatomic, strong) NSString* appTempPath;
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4c447801/src/ios/CDVFile.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVFile.m b/src/ios/CDVFile.m
index 0634215..517e349 100644
--- a/src/ios/CDVFile.m
+++ b/src/ios/CDVFile.m
@@ -201,7 +201,7 @@ NSString* const kCDVFilesystemURLPrefix = @"filesystem";
// Try all installed filesystems, in order. If any one supports mapping from
// path to URL, and returns a URL, then use it.
for (id object in self.fileSystems) {
- localURL = [object fileSystemURLforLocalPath:localPath];
+ localURL = [object URLforFilesystemPath:localPath];
if (localURL)
return localURL;
}
@@ -828,4 +828,31 @@ NSString* const kCDVFilesystemURLPrefix = @"filesystem";
return [self makeEntryForPath:localURL.fullPath fileSystem:localURL.fileSystemType isDirectory:bDirRequest];
}
+#pragma mark Internal methods for testing
+// Internal methods for testing: Get the on-disk location of a local filesystem url.
+// [Currently used for testing file-transfer]
+- (NSString *)_filesystemPathForURL:(CDVFilesystemURL *)localURL
+{
+ CDVLocalFilesystem *fs = [self.fileSystems objectAtIndex:localURL.fileSystemType];
+ if ([fs respondsToSelector:@selector(filesystemPathForURL:)]) {
+ return [fs filesystemPathForURL:localURL];
+ }
+ return nil;
+}
+
+- (void)_getLocalFilesystemPath:(CDVInvokedUrlCommand*)command
+{
+ NSString* localURLstr = [command.arguments objectAtIndex:0];
+ CDVFilesystemURL* localURL = [CDVFilesystemURL fileSystemURLWithString:localURLstr];
+
+ NSString* fsPath = [self _filesystemPathForURL:localURL];
+ CDVPluginResult* result;
+ if (fsPath) {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:fsPath];
+ } else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cannot resolve URL to a file"];
+ }
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+}
+
@end
[02/19] git commit: CB-5405: Use URL formatting for Entry.toURL
Posted by ia...@apache.org.
CB-5405: Use URL formatting for Entry.toURL
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/cd7d925d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/cd7d925d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/cd7d925d
Branch: refs/heads/dev
Commit: cd7d925d05f1387293bb3075dd7b32c4254cece9
Parents: 7872b9c
Author: Ian Clelland <ic...@chromium.org>
Authored: Sun Nov 24 22:43:59 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 10:55:50 2013 -0500
----------------------------------------------------------------------
www/Entry.js | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/cd7d925d/www/Entry.js
----------------------------------------------------------------------
diff --git a/www/Entry.js b/www/Entry.js
index bb4d753..223e2bc 100644
--- a/www/Entry.js
+++ b/www/Entry.js
@@ -169,8 +169,11 @@ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallbac
* Return a URL that can be used to identify this entry.
*/
Entry.prototype.toURL = function() {
+ if (this.filesystem && this.filesystem.__format__) {
+ return this.filesystem.__format__(this.fullPath);
+ }
// fullPath attribute contains the full URL
- return this.fullPath;
+ return "file://localhost" + this.fullPath;
};
/**
[09/19] git commit: CB-5407: Move read,
write and truncate methods into modules
Posted by ia...@apache.org.
CB-5407: Move read, write and truncate methods into 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/ed4a8d7d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/ed4a8d7d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/ed4a8d7d
Branch: refs/heads/dev
Commit: ed4a8d7dda175a40b6476d9ad43132a6a01874f0
Parents: 4859149
Author: Ian Clelland <ic...@chromium.org>
Authored: Thu Nov 28 21:37:00 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 16 +++
src/android/FileUtils.java | 182 ++++++++++++--------------------
src/android/Filesystem.java | 9 ++
src/android/LocalFilesystem.java | 89 ++++++++++++++++
src/android/ReadFileCallback.java | 5 +
5 files changed, 188 insertions(+), 113 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 6071870..17bead8 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -91,5 +91,21 @@ public class ContentFilesystem implements Filesystem {
// TODO Auto-generated method stub
return null;
}
+ @Override
+ public void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
+ @Override
+ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
+ int offset, boolean isBinary) throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Couldn't write to file given its content URI");
+ }
+ @Override
+ public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
+ throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 364e3d4..868e34e 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -159,7 +159,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(3);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
}
}, callbackContext);
@@ -169,7 +169,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, -1);
}
}, callbackContext);
@@ -179,7 +179,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
}
},callbackContext);
@@ -189,7 +189,7 @@ public class FileUtils extends CordovaPlugin {
final int end = args.getInt(2);
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() {
+ public void run() throws MalformedURLException {
readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
}
}, callbackContext);
@@ -715,69 +715,56 @@ public class FileUtils extends CordovaPlugin {
* @param encoding The encoding to return contents as. Typical value is UTF-8. (see http://www.iana.org/assignments/character-sets)
* @param resultType The desired type of data to send to the callback.
* @return Contents of file.
+ * @throws MalformedURLException
*/
- public void readFileAs(final String filename, final int start, final int end, final CallbackContext callbackContext, final String encoding, final int resultType) {
- this.cordova.getThreadPool().execute(new Runnable() {
- public void run() {
- try {
- byte[] bytes = readAsBinaryHelper(filename, start, end);
-
- PluginResult result;
- switch (resultType) {
- case PluginResult.MESSAGE_TYPE_STRING:
- result = new PluginResult(PluginResult.Status.OK, new String(bytes, encoding));
- break;
- case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
- result = new PluginResult(PluginResult.Status.OK, bytes);
- break;
- case PluginResult.MESSAGE_TYPE_BINARYSTRING:
- result = new PluginResult(PluginResult.Status.OK, bytes, true);
- break;
- default: // Base64.
- String contentType = FileHelper.getMimeType(filename, cordova);
- byte[] base64 = Base64.encode(bytes, Base64.NO_WRAP);
- String s = "data:" + contentType + ";base64," + new String(base64, "US-ASCII");
- result = new PluginResult(PluginResult.Status.OK, s);
+ public void readFileAs(final String srcURLstr, final int start, final int end, final CallbackContext callbackContext, final String encoding, final int resultType) throws MalformedURLException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ fs.readFileAtURL(inputURL, start, end, new ReadFileCallback() {
+ public void handleData(byte[] bytes, String contentType) {
+ try {
+ PluginResult result;
+ switch (resultType) {
+ case PluginResult.MESSAGE_TYPE_STRING:
+ result = new PluginResult(PluginResult.Status.OK, new String(bytes, encoding));
+ break;
+ case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
+ result = new PluginResult(PluginResult.Status.OK, bytes);
+ break;
+ case PluginResult.MESSAGE_TYPE_BINARYSTRING:
+ result = new PluginResult(PluginResult.Status.OK, bytes, true);
+ break;
+ default: // Base64.
+ byte[] base64 = Base64.encode(bytes, Base64.NO_WRAP);
+ String s = "data:" + contentType + ";base64," + new String(base64, "US-ASCII");
+ result = new PluginResult(PluginResult.Status.OK, s);
+ }
+
+ callbackContext.sendPluginResult(result);
+ } catch (IOException e) {
+ Log.d(LOG_TAG, e.getLocalizedMessage());
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
}
+ }
+ });
- callbackContext.sendPluginResult(result);
- } catch (FileNotFoundException e) {
- callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_FOUND_ERR));
- } catch (IOException e) {
- Log.d(LOG_TAG, e.getLocalizedMessage());
- callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
- }
- }
- });
- }
- /**
- * Read the contents of a file as binary.
- * This is done synchronously; the result is returned.
- *
- * @param filename The name of the file.
- * @param start Start position in the file.
- * @param end End position to stop at (exclusive).
- * @return Contents of the file as a byte[].
- * @throws IOException
- */
- private byte[] readAsBinaryHelper(String filename, int start, int end) throws IOException {
- int numBytesToRead = end - start;
- byte[] bytes = new byte[numBytesToRead];
- InputStream inputStream = FileHelper.getInputStreamFromUriString(filename, cordova);
- int numBytesRead = 0;
-
- if (start > 0) {
- inputStream.skip(start);
- }
-
- while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
- numBytesToRead -= numBytesRead;
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
+ } catch (FileNotFoundException e) {
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_FOUND_ERR));
+ } catch (IOException e) {
+ Log.d(LOG_TAG, e.getLocalizedMessage());
+ callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
}
-
- return bytes;
}
+
/**
* Write contents of file.
*
@@ -789,43 +776,19 @@ public class FileUtils extends CordovaPlugin {
* @throws NoModificationAllowedException
*/
/**/
- public long write(String filename, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
- if (filename.startsWith("content://")) {
- throw new NoModificationAllowedException("Couldn't write to file given its content URI");
- }
-
- filename = FileHelper.getRealPath(filename, cordova);
-
- boolean append = false;
- if (offset > 0) {
- this.truncateFile(filename, offset);
- append = true;
- }
-
- byte[] rawData;
- if (isBinary) {
- rawData = Base64.decode(data, Base64.DEFAULT);
- } else {
- rawData = data.getBytes();
- }
- ByteArrayInputStream in = new ByteArrayInputStream(rawData);
- try
- {
- FileOutputStream out = new FileOutputStream(filename, append);
- byte buff[] = new byte[rawData.length];
- in.read(buff, 0, buff.length);
- out.write(buff, 0, rawData.length);
- out.flush();
- out.close();
- }
- catch (NullPointerException e)
- {
- // This is a bug in the Android implementation of the Java Stack
- NoModificationAllowedException realException = new NoModificationAllowedException(filename);
- throw realException;
+ public long write(String srcURLstr, String data, int offset, boolean isBinary) throws FileNotFoundException, IOException, NoModificationAllowedException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ long x = fs.writeToFileAtURL(inputURL, data, offset, isBinary); Log.d("TEST",srcURLstr + ": "+x); return x;
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
-
- return rawData.length;
+
}
/**
@@ -836,24 +799,17 @@ public class FileUtils extends CordovaPlugin {
* @throws FileNotFoundException, IOException
* @throws NoModificationAllowedException
*/
- private long truncateFile(String filename, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
- if (filename.startsWith("content://")) {
- throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
- }
-
- filename = FileHelper.getRealPath(filename, cordova);
-
- RandomAccessFile raf = new RandomAccessFile(filename, "rw");
+ private long truncateFile(String srcURLstr, long size) throws FileNotFoundException, IOException, NoModificationAllowedException {
try {
- if (raf.length() >= size) {
- FileChannel channel = raf.getChannel();
- channel.truncate(size);
- return size;
- }
-
- return raf.length();
- } finally {
- raf.close();
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(srcURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+
+ return fs.truncateFileAtURL(inputURL, size);
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
}
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index cbec37a..6955520 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -28,4 +28,13 @@ public interface Filesystem {
JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException;
+ void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException;
+
+ long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset,
+ boolean isBinary) throws NoModificationAllowedException, IOException;
+
+ long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
+ throws IOException, NoModificationAllowedException;
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index e2f5de1..2613329 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -1,10 +1,13 @@
package org.apache.cordova.file;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import org.apache.cordova.CordovaInterface;
@@ -544,5 +547,91 @@ public class LocalFilesystem implements Filesystem {
return null;
}
+ @Override
+ public void readFileAtURL(LocalFilesystemURL inputURL, int start, int end,
+ ReadFileCallback readFileCallback) throws IOException {
+
+ int numBytesToRead = end - start;
+ byte[] bytes = new byte[numBytesToRead];
+ String contentType;
+
+ File file = new File(this.filesystemPathForURL(inputURL));
+ InputStream inputStream = new FileInputStream(file);
+
+ contentType = FileHelper.getMimeTypeForExtension(file.getAbsolutePath());
+ int numBytesRead = 0;
+
+ if (start > 0) {
+ inputStream.skip(start);
+ }
+
+ while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
+ numBytesToRead -= numBytesRead;
+ }
+ inputStream.close();
+ readFileCallback.handleData(bytes, contentType);
+ }
+
+ @Override
+ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
+ int offset, boolean isBinary) throws IOException, NoModificationAllowedException {
+ File file = new File(filesystemPathForURL(inputURL));
+
+ boolean append = false;
+ if (offset > 0) {
+ this.truncateFileAtURL(inputURL, offset);
+ append = true;
+ }
+
+ byte[] rawData;
+ if (isBinary) {
+ rawData = Base64.decode(data, Base64.DEFAULT);
+ } else {
+ rawData = data.getBytes();
+ }
+ ByteArrayInputStream in = new ByteArrayInputStream(rawData);
+ try
+ {
+ FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
+ byte buff[] = new byte[rawData.length];
+ in.read(buff, 0, buff.length);
+ out.write(buff, 0, rawData.length);
+ out.flush();
+ out.close();
+ }
+ catch (NullPointerException e)
+ {
+ // This is a bug in the Android implementation of the Java Stack
+ NoModificationAllowedException realException = new NoModificationAllowedException(inputURL.toString());
+ throw realException;
+ }
+
+ return rawData.length;
+ }
+
+ @Override
+ public long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws IOException {
+ File file = new File(filesystemPathForURL(inputURL));
+
+ if (!file.exists()) {
+ throw new FileNotFoundException("File at " + inputURL.URL + " does not exist.");
+ }
+
+ RandomAccessFile raf = new RandomAccessFile(filesystemPathForURL(inputURL), "rw");
+ try {
+ if (raf.length() >= size) {
+ FileChannel channel = raf.getChannel();
+ channel.truncate(size);
+ return size;
+ }
+
+ return raf.length();
+ } finally {
+ raf.close();
+ }
+
+
+ }
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/ed4a8d7d/src/android/ReadFileCallback.java
----------------------------------------------------------------------
diff --git a/src/android/ReadFileCallback.java b/src/android/ReadFileCallback.java
new file mode 100644
index 0000000..77bb7da
--- /dev/null
+++ b/src/android/ReadFileCallback.java
@@ -0,0 +1,5 @@
+package org.apache.cordova.file;
+
+public interface ReadFileCallback {
+ public void handleData(byte[] data, String contentType);
+}
[10/19] git commit: CB-5407: Move copy/move methods into FS modules
Posted by ia...@apache.org.
CB-5407: Move copy/move 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/4859149e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/4859149e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/4859149e
Branch: refs/heads/dev
Commit: 4859149e6c6bad69f68884b8c6691d2d630a8727
Parents: 02e82a5
Author: Ian Clelland <ic...@chromium.org>
Authored: Thu Nov 28 11:11:10 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 8 +
src/android/FileUtils.java | 289 ++-----------------------------
src/android/Filesystem.java | 3 +
src/android/LocalFilesystem.java | 297 ++++++++++++++++++++++++++++++++
4 files changed, 320 insertions(+), 277 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4859149e/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 3b4e4c3..6071870 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -83,5 +83,13 @@ public class ContentFilesystem implements Filesystem {
// Can probably use same impl as LFS
return null;
}
+ @Override
+ public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
+ Filesystem srcFs, LocalFilesystemURL srcURL, boolean move)
+ throws IOException, InvalidModificationException, JSONException,
+ NoModificationAllowedException, FileExistsException {
+ // TODO Auto-generated method stub
+ return null;
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4859149e/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 8e3bea6..364e3d4 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -465,267 +465,24 @@ public class FileUtils extends CordovaPlugin {
* @throws JSONException
* @throws FileExistsException
*/
- private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException {
- String newFileName = FileHelper.getRealPath(fileName, cordova);
- newParent = FileHelper.getRealPath(newParent, cordova);
-
- // Check for invalid file name
- if (newName != null && newName.contains(":")) {
- throw new EncodingException("Bad file name");
- }
-
- File source = new File(newFileName);
-
- if (!source.exists()) {
- // The file/directory we are copying doesn't exist so we should fail.
- throw new FileNotFoundException("The source does not exist");
- }
-
- File destinationDir = new File(newParent);
- if (!destinationDir.exists()) {
- // The destination does not exist so we should fail.
- throw new FileNotFoundException("The source does not exist");
- }
-
- // Figure out where we should be copying to
- File destination = createDestination(newName, source, destinationDir);
-
- //Log.d(LOG_TAG, "Source: " + source.getAbsolutePath());
- //Log.d(LOG_TAG, "Destin: " + destination.getAbsolutePath());
-
- // Check to see if source and destination are the same file
- if (source.getAbsolutePath().equals(destination.getAbsolutePath())) {
- throw new InvalidModificationException("Can't copy a file onto itself");
- }
-
- if (source.isDirectory()) {
- if (move) {
- return moveDirectory(source, destination);
- } else {
- return copyDirectory(source, destination);
- }
- } else {
- if (move) {
- JSONObject newFileEntry = moveFile(source, destination);
-
- // If we've moved a file given its content URI, we need to clean up.
- if (fileName.startsWith("content://")) {
- notifyDelete(fileName);
- }
-
- return newFileEntry;
- } else {
- return copyFile(source, destination);
- }
- }
- }
-
- /**
- * Creates the destination File object based on name passed in
- *
- * @param newName for the file directory to be called, if null use existing file name
- * @param fp represents the source file
- * @param destination represents the destination file
- * @return a File object that represents the destination
- */
- private File createDestination(String newName, File fp, File destination) {
- File destFile = null;
-
- // I know this looks weird but it is to work around a JSON bug.
- if ("null".equals(newName) || "".equals(newName)) {
- newName = null;
- }
-
- if (newName != null) {
- destFile = new File(destination.getAbsolutePath() + File.separator + newName);
- } else {
- destFile = new File(destination.getAbsolutePath() + File.separator + fp.getName());
- }
- return destFile;
- }
-
- /**
- * Copy a file
- *
- * @param srcFile file to be copied
- * @param destFile destination to be copied to
- * @return a FileEntry object
- * @throws IOException
- * @throws InvalidModificationException
- * @throws JSONException
- */
- private JSONObject copyFile(File srcFile, File destFile) throws IOException, InvalidModificationException, JSONException {
- // Renaming a file to an existing directory should fail
- if (destFile.exists() && destFile.isDirectory()) {
- throw new InvalidModificationException("Can't rename a file to a directory");
- }
-
- copyAction(srcFile, destFile);
-
- return getEntry(destFile);
- }
-
- /**
- * Moved this code into it's own method so moveTo could use it when the move is across file systems
- */
- private void copyAction(File srcFile, File destFile)
- throws FileNotFoundException, IOException {
- FileInputStream istream = new FileInputStream(srcFile);
- FileOutputStream ostream = new FileOutputStream(destFile);
- FileChannel input = istream.getChannel();
- FileChannel output = ostream.getChannel();
-
- try {
- input.transferTo(0, input.size(), output);
- } finally {
- istream.close();
- ostream.close();
- input.close();
- output.close();
- }
- }
-
- /**
- * Copy a directory
- *
- * @param srcDir directory to be copied
- * @param destinationDir destination to be copied to
- * @return a DirectoryEntry object
- * @throws JSONException
- * @throws IOException
- * @throws NoModificationAllowedException
- * @throws InvalidModificationException
- */
- private JSONObject copyDirectory(File srcDir, File destinationDir) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
- // Renaming a file to an existing directory should fail
- if (destinationDir.exists() && destinationDir.isFile()) {
- throw new InvalidModificationException("Can't rename a file to a directory");
- }
-
- // Check to make sure we are not copying the directory into itself
- if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
- throw new InvalidModificationException("Can't copy itself into itself");
- }
-
- // See if the destination directory exists. If not create it.
- if (!destinationDir.exists()) {
- if (!destinationDir.mkdir()) {
- // If we can't create the directory then fail
- throw new NoModificationAllowedException("Couldn't create the destination directory");
- }
- }
-
-
- for (File file : srcDir.listFiles()) {
- File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
- if (file.isDirectory()) {
- copyDirectory(file, destination);
- } else {
- copyFile(file, destination);
- }
- }
-
- return getEntry(destinationDir);
- }
-
- /**
- * Check to see if the user attempted to copy an entry into its parent without changing its name,
- * or attempted to copy a directory into a directory that it contains directly or indirectly.
- *
- * @param srcDir
- * @param destinationDir
- * @return
- */
- private boolean isCopyOnItself(String src, String dest) {
-
- // This weird test is to determine if we are copying or moving a directory into itself.
- // Copy /sdcard/myDir to /sdcard/myDir-backup is okay but
- // Copy /sdcard/myDir to /sdcard/myDir/backup should throw an INVALID_MODIFICATION_ERR
- if (dest.startsWith(src) && dest.indexOf(File.separator, src.length() - 1) != -1) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Move a file
- *
- * @param srcFile file to be copied
- * @param destFile destination to be copied to
- * @return a FileEntry object
- * @throws IOException
- * @throws InvalidModificationException
- * @throws JSONException
- */
- private JSONObject moveFile(File srcFile, File destFile) throws IOException, JSONException, InvalidModificationException {
- // Renaming a file to an existing directory should fail
- if (destFile.exists() && destFile.isDirectory()) {
- throw new InvalidModificationException("Can't rename a file to a directory");
- }
-
- // Try to rename the file
- if (!srcFile.renameTo(destFile)) {
- // Trying to rename the file failed. Possibly because we moved across file system on the device.
- // Now we have to do things the hard way
- // 1) Copy all the old file
- // 2) delete the src file
- copyAction(srcFile, destFile);
- if (destFile.exists()) {
- srcFile.delete();
- } else {
- throw new IOException("moved failed");
- }
+ private JSONObject transferTo(String srcURLstr, String destURLstr, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException {
+ if (srcURLstr == null || destURLstr == null) {
+ // either no source or no destination provided
+ throw new FileNotFoundException();
}
- return getEntry(destFile);
- }
-
- /**
- * Move a directory
- *
- * @param srcDir directory to be copied
- * @param destinationDir destination to be copied to
- * @return a DirectoryEntry object
- * @throws JSONException
- * @throws IOException
- * @throws InvalidModificationException
- * @throws NoModificationAllowedException
- * @throws FileExistsException
- */
- private JSONObject moveDirectory(File srcDir, File destinationDir) throws IOException, JSONException, InvalidModificationException, NoModificationAllowedException, FileExistsException {
- // Renaming a file to an existing directory should fail
- if (destinationDir.exists() && destinationDir.isFile()) {
- throw new InvalidModificationException("Can't rename a file to a directory");
- }
-
- // Check to make sure we are not copying the directory into itself
- if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
- throw new InvalidModificationException("Can't move itself into itself");
- }
+ LocalFilesystemURL srcURL = new LocalFilesystemURL(srcURLstr);
+ LocalFilesystemURL destURL = new LocalFilesystemURL(destURLstr);
- // If the destination directory already exists and is empty then delete it. This is according to spec.
- if (destinationDir.exists()) {
- if (destinationDir.list().length > 0) {
- throw new InvalidModificationException("directory is not empty");
- }
- }
+ Filesystem srcFs = this.filesystemForURL(srcURL);
+ Filesystem destFs = this.filesystemForURL(destURL);
- // Try to rename the directory
- if (!srcDir.renameTo(destinationDir)) {
- // Trying to rename the directory failed. Possibly because we moved across file system on the device.
- // Now we have to do things the hard way
- // 1) Copy all the old files
- // 2) delete the src directory
- copyDirectory(srcDir, destinationDir);
- if (destinationDir.exists()) {
- removeDirRecursively(srcDir);
- } else {
- throw new IOException("moved failed");
- }
+ // Check for invalid file name
+ if (newName != null && newName.contains(":")) {
+ throw new EncodingException("Bad file name");
}
- return getEntry(destinationDir);
+ return destFs.copyFileToURL(destURL, newName, srcFs, srcURL, move);
}
/**
@@ -759,28 +516,6 @@ public class FileUtils extends CordovaPlugin {
}
}
- /**
- * Loops through a directory deleting all the files.
- *
- * @param directory to be removed
- * @return a boolean representing success of failure
- * @throws FileExistsException
- */
- /* TODO: Remove when no longer needed */
- private boolean removeDirRecursively(File directory) throws FileExistsException {
-
- if (directory.isDirectory()) {
- for (File file : directory.listFiles()) {
- removeDirRecursively(file);
- }
- }
-
- if (!directory.delete()) {
- throw new FileExistsException("could not delete: " + directory.getName());
- } else {
- return true;
- }
- }
/**
* Deletes a file or directory. It is an error to attempt to delete a directory that is not empty.
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4859149e/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 625cf5f..cbec37a 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -25,4 +25,7 @@ public interface Filesystem {
JSONObject getParentForLocalURL(LocalFilesystemURL inputURL) throws IOException;
+ JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
+ Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException;
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4859149e/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index f2137b3..e2f5de1 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -1,8 +1,11 @@
package org.apache.cordova.file;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.channels.FileChannel;
import org.apache.cordova.CordovaInterface;
import org.json.JSONArray;
@@ -49,6 +52,10 @@ public class LocalFilesystem implements Filesystem {
return entry;
}
+
+ public JSONObject makeEntryForFile(File file, int fsType) throws JSONException {
+ return makeEntryForPath(this.fullPathForFilesystemPath(file.getAbsolutePath()), fsType, file.isDirectory());
+ }
@Override
public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException {
@@ -248,4 +255,294 @@ public class LocalFilesystem implements Filesystem {
return getEntryForLocalURL(newURL);
}
+ /**
+ * Check to see if the user attempted to copy an entry into its parent without changing its name,
+ * or attempted to copy a directory into a directory that it contains directly or indirectly.
+ *
+ * @param srcDir
+ * @param destinationDir
+ * @return
+ */
+ private boolean isCopyOnItself(String src, String dest) {
+
+ // This weird test is to determine if we are copying or moving a directory into itself.
+ // Copy /sdcard/myDir to /sdcard/myDir-backup is okay but
+ // Copy /sdcard/myDir to /sdcard/myDir/backup should throw an INVALID_MODIFICATION_ERR
+ if (dest.startsWith(src) && dest.indexOf(File.separator, src.length() - 1) != -1) {
+ return true;
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Creates the destination File object based on name passed in
+ *
+ * @param newName for the file directory to be called, if null use existing file name
+ * @param fp represents the source file
+ * @param destination represents the destination file
+ * @return a File object that represents the destination
+ */
+ private File createDestination(String newName, File fp, File destination) {
+ File destFile = null;
+
+ // I know this looks weird but it is to work around a JSON bug.
+ if ("null".equals(newName) || "".equals(newName)) {
+ newName = null;
+ }
+
+ if (newName != null) {
+ destFile = new File(destination.getAbsolutePath() + File.separator + newName);
+ } else {
+ destFile = new File(destination.getAbsolutePath() + File.separator + fp.getName());
+ }
+ return destFile;
+ }
+
+ /**
+ * Copy a file
+ *
+ * @param srcFile file to be copied
+ * @param destFile destination to be copied to
+ * @return a FileEntry object
+ * @throws IOException
+ * @throws InvalidModificationException
+ * @throws JSONException
+ */
+ private JSONObject copyFile(File srcFile, File destFile, int fsType) throws IOException, InvalidModificationException, JSONException {
+ // Renaming a file to an existing directory should fail
+ if (destFile.exists() && destFile.isDirectory()) {
+ throw new InvalidModificationException("Can't rename a file to a directory");
+ }
+
+ copyAction(srcFile, destFile);
+
+ return makeEntryForFile(destFile, fsType);
+ }
+
+ /**
+ * Moved this code into it's own method so moveTo could use it when the move is across file systems
+ */
+ private void copyAction(File srcFile, File destFile)
+ throws FileNotFoundException, IOException {
+ FileInputStream istream = new FileInputStream(srcFile);
+ FileOutputStream ostream = new FileOutputStream(destFile);
+ FileChannel input = istream.getChannel();
+ FileChannel output = ostream.getChannel();
+
+ try {
+ input.transferTo(0, input.size(), output);
+ } finally {
+ istream.close();
+ ostream.close();
+ input.close();
+ output.close();
+ }
+ }
+
+ /**
+ * Copy a directory
+ *
+ * @param srcDir directory to be copied
+ * @param destinationDir destination to be copied to
+ * @return a DirectoryEntry object
+ * @throws JSONException
+ * @throws IOException
+ * @throws NoModificationAllowedException
+ * @throws InvalidModificationException
+ */
+ private JSONObject copyDirectory(File srcDir, File destinationDir, int fsType) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
+ // Renaming a file to an existing directory should fail
+ if (destinationDir.exists() && destinationDir.isFile()) {
+ throw new InvalidModificationException("Can't rename a file to a directory");
+ }
+
+ // Check to make sure we are not copying the directory into itself
+ if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
+ throw new InvalidModificationException("Can't copy itself into itself");
+ }
+
+ // See if the destination directory exists. If not create it.
+ if (!destinationDir.exists()) {
+ if (!destinationDir.mkdir()) {
+ // If we can't create the directory then fail
+ throw new NoModificationAllowedException("Couldn't create the destination directory");
+ }
+ }
+
+
+ for (File file : srcDir.listFiles()) {
+ File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
+ if (file.isDirectory()) {
+ copyDirectory(file, destination, fsType);
+ } else {
+ copyFile(file, destination, fsType);
+ }
+ }
+
+ return makeEntryForFile(destinationDir, fsType);
+ }
+
+ /**
+ * Move a file
+ *
+ * @param srcFile file to be copied
+ * @param destFile destination to be copied to
+ * @return a FileEntry object
+ * @throws IOException
+ * @throws InvalidModificationException
+ * @throws JSONException
+ */
+ private JSONObject moveFile(File srcFile, File destFile, int fsType) throws IOException, JSONException, InvalidModificationException {
+ // Renaming a file to an existing directory should fail
+ if (destFile.exists() && destFile.isDirectory()) {
+ throw new InvalidModificationException("Can't rename a file to a directory");
+ }
+
+ // Try to rename the file
+ if (!srcFile.renameTo(destFile)) {
+ // Trying to rename the file failed. Possibly because we moved across file system on the device.
+ // Now we have to do things the hard way
+ // 1) Copy all the old file
+ // 2) delete the src file
+ copyAction(srcFile, destFile);
+ if (destFile.exists()) {
+ srcFile.delete();
+ } else {
+ throw new IOException("moved failed");
+ }
+ }
+
+ return makeEntryForFile(destFile, fsType);
+ }
+
+ /**
+ * Move a directory
+ *
+ * @param srcDir directory to be copied
+ * @param destinationDir destination to be copied to
+ * @return a DirectoryEntry object
+ * @throws JSONException
+ * @throws IOException
+ * @throws InvalidModificationException
+ * @throws NoModificationAllowedException
+ * @throws FileExistsException
+ */
+ private JSONObject moveDirectory(File srcDir, File destinationDir, int fsType) throws IOException, JSONException, InvalidModificationException, NoModificationAllowedException, FileExistsException {
+ // Renaming a file to an existing directory should fail
+ if (destinationDir.exists() && destinationDir.isFile()) {
+ throw new InvalidModificationException("Can't rename a file to a directory");
+ }
+
+ // Check to make sure we are not copying the directory into itself
+ if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
+ throw new InvalidModificationException("Can't move itself into itself");
+ }
+
+ // If the destination directory already exists and is empty then delete it. This is according to spec.
+ if (destinationDir.exists()) {
+ if (destinationDir.list().length > 0) {
+ throw new InvalidModificationException("directory is not empty");
+ }
+ }
+
+ // Try to rename the directory
+ if (!srcDir.renameTo(destinationDir)) {
+ // Trying to rename the directory failed. Possibly because we moved across file system on the device.
+ // Now we have to do things the hard way
+ // 1) Copy all the old files
+ // 2) delete the src directory
+ copyDirectory(srcDir, destinationDir, fsType);
+ if (destinationDir.exists()) {
+ removeDirRecursively(srcDir);
+ } else {
+ throw new IOException("moved failed");
+ }
+ }
+
+ return makeEntryForFile(destinationDir, fsType);
+ }
+
+
+ @Override
+ public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName,
+ Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException {
+
+
+ String newFileName = this.filesystemPathForURL(srcURL);
+ String newParent = this.filesystemPathForURL(destURL);
+
+
+ File destinationDir = new File(newParent);
+ if (!destinationDir.exists()) {
+ // The destination does not exist so we should fail.
+ throw new FileNotFoundException("The source does not exist");
+ }
+
+
+ if (LocalFilesystem.class.isInstance(srcFs)) {
+
+ /* Same FS, we can shortcut with NSFileManager operations */
+
+
+ File source = new File(newFileName);
+
+ if (!source.exists()) {
+ // The file/directory we are copying doesn't exist so we should fail.
+ throw new FileNotFoundException("The source does not exist");
+ }
+
+ // Figure out where we should be copying to
+ File destination = createDestination(newName, source, destinationDir);
+
+ //Log.d(LOG_TAG, "Source: " + source.getAbsolutePath());
+ //Log.d(LOG_TAG, "Destin: " + destination.getAbsolutePath());
+
+ // Check to see if source and destination are the same file
+ if (source.getAbsolutePath().equals(destination.getAbsolutePath())) {
+ throw new InvalidModificationException("Can't copy a file onto itself");
+ }
+
+ if (source.isDirectory()) {
+ if (move) {
+ return moveDirectory(source, destination, destURL.filesystemType);
+ } else {
+ return copyDirectory(source, destination, destURL.filesystemType);
+ }
+ } else {
+ if (move) {
+ JSONObject newFileEntry = moveFile(source, destination, destURL.filesystemType);
+
+/* // If we've moved a file given its content URI, we need to clean up.
+ // TODO: Move this to where it belongs, in cross-fs mv code below.
+ if (srcURL.URL.getScheme().equals("content")) {
+ notifyDelete(fileName);
+ }
+*/
+ return newFileEntry;
+ } else {
+ return copyFile(source, destination, destURL.filesystemType);
+ }
+ }
+
+
+ } else {
+/* // Need to copy the hard way
+ srcFs.readFileAtURL(srcURL, 0, -1, new ReadFileCallback() {
+ void run(data, mimetype, errorcode) {
+ if (data != null) {
+ //write data to file
+ // send success message -- call original callback?
+ }
+ // error
+ }
+ });
+ return null; // Async, will return later
+*/
+ }
+ return null;
+ }
+
+
}
[12/19] git commit: CB-5407: Add new Android source files to
plugin.xml
Posted by ia...@apache.org.
CB-5407: Add new Android source files to plugin.xml
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/9f3bb54b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/9f3bb54b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/9f3bb54b
Branch: refs/heads/dev
Commit: 9f3bb54bb339b0b25de9451ce24f4c830336095f
Parents: ed4a8d7
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Dec 4 08:25:37 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:10 2013 -0500
----------------------------------------------------------------------
plugin.xml | 10 ++++++++++
1 file changed, 10 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/9f3bb54b/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 3621c16..dba4dae 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -102,6 +102,16 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<source-file src="src/android/FileUtils.java" target-dir="src/org/apache/cordova/file" />
<source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/file" />
<source-file src="src/android/DirectoryManager.java" target-dir="src/org/apache/cordova/file" />
+ <source-file src="src/android/LocalFilesystemURL.java" target-dir="src/org/apache/cordova/file" />
+ <source-file src="src/android/Filesystem.java" target-dir="src/org/apache/cordova/file" />
+ <source-file src="src/android/LocalFilesystem.java" target-dir="src/org/apache/cordova/file" />
+ <source-file src="src/android/ContentFilesystem.java" target-dir="src/org/apache/cordova/file" />
+ <source-file src="src/android/ReadFileCallback.java" target-dir="src/org/apache/cordova/file" />
+
+ <!-- android specific file apis -->
+ <js-module src="www/android/FileSystem.js" name="androidFileSystem">
+ <merges target="window.FileSystem" />
+ </js-module>
</platform>
<!-- amazon-fireos -->
[06/19] git commit: CB-5407: Move readdir methods into FS modules
Posted by ia...@apache.org.
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;
+ }
+
}
[08/19] git commit: CB-5407: Start refactoring android code: Modular
filesystems, rfs, rlfsurl
Posted by ia...@apache.org.
CB-5407: Start refactoring android code: Modular filesystems, rfs, rlfsurl
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/30988f70
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/30988f70
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/30988f70
Branch: refs/heads/dev
Commit: 30988f707b33ca4c140e0ae356cdbfa29761008a
Parents: 225c905
Author: Ian Clelland <ic...@chromium.org>
Authored: Sun Nov 24 22:47:19 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 53 ++++++++++++++
src/android/FileUtils.java | 121 ++++++++++++++++++-------------
src/android/Filesystem.java | 11 +++
src/android/LocalFilesystem.java | 50 +++++++++++++
src/android/LocalFilesystemURL.java | 54 ++++++++++++++
5 files changed, 238 insertions(+), 51 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/30988f70/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
new file mode 100644
index 0000000..bef40a3
--- /dev/null
+++ b/src/android/ContentFilesystem.java
@@ -0,0 +1,53 @@
+package org.apache.cordova.file;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.apache.cordova.CordovaInterface;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.database.Cursor;
+import android.provider.MediaStore;
+
+public class ContentFilesystem implements Filesystem {
+
+ private CordovaInterface cordova;
+
+ public ContentFilesystem(CordovaInterface cordova) {
+ this.cordova = cordova;
+ }
+
+ @Override
+ @SuppressWarnings("deprecation")
+ public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException {
+ File fp = null;
+
+ Cursor cursor = this.cordova.getActivity().managedQuery(inputURL.URL, new String[] { MediaStore.Images.Media.DATA }, null, null, null);
+ // Note: MediaStore.Images/Audio/Video.Media.DATA is always "_data"
+ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
+ cursor.moveToFirst();
+ fp = new File(cursor.getString(column_index));
+
+ if (!fp.exists()) {
+ throw new FileNotFoundException();
+ }
+ if (!fp.canRead()) {
+ throw new IOException();
+ }
+ try {
+ JSONObject entry = new JSONObject();
+ entry.put("isFile", fp.isFile());
+ entry.put("isDirectory", fp.isDirectory());
+ entry.put("name", fp.getName());
+ entry.put("fullPath", "file://" + fp.getAbsolutePath());
+ // The file system can't be specified, as it would lead to an infinite loop.
+ entry.put("filesystem", FileUtils.APPLICATION);
+ return entry;
+ } catch (JSONException e) {
+ throw new IOException();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/30988f70/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 2a6bddf..8c069b5 100755
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -18,15 +18,15 @@
*/
package org.apache.cordova.file;
-import android.database.Cursor;
-import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import org.apache.cordova.CallbackContext;
+import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
+import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
@@ -42,9 +42,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
-import java.net.URL;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;
+import java.util.ArrayList;
/**
* This class provides SD card file and directory services to JavaScript.
@@ -75,7 +75,41 @@ public class FileUtils extends CordovaPlugin {
private interface FileOp {
void run( ) throws Exception;
}
-
+
+ private ArrayList<Filesystem> filesystems;
+
+ @Override
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
+ super.initialize(cordova, webView);
+ this.filesystems = new ArrayList<Filesystem>();
+
+ File fp;
+ String tempRoot;
+ String persistentRoot;
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+ persistentRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
+ tempRoot = Environment.getExternalStorageDirectory().getAbsolutePath() +
+ "/Android/data/" + cordova.getActivity().getPackageName() + "/cache/";
+ } else {
+ persistentRoot = "/data/data/" + cordova.getActivity().getPackageName();
+ tempRoot = "/data/data/" + cordova.getActivity().getPackageName() + "/cache/";
+ }
+ // Create the cache dir if it doesn't exist.
+ fp = new File(tempRoot);
+ fp.mkdirs();
+ this.filesystems.add(new LocalFilesystem(cordova, "temporary", tempRoot));
+ this.filesystems.add(new LocalFilesystem(cordova, "persistent", persistentRoot));
+ this.filesystems.add(new ContentFilesystem(cordova));
+ }
+
+ public Filesystem filesystemForURL(LocalFilesystemURL localURL) {
+ try {
+ return this.filesystems.get(localURL.filesystemType);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ return null;
+ }
+ }
+
/**
* Executes the request and returns whether the action was valid.
*
@@ -377,45 +411,21 @@ public class FileUtils extends CordovaPlugin {
* @throws IOException if the user can't read the file
* @throws JSONException
*/
- @SuppressWarnings("deprecation")
private JSONObject resolveLocalFileSystemURI(String url) throws IOException, JSONException {
String decoded = URLDecoder.decode(url, "UTF-8");
- File fp = null;
-
- // Handle the special case where you get an Android content:// uri.
- if (decoded.startsWith("content:")) {
- Cursor cursor = this.cordova.getActivity().managedQuery(Uri.parse(decoded), new String[] { MediaStore.Images.Media.DATA }, null, null, null);
- // Note: MediaStore.Images/Audio/Video.Media.DATA is always "_data"
- int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- fp = new File(cursor.getString(column_index));
- } else {
- // Test to see if this is a valid URL first
- @SuppressWarnings("unused")
- URL testUrl = new URL(decoded);
-
- if (decoded.startsWith("file://")) {
- int questionMark = decoded.indexOf("?");
- if (questionMark < 0) {
- fp = new File(decoded.substring(7, decoded.length()));
- } else {
- fp = new File(decoded.substring(7, questionMark));
- }
- } else {
- fp = new File(decoded);
- }
- }
-
- if (!fp.exists()) {
- throw new FileNotFoundException();
- }
- if (!fp.canRead()) {
- throw new IOException();
- }
- return getEntry(fp);
- }
-
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(decoded);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
+ }
+ return fs.getEntryForLocalURL(inputURL);
+ } catch (IllegalArgumentException e) {
+ throw new IOException();
+ }
+ }
+
/**
* Read the list of files from this directory.
*
@@ -1001,10 +1011,27 @@ public class FileUtils extends CordovaPlugin {
else {
throw new IOException("No filesystem of type requested");
}
-
+ fs.put("root", makeEntryForPath("/", type, true));
return fs;
}
+ 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("/",1);
+ String name = parts[parts.length-1];
+ 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;
+
+ }
/**
* Returns a JSON object representing the given File.
*
@@ -1012,17 +1039,9 @@ public class FileUtils extends CordovaPlugin {
* @return a JSON representation of the given File
* @throws JSONException
*/
+ @Deprecated
public static JSONObject getEntry(File file) throws JSONException {
- JSONObject entry = new JSONObject();
-
- entry.put("isFile", file.isFile());
- entry.put("isDirectory", file.isDirectory());
- entry.put("name", file.getName());
- entry.put("fullPath", "file://" + file.getAbsolutePath());
- // The file system can't be specified, as it would lead to an infinite loop.
- // entry.put("filesystem", null);
-
- return entry;
+ return makeEntryForPath(file.getAbsolutePath(), 0, file.isDirectory());
}
/**
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/30988f70/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
new file mode 100644
index 0000000..1879ad0
--- /dev/null
+++ b/src/android/Filesystem.java
@@ -0,0 +1,11 @@
+package org.apache.cordova.file;
+
+import java.io.IOException;
+
+import org.json.JSONObject;
+
+public interface Filesystem {
+
+ JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException;
+
+}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/30988f70/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
new file mode 100644
index 0000000..c005867
--- /dev/null
+++ b/src/android/LocalFilesystem.java
@@ -0,0 +1,50 @@
+package org.apache.cordova.file;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.util.Base64;
+
+public class LocalFilesystem implements Filesystem {
+
+ private String name;
+ private String fsRoot;
+ private CordovaInterface cordova;
+
+ public LocalFilesystem(CordovaInterface cordova, String name, String fsRoot) {
+ this.name = name;
+ this.fsRoot = fsRoot;
+ this.cordova = cordova;
+ }
+
+ @Override
+ public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException {
+ File fp = null;
+ fp = new File(this.fsRoot + inputURL.fullPath); //TODO: Proper fs.join
+
+ if (!fp.exists()) {
+ throw new FileNotFoundException();
+ }
+ if (!fp.canRead()) {
+ throw new IOException();
+ }
+ try {
+ JSONObject entry = new JSONObject();
+ entry.put("isFile", fp.isFile());
+ entry.put("isDirectory", fp.isDirectory());
+ entry.put("name", fp.getName());
+ entry.put("fullPath", "file://" + fp.getAbsolutePath());
+ // 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);
+ return entry;
+ } catch (JSONException e) {
+ throw new IOException();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/30988f70/src/android/LocalFilesystemURL.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystemURL.java b/src/android/LocalFilesystemURL.java
new file mode 100644
index 0000000..0dc547e
--- /dev/null
+++ b/src/android/LocalFilesystemURL.java
@@ -0,0 +1,54 @@
+package org.apache.cordova.file;
+
+import android.net.Uri;
+
+public class LocalFilesystemURL {
+ public static final int TEMPORARY = 0;
+ public static final int PERSISTENT = 1;
+
+ Uri URL;
+ int filesystemType;
+ String fullPath;
+
+ public LocalFilesystemURL(Uri URL) {
+ this.URL = URL;
+ this.filesystemType = this.filesystemTypeForLocalURL(URL);
+ this.fullPath = this.fullPathForLocalURL(URL);
+ }
+
+ private String fullPathForLocalURL(Uri URL) {
+ int fsType = this.filesystemTypeForLocalURL(URL);
+ if (fsType == FileUtils.TEMPORARY) {
+ return URL.getPath().substring(10);
+ }
+ if (fsType == FileUtils.PERSISTENT) {
+ return URL.getPath().substring(11);
+ }
+ if (fsType == FileUtils.APPLICATION) {
+ return URL.getPath();
+ }
+ return null;
+ }
+
+ private int filesystemTypeForLocalURL(Uri URL) {
+ if ("filesystem".equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
+ String path = URL.getPath();
+ if (path != null) {
+ if (path.startsWith("/temporary")) {
+ return FileUtils.TEMPORARY;
+ } else if (path.startsWith("/persistent")) {
+ return FileUtils.PERSISTENT;
+ }
+ }
+ } else if ("content".equals(URL.getScheme())) {
+ return FileUtils.APPLICATION;
+ }
+ return -1;
+ }
+
+ public LocalFilesystemURL(String strURL) {
+ this(Uri.parse(strURL));
+ }
+
+
+}
[04/19] git commit: CB-5407: Move remove methods into FS modules
Posted by ia...@apache.org.
CB-5407: Move remove 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/919f99f8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/919f99f8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/919f99f8
Branch: refs/heads/dev
Commit: 919f99f818895800f28589d8f2c2f8b023ec74cb
Parents: 0d0af8f
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Nov 27 13:35:26 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 10 ++++++
src/android/FileUtils.java | 59 ++++++++++++++++++++++-----------
src/android/Filesystem.java | 4 +++
src/android/LocalFilesystem.java | 33 ++++++++++++++++++
4 files changed, 86 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 6b34641..7325ac6 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -54,5 +54,15 @@ public class ContentFilesystem implements Filesystem {
String fileName, JSONObject options, boolean directory) throws IOException {
throw new IOException("Cannot create content url");
}
+ @Override
+ public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL)
+ throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Cannot remove content url");
+ }
+ @Override
+ public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
+ throws NoModificationAllowedException {
+ throw new NoModificationAllowedException("Cannot remove content url");
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 32e2a4a..a373d50 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -288,7 +288,7 @@ public class FileUtils extends CordovaPlugin {
else if (action.equals("remove")) {
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() throws NoModificationAllowedException, InvalidModificationException {
+ public void run() throws NoModificationAllowedException, InvalidModificationException, MalformedURLException {
boolean success= remove(fname);
if (success) {
notifyDelete(fname);
@@ -302,7 +302,7 @@ public class FileUtils extends CordovaPlugin {
else if (action.equals("removeRecursively")) {
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() throws FileExistsException {
+ public void run() throws FileExistsException, MalformedURLException, NoModificationAllowedException {
boolean success = removeRecursively(fname);
if (success) {
callbackContext.success();
@@ -743,16 +743,26 @@ public class FileUtils extends CordovaPlugin {
* @param filePath the directory to be removed
* @return a boolean representing success of failure
* @throws FileExistsException
+ * @throws NoModificationAllowedException
+ * @throws MalformedURLException
*/
- private boolean removeRecursively(String filePath) throws FileExistsException {
- File fp = createFileObject(filePath);
+ private boolean removeRecursively(String baseURLstr) throws FileExistsException, NoModificationAllowedException, MalformedURLException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(baseURLstr);
+ // You can't delete the root directory.
+ if ("".equals(inputURL.fullPath) || "/".equals(inputURL.fullPath)) {
+ throw new NoModificationAllowedException("You can't delete the root directory");
+ }
- // You can't delete the root directory.
- if (atRootDirectory(filePath)) {
- return false;
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+ return fs.recursiveRemoveFileAtLocalURL(inputURL);
+
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
-
- return removeDirRecursively(fp);
}
/**
@@ -762,7 +772,9 @@ public class FileUtils extends CordovaPlugin {
* @return a boolean representing success of failure
* @throws FileExistsException
*/
+ /* TODO: Remove when no longer needed */
private boolean removeDirRecursively(File directory) throws FileExistsException {
+
if (directory.isDirectory()) {
for (File file : directory.listFiles()) {
removeDirRecursively(file);
@@ -784,21 +796,26 @@ public class FileUtils extends CordovaPlugin {
* @return a boolean representing success of failure
* @throws NoModificationAllowedException
* @throws InvalidModificationException
+ * @throws MalformedURLException
*/
- private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException {
- File fp = createFileObject(filePath);
+ private boolean remove(String baseURLstr) throws NoModificationAllowedException, InvalidModificationException, MalformedURLException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(baseURLstr);
+ // You can't delete the root directory.
+ if ("".equals(inputURL.fullPath) || "/".equals(inputURL.fullPath)) {
- // You can't delete the root directory.
- if (atRootDirectory(filePath)) {
- throw new NoModificationAllowedException("You can't delete the root directory");
- }
+ throw new NoModificationAllowedException("You can't delete the root directory");
+ }
- // You can't delete a directory that is not empty
- if (fp.isDirectory() && fp.list().length > 0) {
- throw new InvalidModificationException("You can't delete a directory that is not empty.");
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+ return fs.removeFileAtLocalURL(inputURL);
+
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
-
- return fp.delete();
}
/**
@@ -854,6 +871,7 @@ public class FileUtils extends CordovaPlugin {
* @param filePath to directory
* @return true if we are at the root, false otherwise.
*/
+ /* TODO: Remove when no longer needed */
private boolean atRootDirectory(String filePath) {
filePath = FileHelper.getRealPath(filePath, cordova);
@@ -871,6 +889,7 @@ public class FileUtils extends CordovaPlugin {
* @param filePath
* @return
*/
+ /* TODO: Remove when no longer needed */
private File createFileObject(String filePath) {
filePath = FileHelper.getRealPath(filePath, cordova);
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 19692ad..a4730ba 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -12,4 +12,8 @@ public interface Filesystem {
JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, String fileName,
JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException;
+ boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException, NoModificationAllowedException;
+
+ boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException, NoModificationAllowedException;
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/919f99f8/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 563f8ed..aca9d15 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -148,4 +148,37 @@ public class LocalFilesystem implements Filesystem {
return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemType, directory);
}
+ @Override
+ public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException {
+
+ File fp = new File(filesystemPathForURL(inputURL));
+
+ // You can't delete a directory that is not empty
+ if (fp.isDirectory() && fp.list().length > 0) {
+ throw new InvalidModificationException("You can't delete a directory that is not empty.");
+ }
+
+ return fp.delete();
+ }
+
+ @Override
+ public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL) throws FileExistsException {
+ File directory = new File(filesystemPathForURL(inputURL));
+ return removeDirRecursively(directory);
+ }
+
+ protected boolean removeDirRecursively(File directory) throws FileExistsException {
+ if (directory.isDirectory()) {
+ for (File file : directory.listFiles()) {
+ removeDirRecursively(file);
+ }
+ }
+
+ if (!directory.delete()) {
+ throw new FileExistsException("could not delete: " + directory.getName());
+ } else {
+ return true;
+ }
+ }
+
}
[14/19] git commit: Merge branch "android-file" into dev
Posted by ia...@apache.org.
Merge branch "android-file" into dev
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/0a6adf02
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/0a6adf02
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/0a6adf02
Branch: refs/heads/dev
Commit: 0a6adf0221920c1eb3e3635f2ba5dcfd41836f3b
Parents: 7872b9c 2550617
Author: Ian Clelland <ic...@chromium.org>
Authored: Fri Dec 13 11:15:56 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:15:56 2013 -0500
----------------------------------------------------------------------
plugin.xml | 10 +
src/android/ContentFilesystem.java | 111 ++++
src/android/FileUtils.java | 900 +++++++++----------------------
src/android/Filesystem.java | 40 ++
src/android/LocalFilesystem.java | 617 +++++++++++++++++++++
src/android/LocalFilesystemURL.java | 54 ++
src/android/ReadFileCallback.java | 5 +
www/Entry.js | 5 +-
www/android/FileSystem.js | 27 +
9 files changed, 1113 insertions(+), 656 deletions(-)
----------------------------------------------------------------------
[07/19] git commit: CB-5407: Move getParent into FS modules
Posted by ia...@apache.org.
CB-5407: Move getParent 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/02e82a5e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/02e82a5e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/02e82a5e
Branch: refs/heads/dev
Commit: 02e82a5e9dbf1a1c3398511f23cee9990b591e4a
Parents: 06725c2
Author: Ian Clelland <ic...@chromium.org>
Authored: Mon Dec 2 16:04:53 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 | 53 ++++++++-------------------------
src/android/Filesystem.java | 2 ++
src/android/LocalFilesystem.java | 12 ++++++++
4 files changed, 34 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/02e82a5e/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 17fddcb..3b4e4c3 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -76,5 +76,12 @@ public class ContentFilesystem implements Filesystem {
// TODO Auto-generated method stub
return null;
}
+ @Override
+ public JSONObject getParentForLocalURL(LocalFilesystemURL inputURL)
+ throws IOException {
+ // TODO Auto-generated method stub
+ // Can probably use same impl as LFS
+ return null;
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/02e82a5e/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 8e050c5..8e3bea6 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -260,7 +260,7 @@ public class FileUtils extends CordovaPlugin {
else if (action.equals("getParent")) {
final String fname=args.getString(0);
threadhelper( new FileOp( ){
- public void run() throws JSONException {
+ public void run() throws JSONException, IOException {
JSONObject obj = getParent(fname);
callbackContext.success(obj);
}
@@ -848,47 +848,20 @@ public class FileUtils extends CordovaPlugin {
* @param filePath
* @return
* @throws JSONException
+ * @throws IOException
*/
- private JSONObject getParent(String filePath) throws JSONException {
- filePath = FileHelper.getRealPath(filePath, cordova);
-
- if (atRootDirectory(filePath)) {
- return getEntry(filePath);
- }
- return getEntry(new File(filePath).getParent());
- }
-
- /**
- * Checks to see if we are at the root directory. Useful since we are
- * not allow to delete this directory.
- *
- * @param filePath to directory
- * @return true if we are at the root, false otherwise.
- */
- /* TODO: Remove when no longer needed */
- private boolean atRootDirectory(String filePath) {
- filePath = FileHelper.getRealPath(filePath, cordova);
-
- if (filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + cordova.getActivity().getPackageName() + "/cache") ||
- filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath()) ||
- filePath.equals("/data/data/" + cordova.getActivity().getPackageName())) {
- return true;
+ private JSONObject getParent(String baseURLstr) throws JSONException, IOException {
+ 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.getParentForLocalURL(inputURL);
+
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
}
- return false;
- }
-
- /**
- * Create a File object from the passed in path
- *
- * @param filePath
- * @return
- */
- /* TODO: Remove when no longer needed */
- private File createFileObject(String filePath) {
- filePath = FileHelper.getRealPath(filePath, cordova);
-
- File file = new File(filePath);
- return file;
}
/**
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/02e82a5e/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index d627d10..625cf5f 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -23,4 +23,6 @@ public interface Filesystem {
JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException;
+ JSONObject getParentForLocalURL(LocalFilesystemURL inputURL) throws IOException;
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/02e82a5e/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 3d11cf2..f2137b3 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -236,4 +236,16 @@ public class LocalFilesystem implements Filesystem {
return metadata;
}
+ @Override
+ public JSONObject getParentForLocalURL(LocalFilesystemURL inputURL) throws IOException {
+ LocalFilesystemURL newURL = new LocalFilesystemURL(inputURL.URL);
+
+ if (!("".equals(inputURL.fullPath) || "/".equals(inputURL.fullPath))) {
+ int end = inputURL.fullPath.endsWith("/") ? 1 : 0;
+ int lastPathStartsAt = inputURL.fullPath.lastIndexOf('/', inputURL.fullPath.length()-end)+1;
+ newURL.fullPath = newURL.fullPath.substring(0,lastPathStartsAt);
+ }
+ return getEntryForLocalURL(newURL);
+ }
+
}
[16/19] git commit: Android: Clean up unclosed file objects
Posted by ia...@apache.org.
Android: Clean up unclosed file objects
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/6d0dad63
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/6d0dad63
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/6d0dad63
Branch: refs/heads/dev
Commit: 6d0dad633e4a62c3043604ead3566f31d74ae335
Parents: 0a6adf0
Author: Ian Clelland <ic...@chromium.org>
Authored: Mon Dec 2 15:53:43 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:36 2013 -0500
----------------------------------------------------------------------
src/android/LocalFilesystem.java | 35 ++++++++++++++++++++---------------
1 file changed, 20 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6d0dad63/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index adb3ef6..be1d2d1 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -537,19 +537,20 @@ public class LocalFilesystem implements Filesystem {
String contentType;
File file = new File(this.filesystemPathForURL(inputURL));
- InputStream inputStream = new FileInputStream(file);
-
contentType = FileHelper.getMimeTypeForExtension(file.getAbsolutePath());
+
+ InputStream inputStream = new FileInputStream(file);
int numBytesRead = 0;
-
- if (start > 0) {
- inputStream.skip(start);
- }
-
- while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
- numBytesToRead -= numBytesRead;
+ try {
+ if (start > 0) {
+ inputStream.skip(start);
+ }
+ while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
+ numBytesToRead -= numBytesRead;
+ }
+ } finally {
+ inputStream.close();
}
- inputStream.close();
readFileCallback.handleData(bytes, contentType);
}
@@ -572,12 +573,16 @@ public class LocalFilesystem implements Filesystem {
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
try
{
+ byte buff[] = new byte[rawData.length];
FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
- byte buff[] = new byte[rawData.length];
- in.read(buff, 0, buff.length);
- out.write(buff, 0, rawData.length);
- out.flush();
- out.close();
+ try {
+ in.read(buff, 0, buff.length);
+ out.write(buff, 0, rawData.length);
+ out.flush();
+ } finally {
+ // Always close the output
+ out.close();
+ }
}
catch (NullPointerException e)
{
[11/19] git commit: CB-5407: Update android JS to use FS urls
Posted by ia...@apache.org.
CB-5407: Update android JS to use FS urls
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/225c9059
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/225c9059
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/225c9059
Branch: refs/heads/dev
Commit: 225c9059b5c7bab65e5228ec78103293f3c25157
Parents: cd7d925
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Dec 11 10:48:42 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:09 2013 -0500
----------------------------------------------------------------------
www/android/FileSystem.js | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/225c9059/www/android/FileSystem.js
----------------------------------------------------------------------
diff --git a/www/android/FileSystem.js b/www/android/FileSystem.js
new file mode 100644
index 0000000..2017e29
--- /dev/null
+++ b/www/android/FileSystem.js
@@ -0,0 +1,27 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+module.exports = {
+ __format__: function(fullPath) {
+ return 'filesystem://localhost/'+this.name+(fullPath[0]==='/'?'':'/')+encodeURI(fullPath);
+ }
+};
+
[18/19] git commit: Android: Add method for testing
backwards-compatibility of filetransfer plugin
Posted by ia...@apache.org.
Android: Add method for testing backwards-compatibility of filetransfer plugin
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/83a867cc
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/83a867cc
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/83a867cc
Branch: refs/heads/dev
Commit: 83a867cc7811fa2b0aff5f340c8a1122ee40e6e2
Parents: 4c44780
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Dec 11 11:26:32 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:37 2013 -0500
----------------------------------------------------------------------
src/android/FileUtils.java | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/83a867cc/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 1f345fe..7e852b5 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -361,12 +361,38 @@ public class FileUtils extends CordovaPlugin {
}
},callbackContext);
}
+ else if (action.equals("_getLocalFilesystemPath")) {
+ // Internal method for testing: Get the on-disk location of a local filesystem url.
+ // [Currently used for testing file-transfer]
+ final String localURLstr = args.getString(0);
+ threadhelper( new FileOp( ){
+ public void run() throws FileNotFoundException, JSONException, MalformedURLException {
+ String fname = _filesystemPathForURL(localURLstr);
+ callbackContext.success(fname);
+ }
+ },callbackContext);
+ }
else {
return false;
}
return true;
}
+ /* Internal method for testing: Get the on-disk location of a local filesystem url.
+ */
+ protected String _filesystemPathForURL(String localURLstr) throws MalformedURLException {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(localURLstr);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ throw new MalformedURLException("No installed handlers for this URL");
+ }
+ return fs.filesystemPathForURL(inputURL);
+ } catch (IllegalArgumentException e) {
+ throw new MalformedURLException("Unrecognized filesystem URL");
+ }
+ }
+
/* helper to execute functions async and handle the result codes
*
*/
[19/19] git commit: CB-5407: Fixes for ContentFilesystem
Posted by ia...@apache.org.
CB-5407: Fixes for ContentFilesystem
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/a2b90737
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/a2b90737
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/a2b90737
Branch: refs/heads/dev
Commit: a2b90737e6e1fcf089a4fe12a12f07426091577a
Parents: 83a867c
Author: Ian Clelland <ic...@chromium.org>
Authored: Fri Dec 13 10:52:20 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:37 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 24 ++++++++++++++++++++----
src/android/FileUtils.java | 7 ++++++-
2 files changed, 26 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/a2b90737/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 4e5175b..b1d397e 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -9,6 +9,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;
@@ -108,9 +109,24 @@ public class ContentFilesystem implements Filesystem {
throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
}
- @Override
- public String filesystemPathForURL(LocalFilesystemURL url) {
- return null;
- }
+ @Override
+ public String filesystemPathForURL(LocalFilesystemURL url) {
+ final String[] LOCAL_FILE_PROJECTION = { MediaStore.Images.Media.DATA };
+ ContentResolver contentResolver = this.cordova.getActivity().getContentResolver();
+ Cursor cursor = contentResolver.query(url.URL, LOCAL_FILE_PROJECTION, null, null, null);
+ if (cursor != null) {
+ try {
+ int columnIndex = cursor.getColumnIndex(LOCAL_FILE_PROJECTION[0]);
+ if (columnIndex != -1 && cursor.getCount() > 0) {
+ cursor.moveToFirst();
+ String path = cursor.getString(columnIndex);
+ return path;
+ }
+ } finally {
+ cursor.close();
+ }
+ }
+ return null;
+ }
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/a2b90737/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 7e852b5..1ae7d0c 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -97,6 +97,7 @@ public class FileUtils extends CordovaPlugin {
fp.mkdirs();
this.filesystems.add(new LocalFilesystem(cordova, "temporary", tempRoot));
this.filesystems.add(new LocalFilesystem(cordova, "persistent", persistentRoot));
+ this.filesystems.add(null);
this.filesystems.add(new ContentFilesystem(cordova));
// Initialize static plugin reference for deprecated getEntry method
@@ -121,7 +122,11 @@ public class FileUtils extends CordovaPlugin {
if (fs == null) {
return null;
}
- return Uri.parse("file:///" + fs.filesystemPathForURL(inputURL));
+ String path = fs.filesystemPathForURL(inputURL);
+ if (path != null) {
+ return Uri.parse("file:///" + fs.filesystemPathForURL(inputURL));
+ }
+ return null;
} catch (IllegalArgumentException e) {
return null;
}
[03/19] git commit: CB-5407: Move getmetadata methods into FS modules
Posted by ia...@apache.org.
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;
+ }
+
}
[13/19] git commit: CB-5407: Cleanup
Posted by ia...@apache.org.
CB-5407: Cleanup
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/25506177
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/25506177
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/25506177
Branch: refs/heads/dev
Commit: 2550617750d4b81c66066455417537c800411625
Parents: 9f3bb54
Author: Ian Clelland <ic...@chromium.org>
Authored: Thu Nov 28 21:42:19 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:00:35 2013 -0500
----------------------------------------------------------------------
src/android/FileUtils.java | 76 ++++++++++-------------------------
src/android/LocalFilesystem.java | 20 ---------
2 files changed, 21 insertions(+), 75 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/25506177/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 868e34e..00483bc 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -33,17 +33,11 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import java.io.ByteArrayInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URLDecoder;
-import java.nio.channels.FileChannel;
import java.util.ArrayList;
/**
@@ -632,55 +626,23 @@ public class FileUtils extends CordovaPlugin {
*/
private JSONObject requestFileSystem(int type) throws IOException, JSONException {
JSONObject fs = new JSONObject();
+ LocalFilesystemURL rootURL;
if (type == TEMPORARY) {
- File fp;
fs.put("name", "temporary");
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- fp = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
- "/Android/data/" + cordova.getActivity().getPackageName() + "/cache/");
- // Create the cache dir if it doesn't exist.
- fp.mkdirs();
- fs.put("root", getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() +
- "/Android/data/" + cordova.getActivity().getPackageName() + "/cache/"));
- } else {
- fp = new File("/data/data/" + cordova.getActivity().getPackageName() + "/cache/");
- // Create the cache dir if it doesn't exist.
- fp.mkdirs();
- fs.put("root", getEntry("/data/data/" + cordova.getActivity().getPackageName() + "/cache/"));
- }
+ rootURL = new LocalFilesystemURL("filesystem://localhost/temporary/");
}
else if (type == PERSISTENT) {
fs.put("name", "persistent");
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- fs.put("root", getEntry(Environment.getExternalStorageDirectory()));
- } else {
- fs.put("root", getEntry("/data/data/" + cordova.getActivity().getPackageName()));
- }
+ rootURL = new LocalFilesystemURL("filesystem://localhost/persistent/");
}
else {
throw new IOException("No filesystem of type requested");
}
- fs.put("root", makeEntryForPath("/", type, true));
+ Filesystem rootFs = this.filesystemForURL(rootURL);
+ fs.put("root", rootFs.getEntryForLocalURL(rootURL));
return fs;
}
- 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("/",1);
- String name = parts[parts.length-1];
- 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;
-
- }
/**
* Returns a JSON object representing the given File.
*
@@ -690,18 +652,22 @@ public class FileUtils extends CordovaPlugin {
*/
@Deprecated
public static JSONObject getEntry(File file) throws JSONException {
- return makeEntryForPath(file.getAbsolutePath(), 0, file.isDirectory());
- }
-
- /**
- * Returns a JSON Object representing a directory on the device's file system
- *
- * @param path to the directory
- * @return
- * @throws JSONException
- */
- private JSONObject getEntry(String path) throws JSONException {
- return getEntry(new File(path));
+ String path = file.getAbsolutePath();
+ Boolean isDir = file.isDirectory();
+ JSONObject entry = new JSONObject();
+
+ int end = path.endsWith("/") ? 1 : 0;
+ String[] parts = path.substring(0,path.length()-end).split("/",1);
+ String name = parts[parts.length-1];
+ 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", 0);
+
+ return entry;
}
/**
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/25506177/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index 2613329..adb3ef6 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -86,25 +86,6 @@ 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 {
@@ -575,7 +556,6 @@ public class LocalFilesystem implements Filesystem {
@Override
public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
int offset, boolean isBinary) throws IOException, NoModificationAllowedException {
- File file = new File(filesystemPathForURL(inputURL));
boolean append = false;
if (offset > 0) {
[17/19] git commit: Android: Updates to allow FileTransfer to
continue to work
Posted by ia...@apache.org.
Android: Updates to allow FileTransfer to continue to work
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/6f52e3b1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/6f52e3b1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/6f52e3b1
Branch: refs/heads/dev
Commit: 6f52e3b1e3f4f1f2fd17ea6470df2221b01a65b3
Parents: 6d0dad6
Author: Ian Clelland <ic...@chromium.org>
Authored: Mon Dec 2 15:54:11 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:36 2013 -0500
----------------------------------------------------------------------
src/android/ContentFilesystem.java | 5 +++
src/android/FileUtils.java | 58 ++++++++++++++++++++++++---------
src/android/Filesystem.java | 3 ++
src/android/LocalFilesystem.java | 23 ++++++++-----
4 files changed, 64 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6f52e3b1/src/android/ContentFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java
index 17bead8..4e5175b 100644
--- a/src/android/ContentFilesystem.java
+++ b/src/android/ContentFilesystem.java
@@ -108,4 +108,9 @@ public class ContentFilesystem implements Filesystem {
throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
}
+ @Override
+ public String filesystemPathForURL(LocalFilesystemURL url) {
+ return null;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6f52e3b1/src/android/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java
index 00483bc..1f345fe 100644
--- a/src/android/FileUtils.java
+++ b/src/android/FileUtils.java
@@ -18,6 +18,7 @@
*/
package org.apache.cordova.file;
+import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
@@ -65,6 +66,9 @@ public class FileUtils extends CordovaPlugin {
public static int PERSISTENT = 1;
public static int RESOURCE = 2;
public static int APPLICATION = 3;
+
+ // This field exists only to support getEntry, below, which has been deprecated
+ private static FileUtils filePlugin;
private interface FileOp {
void run( ) throws Exception;
@@ -94,6 +98,11 @@ public class FileUtils extends CordovaPlugin {
this.filesystems.add(new LocalFilesystem(cordova, "temporary", tempRoot));
this.filesystems.add(new LocalFilesystem(cordova, "persistent", persistentRoot));
this.filesystems.add(new ContentFilesystem(cordova));
+
+ // Initialize static plugin reference for deprecated getEntry method
+ if (filePlugin == null) {
+ filePlugin = this;
+ }
}
public Filesystem filesystemForURL(LocalFilesystemURL localURL) {
@@ -104,6 +113,20 @@ public class FileUtils extends CordovaPlugin {
}
}
+ @Override
+ public Uri remapUri(Uri uri) {
+ try {
+ LocalFilesystemURL inputURL = new LocalFilesystemURL(uri);
+ Filesystem fs = this.filesystemForURL(inputURL);
+ if (fs == null) {
+ return null;
+ }
+ return Uri.parse("file:///" + fs.filesystemPathForURL(inputURL));
+ } catch (IllegalArgumentException e) {
+ return null;
+ }
+ }
+
/**
* Executes the request and returns whether the action was valid.
*
@@ -644,7 +667,10 @@ public class FileUtils extends CordovaPlugin {
}
/**
- * Returns a JSON object representing the given File.
+ * Returns a JSON object representing the given File. Deprecated, as this is only used by
+ * FileTransfer, and because it is a static method that should really be an instance method,
+ * since it depends on the actual filesystem roots in use. Internal APIs should be modified
+ * to use URLs instead of raw FS paths wherever possible, when interfacing with this plugin.
*
* @param file the File to convert
* @return a JSON representation of the given File
@@ -652,22 +678,22 @@ public class FileUtils extends CordovaPlugin {
*/
@Deprecated
public static JSONObject getEntry(File file) throws JSONException {
- String path = file.getAbsolutePath();
- Boolean isDir = file.isDirectory();
- JSONObject entry = new JSONObject();
-
- int end = path.endsWith("/") ? 1 : 0;
- String[] parts = path.substring(0,path.length()-end).split("/",1);
- String name = parts[parts.length-1];
- 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", 0);
+ JSONObject entry;
- return entry;
+ if (filePlugin != null) {
+ LocalFilesystem fs;
+ fs = (LocalFilesystem) filePlugin.filesystems.get(0);
+ entry = fs.makeEntryForFile(file, 0);
+ if (entry != null) {
+ return entry;
+ }
+ fs = (LocalFilesystem) filePlugin.filesystems.get(1);
+ entry = fs.makeEntryForFile(file, 1);
+ if (entry != null) {
+ return entry;
+ }
+ }
+ return null;
}
/**
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6f52e3b1/src/android/Filesystem.java
----------------------------------------------------------------------
diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java
index 6955520..57e1794 100644
--- a/src/android/Filesystem.java
+++ b/src/android/Filesystem.java
@@ -37,4 +37,7 @@ public interface Filesystem {
long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
throws IOException, NoModificationAllowedException;
+ // This method should return null if filesystem urls cannot be mapped to paths
+ String filesystemPathForURL(LocalFilesystemURL url);
+
}
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6f52e3b1/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index be1d2d1..755a802 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -30,6 +30,7 @@ public class LocalFilesystem implements Filesystem {
this.cordova = cordova;
}
+ @Override
public String filesystemPathForURL(LocalFilesystemURL url) {
String path = this.fsRoot + url.fullPath;
if (path.endsWith("/")) {
@@ -37,7 +38,14 @@ public class LocalFilesystem implements Filesystem {
}
return path;
}
-
+
+ private String fullPathForFilesystemPath(String absolutePath) {
+ if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) {
+ return absolutePath.substring(this.fsRoot.length());
+ }
+ return null;
+ }
+
public static JSONObject makeEntryForPath(String path, int fsType, Boolean isDir) throws JSONException {
JSONObject entry = new JSONObject();
@@ -57,7 +65,11 @@ public class LocalFilesystem implements Filesystem {
}
public JSONObject makeEntryForFile(File file, int fsType) throws JSONException {
- return makeEntryForPath(this.fullPathForFilesystemPath(file.getAbsolutePath()), fsType, file.isDirectory());
+ String path = this.fullPathForFilesystemPath(file.getAbsolutePath());
+ if (path != null) {
+ return makeEntryForPath(path, fsType, file.isDirectory());
+ }
+ return null;
}
@Override
@@ -199,13 +211,6 @@ public class LocalFilesystem implements Filesystem {
return entries;
}
- private String fullPathForFilesystemPath(String absolutePath) {
- if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) {
- return absolutePath.substring(this.fsRoot.length());
- }
- return null;
- }
-
@Override
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
File file = new File(filesystemPathForURL(inputURL));