|
@@ -1,140 +0,0 @@
|
1
|
|
-//
|
2
|
|
-// RNFetchBlobUtil.m
|
3
|
|
-// RNFetchBlob
|
4
|
|
-//
|
5
|
|
-// Created by Ben Hsieh on 2016/5/24.
|
6
|
|
-// Copyright © 2016年 suzuri04x2. All rights reserved.
|
7
|
|
-//
|
8
|
|
-
|
9
|
|
-#import <Foundation/Foundation.h>
|
10
|
|
-#import "RNFetchBlobUtil.h"
|
11
|
|
-
|
12
|
|
-
|
13
|
|
-////////////////////////////////////////
|
14
|
|
-//
|
15
|
|
-// Util functions
|
16
|
|
-//
|
17
|
|
-////////////////////////////////////////
|
18
|
|
-
|
19
|
|
-@implementation FetchBlobUtils
|
20
|
|
-
|
21
|
|
-
|
22
|
|
-@synthesize taskId;
|
23
|
|
-@synthesize expectedBytes;
|
24
|
|
-@synthesize receivedBytes;
|
25
|
|
-@synthesize respData;
|
26
|
|
-@synthesize callback;
|
27
|
|
-
|
28
|
|
-RCT_EXPORT_MODULE();
|
29
|
|
-
|
30
|
|
-// callback class method to handle request
|
31
|
|
-//+ (void) onBlobResponse:(NSURLResponse * _Nullable)response withData:(NSData * _Nullable)data withError:(NSError * _Nullable)connectionError withCallback:(RCTResponseSenderBlock) callback{
|
32
|
|
-//
|
33
|
|
-// NSHTTPURLResponse* resp = (NSHTTPURLResponse *) response;
|
34
|
|
-// NSString* status = [NSString stringWithFormat:@"%d", resp.statusCode];
|
35
|
|
-//
|
36
|
|
-// if(connectionError)
|
37
|
|
-// {
|
38
|
|
-// callback(@[[connectionError localizedDescription], [NSNull null]]);
|
39
|
|
-// }
|
40
|
|
-// else if(![status isEqualToString:@"200"]) {
|
41
|
|
-// callback(@[status, [NSNull null]]);
|
42
|
|
-// }
|
43
|
|
-// else {
|
44
|
|
-// callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
|
45
|
|
-// }
|
46
|
|
-//
|
47
|
|
-//}
|
48
|
|
-
|
49
|
|
-// removing case of headers
|
50
|
|
-+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
|
51
|
|
-
|
52
|
|
- NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
|
53
|
|
- for(NSString * key in headers) {
|
54
|
|
- [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
|
55
|
|
- }
|
56
|
|
-
|
57
|
|
- return mheaders;
|
58
|
|
-}
|
59
|
|
-
|
60
|
|
-- (id)init {
|
61
|
|
- self = [super init];
|
62
|
|
- return self;
|
63
|
|
-}
|
64
|
|
-
|
65
|
|
-- (id)delegate:(id)delegate {
|
66
|
|
- return delegate;
|
67
|
|
-}
|
68
|
|
-
|
69
|
|
-- (void) sendRequest:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback {
|
70
|
|
- self.taskId = taskId;
|
71
|
|
- self.respData = [[NSMutableData alloc] initWithLength:0];
|
72
|
|
- // Call long-running code on background thread
|
73
|
|
- NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
|
74
|
|
- [conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
|
75
|
|
- [conn start];
|
76
|
|
-
|
77
|
|
- if(!conn) {
|
78
|
|
- callback(@[[NSString stringWithFormat:@"RNFetchBlob could not initialize connection"], [NSNull null]]);
|
79
|
|
- }
|
80
|
|
-}
|
81
|
|
-
|
82
|
|
-
|
83
|
|
-#pragma mark NSURLConnection delegate methods
|
84
|
|
-
|
85
|
|
-
|
86
|
|
-- (void) connection:(NSURLConnection *)connection didReceiveResponse:(nonnull NSURLResponse *)response {
|
87
|
|
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
|
88
|
|
- expectedBytes = [response expectedContentLength];
|
89
|
|
-}
|
90
|
|
-
|
91
|
|
-- (void) connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data {
|
92
|
|
- receivedBytes = data.length;
|
93
|
|
- [respData appendData:data];
|
94
|
|
- [self.bridge.eventDispatcher
|
95
|
|
- sendAppEventWithName:@"RNFetchBlobProgress"
|
96
|
|
- body:@{
|
97
|
|
- @"taskId": taskId,
|
98
|
|
- @"written": [NSString stringWithFormat:@"%@", receivedBytes],
|
99
|
|
- @"total": [NSString stringWithFormat:@"%@", expectedBytes]
|
100
|
|
- }
|
101
|
|
- ];
|
102
|
|
-}
|
103
|
|
-
|
104
|
|
-- (void) connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
|
105
|
|
-
|
106
|
|
- expectedBytes = totalBytesExpectedToWrite;
|
107
|
|
- receivedBytes = totalBytesWritten;
|
108
|
|
- [self.bridge.eventDispatcher
|
109
|
|
- sendAppEventWithName:@"RNFetchBlobProgress"
|
110
|
|
- body:@{
|
111
|
|
- @"taskId": taskId,
|
112
|
|
- @"written": [NSString stringWithFormat:@"%@", receivedBytes],
|
113
|
|
- @"total": [NSString stringWithFormat:@"%@", expectedBytes]
|
114
|
|
- }
|
115
|
|
- ];
|
116
|
|
-
|
117
|
|
-}
|
118
|
|
-
|
119
|
|
-- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
120
|
|
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
121
|
|
- callback(@[[error localizedDescription], [NSNull null]]);
|
122
|
|
-}
|
123
|
|
-
|
124
|
|
-- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse: (NSCachedURLResponse *)cachedResponse {
|
125
|
|
- return nil;
|
126
|
|
-}
|
127
|
|
-
|
128
|
|
-
|
129
|
|
-// handle 301 and 302 responses
|
130
|
|
-- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:response {
|
131
|
|
-
|
132
|
|
- return request;
|
133
|
|
-}
|
134
|
|
-
|
135
|
|
-// request complete
|
136
|
|
-- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
|
137
|
|
- callback(@[[NSNull null], [respData base64EncodedStringWithOptions:0]]);
|
138
|
|
-}
|
139
|
|
-
|
140
|
|
-@end
|