Browse Source

Add option `overwrite` for #173

Ben Hsieh 8 years ago
parent
commit
d86c6aca65

+ 7
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java View File

@@ -16,6 +16,7 @@ public class RNFetchBlobConfig {
16 16
     public String key;
17 17
     public String mime;
18 18
     public Boolean auto;
19
+    public Boolean overwrite = true;
19 20
     public long timeout = 60000;
20 21
     public Boolean increment = false;
21 22
     public ReadableArray binaryContentTypes = null;
@@ -32,6 +33,12 @@ public class RNFetchBlobConfig {
32 33
         }
33 34
         if(options.hasKey("binaryContentTypes"))
34 35
             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");
35 42
         this.key = options.hasKey("key") ? options.getString("key") : null;
36 43
         this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
37 44
         this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -328,7 +328,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
328 328
                                         RNFetchBlob.RCTContext,
329 329
                                         taskId,
330 330
                                         originalResponse.body(),
331
-                                        destPath);
331
+                                        destPath,
332
+                                        options.overwrite);
332 333
                                 break;
333 334
                             default:
334 335
                                 extended = new RNFetchBlobDefaultResp(

+ 2
- 2
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

@@ -34,7 +34,7 @@ public class RNFetchBlobFileResp extends ResponseBody {
34 34
     ReactApplicationContext rctContext;
35 35
     FileOutputStream ofStream;
36 36
 
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 {
38 38
         super();
39 39
         this.rctContext = ctx;
40 40
         this.mTaskId = taskId;
@@ -42,7 +42,7 @@ public class RNFetchBlobFileResp extends ResponseBody {
42 42
         assert path != null;
43 43
         this.mPath = path;
44 44
         if (path != null) {
45
-            boolean appendToExistingFile = path.contains("?append=true");
45
+            boolean appendToExistingFile = !overwrite;
46 46
             path = path.replace("?append=true", "");
47 47
             mPath = path;
48 48
             File f = new File(path);

+ 5
- 1
src/ios/RNFetchBlobNetwork.m View File

@@ -413,7 +413,11 @@ NSOperationQueue *taskQueue;
413 413
             {
414 414
                 [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
415 415
             }
416
+            BOOL overwrite = [options valueForKey:@"overwrite"] == nil ? YES : [[options valueForKey:@"overwrite"] boolValue];
416 417
             BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
418
+            
419
+            appendToExistingFile = !overwrite;
420
+
417 421
             // For solving #141 append response data if the file already exists
418 422
             // base on PR#139 @kejinliang
419 423
             if(appendToExistingFile)
@@ -424,7 +428,7 @@ NSOperationQueue *taskQueue;
424 428
             {
425 429
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
426 430
             }
427
-            writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];
431
+            writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:appendToExistingFile];
428 432
             [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
429 433
             [writeStream open];
430 434
         }