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

Commit 0bd9e0e

Browse files
committed
Fix file upload total byte bug
1 parent 1aff852 commit 0bd9e0e

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ - (NSDictionary *)constantsToExport
6363
callback:(RCTResponseSenderBlock)callback)
6464
{
6565

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) {
6767
// send HTTP request
6868
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];
7070
utils = nil;
7171
}];
7272

@@ -80,10 +80,10 @@ - (NSDictionary *)constantsToExport
8080
headers:(NSDictionary *)headers
8181
body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
8282
{
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) {
8484
// send HTTP request
8585
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];
8787
utils = nil;
8888
}];
8989

src/ios/RNFetchBlobFS.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,17 @@ + (void) readFile:(NSString *)path encoding:(NSString *)encoding
283283
[[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
284284
NSData * fileContent;
285285
NSError * err;
286+
Byte * buffer;
286287
if(asset != nil)
287288
{
288-
Byte * buffer = malloc(asset.size);
289+
buffer = malloc(asset.size);
289290
[asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
290291
if(err != nil)
291292
{
292293
reject(@"RNFetchBlobFS readFile error", @"failed to read asset", [err localizedDescription]);
293294
return;
294295
}
295296
fileContent = [NSData dataWithBytes:buffer length:asset.size];
296-
if(onComplete != nil)
297-
onComplete(fileContent);
298297
free(buffer);
299298
}
300299
else
@@ -305,7 +304,10 @@ + (void) readFile:(NSString *)path encoding:(NSString *)encoding
305304
return;
306305
}
307306
fileContent = [NSData dataWithContentsOfFile:path];
307+
308308
}
309+
if(onComplete != nil)
310+
onComplete(fileContent);
309311

310312
if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
311313
if(resolve != nil)
@@ -329,7 +331,7 @@ + (void) readFile:(NSString *)path encoding:(NSString *)encoding
329331
@catch(NSException * e)
330332
{
331333
if(reject != nil)
332-
reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
334+
reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
333335
}
334336
}
335337

src/ios/RNFetchBlobNetwork.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
3535
- (void) sendRequest;
3636

3737
+ (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;
3939

4040

4141
@end

src/ios/RNFetchBlobNetwork.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
6969
}
7070

7171
// 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
7378
{
7479
self.taskId = taskId;
7580
self.respData = [[NSMutableData alloc] initWithLength:0];
@@ -83,7 +88,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
8388
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
8489
NSURLSession * session;
8590

86-
bodyLength = [[req HTTPBody] length];
91+
bodyLength = contentLength;
8792

8893
// the session trust any SSL certification
8994

@@ -104,6 +109,7 @@ - (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nu
104109
respFile = NO;
105110
}
106111
NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
112+
107113
[task resume];
108114

109115
// network status indicator
@@ -191,7 +197,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
191197
body:@{
192198
@"taskId": taskId,
193199
@"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
194-
@"total": [NSString stringWithFormat:@"%d", totalBytesExpectedToWrite]
200+
@"total": [NSString stringWithFormat:@"%d", bodyLength]
195201
}
196202
];
197203
}

src/ios/RNFetchBlobReqBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
url:(NSString *)url
2020
headers:(NSDictionary *)headers
2121
form:(NSArray *)form
22-
onComplete:(void(^)(NSURLRequest * req))onComplete;
22+
onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
2323

2424
+(void) buildOctetRequest:(NSDictionary *)options
2525
taskId:(NSString *)taskId
2626
method:(NSString *)method
2727
url:(NSString *)url
2828
headers:(NSDictionary *)headers
2929
body:(NSString *)body
30-
onComplete:(void(^)(NSURLRequest * req))onComplete;
30+
onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
3131

3232
@end
3333

src/ios/RNFetchBlobReqBuilder.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ +(void) buildMultipartRequest:(NSDictionary *)options
2828
url:(NSString *)url
2929
headers:(NSDictionary *)headers
3030
form:(NSArray *)form
31-
onComplete:(void(^)(NSURLRequest * req))onComplete
31+
onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
3232
{
3333
NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
3434
// send request
@@ -56,7 +56,7 @@ +(void) buildMultipartRequest:(NSDictionary *)options
5656
[mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
5757
[request setHTTPMethod: method];
5858
[request setAllHTTPHeaderFields:mheaders];
59-
onComplete(request);
59+
onComplete(request, [formData length]);
6060
}];
6161

6262
});
@@ -69,7 +69,7 @@ +(void) buildOctetRequest:(NSDictionary *)options
6969
url:(NSString *)url
7070
headers:(NSDictionary *)headers
7171
body:(NSString *)body
72-
onComplete:(void(^)(NSURLRequest * req))onComplete
72+
onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
7373
{
7474
NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
7575
// send request
@@ -81,6 +81,7 @@ +(void) buildOctetRequest:(NSDictionary *)options
8181
// move heavy task to another thread
8282
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
8383
NSMutableData * blobData;
84+
long size = -1;
8485
// if method is POST or PUT, convert data string format
8586
if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
8687
// generate octet-stream body
@@ -97,10 +98,11 @@ +(void) buildOctetRequest:(NSDictionary *)options
9798
[mheaders setValue:@"application/octet-stream" forKey:@"content-type"];
9899
[request setHTTPMethod: method];
99100
[request setAllHTTPHeaderFields:mheaders];
100-
onComplete(request);
101+
onComplete(request, [content length]);
101102
}];
102103
return;
103104
}
105+
size = [[[NSFileManager defaultManager] attributesOfItemAtPath:orgPath error:nil] fileSize];
104106
[request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
105107
}
106108
// otherwise convert it as BASE64 data string
@@ -117,7 +119,7 @@ +(void) buildOctetRequest:(NSDictionary *)options
117119
[request setHTTPMethod: method];
118120
[request setAllHTTPHeaderFields:mheaders];
119121

120-
onComplete(request);
122+
onComplete(request, size);
121123
});
122124
}
123125

@@ -135,6 +137,7 @@ void __block (^getFieldData)(id field) = ^(id field)
135137
NSString * name = [field valueForKey:@"name"];
136138
NSString * content = [field valueForKey:@"data"];
137139
NSString * contentType = [field valueForKey:@"type"];
140+
contentType = contentType == nil ? @"application/octet-stream" : contentType;
138141
// field is a text field
139142
if([field valueForKey:@"filename"] == nil || content == [NSNull null]) {
140143
[formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

0 commit comments

Comments
 (0)