Browse Source

Fix IOS writeFile behavior

Ben Hsieh 8 years ago
parent
commit
c2985f4245
3 changed files with 19 additions and 7 deletions
  1. 1
    0
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 1
    0
      src/ios/RNFetchBlobFS.m
  3. 17
    7
      src/ios/RNFetchBlobReqBuilder.m

+ 1
- 0
src/ios/RNFetchBlob/RNFetchBlob.m View File

192
     callback(@[[NSNull null], @YES]);
192
     callback(@[[NSNull null], @YES]);
193
 }
193
 }
194
 
194
 
195
+#pragma mark - unlink
195
 RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
196
 RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
196
     NSError * error = nil;
197
     NSError * error = nil;
197
     NSString * tmpPath = nil;
198
     NSString * tmpPath = nil;

+ 1
- 0
src/ios/RNFetchBlobFS.m View File

225
             [os open];
225
             [os open];
226
             while((read = [asset getBytes:buffer fromOffset:cursor length:10240 error:nil]) > 0)
226
             while((read = [asset getBytes:buffer fromOffset:cursor length:10240 error:nil]) > 0)
227
             {
227
             {
228
+                cursor += read;
228
                 [os write:buffer maxLength:read];
229
                 [os write:buffer maxLength:read];
229
             }
230
             }
230
             __block NSNumber * size = [NSNumber numberWithLong:written];
231
             __block NSNumber * size = [NSNumber numberWithLong:written];

+ 17
- 7
src/ios/RNFetchBlobReqBuilder.m View File

36
     
36
     
37
     // send request
37
     // send request
38
     __block NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: encodedUrl]];
38
     __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]];
40
     NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
40
     NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
41
     NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
41
     NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
42
 
42
 
43
     // generate boundary
43
     // generate boundary
44
-    NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
44
+    __block NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
45
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
45
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
46
         __block NSMutableData * postData = [[NSMutableData alloc] init];
46
         __block NSMutableData * postData = [[NSMutableData alloc] init];
47
         // combine multipart/form-data body
47
         // combine multipart/form-data body
148
 
148
 
149
 +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete
149
 +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete
150
 {
150
 {
151
-    NSMutableData * formData = [[NSMutableData alloc] init];
151
+    __block NSMutableData * formData = [[NSMutableData alloc] init];
152
     if(form == nil)
152
     if(form == nil)
153
         onComplete(nil);
153
         onComplete(nil);
154
     else
154
     else
159
         void __block (^getFieldData)(id field) = ^(id field)
159
         void __block (^getFieldData)(id field) = ^(id field)
160
         {
160
         {
161
             NSString * name = [field valueForKey:@"name"];
161
             NSString * name = [field valueForKey:@"name"];
162
-            NSString * content = [field valueForKey:@"data"];
162
+            __block NSString * content = [field valueForKey:@"data"];
163
             NSString * contentType = [field valueForKey:@"type"];
163
             NSString * contentType = [field valueForKey:@"type"];
164
             // skip when the form field `name` or `data` is empty
164
             // skip when the form field `name` or `data` is empty
165
             if(content == nil || name == nil)
165
             if(content == nil || name == nil)
197
                             i++;
197
                             i++;
198
                             if(i < count)
198
                             if(i < count)
199
                             {
199
                             {
200
-                                getFieldData([form objectAtIndex:i]);
200
+                                __block NSDictionary * nextField = [form objectAtIndex:i];
201
+                                getFieldData(nextField);
201
                             }
202
                             }
202
                             else
203
                             else
204
+                            {
203
                                 onComplete(formData);
205
                                 onComplete(formData);
206
+                                getFieldData = nil;
207
+                            }
204
                         }];
208
                         }];
205
                         return ;
209
                         return ;
206
                     }
210
                     }
213
                 [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
217
                 [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
214
                 [formData appendData:blobData];
218
                 [formData appendData:blobData];
215
                 [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
219
                 [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
220
+                blobData = nil;
216
             }
221
             }
217
             i++;
222
             i++;
218
             if(i < count)
223
             if(i < count)
219
             {
224
             {
220
-                getFieldData([form objectAtIndex:i]);
225
+                __block NSDictionary * nextField = [form objectAtIndex:i];
226
+                getFieldData(nextField);
221
             }
227
             }
222
             else
228
             else
229
+            {
223
                 onComplete(formData);
230
                 onComplete(formData);
231
+                getFieldData = nil;
232
+            }
224
 
233
 
225
         };
234
         };
226
-        getFieldData([form objectAtIndex:i]);
235
+        __block NSDictionary * nextField = [form objectAtIndex:i];
236
+        getFieldData(nextField);
227
     }
237
     }
228
 }
238
 }
229
 
239