Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 2a80b5b

Browse files
committed
#27 add append option to writeFile API
1 parent 768b06f commit 2a80b5b

File tree

7 files changed

+85
-34
lines changed

7 files changed

+85
-34
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ public void readFile(String path, String encoding, Promise promise) {
9999
}
100100

101101
@ReactMethod
102-
public void writeFileArray(String path, ReadableArray data, Promise promise) {
103-
RNFetchBlobFS.writeFile(path, data, promise);
102+
public void writeFileArray(String path, ReadableArray data, boolean append, Promise promise) {
103+
RNFetchBlobFS.writeFile(path, data, append, promise);
104104
}
105105

106106
@ReactMethod
107-
public void writeFile(String path, String encoding, String data, Promise promise) {
108-
RNFetchBlobFS.writeFile(path, encoding, data, promise);
107+
public void writeFile(String path, String encoding, String data, boolean append, Promise promise) {
108+
RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
109109
}
110110

111111
@ReactMethod

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class RNFetchBlobFS {
5959
* @param data Array passed from JS context.
6060
* @param promise
6161
*/
62-
static public void writeFile(String path, String encoding, String data, final Promise promise) {
62+
static public void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
6363
AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
6464
@Override
6565
protected Integer doInBackground(String... args) {
@@ -71,7 +71,7 @@ protected Integer doInBackground(String... args) {
7171
File dir = f.getParentFile();
7272
if(!dir.exists())
7373
dir.mkdirs();
74-
FileOutputStream fout = new FileOutputStream(f);
74+
FileOutputStream fout = new FileOutputStream(f, append);
7575
fout.write(stringToBytes(data, encoding));
7676
fout.close();
7777
promise.resolve(Arguments.createArray());
@@ -90,7 +90,7 @@ protected Integer doInBackground(String... args) {
9090
* @param data Array passed from JS context.
9191
* @param promise
9292
*/
93-
static public void writeFile(String path, ReadableArray data, final Promise promise) {
93+
static public void writeFile(String path, ReadableArray data, final boolean append, final Promise promise) {
9494
AsyncTask<Object, Void, Void> task = new AsyncTask<Object, Void, Void>() {
9595
@Override
9696
protected Void doInBackground(Object... args) {
@@ -101,7 +101,7 @@ protected Void doInBackground(Object... args) {
101101
File dir = f.getParentFile();
102102
if(!dir.exists())
103103
dir.mkdirs();
104-
FileOutputStream os = new FileOutputStream(f);
104+
FileOutputStream os = new FileOutputStream(f, append);
105105
byte [] bytes = new byte[data.size()];
106106
for(int i=0;i<data.size();i++) {
107107
bytes[i] = (byte) data.getInt(i);

src/fs.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,44 @@ function readFile(path:string, encoding:string, bufferSize:number):Promise<any>
139139
return RNFetchBlob.readFile(path, encoding)
140140
}
141141

142-
function writeFile(path:string, encoding:string, data:string | Array<number>):Promise {
142+
/**
143+
* Write data to file.
144+
* @param {string} path Path of the file.
145+
* @param {string | number[]} data Data to write to the file.
146+
* @param {string} encoding Encoding of data (Optional).
147+
* @return {Promise}
148+
*/
149+
function writeFile(path:string, data:string | Array<number>, encoding:?string):Promise {
150+
encoding = encoding || 'utf8'
151+
if(typeof path !== 'string')
152+
return Promise.reject('Invalid argument "path" ')
153+
if(encoding.toLocaleLowerCase() === 'ascii') {
154+
if(!Array.isArray(data))
155+
Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
156+
else
157+
return RNFetchBlob.writeFileArray(path, data, false);
158+
} else {
159+
if(typeof data !== 'string')
160+
Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
161+
else
162+
return RNFetchBlob.writeFile(path, encoding, data, false);
163+
}
164+
}
165+
166+
function appendFile(path:string, data:string | Array<number>, encoding:?string):Promise {
167+
encoding = encoding || 'utf8'
143168
if(typeof path !== 'string')
144169
return Promise.reject('Invalid argument "path" ')
145170
if(encoding.toLocaleLowerCase() === 'ascii') {
146171
if(!Array.isArray(data))
147172
Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
148173
else
149-
return RNFetchBlob.writeFileArray(path, data);
174+
return RNFetchBlob.writeFileArray(path, data, true);
150175
} else {
151176
if(typeof data !== 'string')
152177
Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
153178
else
154-
return RNFetchBlob.writeFile(path, encoding, data);
179+
return RNFetchBlob.writeFile(path, encoding, data, true);
155180
}
156181
}
157182

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,12 @@ - (NSDictionary *)constantsToExport
415415
[RNFetchBlobFS readFile:path encoding:encoding resolver:resolve rejecter:reject];
416416
})
417417

418-
RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
419-
[RNFetchBlobFS writeFile:path encoding:encoding data:data resolver:resolve rejecter:reject];
418+
RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
419+
[RNFetchBlobFS writeFile:path encoding:encoding data:data append:append resolver:resolve rejecter:reject];
420420
})
421421

422-
RCT_EXPORT_METHOD(writeFileArray:(NSString *)path data:(NSArray *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
423-
[RNFetchBlobFS writeFileArray:path data:data resolver:resolve rejecter:reject];
422+
RCT_EXPORT_METHOD(writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
423+
[RNFetchBlobFS writeFileArray:path data:data append:append resolver:resolve rejecter:reject];
424424
})
425425

426426
RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback) {

src/ios/RNFetchBlobFS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
+ (BOOL) mkdir:(NSString *) path;
4949
+ (NSDictionary *) stat:(NSString *) path error:(NSError **) error;
5050
+ (BOOL) exists:(NSString *) path;
51-
+ (void) writeFileArray:(NSString *)path data:(NSArray *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
52-
+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
51+
+ (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
52+
+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
5353
+ (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
5454

5555
// constructor

src/ios/RNFetchBlobFS.m

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,17 @@ + (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
8484
return tempPath;
8585
}
8686

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 {
8888
@try {
8989
NSFileManager * fm = [NSFileManager defaultManager];
9090
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
9193
NSString * folder = [path stringByDeletingLastPathComponent];
9294
if(![fm fileExistsAtPath:folder]) {
9395
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
9496
}
97+
// if file exists, write file by encoding and strategy
9598
if(![fm fileExistsAtPath:path]) {
9699
if([[encoding lowercaseString] isEqualToString:@"base64"]){
97100
NSData * byteData = [[NSData alloc] initWithBase64EncodedString:data options:0];
@@ -102,14 +105,21 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
102105
}
103106
else {
104107
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
105-
[fileHandle seekToEndOfFile];
108+
NSData * content = nil;
106109
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];
109122
}
110-
else
111-
[fileHandle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]];
112-
[fileHandle closeFile];
113123
}
114124
fm = nil;
115125
resolve([NSNull null]);
@@ -120,22 +130,38 @@ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString
120130
}
121131
}
122132

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 {
124134
@try {
125135
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+
}
126143
NSMutableData * fileContent = [NSMutableData alloc];
127144
// prevent stack overflow, alloc on heap
128145
char * bytes = (char*) malloc([data count]);
129146
for(int i = 0; i < data.count; i++) {
130147
bytes[i] = [[data objectAtIndex:i] charValue];
131148
}
132-
// if append == NO
133-
// BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
134149
[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+
}
139165
free(bytes);
140166
fm = nil;
141167
resolve([NSNull null]);

test/test-0.6.0.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ let prefix = ((Platform.OS === 'android') ? 'file://' : '')
2828
describe('writeFile test', (report, done) => {
2929
let path = dirs.DocumentDir + '/0.6.0-'+Date.now()+'/writeFileTest'+Date.now()
3030
let data = 'hellofrom'+Date.now()
31-
fs.writeFile(path, 'utf8', data)
31+
fs.writeFile(path, data)
3232
.then(() => fs.readFile(path, 'utf8'))
3333
.then((actual) => {
3434
report(<Assert key="utf8 content should correct" expect={data} actual={actual}/>)
3535
data += 'base64'
36-
return fs.writeFile(path, 'base64', RNFetchBlob.base64.encode('base64'))
36+
return fs.writeFile(path, RNFetchBlob.base64.encode('base64'), 'base64')
3737
})
3838
.then(() => fs.readFile(path, 'base64'))
3939
.then((actual) => {
4040
report(<Assert key="base64 content should correct"
4141
expect={RNFetchBlob.base64.decode(RNFetchBlob.base64.encode(data))}
4242
actual={RNFetchBlob.base64.decode(actual)}/>)
4343
data += 'ascii'
44-
return fs.writeFile(path, 'ascii', getASCIIArray('ascii'));
44+
return fs.writeFile(path, getASCIIArray('ascii'), 'ascii');
4545
})
4646
.then(() => fs.readFile(path, 'ascii'))
4747
.then((actual) => {

0 commit comments

Comments
 (0)