Skip to content

Commit afdb160

Browse files
committed
Added basic support for writing contents to a file
1 parent de49e3f commit afdb160

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

FS.ios.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var base64 = require('base-64');
77
var _readDir = Promise.promisify(RNFSManager.readDir);
88
var _stat = Promise.promisify(RNFSManager.stat);
99
var _readFile = Promise.promisify(RNFSManager.readFile);
10+
var _writeFile = Promise.promisify(RNFSManager.writeFile);
1011

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

2223
readDir(path, rootDir) {
2324
return _readDir(path, rootDir)
24-
.catch(convertError);
25+
.catch(convertError);
2526
},
2627

2728
stat(filepath) {
2829
return _stat(filepath)
29-
.then((result) => {
30-
return {
31-
'ctime': new Date(result.ctime*1000),
32-
'mtime': new Date(result.mtime*1000),
33-
'size': result.size,
34-
'mode': result.mode,
35-
isFile: () => result.type === NSFileTypeRegular,
36-
isDirectory: () => result.type === NSFileTypeDirectory,
37-
};
38-
})
39-
.catch(convertError);
30+
.then((result) => {
31+
return {
32+
'ctime': new Date(result.ctime*1000),
33+
'mtime': new Date(result.mtime*1000),
34+
'size': result.size,
35+
'mode': result.mode,
36+
isFile: () => result.type === NSFileTypeRegular,
37+
isDirectory: () => result.type === NSFileTypeDirectory,
38+
};
39+
})
40+
.catch(convertError);
4041
},
4142

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

55+
writeFile(filepath, contents, options) {
56+
return _writeFile(filepath, base64.encode(contents), options)
57+
.catch(convertError);
58+
},
59+
5460
MainBundle: RNFSManager.MainBundleDirectory,
5561
CachesDirectory: RNFSManager.NSCachesDirectory,
5662
DocumentDirectory: RNFSManager.NSDocumentDirectory,

RNFSManager.m

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,54 +29,65 @@ @implementation RNFSManager
2929
NSArray *paths = NSSearchPathForDirectoriesInDomains(folderInt, NSUserDomainMask, YES);
3030
path = [paths objectAtIndex:0];
3131
}
32-
32+
3333
NSFileManager *fileManager = [NSFileManager defaultManager];
3434
NSError *error;
3535
NSString * dirPath = [path stringByAppendingPathComponent:directory];
3636
NSArray *contents = [fileManager contentsOfDirectoryAtPath:dirPath error:&error];
37-
37+
3838
contents = [contents mapObjectsUsingBlock:^id(id obj, NSUInteger idx) {
3939
return @{
4040
@"name": (NSString*)obj,
4141
@"path": [dirPath stringByAppendingPathComponent:(NSString*)obj]
4242
};
4343
}];
44-
44+
4545
if (error) {
4646
return callback([self makeErrorPayload:error]);
4747
}
48-
48+
4949
callback(@[[NSNull null], contents]);
5050
}
5151

5252
RCT_EXPORT_METHOD(stat:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
5353
NSError *error;
5454
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
55-
// NSLog(@"%@", attributes);
56-
55+
5756
if (error) {
5857
return callback([self makeErrorPayload:error]);
5958
}
60-
59+
6160
attributes = @{
62-
@"ctime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileCreationDate]],
63-
@"mtime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileModificationDate]],
64-
@"size": [attributes objectForKey:NSFileSize],
65-
@"type": [attributes objectForKey:NSFileType],
66-
@"mode": [NSNumber numberWithInteger:[[NSString stringWithFormat:@"%o", [(NSNumber*)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]]
67-
};
68-
61+
@"ctime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileCreationDate]],
62+
@"mtime": [self dateToTimeIntervalNumber:(NSDate*)[attributes objectForKey:NSFileModificationDate]],
63+
@"size": [attributes objectForKey:NSFileSize],
64+
@"type": [attributes objectForKey:NSFileType],
65+
@"mode": [NSNumber numberWithInteger:[[NSString stringWithFormat:@"%o", [(NSNumber*)[attributes objectForKey:NSFilePosixPermissions] integerValue]] integerValue]]
66+
};
67+
6968
callback(@[[NSNull null], attributes]);
7069
}
7170

71+
RCT_EXPORT_METHOD(writeFile:(NSString*)filepath contents:(NSString*)base64Content attributes:(NSDictionary*)attributes callback:(RCTResponseSenderBlock)callback){
72+
73+
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];
74+
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
75+
76+
if (!success) {
77+
return callback(@[[NSString stringWithFormat:@"Could not write file at path %@", filepath]]);
78+
}
79+
80+
callback(@[[NSNull null], [NSNumber numberWithBool:success]]);
81+
}
82+
7283
RCT_EXPORT_METHOD(readFile:(NSString*)filepath callback:(RCTResponseSenderBlock)callback){
7384
NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath];
7485
NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
7586
NSLog(@"%@", base64Content);
7687
if (!base64Content) {
7788
return callback(@[[NSString stringWithFormat:@"Could not read file at path %@", filepath]]);
7889
}
79-
90+
8091
callback(@[[NSNull null], base64Content]);
8192
}
8293

0 commit comments

Comments
 (0)