Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Support for including files to post with TTURLRequest (useful for u…

…ploading video *wink*wink*)
  • Loading branch information...
commit b9f99c076449ff1e510f7cb565a1c523c54166bc 1 parent 76aaabe
@joehewitt joehewitt authored
View
2  src/TTPhotoViewController.m
@@ -3,7 +3,6 @@
#import "Three20/TTURLRequest.h"
#import "Three20/TTUnclippedView.h"
#import "Three20/TTPhotoView.h"
-#import "Three20/TTURLRequestQueue.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -52,7 +51,6 @@ - (id)init {
}
- (void)dealloc {
- [[TTURLRequestQueue mainQueue] cancelRequestsWithDelegate:self];
[_thumbsController release];
[_slideshowTimer invalidate];
_slideshowTimer = nil;
View
42 src/TTURLRequest.m
@@ -43,6 +43,7 @@ - (id)init {
_parameters = nil;
_contentType = nil;
_delegates = TTCreateNonRetainingArray();
+ _files = nil;
_response = nil;
_cachePolicy = TTURLRequestCachePolicyAny;
_cacheExpirationAge = 0;
@@ -63,6 +64,7 @@ - (void)dealloc {
[_parameters release];
[_contentType release];
[_delegates release];
+ [_files release];
[_response release];
[_timestamp release];
[_cacheKey release];
@@ -115,9 +117,8 @@ - (NSData*)generatePostBody {
dataUsingEncoding:NSUTF8StringEncoding]];
for (id key in [_parameters keyEnumerator]) {
- if (![[_parameters objectForKey:key] isKindOfClass:[UIImage class]]) {
- NSString* value = [_parameters valueForKey:key];
-
+ NSString* value = [_parameters valueForKey:key];
+ if (![value isKindOfClass:[UIImage class]]) {
[body appendData:[[NSString
stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]
dataUsingEncoding:NSUTF8StringEncoding]];
@@ -131,24 +132,39 @@ - (NSData*)generatePostBody {
if ([[_parameters objectForKey:key] isKindOfClass:[UIImage class]]) {
UIImage* image = [_parameters objectForKey:key];
CGFloat quality = [TTURLRequestQueue mainQueue].imageCompressionQuality;
- NSData* imageData = UIImageJPEGRepresentation(image, quality);
+ NSData* data = UIImageJPEGRepresentation(image, quality);
[body appendData:[[NSString
- stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"photo\"\r\n", key]
+ stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image\"\r\n", key]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString
- stringWithFormat:@"Content-Length: %d\r\n", imageData.length]
+ stringWithFormat:@"Content-Length: %d\r\n", data.length]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString
stringWithString:@"Content-Type: image/jpeg\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:imageData];
+ [body appendData:data];
[body appendData:[endLine dataUsingEncoding:NSUTF8StringEncoding]];
-// [imageData release];
imageKey = key;
}
}
+ for (NSInteger i = 0; i < _files.count; i += 3) {
+ NSData* data = [_files objectAtIndex:i];
+ NSString* mimeType = [_files objectAtIndex:i+1];
+ NSString* fileName = [_files objectAtIndex:i+2];
+
+ [body appendData:[[NSString stringWithFormat:
+ @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fileName, fileName]
+ dataUsingEncoding:NSUTF8StringEncoding]];
+ [body appendData:[[NSString stringWithFormat:@"Content-Length: %d\r\n", data.length]
+ dataUsingEncoding:NSUTF8StringEncoding]];
+ [body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimeType]
+ dataUsingEncoding:NSUTF8StringEncoding]];
+ [body appendData:data];
+ [body appendData:[endLine dataUsingEncoding:NSUTF8StringEncoding]];
+ }
+
// If an image was found, remove it from the dictionary to save memory while we
// perform the upload
if (imageKey) {
@@ -195,6 +211,16 @@ - (NSString*)cacheKey {
return _cacheKey;
}
+- (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)fileName {
+ if (!_files) {
+ _files = [[NSMutableArray alloc] init];
+ }
+
+ [_files addObject:data];
+ [_files addObject:mimeType];
+ [_files addObject:fileName];
+}
+
- (BOOL)send {
if (_parameters) {
TTLOG(@"SEND %@ %@", self.URL, self.parameters);
View
6 src/Three20/TTURLRequest.h
@@ -9,6 +9,7 @@
NSMutableDictionary* _parameters;
NSString* _contentType;
NSMutableArray* _delegates;
+ NSMutableArray* _files;
id<TTURLResponse> _response;
TTURLRequestCachePolicy _cachePolicy;
NSTimeInterval _cacheExpirationAge;
@@ -84,6 +85,11 @@
- (id)initWithURL:(NSString*)URL delegate:(id<TTURLRequestDelegate>)delegate;
/**
+ * Adds a file whose data will be posted.
+ */
+- (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)fileName;
+
+/**
* Attempts to send a request.
*
* If the request can be resolved by the cache, it will happen synchronously. Otherwise,
Please sign in to comment.
Something went wrong with that request. Please try again.