Selaa lähdekoodia

Fix file upload total byte bug

Ben Hsieh 8 vuotta sitten
vanhempi
commit
0bd9e0efa0

+ 4
- 4
src/ios/RNFetchBlob/RNFetchBlob.m Näytä tiedosto

63
                   callback:(RCTResponseSenderBlock)callback)
63
                   callback:(RCTResponseSenderBlock)callback)
64
 {
64
 {
65
     
65
     
66
-    [RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req) {
66
+    [RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req, long bodyLength) {
67
         // send HTTP request
67
         // send HTTP request
68
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
68
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
69
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
69
+        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
70
         utils = nil;
70
         utils = nil;
71
     }];
71
     }];
72
     
72
     
80
                   headers:(NSDictionary *)headers
80
                   headers:(NSDictionary *)headers
81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
82
 {
82
 {
83
-    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req) {
83
+    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
84
         // send HTTP request
84
         // send HTTP request
85
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
85
         RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
86
-        [utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:req callback:callback];
86
+        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
87
         utils = nil;
87
         utils = nil;
88
     }];
88
     }];
89
     
89
     

+ 6
- 4
src/ios/RNFetchBlobFS.m Näytä tiedosto

283
         [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
283
         [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
284
             NSData * fileContent;
284
             NSData * fileContent;
285
             NSError * err;
285
             NSError * err;
286
+            Byte * buffer;
286
             if(asset != nil)
287
             if(asset != nil)
287
             {
288
             {
288
-                Byte * buffer = malloc(asset.size);
289
+                buffer = malloc(asset.size);
289
                 [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
290
                 [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
290
                 if(err != nil)
291
                 if(err != nil)
291
                 {
292
                 {
293
                     return;
294
                     return;
294
                 }
295
                 }
295
                 fileContent = [NSData dataWithBytes:buffer length:asset.size];
296
                 fileContent = [NSData dataWithBytes:buffer length:asset.size];
296
-                if(onComplete != nil)
297
-                    onComplete(fileContent);
298
                 free(buffer);
297
                 free(buffer);
299
             }
298
             }
300
             else
299
             else
305
                     return;
304
                     return;
306
                 }
305
                 }
307
                 fileContent = [NSData dataWithContentsOfFile:path];
306
                 fileContent = [NSData dataWithContentsOfFile:path];
307
+                
308
             }
308
             }
309
+            if(onComplete != nil)
310
+                onComplete(fileContent);
309
             
311
             
310
             if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
312
             if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
311
                 if(resolve != nil)
313
                 if(resolve != nil)
329
     @catch(NSException * e)
331
     @catch(NSException * e)
330
     {
332
     {
331
         if(reject != nil)
333
         if(reject != nil)
332
-        reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
334
+            reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
333
     }
335
     }
334
 }
336
 }
335
 
337
 

+ 1
- 1
src/ios/RNFetchBlobNetwork.h Näytä tiedosto

35
 - (void) sendRequest;
35
 - (void) sendRequest;
36
 
36
 
37
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
37
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
38
-- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
38
+- (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
39
 
39
 
40
 
40
 
41
 @end
41
 @end

+ 9
- 3
src/ios/RNFetchBlobNetwork.m Näytä tiedosto

69
 }
69
 }
70
 
70
 
71
 // send HTTP request
71
 // send HTTP request
72
-- (void) sendRequest:(NSDictionary  * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback
72
+- (void) sendRequest:(NSDictionary  * _Nullable )options
73
+       contentLength:(long) contentLength
74
+              bridge:(RCTBridge * _Nullable)bridgeRef
75
+              taskId:(NSString * _Nullable)taskId
76
+         withRequest:(NSURLRequest * _Nullable)req
77
+            callback:(_Nullable RCTResponseSenderBlock) callback
73
 {
78
 {
74
     self.taskId = taskId;
79
     self.taskId = taskId;
75
     self.respData = [[NSMutableData alloc] initWithLength:0];
80
     self.respData = [[NSMutableData alloc] initWithLength:0];
83
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
88
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
84
     NSURLSession * session;
89
     NSURLSession * session;
85
     
90
     
86
-    bodyLength = [[req HTTPBody] length];
91
+    bodyLength = contentLength;
87
     
92
     
88
     // the session trust any SSL certification
93
     // the session trust any SSL certification
89
 
94
 
104
         respFile = NO;
109
         respFile = NO;
105
     }
110
     }
106
     NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
111
     NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
112
+    
107
     [task resume];
113
     [task resume];
108
     
114
     
109
     // network status indicator
115
     // network status indicator
191
      body:@{
197
      body:@{
192
             @"taskId": taskId,
198
             @"taskId": taskId,
193
             @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
199
             @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
194
-            @"total": [NSString stringWithFormat:@"%d", totalBytesExpectedToWrite]
200
+            @"total": [NSString stringWithFormat:@"%d", bodyLength]
195
             }
201
             }
196
      ];
202
      ];
197
 }
203
 }

+ 2
- 2
src/ios/RNFetchBlobReqBuilder.h Näytä tiedosto

19
                           url:(NSString *)url
19
                           url:(NSString *)url
20
                       headers:(NSDictionary *)headers
20
                       headers:(NSDictionary *)headers
21
                          form:(NSArray *)form
21
                          form:(NSArray *)form
22
-                   onComplete:(void(^)(NSURLRequest * req))onComplete;
22
+                   onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
23
 
23
 
24
 +(void) buildOctetRequest:(NSDictionary *)options
24
 +(void) buildOctetRequest:(NSDictionary *)options
25
                    taskId:(NSString *)taskId
25
                    taskId:(NSString *)taskId
27
                       url:(NSString *)url
27
                       url:(NSString *)url
28
                   headers:(NSDictionary *)headers
28
                   headers:(NSDictionary *)headers
29
                      body:(NSString *)body
29
                      body:(NSString *)body
30
-               onComplete:(void(^)(NSURLRequest * req))onComplete;
30
+               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
31
 
31
 
32
 @end
32
 @end
33
 
33
 

+ 8
- 5
src/ios/RNFetchBlobReqBuilder.m Näytä tiedosto

28
                           url:(NSString *)url
28
                           url:(NSString *)url
29
                       headers:(NSDictionary *)headers
29
                       headers:(NSDictionary *)headers
30
                          form:(NSArray *)form
30
                          form:(NSArray *)form
31
-                   onComplete:(void(^)(NSURLRequest * req))onComplete
31
+                   onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
32
 {
32
 {
33
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
33
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
34
     // send request
34
     // send request
56
             [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
56
             [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
57
             [request setHTTPMethod: method];
57
             [request setHTTPMethod: method];
58
             [request setAllHTTPHeaderFields:mheaders];
58
             [request setAllHTTPHeaderFields:mheaders];
59
-            onComplete(request);
59
+            onComplete(request, [formData length]);
60
         }];
60
         }];
61
         
61
         
62
     });
62
     });
69
                       url:(NSString *)url
69
                       url:(NSString *)url
70
                   headers:(NSDictionary *)headers
70
                   headers:(NSDictionary *)headers
71
                      body:(NSString *)body
71
                      body:(NSString *)body
72
-               onComplete:(void(^)(NSURLRequest * req))onComplete
72
+               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
73
 {
73
 {
74
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
74
     NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
75
     // send request
75
     // send request
81
     // move heavy task to another thread
81
     // move heavy task to another thread
82
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
82
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
83
         NSMutableData * blobData;
83
         NSMutableData * blobData;
84
+        long size = -1;
84
         // if method is POST or PUT, convert data string format
85
         // if method is POST or PUT, convert data string format
85
         if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
86
         if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
86
             // generate octet-stream body
87
             // generate octet-stream body
97
                             [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
98
                             [mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
98
                             [request setHTTPMethod: method];
99
                             [request setHTTPMethod: method];
99
                             [request setAllHTTPHeaderFields:mheaders];
100
                             [request setAllHTTPHeaderFields:mheaders];
100
-                            onComplete(request);
101
+                            onComplete(request, [content length]);
101
                         }];
102
                         }];
102
                         return;
103
                         return;
103
                     }
104
                     }
105
+                    size = [[[NSFileManager defaultManager] attributesOfItemAtPath:orgPath error:nil] fileSize];
104
                     [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
106
                     [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
105
                 }
107
                 }
106
                 // otherwise convert it as BASE64 data string
108
                 // otherwise convert it as BASE64 data string
117
         [request setHTTPMethod: method];
119
         [request setHTTPMethod: method];
118
         [request setAllHTTPHeaderFields:mheaders];
120
         [request setAllHTTPHeaderFields:mheaders];
119
         
121
         
120
-        onComplete(request);
122
+        onComplete(request, size);
121
     });
123
     });
122
 }
124
 }
123
 
125
 
135
             NSString * name = [field valueForKey:@"name"];
137
             NSString * name = [field valueForKey:@"name"];
136
             NSString * content = [field valueForKey:@"data"];
138
             NSString * content = [field valueForKey:@"data"];
137
             NSString * contentType = [field valueForKey:@"type"];
139
             NSString * contentType = [field valueForKey:@"type"];
140
+            contentType = contentType == nil ? @"application/octet-stream" : contentType;
138
             // field is a text field
141
             // field is a text field
139
             if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
142
             if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
140
                 [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
143
                 [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];