forked from eklipse2k8/CocoaRestClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRCMultipartRequest.m
More file actions
89 lines (69 loc) · 3.09 KB
/
Copy pathCRCMultipartRequest.m
File metadata and controls
89 lines (69 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// CRCMultipartRequest.m
// CocoaRestClient
//
// Created by Adam Venturella on 7/8/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CRCMultipartRequest.h"
#import "CocoaRestClientAppDelegate.h"
@implementation CRCMultipartRequest
+(void)createRequest:(NSMutableURLRequest *)request
{
CocoaRestClientAppDelegate * delegate = (CocoaRestClientAppDelegate *)[[NSApplication sharedApplication] delegate];
NSMutableData * body = [NSMutableData data];
NSString * formBoundary = [CRCMultipartRequest generateBoundary];
NSString * headerfield = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", formBoundary];
[request addValue:headerfield forHTTPHeaderField:@"Content-Type"];
if([delegate.paramsTable count] > 0)
{
for(NSDictionary * row in delegate.paramsTable)
{
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",formBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@", [row objectForKey:@"key"], [row objectForKey:@"value"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
NSInteger count = [delegate.filesTable count];
if(count > 0)
{
for(NSDictionary * row in delegate.filesTable)
{
NSError *err = nil;
NSString *uti;
NSString *mimeType;
NSURL *path = [row objectForKey:@"url"];
if([[NSFileManager defaultManager] fileExistsAtPath:[path relativePath]])
{
if (!(uti = [[NSWorkspace sharedWorkspace] typeOfFile:[path relativePath] error:&err]))
{
mimeType = @"application/octet-stream";
}
else
{
mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti, kUTTagClassMIMEType);
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", formBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [row objectForKey:@"key"],[[path relativePath] lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
// there is most certainly a smarter way to handle this
// for now, though, it's just a simple rule.
NSRange result = [mimeType rangeOfString:@"image"];
if(result.length > 0)
[body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:[path relativePath]]];
}
}
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", formBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody: body];
}
+(NSString *)generateBoundary
{
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef stringRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
NSString *uuid = [NSString stringWithString:(__bridge NSString *)stringRef];
CFRelease(stringRef);
CFRelease(uuidRef);
return uuid;
}
@end