Skip to content

Commit bdc446d

Browse files
committed
Add option to define content type when uploading.
Thanks to @locandy for this feature. Merge remote-tracking branch 'locandy/master' into develop Conflicts: README.md
2 parents 0fabd9f + 4eda999 commit bdc446d

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

README.md

100644100755
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ NSURLRequest *sigendRequest = [theRequest signedURLRequest];
188188
// Invoke the request with you preferd method
189189
</pre>
190190

191+
#### Fully Parametrized File Upload
192+
193+
<pre>
194+
// Get fileSize
195+
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:uploadFile.localDataFilePath error:nil];
196+
NSNumber *fileSize = [fileAttributes valueForKey:NSFileSize];
197+
198+
// Create a stream wrapper for the upload file
199+
NXOAuth2FileStreamWrapper* w =[NXOAuth2FileStreamWrapper
200+
wrapperWithStream:[NSInputStream inputStreamWithFileAtPath:uploadFile.localDataFilePath]
201+
contentLength:[fileSize unsignedLongLongValue]
202+
fileName:uploadFile.remoteFilename];
203+
204+
if([uploadFile.remoteFilename hasSuffix:@".json"])
205+
w.contentType = @"application/json";
206+
else
207+
if([uploadFile.remoteFilename hasSuffix:@".jpg"])
208+
w.contentType = @"image/jpeg";
209+
210+
// POST Content-Disposition: form-data; name="file"; filename=uploadFile.remoteFilename
211+
[NXOAuth2Request performMethod:@"POST" onResource:uploadFile.uploadURL usingParameters:@{@"file": w} withAccount:account
212+
sendProgressHandler:... responseHandler: ...];
213+
</pre>
214+
215+
191216
## Contributing & Pull Requests
192217

193218
Patches and pull requests are welcome! We are sorry if it takes a while to review them, but sooner or later we will get to yours.

Sources/OAuth2Client/NXOAuth2FileStreamWrapper.h

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
NSInputStream *stream;
1919
unsigned long long contentLength;
2020
NSString *fileName;
21+
NSString *contentType;
2122
}
2223
@property (nonatomic, strong, readonly) NSInputStream *stream;
2324
@property (nonatomic, assign, readonly) unsigned long long contentLength;
2425
@property (nonatomic, copy, readonly) NSString *fileName;
26+
@property (nonatomic, copy) NSString *contentType; // optional DEFAULT: "application/octettstream"
2527

2628
+ (id)wrapperWithStream:(NSInputStream *)stream contentLength:(unsigned long long)contentLength DEPRECATED_ATTRIBUTE;
2729
- (id)initWithStream:(NSInputStream *)stream contentLength:(unsigned long long)contentLength DEPRECATED_ATTRIBUTE;

Sources/OAuth2Client/NXOAuth2FileStreamWrapper.m

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ - (id)initWithStream:(NSInputStream *)aStream contentLength:(unsigned long long)
5151
stream = aStream;
5252
contentLength = aContentLength;
5353
fileName = [aFileName copy];
54+
contentType = @"application/octet-stream"; // DEFAULT if not assigned by property
5455
}
5556
return self;
5657
}
5758

5859

5960
#pragma mark Accessors
6061

61-
@synthesize stream, contentLength, fileName;
62+
@synthesize stream, contentLength, fileName, contentType;
6263

6364

6465
@end

Sources/OAuth2Client/NXOAuth2PostBodyPart.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (id)initWithName:(NSString *)name content:(id)content;
4343
} else if ([content isKindOfClass:[NSData class]]) {
4444
return [self initWithName:name dataContent:content];
4545
} else if ([content isKindOfClass:[NXOAuth2FileStreamWrapper class]]) {
46-
return [self initWithName:name streamContent:[content stream] streamLength:[content contentLength] fileName:[content fileName]];
46+
return [self initWithName:name streamContent:[content stream] streamLength:[content contentLength] fileName:[content fileName] contentType:[content contentType]];
4747
} else {
4848
NSAssert1(NO, @"NXOAuth2PostBodyPart with illegal type:\n%@", [content class]);
4949
return nil;
@@ -60,6 +60,16 @@ - (id)initWithName:(NSString *)name streamContent:(NSInputStream *)stream stream
6060
return [self initWithHeaders:headers streamContent:stream length:streamLength];
6161
}
6262

63+
- (id)initWithName:(NSString *)name streamContent:(NSInputStream *)stream streamLength:(unsigned long long)streamLength fileName:(NSString *)fileName contentType:(NSString *)contentType;
64+
{
65+
NSMutableString *headers = [NSMutableString string];
66+
[headers appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, fileName];
67+
[headers appendString:@"Content-Transfer-Encoding: binary\r\n"];
68+
[headers appendFormat:@"Content-Type: %@\r\n", contentType];
69+
[headers appendString:@"\r\n"];
70+
return [self initWithHeaders:headers streamContent:stream length:streamLength];
71+
}
72+
6373
- (id)initWithName:(NSString *)name dataContent:(NSData *)data;
6474
{
6575
NSMutableString *headers = [NSMutableString string];

0 commit comments

Comments
 (0)