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

@@ -32,7 +32,6 @@
#define RKLogComponent lcl_cRestKitCoreData
@interface RKManagedObjectSeeder (Private)
- (NSString *)mimeTypeForExtension:(NSString *)extension;
- (id)initWithObjectManager:(RKObjectManager*)manager;
- (void)seedObjectsFromFileNames:(NSArray*)fileNames;
@end
@@ -122,7 +121,7 @@ NSString* const RKDefaultSeedDatabaseFileName = @"RKSeedDatabase.sqlite";
NSString* payload = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (payload) {
NSString* MIMEType = [self mimeTypeForExtension:[fileName pathExtension]];
NSString* MIMEType = [fileName MIMETypeForPathExtension];
if (MIMEType == nil) {
// Default the MIME type to the value of the Accept header if we couldn't detect it...
MIMEType = _manager.acceptMIMEType;
@@ -180,21 +179,4 @@ NSString* const RKDefaultSeedDatabaseFileName = @"RKSeedDatabase.sqlite";
exit(1);
}
- (NSString *)mimeTypeForExtension:(NSString *)extension {
if (NULL != UTTypeCreatePreferredIdentifierForTag) {
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)extension, 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