-
Notifications
You must be signed in to change notification settings - Fork 1
/
DMGhostbinUploader.m
105 lines (83 loc) · 2.54 KB
/
DMGhostbinUploader.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
#import "DMGhostbinUploader.h"
@implementation DMGhostbinUploader
@synthesize delegate = _delegate;
@synthesize uploading = _uploading;
- (void)beginUploadingText:(NSString *)text language:(NSString *)language expire:(NSTimeInterval)expireInterval
{
if (_uploading) {
return;
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://ghostbin.com/paste/new"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSString stringWithFormat:@"text=%@&lang=%@&expire=%i%s", text, language, (int)expireInterval, (expireInterval == -1 ? "" : "s")] dataUsingEncoding:NSUTF8StringEncoding]];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_connection release];
_uploading = YES;
}
- (void)cancelUpload
{
[_connection cancel];
_connection = nil;
_uploading = NO;
}
- (void)dealloc
{
if (_responseData) {
[_responseData release];
_responseData = nil;
}
[super dealloc];
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if (_responseData) {
[_responseData release];
_responseData = nil;
}
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse*)cachedResponse
{
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// The Ghostbin URL is acquired in connection:willSendRequest:redirectResponse.
if (_responseData) {
[_responseData release];
_responseData = nil;
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (error) {
if ([_delegate respondsToSelector:@selector(uploader:didFailWithError:)]) {
[_delegate uploader:self didFailWithError:error];
}
}
if (_responseData) {
[_responseData release];
_responseData = nil;
}
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;
{
if (redirectResponse) {
[connection cancel];
_uploading = NO;
if ([_delegate respondsToSelector:@selector(uploader:didFinishUploadingWithURL:)]) {
[_delegate uploader:self didFinishUploadingWithURL:[[[request URL] copy] autorelease]];
}
return nil;
} else {
return request;
}
}
@end