Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Photo uploading (from local device) is now working.

Photo with source URL is work in progress.
  • Loading branch information...
commit f8cfc9e40224c7134fc9be9b466cb9a0daa7aef9 1 parent 4ec4a2d
Igor Sutton authored
2  NSDictionary+TumblrKit.h
View
@@ -27,6 +27,6 @@
*/
@interface NSDictionary (TumblrKit)
-- (NSString *)multipartMIMEString;
+- (NSData *)multipartMIMEData;
@end
18 NSDictionary+TumblrKit.m
View
@@ -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;
}
4 TKPost.h
View
@@ -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;
20 TKPost.m
View
@@ -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
13 TKTumblr.m
View
@@ -97,14 +97,19 @@ - (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;
@@ -112,7 +117,7 @@ - (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain
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];
Please sign in to comment.
Something went wrong with that request. Please try again.