Skip to content

Commit 2fb8f2e

Browse files
committed
File uploads can have a custom content-type if created as stream parameter.
1 parent 190223f commit 2fb8f2e

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ - (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
}

Sources/OAuth2Client/NXOAuth2PostBodyPart.m

Lines changed: 12 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,17 @@ - (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+
NSLog(@"%@: headers '%@'", self.class, headers);
71+
return [self initWithHeaders:headers streamContent:stream length:streamLength];
72+
}
73+
6374
- (id)initWithName:(NSString *)name dataContent:(NSData *)data;
6475
{
6576
NSMutableString *headers = [NSMutableString string];

0 commit comments

Comments
 (0)