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

Commit d86c6ac

Browse files
committed
Add option overwrite for #173
1 parent ea2b11a commit d86c6ac

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class RNFetchBlobConfig {
1616
public String key;
1717
public String mime;
1818
public Boolean auto;
19+
public Boolean overwrite = true;
1920
public long timeout = 60000;
2021
public Boolean increment = false;
2122
public ReadableArray binaryContentTypes = null;
@@ -32,6 +33,12 @@ public class RNFetchBlobConfig {
3233
}
3334
if(options.hasKey("binaryContentTypes"))
3435
this.binaryContentTypes = options.getArray("binaryContentTypes");
36+
if(this.path != null && path.toLowerCase().contains("?append=true")) {
37+
this.overwrite = false;
38+
}
39+
40+
if(options.hasKey("overwrite"))
41+
this.overwrite = options.getBoolean("overwrite");
3542
this.key = options.hasKey("key") ? options.getString("key") : null;
3643
this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
3744
this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ public Response intercept(Chain chain) throws IOException {
328328
RNFetchBlob.RCTContext,
329329
taskId,
330330
originalResponse.body(),
331-
destPath);
331+
destPath,
332+
options.overwrite);
332333
break;
333334
default:
334335
extended = new RNFetchBlobDefaultResp(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public class RNFetchBlobFileResp extends ResponseBody {
3434
ReactApplicationContext rctContext;
3535
FileOutputStream ofStream;
3636

37-
public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path) throws IOException {
37+
public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path, boolean overwrite) throws IOException {
3838
super();
3939
this.rctContext = ctx;
4040
this.mTaskId = taskId;
4141
this.originalBody = body;
4242
assert path != null;
4343
this.mPath = path;
4444
if (path != null) {
45-
boolean appendToExistingFile = path.contains("?append=true");
45+
boolean appendToExistingFile = !overwrite;
4646
path = path.replace("?append=true", "");
4747
mPath = path;
4848
File f = new File(path);

src/ios/RNFetchBlobNetwork.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
413413
{
414414
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
415415
}
416+
BOOL overwrite = [options valueForKey:@"overwrite"] == nil ? YES : [[options valueForKey:@"overwrite"] boolValue];
416417
BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
418+
419+
appendToExistingFile = !overwrite;
420+
417421
// For solving #141 append response data if the file already exists
418422
// base on PR#139 @kejinliang
419423
if(appendToExistingFile)
@@ -424,7 +428,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
424428
{
425429
[fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
426430
}
427-
writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];
431+
writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:appendToExistingFile];
428432
[writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
429433
[writeStream open];
430434
}

0 commit comments

Comments
 (0)