-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMImageUploadOperation.m
149 lines (118 loc) · 7.41 KB
/
GMImageUploadOperation.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
//
// GMImageUploadOperation.m
//
// Created by Gersham Meharg on 11-02-18.
// Copyright 2011 Gersham Meharg. All rights reserved.
//
#import "GMImageUploadOperation.h"
#import "GMImageManager.h"
#import "GMImageUploadOperationResult.h"
@implementation GMImageUploadOperation
@synthesize url = _url;
@synthesize completion = _completion;
@synthesize result = _result;
@synthesize delegate = _delegate;
@synthesize data = _data;
- (void)main {
NSLog(@"Uploading image to %@", _url);
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyyMMddHHmmss"];
NSString *imageName = [NSString stringWithFormat:@"Image_%@", [format stringFromDate:[NSDate date]]];
NSString *methodString = @"POST";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:_url];
[request setHTTPMethod:methodString];
[request setValue:UIAppDelegate.apiKey forHTTPHeaderField:@"APIKEY"];
NSString *boundary = @"---------------------------Boundary Line---------------------------";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.jpg\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:_data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set body with request.
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
NSError *error = nil;
NSHTTPURLResponse *response;
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionaryWithCapacity:2];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.result = [GMImageUploadOperationResult new];
_result.httpCode = [response statusCode];
_result.url = _url;
_result.completion = _completion;
if ([response statusCode] == 201 || [response statusCode] == 200) {
NSLog(@"Image uploaded, bytes: %i", body.length);
NSError *error = nil;
_result.json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
if (error != nil) {
NSLog(@"Error parsing JSON %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionaryWithCapacity:2];
[errorInfo setObject:@"JSON Parsing Error" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:@"Error parsing JSON" forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
}
} else if ([response statusCode] == 401) {
NSLog(@"Authentication failed for image upload");
_result.httpCode = 401;
[errorInfo setObject:@"Authentication Failure" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:@"The supplied API Key was not found" forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
} else if ([response statusCode] == 400) {
_result.httpCode = 400;
[errorInfo setObject:@"Upload Error" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:responseString forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
} else if ([response statusCode] == 404) {
_result.httpCode = 404;
[errorInfo setObject:@"Upload Error" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:responseString forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
} else if (error != nil) {
[errorInfo setObject:@"Connection Error" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:error.localizedDescription forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
} else {
[errorInfo setObject:@"Upload Error" forKey:NSLocalizedDescriptionKey];
[errorInfo setObject:responseString forKey:NSLocalizedFailureReasonErrorKey];
_result.error = [NSError errorWithDomain:@"NetworkOperationError" code:_result.httpCode userInfo:errorInfo];
_result.errorTitle = [_result.error.userInfo objectForKey:NSLocalizedDescriptionKey];
_result.errorDescription = [_result.error.userInfo objectForKey:NSLocalizedFailureReasonErrorKey];
}
// Send the result to the delegate
if ([_delegate respondsToSelector:@selector(uploadResult:)]) {
[_delegate performSelectorOnMainThread:@selector(uploadResult:) withObject:_result waitUntilDone:YES];
} else if (_delegate != nil) {
NSLog(@"* Delegate doesn't respond to networkOperationResult");
}
}
- (NSString *)encodeFormPostParameters: (NSDictionary *)postParameters {
NSMutableString *formPostParams = [[NSMutableString alloc] init];
NSEnumerator *keys = [postParameters keyEnumerator];
NSString *name = [keys nextObject];
while (nil != name) {
NSString *encodedValue = ((__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef) [postParameters objectForKey:name], NULL, CFSTR("=/:"), kCFStringEncodingUTF8));
[formPostParams appendString: name];
[formPostParams appendString: @"="];
[formPostParams appendString: encodedValue];
name = [keys nextObject];
if (nil != name) {
[formPostParams appendString: @"&"];
}
}
return formPostParams;
}
@end