|
@@ -84,14 +84,17 @@ NSMutableDictionary *fileStreams = nil;
|
84
|
84
|
return tempPath;
|
85
|
85
|
}
|
86
|
86
|
|
87
|
|
-+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
|
87
|
++ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
88
|
88
|
@try {
|
89
|
89
|
NSFileManager * fm = [NSFileManager defaultManager];
|
90
|
90
|
NSError * err = nil;
|
|
91
|
+ // check if the folder exists, if not exists, create folders recursively
|
|
92
|
+ // after the folders created, write data into the file
|
91
|
93
|
NSString * folder = [path stringByDeletingLastPathComponent];
|
92
|
94
|
if(![fm fileExistsAtPath:folder]) {
|
93
|
95
|
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
|
94
|
96
|
}
|
|
97
|
+ // if file exists, write file by encoding and strategy
|
95
|
98
|
if(![fm fileExistsAtPath:path]) {
|
96
|
99
|
if([[encoding lowercaseString] isEqualToString:@"base64"]){
|
97
|
100
|
NSData * byteData = [[NSData alloc] initWithBase64EncodedString:data options:0];
|
|
@@ -102,14 +105,21 @@ NSMutableDictionary *fileStreams = nil;
|
102
|
105
|
}
|
103
|
106
|
else {
|
104
|
107
|
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
|
105
|
|
- [fileHandle seekToEndOfFile];
|
|
108
|
+ NSData * content = nil;
|
106
|
109
|
if([[encoding lowercaseString] isEqualToString:@"base64"]) {
|
107
|
|
- NSData * byteData = [[NSData alloc] initWithBase64EncodedString:data options:0];
|
108
|
|
- [fileHandle writeData:byteData];
|
|
110
|
+ content = [[NSData alloc] initWithBase64EncodedString:data options:0];
|
|
111
|
+ }
|
|
112
|
+ else {
|
|
113
|
+ content = [data dataUsingEncoding:NSUTF8StringEncoding];
|
|
114
|
+ }
|
|
115
|
+ if(append == YES) {
|
|
116
|
+ [fileHandle seekToEndOfFile];
|
|
117
|
+ [fileHandle writeData:content];
|
|
118
|
+ [fileHandle closeFile];
|
|
119
|
+ }
|
|
120
|
+ else {
|
|
121
|
+ [content writeToFile:path atomically:YES];
|
109
|
122
|
}
|
110
|
|
- else
|
111
|
|
- [fileHandle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]];
|
112
|
|
- [fileHandle closeFile];
|
113
|
123
|
}
|
114
|
124
|
fm = nil;
|
115
|
125
|
resolve([NSNull null]);
|
|
@@ -120,22 +130,38 @@ NSMutableDictionary *fileStreams = nil;
|
120
|
130
|
}
|
121
|
131
|
}
|
122
|
132
|
|
123
|
|
-+ (void) writeFileArray:(NSString *)path data:(NSArray *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
|
133
|
++ (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
124
|
134
|
@try {
|
125
|
135
|
NSFileManager * fm = [NSFileManager defaultManager];
|
|
136
|
+ NSError * err = nil;
|
|
137
|
+ // check if the folder exists, if not exists, create folders recursively
|
|
138
|
+ // after the folders created, write data into the file
|
|
139
|
+ NSString * folder = [path stringByDeletingLastPathComponent];
|
|
140
|
+ if(![fm fileExistsAtPath:folder]) {
|
|
141
|
+ [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
|
|
142
|
+ }
|
126
|
143
|
NSMutableData * fileContent = [NSMutableData alloc];
|
127
|
144
|
// prevent stack overflow, alloc on heap
|
128
|
145
|
char * bytes = (char*) malloc([data count]);
|
129
|
146
|
for(int i = 0; i < data.count; i++) {
|
130
|
147
|
bytes[i] = [[data objectAtIndex:i] charValue];
|
131
|
148
|
}
|
132
|
|
- // if append == NO
|
133
|
|
-// BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
|
134
|
149
|
[fileContent appendBytes:bytes length:data.count];
|
135
|
|
- NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
|
136
|
|
- [fileHandle seekToEndOfFile];
|
137
|
|
- [fileHandle writeData:fileContent];
|
138
|
|
- [fileHandle closeFile];
|
|
150
|
+ if(![fm fileExistsAtPath:path]) {
|
|
151
|
+ [fm createFileAtPath:path contents:fileContent attributes:NULL];
|
|
152
|
+ }
|
|
153
|
+ // if file exists, write file
|
|
154
|
+ else {
|
|
155
|
+ if(append == YES) {
|
|
156
|
+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
|
|
157
|
+ [fileHandle seekToEndOfFile];
|
|
158
|
+ [fileHandle writeData:fileContent];
|
|
159
|
+ [fileHandle closeFile];
|
|
160
|
+ }
|
|
161
|
+ else {
|
|
162
|
+ [fileContent writeToFile:path atomically:YES];
|
|
163
|
+ }
|
|
164
|
+ }
|
139
|
165
|
free(bytes);
|
140
|
166
|
fm = nil;
|
141
|
167
|
resolve([NSNull null]);
|