Repository: cordova-plugin-file Updated Branches: refs/heads/master 272d27ef5 -> 4435819f6
CB-6525 android, ios: Allow file: URLs in all APIs. Fixes FileTransfer.download not being called. Root problem of downlaod was that the target URL was not being properly resolved to an Entry on the native side. This wasn't caught by mobilespec due to the requestFileSystem() using cdvfile: URLs still, while resolveLocalFileSystemURL(entry.toURL()), would yield a file: URL. This commit normalizes fs.root DirectoryEntries so that they are file:// as well, and initializes them on start-up with a new "getAllFileSystems" exec() call. It also aims to ensure that JSON entries always have nativeURL set. 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/4435819f Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/4435819f Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/4435819f Branch: refs/heads/master Commit: 4435819f6356e14602a4173d32e08576be473eca Parents: 272d27e Author: Andrew Grieve <[email protected]> Authored: Tue Apr 29 22:25:59 2014 -0400 Committer: Andrew Grieve <[email protected]> Committed: Tue Apr 29 22:25:59 2014 -0400 ---------------------------------------------------------------------- plugin.xml | 13 ++++- src/android/ContentFilesystem.java | 8 +++ src/android/FileUtils.java | 30 +++++++++- src/android/Filesystem.java | 16 +----- src/android/LocalFilesystem.java | 14 +---- src/ios/CDVAssetLibraryFilesystem.m | 8 +-- src/ios/CDVFile.h | 3 +- src/ios/CDVFile.m | 98 +++++++++++++++++--------------- src/ios/CDVLocalFilesystem.m | 18 +++--- www/FileSystem.js | 4 +- www/fileSystems-roots.js | 44 ++++++++++++++ www/fileSystems.js | 25 ++++++++ www/requestFileSystem.js | 10 +++- www/resolveLocalFileSystemURI.js | 8 ++- 14 files changed, 200 insertions(+), 99 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/plugin.xml ---------------------------------------------------------------------- diff --git a/plugin.xml b/plugin.xml index cedabbf..3bced58 100644 --- a/plugin.xml +++ b/plugin.xml @@ -92,6 +92,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" <clobbers target="window.ProgressEvent" /> </js-module> + <js-module src="www/fileSystems.js" name="fileSystems" /> + <js-module src="www/requestFileSystem.js" name="requestFileSystem"> <clobbers target="window.requestFileSystem" /> </js-module> @@ -128,7 +130,10 @@ xmlns:android="http://schemas.android.com/apk/res/android" <!-- android specific file apis --> <js-module src="www/android/FileSystem.js" name="androidFileSystem"> - <merges target="window.FileSystem" /> + <merges target="FileSystem" /> + </js-module> + <js-module src="www/fileSystems-roots.js" name="fileSystems-roots"> + <runs/> </js-module> </platform> @@ -196,7 +201,11 @@ xmlns:android="http://schemas.android.com/apk/res/android" <!-- ios specific file apis --> <js-module src="www/ios/FileSystem.js" name="iosFileSystem"> - <merges target="window.FileSystem" /> + <merges target="FileSystem" /> + </js-module> + + <js-module src="www/fileSystems-roots.js" name="fileSystems-roots"> + <runs/> </js-module> <framework src="AssetsLibrary.framework" /> http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/android/ContentFilesystem.java ---------------------------------------------------------------------- diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java index 4206033..d35ee5c 100644 --- a/src/android/ContentFilesystem.java +++ b/src/android/ContentFilesystem.java @@ -49,6 +49,14 @@ public class ContentFilesystem extends Filesystem { @Override public JSONObject getEntryForLocalURL(LocalFilesystemURL inputURL) throws IOException { + if ("/".equals(inputURL.fullPath)) { + try { + return LocalFilesystem.makeEntryForURL(inputURL, true, inputURL.URL.toString()); + } catch (JSONException e) { + throw new IOException(); + } + } + // Get the cursor to validate that the file exists Cursor cursor = openCursorForURL(inputURL); String filePath = null; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/android/FileUtils.java ---------------------------------------------------------------------- diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index a7e86fa..5fbe1f6 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -338,7 +338,13 @@ public class FileUtils extends CordovaPlugin { } }, callbackContext); } - else if (action.equals("requestFileSystem")) { + else if (action.equals("requestAllFileSystems")) { + threadhelper( new FileOp( ){ + public void run() throws IOException, JSONException { + callbackContext.success(requestAllFileSystems()); + } + }, callbackContext); + } else if (action.equals("requestFileSystem")) { final int fstype=args.getInt(0); final long size = args.optLong(1); threadhelper( new FileOp( ){ @@ -819,11 +825,31 @@ public class FileUtils extends CordovaPlugin { if (rootFs == null) { throw new IOException("No filesystem of type requested"); } + LocalFilesystemURL rootURL = new LocalFilesystemURL(LocalFilesystemURL.FILESYSTEM_PROTOCOL + "://localhost/"+rootFs.name+"/"); + fs.put("name", rootFs.name); - fs.put("root", Filesystem.makeEntryForPath("/", rootFs.name, true)); + fs.put("root", rootFs.getEntryForLocalURL(rootURL)); return fs; } + + /** + * Requests a filesystem in which to store application data. + * + * @param type of file system requested + * @return a JSONObject representing the file system + * @throws IOException + * @throws JSONException + */ + private JSONArray requestAllFileSystems() throws IOException, JSONException { + JSONArray ret = new JSONArray(); + for (Filesystem fs : filesystems) { + LocalFilesystemURL rootURL = new LocalFilesystemURL(LocalFilesystemURL.FILESYSTEM_PROTOCOL + "://localhost/"+fs.name+"/"); + ret.put(fs.getEntryForLocalURL(rootURL)); + } + return ret; + } + /** * Returns a JSON object representing the given File. Internal APIs should be modified * to use URLs instead of raw FS paths wherever possible, when interfacing with this plugin. http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/android/Filesystem.java ---------------------------------------------------------------------- diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java index 954c098..f03aace 100644 --- a/src/android/Filesystem.java +++ b/src/android/Filesystem.java @@ -37,11 +37,6 @@ public abstract class Filesystem { public void handleData(InputStream inputStream, String contentType) throws IOException; } - 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(); @@ -59,15 +54,8 @@ public abstract class Filesystem { // Backwards compatibility entry.put("filesystem", "temporary".equals(fsName) ? 0 : 1); - if (nativeURL != null) { - entry.put("nativeURL", nativeURL); - } + entry.put("nativeURL", nativeURL); return entry; - - } - - public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Boolean isDir) throws JSONException { - return makeEntryForURL(inputURL, isDir, null); } public static JSONObject makeEntryForURL(LocalFilesystemURL inputURL, Boolean isDir, String nativeURL) throws JSONException { @@ -153,7 +141,7 @@ public abstract class Filesystem { // Delete original srcFs.removeFileAtLocalURL(srcURL); } - return makeEntryForURL(destination, false); + return getEntryForLocalURL(destination); } else { throw new NoModificationAllowedException("Cannot move file at source URL"); } http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/android/LocalFilesystem.java ---------------------------------------------------------------------- diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index c0e14e4..41bd427 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -141,19 +141,7 @@ public class LocalFilesystem extends Filesystem { 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", inputURL.fullPath); - // The file system can't be specified, as it would lead to an infinite loop. - // But we can specify the name of the FS, and the rest can be reconstructed - // in JS. - entry.put("filesystemName", inputURL.filesystemName); - // Backwards compatibility - entry.put("filesystem", "temporary".equals(name) ? 0 : 1); - entry.put("nativeURL", Uri.fromFile(fp).toString()); - return entry; + return LocalFilesystem.makeEntryForURL(inputURL, fp.isDirectory(), Uri.fromFile(fp).toString()); } catch (JSONException e) { throw new IOException(); } http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/ios/CDVAssetLibraryFilesystem.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVAssetLibraryFilesystem.m b/src/ios/CDVAssetLibraryFilesystem.m index d71d801..965251a 100644 --- a/src/ios/CDVAssetLibraryFilesystem.m +++ b/src/ios/CDVAssetLibraryFilesystem.m @@ -56,10 +56,10 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; } - (NSDictionary *)makeEntryForLocalURL:(CDVFilesystemURL *)url { - return [self makeEntryForPath:url.fullPath fileSystemName:self.name isDirectory:NO]; + return [self makeEntryForPath:url.fullPath isDirectory:NO]; } -- (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystemName:(NSString *)fsName isDirectory:(BOOL)isDir +- (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir { NSMutableDictionary* dirEntry = [NSMutableDictionary dictionaryWithCapacity:5]; NSString* lastPart = [fullPath lastPathComponent]; @@ -70,8 +70,8 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; [dirEntry setObject:[NSNumber numberWithBool:isDir] forKey:@"isDirectory"]; [dirEntry setObject:fullPath forKey:@"fullPath"]; [dirEntry setObject:lastPart forKey:@"name"]; - [dirEntry setObject: [NSNumber numberWithInt:([fsName isEqualToString:@"temporary"] ? 0 : 1)] forKey: @"filesystem"]; - [dirEntry setObject:fsName forKey: @"filesystemName"]; + [dirEntry setObject:self.name forKey: @"filesystemName"]; + dirEntry[@"nativeURL"] = [NSString stringWithFormat:@"assets-library:/%@",fullPath]; return dirEntry; } http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/ios/CDVFile.h ---------------------------------------------------------------------- diff --git a/src/ios/CDVFile.h b/src/ios/CDVFile.h index 63f744c..490d552 100644 --- a/src/ios/CDVFile.h +++ b/src/ios/CDVFile.h @@ -78,6 +78,7 @@ typedef int CDVFileError; - (void)getFileMetadataForURL:(CDVFilesystemURL *)localURL callback:(void (^)(CDVPluginResult *))callback; - (NSDictionary *)makeEntryForLocalURL:(CDVFilesystemURL *)url; +- (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir; @property (nonatomic,strong) NSString *name; @@ -98,7 +99,7 @@ typedef int CDVFileError; } - (NSNumber*)checkFreeDiskSpace:(NSString*)appPath; -- (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystemName:(NSString *)fsName isDirectory:(BOOL)isDir; +- (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir; - (NSDictionary *)makeEntryForURL:(NSURL *)URL; - (CDVFilesystemURL *)fileSystemURLforLocalPath:(NSString *)localPath; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/ios/CDVFile.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVFile.m b/src/ios/CDVFile.m index 8d0a3d8..11b8dd3 100644 --- a/src/ios/CDVFile.m +++ b/src/ios/CDVFile.m @@ -343,6 +343,21 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; return self; } +- (CDVFilesystemURL *)fileSystemURLforArg:(NSString *)urlArg +{ + CDVFilesystemURL* ret = nil; + if ([urlArg hasPrefix:@"file://"]) { + /* This looks like a file url. Get the path, and see if any handlers recognize it. */ + NSURL *fileURL = [NSURL URLWithString:urlArg]; + NSURL *resolvedFileURL = [fileURL URLByResolvingSymlinksInPath]; + NSString *path = [resolvedFileURL path]; + ret = [self fileSystemURLforLocalPath:path]; + } else { + ret = [CDVFilesystemURL fileSystemURLWithString:urlArg]; + } + return ret; +} + - (CDVFilesystemURL *)fileSystemURLforLocalPath:(NSString *)localPath { CDVFilesystemURL *localURL = nil; @@ -432,6 +447,17 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; } + +- (void)requestAllFileSystems:(CDVInvokedUrlCommand*)command +{ + NSMutableArray* ret = [[NSMutableArray alloc] init]; + for (NSObject<CDVFileSystem>* root in fileSystems_) { + [ret addObject:[self makeEntryForPath:@"/" fileSystemName:root.name isDirectory:YES]]; + } + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:ret]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; +} + /* Creates and returns a dictionary representing an Entry Object * * IN: @@ -449,18 +475,8 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; */ - (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystemName:(NSString *)fsName isDirectory:(BOOL)isDir { - NSMutableDictionary* dirEntry = [NSMutableDictionary dictionaryWithCapacity:5]; - NSString* lastPart = [fullPath lastPathComponent]; - if (isDir && ![fullPath hasSuffix:@"/"]) { - fullPath = [fullPath stringByAppendingString:@"/"]; - } - [dirEntry setObject:[NSNumber numberWithBool:!isDir] forKey:@"isFile"]; - [dirEntry setObject:[NSNumber numberWithBool:isDir] forKey:@"isDirectory"]; - [dirEntry setObject:fullPath forKey:@"fullPath"]; - [dirEntry setObject:lastPart forKey:@"name"]; - [dirEntry setObject: [NSNumber numberWithInt:([fsName isEqualToString:@"temporary"] ? 0 : 1)] forKey: @"filesystem"]; - [dirEntry setObject:fsName forKey: @"filesystemName"]; - return dirEntry; + NSObject<CDVFileSystem> *fs = [self fileSystemByName:fsName]; + return [fs makeEntryForPath:fullPath isDirectory:isDir]; } - (NSDictionary *)makeEntryForLocalURL:(CDVFilesystemURL *)localURL @@ -471,7 +487,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (NSDictionary *)makeEntryForURL:(NSURL *)URL { - CDVFilesystemURL *fsURL = [CDVFilesystemURL fileSystemURLWithURL:URL]; + CDVFilesystemURL* fsURL = [self fileSystemURLforArg:[URL absoluteString]]; return [self makeEntryForLocalURL:fsURL]; } @@ -492,18 +508,8 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; // arguments NSString* localURIstr = [command.arguments objectAtIndex:0]; CDVPluginResult* result; - CDVFilesystemURL* inputURI; + CDVFilesystemURL* inputURI = [self fileSystemURLforArg:localURIstr]; - /* Backwards-compatibility: Check for file:// urls */ - if ([localURIstr hasPrefix:@"file://"]) { - /* This looks like a file url. Get the path, and see if any handlers recognize it. */ - NSURL *fileURL = [NSURL URLWithString:localURIstr]; - NSURL *resolvedFileURL = [fileURL URLByResolvingSymlinksInPath]; - NSString *path = [resolvedFileURL path]; - inputURI = [self fileSystemURLforLocalPath:path]; - } else { - inputURI = [CDVFilesystemURL fileSystemURLWithString:localURIstr]; - } if (inputURI == nil || inputURI.fileSystemName == nil) { result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:ENCODING_ERR]; } else { @@ -578,7 +584,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)getFile:(CDVInvokedUrlCommand*)command { NSString* baseURIstr = [command.arguments objectAtIndex:0]; - CDVFilesystemURL* baseURI = [CDVFilesystemURL fileSystemURLWithString:baseURIstr]; + CDVFilesystemURL* baseURI = [self fileSystemURLforArg:baseURIstr]; NSString* requestedPath = [command.arguments objectAtIndex:1]; NSDictionary* options = [command.arguments objectAtIndex:2 withDefault:nil]; @@ -601,7 +607,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)getParent:(CDVInvokedUrlCommand*)command { // arguments are URL encoded - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command.arguments objectAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; CDVPluginResult* result = [fs getParentForURL:localURI]; @@ -616,8 +622,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)getMetadata:(CDVInvokedUrlCommand*)command { // arguments - NSString* localURIstr = [command.arguments objectAtIndex:0]; - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:localURIstr]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; [fs getMetadataForURL:localURI callback:^(CDVPluginResult* result) { [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; @@ -632,7 +637,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)setMetadata:(CDVInvokedUrlCommand*)command { // arguments - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command.arguments objectAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSDictionary* options = [command.arguments objectAtIndex:1 withDefault:nil]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; CDVPluginResult* result = [fs setMetadataForURL:localURI withObject:options]; @@ -652,7 +657,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)remove:(CDVInvokedUrlCommand*)command { // arguments - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command.arguments objectAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; CDVPluginResult* result = nil; if ([localURI.fullPath isEqualToString:@""]) { @@ -676,7 +681,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)removeRecursively:(CDVInvokedUrlCommand*)command { // arguments - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command.arguments objectAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; CDVPluginResult* result = nil; if ([localURI.fullPath isEqualToString:@""]) { @@ -724,8 +729,8 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; return; } - CDVFilesystemURL* srcURL = [CDVFilesystemURL fileSystemURLWithString:srcURLstr]; - CDVFilesystemURL* destURL = [CDVFilesystemURL fileSystemURLWithString:destURLstr]; + CDVFilesystemURL* srcURL = [self fileSystemURLforArg:srcURLstr]; + CDVFilesystemURL* destURL = [self fileSystemURLforArg:destURLstr]; NSObject<CDVFileSystem> *srcFs = [self filesystemForURL:srcURL]; NSObject<CDVFileSystem> *destFs = [self filesystemForURL:destURL]; @@ -748,8 +753,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)getFileMetadata:(CDVInvokedUrlCommand*)command { // arguments - NSString* localURIstr = [command.arguments objectAtIndex:0]; - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:localURIstr]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; [fs getFileMetadataForURL:localURI callback:^(CDVPluginResult* result) { @@ -759,7 +763,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)readEntries:(CDVInvokedUrlCommand*)command { - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command.arguments objectAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; CDVPluginResult *result = [fs readEntriesAtURL:localURI]; @@ -777,12 +781,18 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)readAsText:(CDVInvokedUrlCommand*)command { // arguments - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command argumentAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSString* encoding = [command argumentAtIndex:1]; NSInteger start = [[command argumentAtIndex:2] integerValue]; NSInteger end = [[command argumentAtIndex:3] integerValue]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; + + if (fs == nil) { + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + return; + } // TODO: implement if ([@"UTF-8" caseInsensitiveCompare : encoding] != NSOrderedSame) { @@ -826,7 +836,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)readAsDataURL:(CDVInvokedUrlCommand*)command { - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command argumentAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSInteger start = [[command argumentAtIndex:1] integerValue]; NSInteger end = [[command argumentAtIndex:2] integerValue]; @@ -858,7 +868,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)readAsArrayBuffer:(CDVInvokedUrlCommand*)command { - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command argumentAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSInteger start = [[command argumentAtIndex:1] integerValue]; NSInteger end = [[command argumentAtIndex:2] integerValue]; @@ -880,7 +890,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)readAsBinaryString:(CDVInvokedUrlCommand*)command { - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command argumentAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; NSInteger start = [[command argumentAtIndex:1] integerValue]; NSInteger end = [[command argumentAtIndex:2] integerValue]; @@ -906,7 +916,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)truncate:(CDVInvokedUrlCommand*)command { // arguments - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:[command argumentAtIndex:0]]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; unsigned long long pos = (unsigned long long)[[command.arguments objectAtIndex:1] longLongValue]; NSObject<CDVFileSystem> *fs = [self filesystemForURL:localURI]; @@ -928,8 +938,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; NSArray* arguments = command.arguments; // arguments - NSString* localURIstr = [arguments objectAtIndex:0]; - CDVFilesystemURL* localURI = [CDVFilesystemURL fileSystemURLWithString:localURIstr]; + CDVFilesystemURL* localURI = [self fileSystemURLforArg:command.arguments[0]]; id argData = [arguments objectAtIndex:1]; unsigned long long pos = (unsigned long long)[[arguments objectAtIndex:2] longLongValue]; @@ -1031,8 +1040,7 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; - (void)_getLocalFilesystemPath:(CDVInvokedUrlCommand*)command { - NSString* localURLstr = [command.arguments objectAtIndex:0]; - CDVFilesystemURL* localURL = [CDVFilesystemURL fileSystemURLWithString:localURLstr]; + CDVFilesystemURL* localURL = [self fileSystemURLforArg:command.arguments[0]]; NSString* fsPath = [self filesystemPathForURL:localURL]; CDVPluginResult* result; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/src/ios/CDVLocalFilesystem.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVLocalFilesystem.m b/src/ios/CDVLocalFilesystem.m index 3682ab1..d3cc619 100644 --- a/src/ios/CDVLocalFilesystem.m +++ b/src/ios/CDVLocalFilesystem.m @@ -61,12 +61,12 @@ // see if exists and is file or dir BOOL bExists = [fileMgr fileExistsAtPath:path isDirectory:&isDir]; if (bExists) { - return [self makeEntryForPath:url.fullPath fileSystemName:url.fileSystemName isDirectory:isDir]; + return [self makeEntryForPath:url.fullPath isDirectory:isDir]; } else { return nil; } } -- (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystemName:(NSString *)fsName isDirectory:(BOOL)isDir +- (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir { NSMutableDictionary* dirEntry = [NSMutableDictionary dictionaryWithCapacity:5]; NSString* lastPart = [[self stripQueryParametersFromPath:fullPath] lastPathComponent]; @@ -77,11 +77,9 @@ [dirEntry setObject:[NSNumber numberWithBool:isDir] forKey:@"isDirectory"]; [dirEntry setObject:fullPath forKey:@"fullPath"]; [dirEntry setObject:lastPart forKey:@"name"]; - [dirEntry setObject: [NSNumber numberWithInt:([fsName isEqualToString:@"temporary"] ? 0 : 1)] forKey: @"filesystem"]; - [dirEntry setObject:fsName forKey: @"filesystemName"]; + [dirEntry setObject:self.name forKey: @"filesystemName"]; dirEntry[@"nativeURL"] = [[NSURL fileURLWithPath:[self filesystemPathForFullPath:fullPath]] absoluteString]; - return dirEntry; } @@ -228,7 +226,7 @@ } else { // NSLog(@"newly created file/dir (%@) exists: %d", reqFullPath, [fileMgr fileExistsAtPath:reqFullPath]); // file existed or was created - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self makeEntryForPath:requestedURL.fullPath fileSystemName:baseURI.fileSystemName isDirectory:bDirRequest]]; + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self makeEntryForPath:requestedURL.fullPath isDirectory:bDirRequest]]; } } // are all possible conditions met? } @@ -255,7 +253,7 @@ BOOL bIsDir; BOOL bExists = [fileMgr fileExistsAtPath:[self filesystemPathForURL:newURI] isDirectory:&bIsDir]; if (bExists) { - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self makeEntryForPath:newURI.fullPath fileSystemName:newURI.fileSystemName isDirectory:bIsDir]]; + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self makeEntryForPath:newURI.fullPath isDirectory:bIsDir]]; } else { // invalid path or file does not exist result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR]; @@ -424,7 +422,7 @@ NSString* entryPath = [fileSystemPath stringByAppendingPathComponent:name]; BOOL bIsDir = NO; [fileMgr fileExistsAtPath:entryPath isDirectory:&bIsDir]; - NSDictionary* entryDict = [self makeEntryForPath:[self fullPathForFileSystemPath:entryPath] fileSystemName:localURI.fileSystemName isDirectory:bIsDir]; + NSDictionary* entryDict = [self makeEntryForPath:[self fullPathForFileSystemPath:entryPath] isDirectory:bIsDir]; [entries addObject:entryDict]; } } @@ -606,7 +604,7 @@ } if (bSuccess) { // should verify it is there and of the correct type??? - NSDictionary* newEntry = [self makeEntryForPath:newFullPath fileSystemName:destURL.fileSystemName isDirectory:bSrcIsDir]; + NSDictionary* newEntry = [self makeEntryForPath:newFullPath isDirectory:bSrcIsDir]; result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:newEntry]; } else { if (error) { @@ -628,7 +626,7 @@ BOOL bSuccess = [data writeToFile:newFileSystemPath atomically:YES]; if (bSuccess) { // should verify it is there and of the correct type??? - NSDictionary* newEntry = [self makeEntryForPath:newFullPath fileSystemName:destURL.fileSystemName isDirectory:NO]; + NSDictionary* newEntry = [self makeEntryForPath:newFullPath isDirectory:NO]; result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:newEntry]; } else { result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:ABORT_ERR]; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/www/FileSystem.js ---------------------------------------------------------------------- diff --git a/www/FileSystem.js b/www/FileSystem.js index 7ac4671..efa6740 100644 --- a/www/FileSystem.js +++ b/www/FileSystem.js @@ -29,9 +29,9 @@ var DirectoryEntry = require('./DirectoryEntry'); * {DirectoryEntry} root directory of the file system (readonly) */ var FileSystem = function(name, root) { - this.name = name || null; + this.name = name; if (root) { - this.root = new DirectoryEntry(root.name, root.fullPath, this); + this.root = new DirectoryEntry(root.name, root.fullPath, this, root.nativeURL); } else { this.root = new DirectoryEntry(this.name, '/', this); } http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/www/fileSystems-roots.js ---------------------------------------------------------------------- diff --git a/www/fileSystems-roots.js b/www/fileSystems-roots.js new file mode 100644 index 0000000..81c2277 --- /dev/null +++ b/www/fileSystems-roots.js @@ -0,0 +1,44 @@ +/* + * + * 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. + * +*/ + +// Map of fsName -> FileSystem. +var fsMap = null; +var FileSystem = require('./FileSystem'); +var exec = require('cordova/exec'); + +// Overridden by iOS & Android to populate fsMap. +require('./fileSystems').getFs = function(name, callback) { + if (fsMap) { + callback(fsMap[name]); + } else { + exec(success, null, "File", "requestAllFileSystems", []); + function success(response) { + fsMap = {}; + for (var i = 0; i < response.length; ++i) { + var fsRoot = response[i]; + var fs = new FileSystem(fsRoot.filesystemName, fsRoot); + fsMap[fs.name] = fs; + } + callback(fsMap[name]); + } + } +}; + http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/www/fileSystems.js ---------------------------------------------------------------------- diff --git a/www/fileSystems.js b/www/fileSystems.js new file mode 100644 index 0000000..725af73 --- /dev/null +++ b/www/fileSystems.js @@ -0,0 +1,25 @@ +/* + * + * 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. + * +*/ + +// Overridden by iOS & Android to populate fsMap. +module.exports.getFs = function(name, callback) { + callback(null); +}; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/www/requestFileSystem.js ---------------------------------------------------------------------- diff --git a/www/requestFileSystem.js b/www/requestFileSystem.js index 7a1cc8f..3e1b6a8 100644 --- a/www/requestFileSystem.js +++ b/www/requestFileSystem.js @@ -23,6 +23,7 @@ var argscheck = require('cordova/argscheck'), FileError = require('./FileError'), FileSystem = require('./FileSystem'), exec = require('cordova/exec'); +var fileSystems = require('./fileSystems'); /** * Request a file system in which to store application data. @@ -44,9 +45,12 @@ var requestFileSystem = function(type, size, successCallback, errorCallback) { var success = function(file_system) { if (file_system) { if (successCallback) { - // grab the name and root from the file system object - var result = new FileSystem(file_system.name, file_system.root); - successCallback(result); + fileSystems.getFs(file_system.name, function(fs) { + if (!fs) { + fs = new FileSystem(file_system.name, file_system.root); + } + successCallback(fs); + }); } } else { http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/4435819f/www/resolveLocalFileSystemURI.js ---------------------------------------------------------------------- diff --git a/www/resolveLocalFileSystemURI.js b/www/resolveLocalFileSystemURI.js index f2fd105..9afbd36 100644 --- a/www/resolveLocalFileSystemURI.js +++ b/www/resolveLocalFileSystemURI.js @@ -24,6 +24,7 @@ var argscheck = require('cordova/argscheck'), FileEntry = require('./FileEntry'), FileError = require('./FileError'), exec = require('cordova/exec'); +var fileSystems = require('./fileSystems'); /** * Look up file system Entry referred to by local URI. @@ -50,9 +51,10 @@ module.exports.resolveLocalFileSystemURL = function(uri, successCallback, errorC if (successCallback) { // 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, entry.nativeURL) : new FileEntry(entry.name, entry.fullPath, fs, entry.nativeURL); - successCallback(result); + fileSystems.getFs(fsName, function(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); + }); } } else {
