forked from itinance/react-native-fs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Uploader.m
227 lines (196 loc) · 9.37 KB
/
Uploader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#import "Uploader.h"
#import "RNFSManager.h"
@implementation RNFSUploadParams
@end
@interface RNFSUploader()
@property (copy) RNFSUploadParams* params;
@property (retain) NSURLSessionDataTask* task;
@property (retain) NSMutableDictionary* responseData;
@end
@implementation RNFSUploader
- (void)uploadFiles:(RNFSUploadParams*)params
{
_params = params;
_responseData = [[NSMutableDictionary alloc] init];
NSString *method = _params.method;
NSURL *url = [NSURL URLWithString:_params.toUrl];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:method];
// set headers
for (NSString *key in _params.headers) {
id val = [_params.headers objectForKey:key];
if ([val respondsToSelector:@selector(stringValue)]) {
val = [val stringValue];
}
if (![val isKindOfClass:[NSString class]]) {
continue;
}
[req setValue:val forHTTPHeaderField:key];
}
if (_params.background) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *file = [_params.files objectAtIndex:0];
NSString *filepath = file[@"filepath"];
NSError* error = [self validateFilePath:filepath withFileManager:fileManager];
if(error != nil){
return _params.errorCallback(error);
}
_task = [self createBackgroundUploadTaskFromReq:req fromFilePath:filepath];
}
else{
NSLog(@"---CREATING FG UPLOAD");
NSString *formBoundaryString = [self generateBoundaryString];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", formBoundaryString];
[req setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *formBoundaryData = [[NSString stringWithFormat:@"--%@\r\n", formBoundaryString] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData* reqBody = [NSMutableData data];
[self setBodyFormFields:reqBody withBoundaryData:formBoundaryData];
NSError* error = [self setBodyFiles:reqBody withBoundaryData:formBoundaryData];
if(error != nil){
return _params.errorCallback(error);
}
NSData* end = [[NSString stringWithFormat:@"--%@--\r\n", formBoundaryString] dataUsingEncoding:NSUTF8StringEncoding];
[reqBody appendData:end];
[req setHTTPBody:reqBody];
_task = [self createUploadTaskFromReq:req];
}
[_task resume];
_params.beginCallback();
}
- (void)stopUpload
{
[_task cancel];
}
- (void)setBodyFormFields:(NSMutableData *)reqBody withBoundaryData:(NSData *)formBoundaryData
{
for (NSString *key in _params.fields) {
id val = [_params.fields objectForKey:key];
if ([val respondsToSelector:@selector(stringValue)]) {
val = [val stringValue];
}
if (![val isKindOfClass:[NSString class]]) {
continue;
}
[reqBody appendData:formBoundaryData];
[reqBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:[val dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
}
- (NSError *)setBodyFiles:(NSMutableData *)reqBody withBoundaryData:(NSData *)formBoundaryData
{
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSDictionary *file in _params.files) {
NSString *name = file[@"name"];
NSString *filename = file[@"filename"];
NSString *filepath = file[@"filepath"];
NSString *filetype = file[@"filetype"];
NSError* error = [self validateFilePath:filepath withFileManager:fileManager];
if(error != nil){
return error;
}
[reqBody appendData:formBoundaryData];
[reqBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name.length ? name : filename, filename] dataUsingEncoding:NSUTF8StringEncoding]];
if (filetype) {
[reqBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", filetype] dataUsingEncoding:NSUTF8StringEncoding]];
} else {
[reqBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", [self mimeTypeForPath:filename]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSData *fileData = [NSData dataWithContentsOfFile:filepath];
[reqBody appendData:[[NSString stringWithFormat:@"Content-Length: %ld\r\n\r\n", (long)[fileData length]] dataUsingEncoding:NSUTF8StringEncoding]];
[reqBody appendData:fileData];
[reqBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
return nil;
}
- (NSURLSessionDataTask *)createUploadTaskFromReq: (NSMutableURLRequest *)req
{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:(id)self delegateQueue:[NSOperationQueue mainQueue]];
return [session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"---INLINE COMPLETED EMIT---");
NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return _params.completeCallback(str, response);
}];
}
- (NSURLSessionDataTask *)createBackgroundUploadTaskFromReq: (NSMutableURLRequest *)req fromFilePath:(NSString *)filepath
{
NSURL *fileUrl = [NSURL fileURLWithPath:filepath];
NSString *uuid = [[NSUUID UUID] UUIDString];
NSLog(@"---CREATING BG UPLOAD TASK of file %@ in session %@", filepath, uuid);
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:uuid];
sessionConfiguration.sessionSendsLaunchEvents = YES;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:(id)self delegateQueue:[NSOperationQueue mainQueue]];
return [session uploadTaskWithRequest:req fromFile:fileUrl];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"---COMPLETED EMIT---");
if(error != nil) {
return _params.errorCallback(error);
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
NSNumber *statusCode = [NSNumber numberWithUnsignedInteger:((NSHTTPURLResponse *)httpResponse).statusCode];
NSMutableData *responseForTask = [_responseData objectForKey:@(task.taskIdentifier)];
NSString *body = @"";
if (responseForTask) {
body = [[NSString alloc] initWithData:responseForTask encoding:NSUTF8StringEncoding];
NSLog(@"---WITH DATA---");
[_responseData removeObjectForKey:@(task.taskIdentifier)];
}
if(!statusCode || ![statusCode isEqualToNumber:[NSNumber numberWithInt:200]]){
NSString *errorDescription = [NSString stringWithFormat:@"Status Code: %d. Body: %@",
statusCode != nil ? [statusCode integerValue] : 0,
body];
NSString *errorDomain = [RNFSManager getErrorDomain];
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[userInfo setObject:errorDescription forKey:NSLocalizedDescriptionKey];
NSError *serverError = [NSError errorWithDomain:errorDomain code:500 userInfo:userInfo];
return _params.errorCallback(serverError);
}
return _params.completeCallback(body, httpResponse);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
NSLog(@"---PROGRESS EMIT---");
return _params.progressCallback([NSNumber numberWithLongLong:totalBytesExpectedToSend], [NSNumber numberWithLongLong:totalBytesSent]);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSLog(@"---RECEIVED DATA---");
NSMutableData *responseData = [_responseData objectForKey:@(dataTask.taskIdentifier)];
if (!responseData) {
responseData = [NSMutableData dataWithData:data];
[_responseData setObject:responseData forKey:@(dataTask.taskIdentifier)];
} else {
[responseData appendData:data];
}
}
- (NSError *)validateFilePath:(NSString *)filepath withFileManager:(NSFileManager*)fileManager
{
if (![fileManager fileExistsAtPath:filepath]){
NSLog(@"Failed to open target file at path: %@", filepath);
NSError* error = [NSError errorWithDomain:@"Uploader" code:NSURLErrorFileDoesNotExist userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to open target file at path: %@", filepath]}];
return error;
}
return nil;
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
[RNFSManager URLSessionDidFinishEventsForBackgroundURLSession:session];
}
- (NSString *)generateBoundaryString
{
NSString *uuid = [[NSUUID UUID] UUIDString];
return [NSString stringWithFormat:@"----%@", uuid];
}
- (NSString *)mimeTypeForPath:(NSString *)filepath
{
NSString *fileExtension = [filepath pathExtension];
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
if (contentType) {
return contentType;
}
return @"application/octet-stream";
}
@end