aboutsummaryrefslogtreecommitdiffstats
path: root/iPhone/CordovaLib/Classes/CDVFile.m
diff options
context:
space:
mode:
Diffstat (limited to 'iPhone/CordovaLib/Classes/CDVFile.m')
-rwxr-xr-xiPhone/CordovaLib/Classes/CDVFile.m318
1 files changed, 181 insertions, 137 deletions
diff --git a/iPhone/CordovaLib/Classes/CDVFile.m b/iPhone/CordovaLib/Classes/CDVFile.m
index d52405d..9487dd4 100755
--- a/iPhone/CordovaLib/Classes/CDVFile.m
+++ b/iPhone/CordovaLib/Classes/CDVFile.m
@@ -52,7 +52,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
self.appLibraryPath = [paths objectAtIndex:0];
- self.appTempPath = [NSTemporaryDirectory ()stringByStandardizingPath]; // remove trailing slash from NSTemporaryDirectory()
+ self.appTempPath = [NSTemporaryDirectory()stringByStandardizingPath]; // remove trailing slash from NSTemporaryDirectory()
self.persistentPath = [NSString stringWithFormat:@"/%@", [self.appDocsPath lastPathComponent]];
self.temporaryPath = [NSString stringWithFormat:@"/%@", [self.appTempPath lastPathComponent]];
@@ -164,7 +164,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:QUOTA_EXCEEDED_ERR];
} else {
NSMutableDictionary* fileSystem = [NSMutableDictionary dictionaryWithCapacity:2];
- [fileSystem setObject:(type == TEMPORARY ? kW3FileTemporary:kW3FilePersistent) forKey:@"name"];
+ [fileSystem setObject:(type == TEMPORARY ? kW3FileTemporary : kW3FilePersistent) forKey:@"name"];
NSDictionary* dirEntry = [self getDirectoryEntry:fullPath isDirectory:YES];
[fileSystem setObject:dirEntry forKey:@"root"];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:fileSystem];
@@ -227,10 +227,9 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
NSURL* testUri = [NSURL URLWithString:strUri];
CDVPluginResult* result = nil;
- if (!testUri || ![testUri isFileURL]) {
- // issue ENCODING_ERR
+ if (!testUri) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:ENCODING_ERR];
- } else {
+ } else if ([testUri isFileURL]) {
NSFileManager* fileMgr = [[NSFileManager alloc] init];
NSString* path = [testUri path];
// NSLog(@"url path: %@", path);
@@ -262,7 +261,13 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// return NOT_FOUND_ERR
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR];
}
+ } else if ([strUri hasPrefix:@"assets-library://"]) {
+ NSDictionary* fileSystem = [self getDirectoryEntry:strUri isDirectory:NO];
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:fileSystem];
+ } else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:ENCODING_ERR];
}
+
if (result != nil) {
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
@@ -486,7 +491,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// 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.
- ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
+ ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset) {
if (asset) {
// We have the asset! Retrieve the metadata and send it off.
NSDate* date = [asset valueForProperty:ALAssetPropertyDate];
@@ -499,7 +504,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
}
};
// TODO(maxw): Consider making this a class variable since it's the same every time.
- ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
+ ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError* error) {
// Retrieving the asset failed for some reason. Send the appropriate error.
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
@@ -525,7 +530,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
} else {
// didn't get fileAttribs
CDVFileError errorCode = ABORT_ERR;
- NSLog (@"error getting metadata: %@", [error localizedDescription]);
+ NSLog(@"error getting metadata: %@", [error localizedDescription]);
if ([error code] == NSFileNoSuchFileError) {
errorCode = NOT_FOUND_ERR;
}
@@ -779,7 +784,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// 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.
- ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
+ ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset) {
if (asset) {
// We have the asset! Get the data and try to copy it over.
if (![fileMgr fileExistsAtPath:destRootPath]) {
@@ -796,7 +801,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// We're good to go! Write the file to the new destination.
ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
- Byte* buffer = (Byte*)malloc ([assetRepresentation size]);
+ Byte* buffer = (Byte*)malloc([assetRepresentation size]);
NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil];
NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
[data writeToFile:newFullPath atomically:YES];
@@ -808,7 +813,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
};
- ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
+ ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError* error) {
// Retrieving the asset failed for some reason. Send the appropriate error.
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
@@ -953,7 +958,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// 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.
- ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
+ ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset) {
if (asset) {
// We have the asset! Populate the dictionary and send it off.
NSMutableDictionary* fileInfo = [NSMutableDictionary dictionaryWithCapacity:5];
@@ -975,7 +980,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
};
- ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
+ ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError* error) {
// Retrieving the asset failed for some reason. Send the appropriate error.
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
@@ -1053,154 +1058,181 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
+- (void)readFileWithPath:(NSString*)path start:(NSInteger)start end:(NSInteger)end callback:(void (^)(NSData*, NSString* mimeType, CDVFileError))callback
+{
+ if (path == nil) {
+ callback(nil, nil, SYNTAX_ERR);
+ } else {
+ [self.commandDelegate runInBackground:^ {
+ if ([path hasPrefix:kCDVAssetsLibraryPrefix]) {
+ // 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.
+ ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* asset) {
+ if (asset) {
+ // We have the asset! Get the data and send it off.
+ ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
+ Byte* buffer = (Byte*)malloc([assetRepresentation size]);
+ NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil];
+ NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
+ NSString* MIMEType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)[assetRepresentation UTI], kUTTagClassMIMEType);
+
+ callback(data, MIMEType, NO_ERROR);
+ } else {
+ callback(nil, nil, NOT_FOUND_ERR);
+ }
+ };
+ ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError* error) {
+ // Retrieving the asset failed for some reason. Send the appropriate error.
+ NSLog(@"Error: %@", error);
+ callback(nil, nil, SECURITY_ERR);
+ };
+
+ ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
+ [assetsLibrary assetForURL:[NSURL URLWithString:path] resultBlock:resultBlock failureBlock:failureBlock];
+ } else {
+ NSString* mimeType = [self getMimeTypeFromPath:path];
+ if (mimeType == nil) {
+ mimeType = @"*/*";
+ }
+ NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path];
+ if (start > 0) {
+ [file seekToFileOffset:start];
+ }
+
+ NSData* readData;
+ if (end < 0) {
+ readData = [file readDataToEndOfFile];
+ } else {
+ readData = [file readDataOfLength:(end - start)];
+ }
+
+ [file closeFile];
+
+ callback(readData, mimeType, readData != nil ? NO_ERROR : NOT_FOUND_ERR);
+ }
+ }];
+ }
+}
+
/* read and return file data
* IN:
* NSArray* arguments
* 0 - NSString* fullPath
- * 1 - NSString* encoding - NOT USED, iOS reads and writes using UTF8!
- * 2 - NSString* start - OPTIONAL, only provided when not == 0.
- * 3 - NSString* end - OPTIONAL, only provided when not == length.
+ * 1 - NSString* encoding
+ * 2 - NSString* start
+ * 3 - NSString* end
*/
- (void)readAsText:(CDVInvokedUrlCommand*)command
{
// arguments
- NSString* argPath = [command.arguments objectAtIndex:0];
- NSInteger start = 0;
- NSInteger end = -1;
-
- if ([command.arguments count] >= 3) {
- start = [[command.arguments objectAtIndex:2] integerValue];
- }
- if ([command.arguments count] >= 4) {
- end = [[command.arguments objectAtIndex:3] integerValue];
+ NSString* path = [command argumentAtIndex:0];
+ NSString* encoding = [command argumentAtIndex:1];
+ NSInteger start = [[command argumentAtIndex:2] integerValue];
+ NSInteger end = [[command argumentAtIndex:3] integerValue];
+
+ // TODO: implement
+ if (![@"UTF-8" isEqualToString : encoding]) {
+ NSLog(@"Only UTF-8 encodings are currently supported by readAsText");
+ CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:ENCODING_ERR];
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ return;
}
- // NSString* encoding = [command.arguments objectAtIndex:2]; // not currently used
- CDVPluginResult* result = nil;
-
- NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:argPath];
-
- if ([argPath hasPrefix:kCDVAssetsLibraryPrefix]) {
- // can't read assets-library URLs as text
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_READABLE_ERR];
- } else if (!file) {
- // invalid path entry
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR];
- } else {
- if (start > 0) {
- [file seekToFileOffset:start];
- }
-
- NSData* readData;
- if (end < 0) {
- readData = [file readDataToEndOfFile];
- } else {
- readData = [file readDataOfLength:(end - start)];
+ [self readFileWithPath:path start:start end:end callback:^(NSData* data, NSString* mimeType, CDVFileError errorCode) {
+ CDVPluginResult* result = nil;
+ if (data != nil) {
+ NSString* str = [[NSString alloc] initWithBytesNoCopy:(void*)[data bytes] length:[data length] encoding:NSUTF8StringEncoding freeWhenDone:NO];
+ // Check that UTF8 conversion did not fail.
+ if (str != nil) {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:str];
+ result.associatedObject = data;
+ } else {
+ errorCode = ENCODING_ERR;
+ }
}
-
- [file closeFile];
- NSString* pNStrBuff = nil;
- if (readData) {
- pNStrBuff = [[NSString alloc] initWithBytes:[readData bytes] length:[readData length] encoding:NSUTF8StringEncoding];
- } else {
- // return empty string if no data
- pNStrBuff = @"";
+ if (result == nil) {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:errorCode];
}
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:pNStrBuff];
- }
- [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ }];
}
/* Read content of text file and return as base64 encoded data url.
* IN:
* NSArray* arguments
* 0 - NSString* fullPath
+ * 1 - NSString* start
+ * 2 - NSString* end
*
* Determines the mime type from the file extension, returns ENCODING_ERR if mimetype can not be determined.
*/
- (void)readAsDataURL:(CDVInvokedUrlCommand*)command
{
- // arguments
- NSString* argPath = [command.arguments objectAtIndex:0];
- NSInteger start = 0;
- NSInteger end = -1;
-
- if ([command.arguments count] >= 2) {
- start = [[command.arguments objectAtIndex:1] integerValue];
- }
- if ([command.arguments count] >= 3) {
- end = [[command.arguments objectAtIndex:2] integerValue];
- }
+ NSString* path = [command argumentAtIndex:0];
+ NSInteger start = [[command argumentAtIndex:1] integerValue];
+ NSInteger end = [[command argumentAtIndex:2] integerValue];
+
+ [self readFileWithPath:path start:start end:end callback:^(NSData* data, NSString* mimeType, CDVFileError errorCode) {
+ CDVPluginResult* result = nil;
+ if (data != nil) {
+ // TODO: Would be faster to base64 encode directly to the final string.
+ NSString* output = [NSString stringWithFormat:@"data:%@;base64,%@", mimeType, [data base64EncodedString]];
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:output];
+ } else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:errorCode];
+ }
- CDVFileError errCode = ABORT_ERR;
- __block CDVPluginResult* result = nil;
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ }];
+}
- if (!argPath) {
- errCode = SYNTAX_ERR;
- } else if ([argPath hasPrefix:kCDVAssetsLibraryPrefix]) {
- // 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.
- ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
- if (asset) {
- // We have the asset! Get the data and send it off.
- ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
- Byte* buffer = (Byte*)malloc ([assetRepresentation size]);
- NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil];
- NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
- NSString* mimeType = [self getMimeTypeFromPath:[assetRepresentation filename]];
- NSString* dataString = [NSString stringWithFormat:@"data:%@;base64,%@", mimeType, [data base64EncodedString]];
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:dataString];
- [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
- } else {
- // We couldn't find the asset. Send the appropriate error.
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:NOT_FOUND_ERR];
- [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
- }
- };
- ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
- // Retrieving the asset failed for some reason. Send the appropriate error.
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[error localizedDescription]];
- [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
- };
+/* Read content of text file and return as an arraybuffer
+ * IN:
+ * NSArray* arguments
+ * 0 - NSString* fullPath
+ * 1 - NSString* start
+ * 2 - NSString* end
+ */
- ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
- [assetsLibrary assetForURL:[NSURL URLWithString:argPath] resultBlock:resultBlock failureBlock:failureBlock];
- return;
- } else {
- NSString* mimeType = [self getMimeTypeFromPath:argPath];
- if (!mimeType) {
- // can't return as data URL if can't figure out the mimeType
- errCode = ENCODING_ERR;
+- (void)readAsArrayBuffer:(CDVInvokedUrlCommand*)command
+{
+ NSString* path = [command argumentAtIndex:0];
+ NSInteger start = [[command argumentAtIndex:1] integerValue];
+ NSInteger end = [[command argumentAtIndex:2] integerValue];
+
+ [self readFileWithPath:path start:start end:end callback:^(NSData* data, NSString* mimeType, CDVFileError errorCode) {
+ CDVPluginResult* result = nil;
+ if (data != nil) {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:data];
} else {
- NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:argPath];
- if (start > 0) {
- [file seekToFileOffset:start];
- }
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:errorCode];
+ }
- NSData* readData;
- if (end < 0) {
- readData = [file readDataToEndOfFile];
- } else {
- readData = [file readDataOfLength:(end - start)];
- }
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ }];
+}
- [file closeFile];
- if (readData) {
- NSString* output = [NSString stringWithFormat:@"data:%@;base64,%@", mimeType, [readData base64EncodedString]];
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:output];
- } else {
- errCode = NOT_FOUND_ERR;
- }
+- (void)readAsBinaryString:(CDVInvokedUrlCommand*)command
+{
+ NSString* path = [command argumentAtIndex:0];
+ NSInteger start = [[command argumentAtIndex:1] integerValue];
+ NSInteger end = [[command argumentAtIndex:2] integerValue];
+
+ [self readFileWithPath:path start:start end:end callback:^(NSData* data, NSString* mimeType, CDVFileError errorCode) {
+ CDVPluginResult* result = nil;
+ if (data != nil) {
+ NSString* payload = [[NSString alloc] initWithBytesNoCopy:(void*)[data bytes] length:[data length] encoding:NSASCIIStringEncoding freeWhenDone:NO];
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
+ result.associatedObject = data;
+ } else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsInt:errorCode];
}
- }
- if (!result) {
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:errCode];
- }
- // NSLog(@"readAsDataURL return: %@", jsString);
- [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+
+ [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
+ }];
}
/* helper function to get the mimeType from the file extension
@@ -1219,7 +1251,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
mimeType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass(typeId, kUTTagClassMIMEType);
if (!mimeType) {
// special case for m4a
- if ([(__bridge NSString*) typeId rangeOfString:@"m4a-audio"].location != NSNotFound) {
+ if ([(__bridge NSString*)typeId rangeOfString : @"m4a-audio"].location != NSNotFound) {
mimeType = @"audio/mp4";
} else if ([[fullPath pathExtension] rangeOfString:@"wav"].location != NSNotFound) {
mimeType = @"audio/wav";
@@ -1271,7 +1303,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
* IN:
* NSArray* arguments
* 0 - NSString* file path to write to
- * 1 - NSString* data to write
+ * 1 - NSString* or NSData* data to write
* 2 - NSNumber* position to begin writing
*/
- (void)write:(CDVInvokedUrlCommand*)command
@@ -1281,7 +1313,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
// arguments
NSString* argPath = [arguments objectAtIndex:0];
- NSString* argData = [arguments objectAtIndex:1];
+ id argData = [arguments objectAtIndex:1];
unsigned long long pos = (unsigned long long)[[arguments objectAtIndex:2] longLongValue];
// text can't be written into assets-library files
@@ -1295,15 +1327,22 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
[self truncateFile:fullPath atPosition:pos];
- [self writeToFile:fullPath withData:argData append:YES callback:callbackId];
+ if ([argData isKindOfClass:[NSString class]]) {
+ [self writeToFile:fullPath withString:argData encoding:NSUTF8StringEncoding append:YES callback:callbackId];
+ } else if ([argData isKindOfClass:[NSData class]]) {
+ [self writeToFile:fullPath withData:argData append:YES callback:callbackId];
+ } else {
+ CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid parameter type"];
+ [self.commandDelegate sendPluginResult:result callbackId:callbackId];
+ }
+
}
-- (void)writeToFile:(NSString*)filePath withData:(NSString*)data append:(BOOL)shouldAppend callback:(NSString*)callbackId
+- (void)writeToFile:(NSString*)filePath withData:(NSData*)encData append:(BOOL)shouldAppend callback:(NSString*)callbackId
{
CDVPluginResult* result = nil;
CDVFileError errCode = INVALID_MODIFICATION_ERR;
int bytesWritten = 0;
- NSData* encData = [data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
if (filePath) {
NSOutputStream* fileStream = [NSOutputStream outputStreamToFileAtPath:filePath append:shouldAppend];
@@ -1333,6 +1372,11 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
+- (void)writeToFile:(NSString*)filePath withString:(NSString*)stringData encoding:(NSStringEncoding)encoding append:(BOOL)shouldAppend callback:(NSString*)callbackId
+{
+ [self writeToFile:filePath withData:[stringData dataUsingEncoding:encoding allowLossyConversion:YES] append:shouldAppend callback:callbackId];
+}
+
- (void)testFileExists:(CDVInvokedUrlCommand*)command
{
// arguments
@@ -1343,7 +1387,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
NSString* appFile = argPath; // [ self getFullPath: argPath];
BOOL bExists = [fMgr fileExistsAtPath:appFile];
- CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(bExists ? 1:0)];
+ CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(bExists ? 1 : 0)];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
@@ -1359,7 +1403,7 @@ NSString* const kCDVAssetsLibraryPrefix = @"assets-library://";
BOOL bIsDir = NO;
BOOL bExists = [fMgr fileExistsAtPath:appFile isDirectory:&bIsDir];
- CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:((bExists && bIsDir) ? 1:0)];
+ CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:((bExists && bIsDir) ? 1 : 0)];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}