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

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

328
                                         RNFetchBlob.RCTContext,
328
                                         RNFetchBlob.RCTContext,
329
                                         taskId,
329
                                         taskId,
330
                                         originalResponse.body(),
330
                                         originalResponse.body(),
331
-                                        destPath);
331
+                                        destPath,
332
+                                        options.overwrite);
332
                                 break;
333
                                 break;
333
                             default:
334
                             default:
334
                                 extended = new RNFetchBlobDefaultResp(
335
                                 extended = new RNFetchBlobDefaultResp(

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

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

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

413
             {
413
             {
414
                 [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
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
             BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
417
             BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
418
+            
419
+            appendToExistingFile = !overwrite;
420
+
417
             // For solving #141 append response data if the file already exists
421
             // For solving #141 append response data if the file already exists
418
             // base on PR#139 @kejinliang
422
             // base on PR#139 @kejinliang
419
             if(appendToExistingFile)
423
             if(appendToExistingFile)
424
             {
428
             {
425
                 [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
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
             [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
432
             [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
429
             [writeStream open];
433
             [writeStream open];
430
         }
434
         }