Skip to content

Commit

Permalink
Added basic support for writing contents to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed May 8, 2015
1 parent de49e3f commit afdb160
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
30 changes: 18 additions & 12 deletions FS.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var base64 = require('base-64');
var _readDir = Promise.promisify(RNFSManager.readDir);
var _stat = Promise.promisify(RNFSManager.stat);
var _readFile = Promise.promisify(RNFSManager.readFile);
var _writeFile = Promise.promisify(RNFSManager.writeFile);

var convertError = (err) => {
var error = new Error(err.description);
Expand All @@ -21,22 +22,22 @@ var RNFS = {

readDir(path, rootDir) {
return _readDir(path, rootDir)
.catch(convertError);
.catch(convertError);
},

stat(filepath) {
return _stat(filepath)
.then((result) => {
return {
'ctime': new Date(result.ctime*1000),
'mtime': new Date(result.mtime*1000),
'size': result.size,
'mode': result.mode,
isFile: () => result.type === NSFileTypeRegular,
isDirectory: () => result.type === NSFileTypeDirectory,
};
})
.catch(convertError);
.then((result) => {
return {
'ctime': new Date(result.ctime*1000),
'mtime': new Date(result.mtime*1000),
'size': result.size,
'mode': result.mode,
isFile: () => result.type === NSFileTypeRegular,
isDirectory: () => result.type === NSFileTypeDirectory,
};
})
.catch(convertError);
},

readFile(filepath, shouldDecode) {
Expand All @@ -51,6 +52,11 @@ var RNFS = {
return p.catch(convertError);
},

writeFile(filepath, contents, options) {
return _writeFile(filepath, base64.encode(contents), options)
.catch(convertError);
},

MainBundle: RNFSManager.MainBundleDirectory,
CachesDirectory: RNFSManager.NSCachesDirectory,
DocumentDirectory: RNFSManager.NSDocumentDirectory,
Expand Down
41 changes: 26 additions & 15 deletions RNFSManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,65 @@ @implementation RNFSManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(folderInt, NSUserDomainMask, YES);
path = [paths objectAtIndex:0];
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString * dirPath = [path stringByAppendingPathComponent:directory];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error];

contents = [contents mapObjectsUsingBlock:^id(id obj, NSUInteger idx) {
return @{
@"name": (NSString*)obj,
@"path": [dirPath stringByAppendingPathComponent:(NSString*)obj]
};
}];

if (error) {
return callback([self makeErrorPayload:error]);
}

callback(@[[NSNull null], contents]);
}

RCT_EXPORT_METHOD(stat:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
NSError *error;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
// NSLog(@"%@", attributes);


if (error) {
return callback([self makeErrorPayload:error]);
}

attributes = @{
@"ctime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileCreationDate]],
@"mtime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileModificationDate]],
@"size": [attributes objectForKey:NSFileSize],
@"type": [attributes objectForKey:NSFileType],
@"mode": [NSNumber numberWithInteger:[[NSString stringWithFormat:@"%o", [(NSNumber*)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]]
};
@"ctime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileCreationDate]],
@"mtime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileModificationDate]],
@"size": [attributes objectForKey:NSFileSize],
@"type": [attributes objectForKey:NSFileType],
@"mode": [NSNumber numberWithInteger:[[NSString stringWithFormat:@"%o", [(NSNumber*)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]]
};

callback(@[[NSNull null], attributes]);
}

RCT_EXPORT_METHOD(writeFile:(NSString*)filepath contents:(NSString*)base64Content attributes:(NSDictionary*)attributes callback:(RCTResponseSenderBlock)callback){

NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];

if (!success) {
return callback(@[[NSString stringWithFormat:@"Could not write file at path %@", filepath]]);
}

callback(@[[NSNull null], [NSNumber numberWithBool:success]]);
}

RCT_EXPORT_METHOD(readFile:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath];
NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSLog(@"%@", base64Content);
if (!base64Content) {
return callback(@[[NSString stringWithFormat:@"Could not read file at path %@", filepath]]);
}

callback(@[[NSNull null], base64Content]);
}

Expand Down

0 comments on commit afdb160

Please sign in to comment.