-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSNData.m
executable file
·82 lines (57 loc) · 2.18 KB
/
FSNData.m
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
//
// FSNData.m
// FSN
//
// Created by George King on 10/11/11.
// Copyright 2011-2012 Foursquare Labs, Inc. All rights reserved.
// Permission to use this file is granted in FSNetworking/license.txt (apache v2).
//
#import "FSN_categories.h"
#import "FSNData.h"
NSString *stringForMimeType(MimeType type) {
switch (type) {
case MimeType_image_jpeg: return @"image/jpeg";
case MimeType_application_json: return @"application/json";
case MimeType_unknown: return nil;
default:
NSLog(@"ERROR: FSNData: unknown MimeType: %d", type);
// do not return "application/octet-stream"; instead, let the recipient guess
// http://en.wikipedia.org/wiki/Internet_media_type
return nil;
}
}
@implementation FSNData
- (id)description {
NSString *fn = self.fileName ? [NSString stringWithFormat:@"; fileName=%@", self.fileName] : @"";
return [NSString stringWithFormat:@"<FSNData: %p; data: %p; length: %u; mimeType: %@%@>",
self, _data, _data.length, self.mimeTypeString, fn];
}
- (id)initWithData:(NSData *)data mimeType:(MimeType)mimeType fileName:(NSString*)fileName {
self = [super init];
if (!self) return nil;
self.data = data;
self.mimeType = mimeType;
self.fileName = fileName;
return self;
}
+ (id)withData:(NSData *)data mimeType:(MimeType)mimeType fileName:(NSString*)fileName {
return [[self alloc] initWithData:data mimeType:mimeType fileName:fileName];
}
+ (id)withDataPath:(NSString*)path fileName:(NSString*)fileName {
NSError *e = nil;
NSData *d = [NSData dataWithContentsOfFile:path options:0 error:&e];
if (e) {
FSNLog(@"ERROR: failed to read from data path: %@", path);
return nil;
}
return [self withData:d mimeType:MimeType_unknown fileName:fileName];
}
#if TARGET_OS_IPHONE
+ (id)withImage:(UIImage*)image jpegQuality:(CGFloat)quality fileName:(NSString*)fileName {
return [self withData:UIImageJPEGRepresentation(image, quality) mimeType:MimeType_image_jpeg fileName:fileName];
}
#endif
- (NSString *)mimeTypeString {
return stringForMimeType(self.mimeType);
}
@end