This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ORSTwitPicDispatcher.m
99 lines (85 loc) · 3.84 KB
/
ORSTwitPicDispatcher.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
//
// ORSTwitPicDispatcher.h
// Canary
//
// Created by Nicholas Toumpelis on 12/04/2009.
// Copyright 2009 Ocean Road Software. All rights reserved.
//
// Version 0.7
#import "ORSTwitPicDispatcher.h"
@implementation ORSTwitPicDispatcher
- (NSString *) uploadData:(NSData *)imageData
withUsername:(NSString *)username
password:(NSString *)password
filename:(NSString *)filename {
@synchronized(self) {
NSURL *url = [NSURL URLWithString:@"http://twitpic.com/api/upload"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest
requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:21.0];
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString
stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString
stringWithFormat:@"multipart/form-data; boundary=%@",
stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",
stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:
@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"canary"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",
stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:
@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",
stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:
@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
NSString *mimeType = NULL;
if ([filename hasSuffix:@".jpeg"] || [filename hasSuffix:@".jpg"] ||
[filename hasSuffix:@".jpe"]) {
mimeType = @"image/jpeg";
} else if ([filename hasSuffix:@".png"]) {
mimeType = @"image/png";
} else if ([filename hasSuffix:@".gif"]) {
mimeType = @"image/gif";
}
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",
stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n",
filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString
stringWithFormat:@"Content-Type: %@\r\n", mimeType]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:
@"Content-Transfer-Encoding: binary\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",
stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSURLResponse *response = nil;
NSError *dataError, *documentError, *nodeError = nil;
NSString *stringNode = [[NSString alloc] initWithData:[NSURLConnection
sendSynchronousRequest:postRequest returningResponse:&response
error:&dataError] encoding:NSUTF8StringEncoding];
NSXMLDocument *document = [[NSXMLDocument alloc]
initWithXMLString:stringNode
options:NSXMLDocumentTidyXML error:&documentError];
NSXMLNode *root = [document rootElement];
return [(NSXMLNode *)[[root nodesForXPath:@".//mediaurl"
error:&nodeError] objectAtIndex:0] stringValue];
}
}
@end