Browse files

Photo uploading (from local device) is now working.

Photo with source URL is work in progress.
  • Loading branch information...
1 parent 4ec4a2d commit f8cfc9e40224c7134fc9be9b466cb9a0daa7aef9 Igor Sutton committed Jan 13, 2011
Showing with 46 additions and 11 deletions.
  1. +1 −1 NSDictionary+TumblrKit.h
  2. +13 −5 NSDictionary+TumblrKit.m
  3. +4 −0 TKPost.h
  4. +19 −1 TKPost.m
  5. +9 −4 TKTumblr.m
View
2 NSDictionary+TumblrKit.h
@@ -27,6 +27,6 @@
*/
@interface NSDictionary (TumblrKit)
-- (NSString *)multipartMIMEString;
+- (NSData *)multipartMIMEData;
@end
View
18 NSDictionary+TumblrKit.m
@@ -25,10 +25,10 @@
@implementation NSDictionary (TumblrKit)
-- (NSString *)multipartMIMEString
+- (NSData *)multipartMIMEData
{
NSString *format = @"--%@\nContent-Disposition: form-data; name=\"%@\"\n\n%@\n";
- NSMutableString *result = [NSMutableString string];
+ NSMutableData *result = [NSMutableData data];
NSMutableDictionary *dict_ = [self mutableCopy];
// Hack: it seems the order we send the data in the HTTP post body matter
@@ -42,13 +42,21 @@ - (NSString *)multipartMIMEString
id value = [dict_ objectForKey:@"type"];
[dict_ removeObjectForKey:@"type"];
- [result appendFormat:format, [NSString MIMEBoundary], @"type", value];
+ [result appendData:[[NSString stringWithFormat:format, [NSString MIMEBoundary], @"type", value] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *key in dict_) {
- [result appendFormat:format, [NSString MIMEBoundary], key, [dict_ objectForKey:key]];
+ if ([key isEqualToString:@"data"]) {
+ [result appendData:[[NSString stringWithFormat:@"--%@\nContent-Disposition: form-data; name=\"%@\"\n\n", [NSString MIMEBoundary], key] dataUsingEncoding:NSUTF8StringEncoding]];
+ [result appendData:[dict_ objectForKey:key]];
+ [result appendData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
+ }
+ else {
+ [result appendData:[[NSString stringWithFormat:format, [NSString MIMEBoundary], key, [dict_ objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
+ }
}
- [result appendFormat:@"\n--%@--\n", [NSString MIMEBoundary]];
+ [result appendData:[[NSString stringWithFormat:@"\n--%@--\n", [NSString MIMEBoundary]] dataUsingEncoding:NSUTF8StringEncoding]];
+
return result;
}
View
4 TKPost.h
@@ -194,12 +194,16 @@ typedef enum
@interface TKPostPhoto : TKPost
{
NSMutableString *caption;
+ NSString *source;
+ NSImage *image;
NSUInteger width;
NSUInteger height;
}
@property (assign) NSUInteger width;
@property (assign) NSUInteger height;
+@property (copy) NSString *source;
+@property (retain) NSImage *image;
- (NSString *)caption;
- (void)setCaption:(NSString *)aCaption;
View
20 TKPost.m
@@ -413,13 +413,15 @@ - (NSDictionary *)attributesAsDictionary
@implementation TKPostPhoto
-@synthesize width, height;
+@synthesize width, height, source, image;
- (id)init
{
if ((self = [super init]) != nil) {
type = TKPostTypePhoto;
caption = [[NSMutableString alloc] init];
+ image = nil;
+ source = nil;
width = 0;
height = 0;
}
@@ -439,7 +441,9 @@ - (id)initWithAttributes:(NSDictionary *)attributeDict
- (void)dealloc
{
+ [source release];
[caption release];
+ [image release];
[super dealloc];
}
@@ -466,6 +470,20 @@ - (NSString *)description
return [NSString stringWithFormat:@"%@, Photo Caption: %@, Photo Width: %i, Photo Height: %i", [super description], self.caption, self.width, self.height];
}
+- (NSDictionary *)attributesAsDictionary
+{
+ NSMutableDictionary *dict = (NSMutableDictionary *)[super attributesAsDictionary];
+ if (source != nil)
+ [dict setObject:source forKey:@"source"];
+ if (image != nil && source == nil) {
+ NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0];
+ [dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"];
+ }
+ [dict setObject:caption forKey:@"caption"];
+
+ return dict;
+}
+
@end
@implementation TKPostVideo
View
13 TKTumblr.m
@@ -97,22 +97,27 @@ - (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain
[postDict addEntriesFromDictionary:[thePost attributesAsDictionary]];
- NSString *postString = [postDict multipartMIMEString];
+ NSData *postBody = [postDict multipartMIMEData];
NSMutableURLRequest *theURLRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.tumblr.com/api/write"]];
[theURLRequest setHTTPMethod:@"POST"];
[theURLRequest setValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
- [theURLRequest setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-Length"];
+ [theURLRequest setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];
[theURLRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", [NSString MIMEBoundary]] forHTTPHeaderField:@"Content-Type"];
- [theURLRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
+ [theURLRequest setHTTPBody:postBody];
+
+
+ if (delegate && [delegate respondsToSelector:@selector(tumblrWillUploadPost:withDomain:)]) {
+ [delegate tumblrWillUploadPost:thePost withDomain:theDomain];
+ }
NSError *error = nil;
NSHTTPURLResponse *theURLResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theURLRequest
returningResponse:&theURLResponse
error:&error];
- // Release the request before we can enter some potentially danger
+ // Release the request before we can enter some potentially dangerous
// code path.
[theURLRequest release];

0 comments on commit f8cfc9e

Please sign in to comment.