@@ -29,54 +29,65 @@ @implementation RNFSManager
29
29
NSArray *paths = NSSearchPathForDirectoriesInDomains (folderInt, NSUserDomainMask, YES );
30
30
path = [paths objectAtIndex: 0 ];
31
31
}
32
-
32
+
33
33
NSFileManager *fileManager = [NSFileManager defaultManager ];
34
34
NSError *error;
35
35
NSString * dirPath = [path stringByAppendingPathComponent: directory];
36
36
NSArray *contents = [fileManager contentsOfDirectoryAtPath: dirPath error: &error];
37
-
37
+
38
38
contents = [contents mapObjectsUsingBlock: ^id (id obj, NSUInteger idx) {
39
39
return @{
40
40
@" name" : (NSString *)obj,
41
41
@" path" : [dirPath stringByAppendingPathComponent: (NSString *)obj]
42
42
};
43
43
}];
44
-
44
+
45
45
if (error) {
46
46
return callback ([self makeErrorPayload: error]);
47
47
}
48
-
48
+
49
49
callback (@[[NSNull null ], contents]);
50
50
}
51
51
52
52
RCT_EXPORT_METHOD (stat:(NSString *)filepath callback:(RCTResponseSenderBlock)callback){
53
53
NSError *error;
54
54
NSDictionary *attributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: filepath error: &error];
55
- // NSLog(@"%@", attributes);
56
-
55
+
57
56
if (error) {
58
57
return callback ([self makeErrorPayload: error]);
59
58
}
60
-
59
+
61
60
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
+
69
68
callback (@[[NSNull null ], attributes]);
70
69
}
71
70
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
+
72
83
RCT_EXPORT_METHOD (readFile:(NSString *)filepath callback:(RCTResponseSenderBlock)callback){
73
84
NSData *content = [[NSFileManager defaultManager ] contentsAtPath: filepath];
74
85
NSString *base64Content = [content base64EncodedStringWithOptions: NSDataBase64EncodingEndLineWithLineFeed ];
75
86
NSLog (@" %@ " , base64Content);
76
87
if (!base64Content) {
77
88
return callback (@[[NSString stringWithFormat: @" Could not read file at path %@ " , filepath]]);
78
89
}
79
-
90
+
80
91
callback (@[[NSNull null ], base64Content]);
81
92
}
82
93
0 commit comments