Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
@@ -27,6 +27,6 @@
27 27 */
28 28 @interface NSDictionary (TumblrKit)
29 29
30   -- (NSString *)multipartMIMEString;
  30 +- (NSData *)multipartMIMEData;
31 31
32 32 @end
18 NSDictionary+TumblrKit.m
@@ -25,10 +25,10 @@
25 25
26 26 @implementation NSDictionary (TumblrKit)
27 27
28   -- (NSString *)multipartMIMEString
  28 +- (NSData *)multipartMIMEData
29 29 {
30 30 NSString *format = @"--%@\nContent-Disposition: form-data; name=\"%@\"\n\n%@\n";
31   - NSMutableString *result = [NSMutableString string];
  31 + NSMutableData *result = [NSMutableData data];
32 32 NSMutableDictionary *dict_ = [self mutableCopy];
33 33
34 34 // Hack: it seems the order we send the data in the HTTP post body matter
@@ -42,13 +42,21 @@ - (NSString *)multipartMIMEString
42 42
43 43 id value = [dict_ objectForKey:@"type"];
44 44 [dict_ removeObjectForKey:@"type"];
45   - [result appendFormat:format, [NSString MIMEBoundary], @"type", value];
  45 + [result appendData:[[NSString stringWithFormat:format, [NSString MIMEBoundary], @"type", value] dataUsingEncoding:NSUTF8StringEncoding]];
46 46
47 47 for (NSString *key in dict_) {
48   - [result appendFormat:format, [NSString MIMEBoundary], key, [dict_ objectForKey:key]];
  48 + if ([key isEqualToString:@"data"]) {
  49 + [result appendData:[[NSString stringWithFormat:@"--%@\nContent-Disposition: form-data; name=\"%@\"\n\n", [NSString MIMEBoundary], key] dataUsingEncoding:NSUTF8StringEncoding]];
  50 + [result appendData:[dict_ objectForKey:key]];
  51 + [result appendData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
  52 + }
  53 + else {
  54 + [result appendData:[[NSString stringWithFormat:format, [NSString MIMEBoundary], key, [dict_ objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
  55 + }
49 56 }
50 57
51   - [result appendFormat:@"\n--%@--\n", [NSString MIMEBoundary]];
  58 + [result appendData:[[NSString stringWithFormat:@"\n--%@--\n", [NSString MIMEBoundary]] dataUsingEncoding:NSUTF8StringEncoding]];
  59 +
52 60 return result;
53 61 }
54 62
4 TKPost.h
@@ -194,12 +194,16 @@ typedef enum
194 194 @interface TKPostPhoto : TKPost
195 195 {
196 196 NSMutableString *caption;
  197 + NSString *source;
  198 + NSImage *image;
197 199 NSUInteger width;
198 200 NSUInteger height;
199 201 }
200 202
201 203 @property (assign) NSUInteger width;
202 204 @property (assign) NSUInteger height;
  205 +@property (copy) NSString *source;
  206 +@property (retain) NSImage *image;
203 207
204 208 - (NSString *)caption;
205 209 - (void)setCaption:(NSString *)aCaption;
20 TKPost.m
@@ -413,13 +413,15 @@ - (NSDictionary *)attributesAsDictionary
413 413
414 414 @implementation TKPostPhoto
415 415
416   -@synthesize width, height;
  416 +@synthesize width, height, source, image;
417 417
418 418 - (id)init
419 419 {
420 420 if ((self = [super init]) != nil) {
421 421 type = TKPostTypePhoto;
422 422 caption = [[NSMutableString alloc] init];
  423 + image = nil;
  424 + source = nil;
423 425 width = 0;
424 426 height = 0;
425 427 }
@@ -439,7 +441,9 @@ - (id)initWithAttributes:(NSDictionary *)attributeDict
439 441
440 442 - (void)dealloc
441 443 {
  444 + [source release];
442 445 [caption release];
  446 + [image release];
443 447 [super dealloc];
444 448 }
445 449
@@ -466,6 +470,20 @@ - (NSString *)description
466 470 return [NSString stringWithFormat:@"%@, Photo Caption: %@, Photo Width: %i, Photo Height: %i", [super description], self.caption, self.width, self.height];
467 471 }
468 472
  473 +- (NSDictionary *)attributesAsDictionary
  474 +{
  475 + NSMutableDictionary *dict = (NSMutableDictionary *)[super attributesAsDictionary];
  476 + if (source != nil)
  477 + [dict setObject:source forKey:@"source"];
  478 + if (image != nil && source == nil) {
  479 + NSBitmapImageRep *bitmap = [[image representations] objectAtIndex:0];
  480 + [dict setObject:[bitmap representationUsingType:NSJPEGFileType properties:nil] forKey:@"data"];
  481 + }
  482 + [dict setObject:caption forKey:@"caption"];
  483 +
  484 + return dict;
  485 +}
  486 +
469 487 @end
470 488
471 489 @implementation TKPostVideo
13 TKTumblr.m
@@ -97,14 +97,19 @@ - (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain
97 97
98 98 [postDict addEntriesFromDictionary:[thePost attributesAsDictionary]];
99 99
100   - NSString *postString = [postDict multipartMIMEString];
  100 + NSData *postBody = [postDict multipartMIMEData];
101 101 NSMutableURLRequest *theURLRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.tumblr.com/api/write"]];
102 102
103 103 [theURLRequest setHTTPMethod:@"POST"];
104 104 [theURLRequest setValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
105   - [theURLRequest setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-Length"];
  105 + [theURLRequest setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];
106 106 [theURLRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", [NSString MIMEBoundary]] forHTTPHeaderField:@"Content-Type"];
107   - [theURLRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
  107 + [theURLRequest setHTTPBody:postBody];
  108 +
  109 +
  110 + if (delegate && [delegate respondsToSelector:@selector(tumblrWillUploadPost:withDomain:)]) {
  111 + [delegate tumblrWillUploadPost:thePost withDomain:theDomain];
  112 + }
108 113
109 114 NSError *error = nil;
110 115 NSHTTPURLResponse *theURLResponse = nil;
@@ -112,7 +117,7 @@ - (BOOL)uploadPost:(TKPost *)thePost withDomain:(NSString *)theDomain
112 117 returningResponse:&theURLResponse
113 118 error:&error];
114 119
115   - // Release the request before we can enter some potentially danger
  120 + // Release the request before we can enter some potentially dangerous
116 121 // code path.
117 122 [theURLRequest release];
118 123

0 comments on commit f8cfc9e

Please sign in to comment.
Something went wrong with that request. Please try again.