CB-6106: Add support for nativeURL attribute on Entry 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/1e1b530c Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/1e1b530c Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/1e1b530c Branch: refs/heads/master Commit: 1e1b530c85971934944c28f7914893f3dc9c4ae8 Parents: e020449 Author: Ian Clelland <[email protected]> Authored: Thu Feb 27 14:45:38 2014 -0500 Committer: Ian Clelland <[email protected]> Committed: Thu Feb 27 16:44:38 2014 -0500 ---------------------------------------------------------------------- src/android/ContentFilesystem.java | 6 +++--- src/android/Filesystem.java | 14 +++++++++++++- src/android/LocalFilesystem.java | 17 +++++++++++------ src/ios/CDVLocalFilesystem.m | 19 ++++++++++++------- www/DirectoryEntry.js | 8 ++++---- www/DirectoryReader.js | 1 + www/Entry.js | 21 +++++++++++++++++---- www/FileEntry.js | 4 ++-- www/resolveLocalFileSystemURI.js | 2 +- 9 files changed, 64 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/src/android/ContentFilesystem.java ---------------------------------------------------------------------- diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java index 5c34ea2..52ec217 100644 --- a/src/android/ContentFilesystem.java +++ b/src/android/ContentFilesystem.java @@ -42,7 +42,7 @@ public class ContentFilesystem extends Filesystem { cursor.close(); } try { - return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, false /*fp.isDirectory()*/); + return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, false /*fp.isDirectory()*/, inputURL.URL.toString()); } catch (JSONException e) { throw new IOException(); } @@ -71,7 +71,7 @@ public class ContentFilesystem extends Filesystem { } } // Return the directory - return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory); + return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory, Uri.fromFile(fp).toString()); } @@ -159,7 +159,7 @@ public class ContentFilesystem extends Filesystem { if (move) { srcFs.removeFileAtLocalURL(srcURL); } - return makeEntryForURL(destinationURL, false); + return makeEntryForURL(destinationURL, false, destinationURL.URL.toString()); } else { // Need to copy the hard way return super.copyFileToURL(destURL, newName, srcFs, srcURL, move); http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/src/android/Filesystem.java ---------------------------------------------------------------------- diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java index 904f728..8cd730e 100644 --- a/src/android/Filesystem.java +++ b/src/android/Filesystem.java @@ -21,6 +21,11 @@ public abstract class Filesystem { public static JSONObject makeEntryForPath(String path, String fsName, Boolean isDir) throws JSONException { + return makeEntryForPath(path, fsName, isDir, null); + } + + public static JSONObject makeEntryForPath(String path, String fsName, Boolean isDir, String nativeURL) + throws JSONException { JSONObject entry = new JSONObject(); int end = path.endsWith("/") ? 1 : 0; @@ -36,12 +41,19 @@ public abstract class Filesystem { // Backwards compatibility entry.put("filesystem", "temporary".equals(fsName) ? 0 : 1); + if (nativeURL != null) { + entry.put("nativeURL", nativeURL); + } return entry; } public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Boolean isDir) throws JSONException { - return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, isDir); + return makeEntryForURL(inputURL, isDir, null); + } + + public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Boolean isDir, String nativeURL) throws JSONException { + return makeEntryForPath(inputURL.fullPath, inputURL.filesystemName, isDir, nativeURL); } abstract JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/src/android/LocalFilesystem.java ---------------------------------------------------------------------- diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index 7260000..ac7e189 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -32,9 +32,8 @@ public class LocalFilesystem extends Filesystem { this.cordova = cordova; } - @Override - public String filesystemPathForURL(LocalFilesystemURL url) { - String path = new File(this.fsRoot, url.fullPath).toString(); + public String filesystemPathForFullPath(String fullPath) { + String path = new File(this.fsRoot, fullPath).toString(); int questionMark = path.indexOf("?"); if (questionMark >= 0) { path = path.substring(0, questionMark); @@ -44,6 +43,11 @@ public class LocalFilesystem extends Filesystem { } return path; } + + @Override + public String filesystemPathForURL(LocalFilesystemURL url) { + return filesystemPathForFullPath(url.fullPath); + } private String fullPathForFilesystemPath(String absolutePath) { if (absolutePath != null && absolutePath.startsWith(this.fsRoot)) { @@ -103,7 +107,7 @@ public class LocalFilesystem extends Filesystem { public JSONObject makeEntryForFile(File file) throws JSONException { String path = this.fullPathForFilesystemPath(file.getAbsolutePath()); if (path != null) { - return makeEntryForPath(path, this.name, file.isDirectory()); + return makeEntryForPath(path, this.name, file.isDirectory(), Uri.fromFile(file).toString()); } return null; } @@ -130,6 +134,7 @@ public class LocalFilesystem extends Filesystem { entry.put("filesystemName", inputURL.filesystemName); // Backwards compatibility entry.put("filesystem", "temporary".equals(name) ? 0 : 1); + entry.put("nativeURL", Uri.fromFile(fp).toString()); return entry; } catch (JSONException e) { throw new IOException(); @@ -194,7 +199,7 @@ public class LocalFilesystem extends Filesystem { } // Return the directory - return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory); + return makeEntryForPath(requestedURL.fullPath, requestedURL.filesystemName, directory, Uri.fromFile(fp).toString()); } @Override @@ -246,7 +251,7 @@ public class LocalFilesystem extends Filesystem { for (int i = 0; i < files.length; i++) { if (files[i].canRead()) { try { - entries.put(makeEntryForPath(fullPathForFilesystemPath(files[i].getAbsolutePath()), inputURL.filesystemName, files[i].isDirectory())); + entries.put(makeEntryForPath(fullPathForFilesystemPath(files[i].getAbsolutePath()), inputURL.filesystemName, files[i].isDirectory(), Uri.fromFile(files[i]).toString())); } catch (JSONException e) { } } http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/src/ios/CDVLocalFilesystem.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVLocalFilesystem.m b/src/ios/CDVLocalFilesystem.m index 00114b9..5cad489 100644 --- a/src/ios/CDVLocalFilesystem.m +++ b/src/ios/CDVLocalFilesystem.m @@ -79,6 +79,7 @@ [dirEntry setObject:lastPart forKey:@"name"]; [dirEntry setObject: [NSNumber numberWithInt:([fsName isEqualToString:@"temporary"] ? 0 : 1)] forKey: @"filesystem"]; [dirEntry setObject:fsName forKey: @"filesystemName"]; + [dirEntry setObject:[NSString stringWithFormat:@"file://%@",[self filesystemPathForFullPath:fullPath]] forKey:@"nativeURL"]; return dirEntry; } @@ -92,6 +93,16 @@ return fullPath; } +- (NSString *)filesystemPathForFullPath:(NSString *)fullPath +{ + NSString *path = nil; + NSString *strippedFullPath = [self stripQueryParametersFromPath:fullPath]; + path = [NSString stringWithFormat:@"%@%@", self.fsRoot, strippedFullPath]; + if ([path hasSuffix:@"/"]) { + path = [path substringToIndex:([path length]-1)]; + } + return path; +} /* * IN * NSString localURI @@ -103,13 +114,7 @@ */ - (NSString *)filesystemPathForURL:(CDVFilesystemURL *)url { - NSString *path = nil; - NSString *fullPath = [self stripQueryParametersFromPath:url.fullPath]; - path = [NSString stringWithFormat:@"%@%@", self.fsRoot, fullPath]; - if ([path hasSuffix:@"/"]) { - path = [path substringToIndex:([path length]-1)]; - } - return path; + return [self filesystemPathForFullPath:url.fullPath]; } - (CDVFilesystemURL *)URLforFullPath:(NSString *)fullPath http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/www/DirectoryEntry.js ---------------------------------------------------------------------- diff --git a/www/DirectoryEntry.js b/www/DirectoryEntry.js index f688528..417c97e 100644 --- a/www/DirectoryEntry.js +++ b/www/DirectoryEntry.js @@ -35,8 +35,8 @@ var argscheck = require('cordova/argscheck'), * {DOMString} fullPath the absolute full path to the directory (readonly) * {FileSystem} filesystem on which the directory resides (readonly) */ -var DirectoryEntry = function(name, fullPath, fileSystem) { - DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath, fileSystem); +var DirectoryEntry = function(name, fullPath, fileSystem, nativeURL) { + DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath, fileSystem, nativeURL); }; utils.extend(DirectoryEntry, Entry); @@ -60,7 +60,7 @@ DirectoryEntry.prototype.getDirectory = function(path, options, successCallback, argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments); var fs = this.filesystem; var win = successCallback && function(result) { - var entry = new DirectoryEntry(result.name, result.fullPath, fs); + var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL); successCallback(entry); }; var fail = errorCallback && function(code) { @@ -96,7 +96,7 @@ DirectoryEntry.prototype.getFile = function(path, options, successCallback, erro var fs = this.filesystem; var win = successCallback && function(result) { var FileEntry = require('./FileEntry'); - var entry = new FileEntry(result.name, result.fullPath, fs); + var entry = new FileEntry(result.name, result.fullPath, fs, result.nativeURL); successCallback(entry); }; var fail = errorCallback && function(code) { http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/www/DirectoryReader.js ---------------------------------------------------------------------- diff --git a/www/DirectoryReader.js b/www/DirectoryReader.js index 70f444c..2894c9a 100644 --- a/www/DirectoryReader.js +++ b/www/DirectoryReader.js @@ -58,6 +58,7 @@ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) entry.name = result[i].name; entry.fullPath = result[i].fullPath; entry.filesystem = new (require('./FileSystem'))(result[i].filesystemName); + entry.nativeURL = result[i].nativeURL; retVal.push(entry); } reader.hasReadEntries = true; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/www/Entry.js ---------------------------------------------------------------------- diff --git a/www/Entry.js b/www/Entry.js index 6f6ad61..cda4eb6 100644 --- a/www/Entry.js +++ b/www/Entry.js @@ -40,13 +40,18 @@ var argscheck = require('cordova/argscheck'), * @param fileSystem * {FileSystem} the filesystem on which this entry resides * (readonly) + * @param nativeURL + * {DOMString} an alternate URL which can be used by native + * webview controls, for example media players. + * (optional, readonly) */ -function Entry(isFile, isDirectory, name, fullPath, fileSystem) { +function Entry(isFile, isDirectory, name, fullPath, fileSystem, nativeURL) { this.isFile = !!isFile; this.isDirectory = !!isDirectory; this.name = name || ''; this.fullPath = fullPath || ''; this.filesystem = fileSystem || null; + this.nativeURL = nativeURL || null; } /** @@ -110,7 +115,7 @@ Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallbac if (entry) { if (successCallback) { // create appropriate Entry object - var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, fs); + var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL); successCallback(result); } } @@ -151,7 +156,7 @@ Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallbac if (entry) { if (successCallback) { // create appropriate Entry object - var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, fs); + var result = (entry.isDirectory) ? new (require('./DirectoryEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL) : new (require('org.apache.cordova.file.FileEntry'))(entry.name, entry.fullPath, fs, entry.nativeURL); successCallback(result); } } @@ -177,6 +182,14 @@ Entry.prototype.toURL = function() { }; /** + * Return a URL that can be used to as the src attribute of a <video> or + * <audio> tag, in case it is different from the URL returned by .toURL(). + */ +Entry.prototype.toNativeURL = function() { + return this.nativeURL || this.toURL(); +}; + +/** * Returns a URI that can be used to identify this entry. * * @param {DOMString} mimeType for a FileEntry, the mime type to be used to interpret the file, when loaded through this URI. @@ -215,7 +228,7 @@ Entry.prototype.getParent = function(successCallback, errorCallback) { var fs = this.filesystem; var win = successCallback && function(result) { var DirectoryEntry = require('./DirectoryEntry'); - var entry = new DirectoryEntry(result.name, result.fullPath, fs); + var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL); successCallback(entry); }; var fail = errorCallback && function(code) { http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/www/FileEntry.js ---------------------------------------------------------------------- diff --git a/www/FileEntry.js b/www/FileEntry.js index 20c0a5c..ac11d06 100644 --- a/www/FileEntry.js +++ b/www/FileEntry.js @@ -35,8 +35,8 @@ var utils = require('cordova/utils'), * {DOMString} fullPath the absolute full path to the file (readonly) * {FileSystem} filesystem on which the file resides (readonly) */ -var FileEntry = function(name, fullPath, fileSystem) { - FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath, fileSystem]); +var FileEntry = function(name, fullPath, fileSystem, nativeURL) { + FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath, fileSystem, nativeURL]); }; utils.extend(FileEntry, Entry); http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e1b530c/www/resolveLocalFileSystemURI.js ---------------------------------------------------------------------- diff --git a/www/resolveLocalFileSystemURI.js b/www/resolveLocalFileSystemURI.js index 8c97d31..f2fd105 100644 --- a/www/resolveLocalFileSystemURI.js +++ b/www/resolveLocalFileSystemURI.js @@ -51,7 +51,7 @@ module.exports.resolveLocalFileSystemURL = function(uri, successCallback, errorC // create appropriate Entry object var fsName = entry.filesystemName || (entry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'); var fs = new FileSystem(fsName, {name:"", fullPath:"/"}); - var result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath, fs) : new FileEntry(entry.name, entry.fullPath, fs); + var result = (entry.isDirectory) ? new DirectoryEntry(entry.name, entry.fullPath, fs, entry.nativeURL) : new FileEntry(entry.name, entry.fullPath, fs, entry.nativeURL); successCallback(result); } }
