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

Commit c2985f4

Browse files
committed
Fix IOS writeFile behavior
1 parent e5d7012 commit c2985f4

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ - (NSDictionary *)constantsToExport
192192
callback(@[[NSNull null], @YES]);
193193
}
194194

195+
#pragma mark - unlink
195196
RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
196197
NSError * error = nil;
197198
NSString * tmpPath = nil;

src/ios/RNFetchBlobFS.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ + (NSNumber *) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:
225225
[os open];
226226
while((read = [asset getBytes:buffer fromOffset:cursor length:10240 error:nil]) > 0)
227227
{
228+
cursor += read;
228229
[os write:buffer maxLength:read];
229230
}
230231
__block NSNumber * size = [NSNumber numberWithLong:written];

src/ios/RNFetchBlobReqBuilder.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ +(void) buildMultipartRequest:(NSDictionary *)options
3636

3737
// send request
3838
__block NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: encodedUrl]];
39-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
39+
__block NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
4040
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
4141
NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
4242

4343
// generate boundary
44-
NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
44+
__block NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
4545
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
4646
__block NSMutableData * postData = [[NSMutableData alloc] init];
4747
// combine multipart/form-data body
@@ -148,7 +148,7 @@ +(void) buildOctetRequest:(NSDictionary *)options
148148

149149
+(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete
150150
{
151-
NSMutableData * formData = [[NSMutableData alloc] init];
151+
__block NSMutableData * formData = [[NSMutableData alloc] init];
152152
if(form == nil)
153153
onComplete(nil);
154154
else
@@ -159,7 +159,7 @@ +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(
159159
void __block (^getFieldData)(id field) = ^(id field)
160160
{
161161
NSString * name = [field valueForKey:@"name"];
162-
NSString * content = [field valueForKey:@"data"];
162+
__block NSString * content = [field valueForKey:@"data"];
163163
NSString * contentType = [field valueForKey:@"type"];
164164
// skip when the form field `name` or `data` is empty
165165
if(content == nil || name == nil)
@@ -197,10 +197,14 @@ void __block (^getFieldData)(id field) = ^(id field)
197197
i++;
198198
if(i < count)
199199
{
200-
getFieldData([form objectAtIndex:i]);
200+
__block NSDictionary * nextField = [form objectAtIndex:i];
201+
getFieldData(nextField);
201202
}
202203
else
204+
{
203205
onComplete(formData);
206+
getFieldData = nil;
207+
}
204208
}];
205209
return ;
206210
}
@@ -213,17 +217,23 @@ void __block (^getFieldData)(id field) = ^(id field)
213217
[formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
214218
[formData appendData:blobData];
215219
[formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
220+
blobData = nil;
216221
}
217222
i++;
218223
if(i < count)
219224
{
220-
getFieldData([form objectAtIndex:i]);
225+
__block NSDictionary * nextField = [form objectAtIndex:i];
226+
getFieldData(nextField);
221227
}
222228
else
229+
{
223230
onComplete(formData);
231+
getFieldData = nil;
232+
}
224233

225234
};
226-
getFieldData([form objectAtIndex:i]);
235+
__block NSDictionary * nextField = [form objectAtIndex:i];
236+
getFieldData(nextField);
227237
}
228238
}
229239

0 commit comments

Comments
 (0)