CB-5233: ios: Fixes for asset-library 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/1e6a5d44 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/1e6a5d44 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/1e6a5d44 Branch: refs/heads/master Commit: 1e6a5d449718acaf5f9404dc517490c76be02306 Parents: 5294f27 Author: Ian Clelland <[email protected]> Authored: Tue Feb 11 11:57:45 2014 -0500 Committer: Ian Clelland <[email protected]> Committed: Tue Feb 11 14:11:27 2014 -0500 ---------------------------------------------------------------------- RELEASENOTES.md | 1 + src/ios/CDVAssetLibraryFilesystem.m | 30 ++++++++++++++++++++++-------- src/ios/CDVFile.m | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e6a5d44/RELEASENOTES.md ---------------------------------------------------------------------- diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 3c2eae5..8427d64 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -129,3 +129,4 @@ * CB-5959: Entry.getMetadata should return size attribute * CB-6010: Test properly for presence of URLforFilesystemPath method * CB-6012: Preserve query strings on cdvfile:// URLs where necessary +* CB-5233: Make asset-library urls work properly on iOS http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e6a5d44/src/ios/CDVAssetLibraryFilesystem.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVAssetLibraryFilesystem.m b/src/ios/CDVAssetLibraryFilesystem.m index cca4038..2aa2a36 100644 --- a/src/ios/CDVAssetLibraryFilesystem.m +++ b/src/ios/CDVAssetLibraryFilesystem.m @@ -31,6 +31,24 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; @implementation CDVAssetLibraryFilesystem @synthesize name=_name; + +/* + The CDVAssetLibraryFilesystem works with resources which are identified + by iOS as + asset-library://<path> + and represents them internally as URLs of the form + cdvfile://localhost/assets-library/<path> + */ + +- (NSURL *)assetLibraryURLForLocalURL:(CDVFilesystemURL *)url +{ + if ([url.url.scheme isEqualToString:kCDVFilesystemURLPrefix]) { + NSString *path = [[url.url absoluteString] substringFromIndex:[@"cdvfile://localhost/assets-library" length]]; + return [NSURL URLWithString:[NSString stringWithFormat:@"assets-library:/%@", path]]; + } + return url.url; +} + - (CDVPluginResult *)entryForLocalURI:(CDVFilesystemURL *)url { NSDictionary* entry = [self makeEntryForLocalURL:url]; @@ -38,7 +56,7 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; } - (NSDictionary *)makeEntryForLocalURL:(CDVFilesystemURL *)url { - return [self makeEntryForPath:[url.url absoluteString] fileSystemName:self.name isDirectory:NO]; + return [self makeEntryForPath:url.fullPath fileSystemName:self.name isDirectory:NO]; } - (NSDictionary*)makeEntryForPath:(NSString*)fullPath fileSystemName:(NSString *)fsName isDirectory:(BOOL)isDir @@ -133,7 +151,7 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; }; ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; - [assetsLibrary assetForURL:url.url resultBlock:resultBlock failureBlock:failureBlock]; + [assetsLibrary assetForURL:[self assetLibraryURLForLocalURL:url] resultBlock:resultBlock failureBlock:failureBlock]; return; } @@ -196,8 +214,6 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; - (void)readFileAtURL:(CDVFilesystemURL *)localURL start:(NSInteger)start end:(NSInteger)end callback:(void (^)(NSData*, NSString* mimeType, CDVFileError))callback { - NSString *path = [self fileSystemPathForURL:localURL]; - ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset) { if (asset) { // We have the asset! Get the data and send it off. @@ -220,13 +236,11 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; }; ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; - [assetsLibrary assetForURL:[NSURL URLWithString:path] resultBlock:resultBlock failureBlock:failureBlock]; + [assetsLibrary assetForURL:[self assetLibraryURLForLocalURL:localURL] resultBlock:resultBlock failureBlock:failureBlock]; } - (void)getFileMetadataForURL:(CDVFilesystemURL *)localURL callback:(void (^)(CDVPluginResult *))callback { - NSString *path = [self fileSystemPathForURL:localURL]; - // In this case, we need to use an asynchronous method to retrieve the file. // Because of this, we can't just assign to `result` and send it at the end of the method. // Instead, we return after calling the asynchronous method and send `result` in each of the blocks. @@ -256,7 +270,7 @@ NSString* const kCDVAssetsLibraryScheme = @"assets-library"; }; ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; - [assetsLibrary assetForURL:[NSURL URLWithString:path] resultBlock:resultBlock failureBlock:failureBlock]; + [assetsLibrary assetForURL:[self assetLibraryURLForLocalURL:localURL] resultBlock:resultBlock failureBlock:failureBlock]; return; } @end http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/1e6a5d44/src/ios/CDVFile.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVFile.m b/src/ios/CDVFile.m index 9c915e9..7e561bf 100644 --- a/src/ios/CDVFile.m +++ b/src/ios/CDVFile.m @@ -94,6 +94,8 @@ NSString* const kCDVFilesystemURLPrefix = @"cdvfile"; return @""; } return [path substringFromIndex:slashRange.location]; + } else if ([[uri scheme] isEqualToString:kCDVAssetsLibraryScheme]) { + return [[uri absoluteString] substringFromIndex:[kCDVAssetsLibraryScheme length]+2]; } return nil; }
