-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDCHTTPUpload.m
More file actions
75 lines (71 loc) · 2.51 KB
/
DCHTTPUpload.m
File metadata and controls
75 lines (71 loc) · 2.51 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
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DCHTTPUpload.m
//
// Created by Dalton Cherry on 5/7/14.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#import "DCHTTPUpload.h"
@implementation DCHTTPUpload
////////////////////////////////////////////////////////////////////////////////////////////////////
static inline NSString * DCContentTypeForPathExtension(NSString *extension) {
#ifdef __UTTYPE__
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
if (!contentType) {
return @"application/octet-stream";
} else {
return contentType;
}
#else
#pragma unused (extension)
return @"application/octet-stream";
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(NSString*)mimeType
{
if(!_mimeType)
{
if(self.fileURL)
_mimeType = DCContentTypeForPathExtension([self.fileURL pathExtension]);
else
_mimeType = @"application/octet-stream";
}
return _mimeType;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(NSString*)fileName
{
if(!_fileName)
{
if(self.fileURL)
_fileName = [self.fileURL lastPathComponent];
}
return _fileName;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-(NSData*)getData
{
if(self.fileURL)
return [NSData dataWithContentsOfURL:self.fileURL];
return self.data;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(DCHTTPUpload*)uploadWithFile:(NSURL*)fileURL
{
DCHTTPUpload *upload = [DCHTTPUpload new];
upload.fileURL = fileURL;
return upload;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+(DCHTTPUpload*)uploadWithData:(NSData*)data fileName:(NSString*)fileName mimeType:(NSString*)type
{
DCHTTPUpload *upload = [DCHTTPUpload new];
upload.data = data;
upload.fileName = fileName;
upload.mimeType = type;
return upload;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@end