Implemented MIMETypeForPathExtension for NSURL and NSString. refs #409

Refactored duplicated code for returning the MIME Type based on file path extension using
Core Services UTI.
This commit is contained in:
Blake Watters
2011-10-16 02:04:00 -04:00
parent 0e08147d5b
commit 585d4aafc8
8 changed files with 64 additions and 45 deletions

View File

@@ -18,6 +18,11 @@
// limitations under the License.
//
#if TARGET_OS_MAC
#import <CoreServices/CoreServices.h>
#elif TARGET_OS_IPHONE
#import <MobileCoreServices/UTType.h>
#endif
#import "NSString+RestKit.h"
#import "../Network/RKClient.h"
#import "RKFixCategoryBug.h"
@@ -112,4 +117,19 @@ RK_FIX_CATEGORY_BUG(NSString_RestKit)
return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
- (NSString *)MIMETypeForPathExtension {
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[self pathExtension], NULL);
if (uti != NULL) {
CFStringRef mime = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
CFRelease(uti);
if (mime != NULL) {
NSString *type = [NSString stringWithString:(NSString *)mime];
CFRelease(mime);
return type;
}
}
return nil;
}
@end