diff --git a/Code/CoreData/RKManagedObjectSeeder.m b/Code/CoreData/RKManagedObjectSeeder.m index 49f909eb..a54296ea 100644 --- a/Code/CoreData/RKManagedObjectSeeder.m +++ b/Code/CoreData/RKManagedObjectSeeder.m @@ -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 diff --git a/Code/Network/RKParamsAttachment.m b/Code/Network/RKParamsAttachment.m index 5a977c55..64df8d3a 100644 --- a/Code/Network/RKParamsAttachment.m +++ b/Code/Network/RKParamsAttachment.m @@ -18,13 +18,11 @@ // limitations under the License. // -#if TARGET_OS_IPHONE -#import -#endif #import "RKParamsAttachment.h" #import "RKLog.h" #import "NSData+MD5.h" #import "FileMD5Hash.h" +#import "../Support/NSString+RestKit.h" // Set Logging Component #undef RKLogComponent @@ -35,10 +33,6 @@ */ extern NSString* const kRKStringBoundary; -@interface RKParamsAttachment (Private) -- (NSString *)mimeTypeForExtension:(NSString *)extension; -@end - @implementation RKParamsAttachment @synthesize filePath = _filePath; @@ -88,7 +82,9 @@ extern NSString* const kRKStringBoundary; NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:filePath], @"Expected file to exist at path: %@", filePath); _filePath = [filePath retain]; _fileName = [[filePath lastPathComponent] retain]; - _MIMEType = [[self mimeTypeForExtension:[filePath pathExtension]] retain]; + NSString *MIMEType = [filePath MIMETypeForPathExtension]; + if (! MIMEType) MIMEType = @"application/octet-stream"; + _MIMEType = [MIMEType retain]; _bodyStream = [[NSInputStream alloc] initWithFileAtPath:filePath]; NSError* error; @@ -125,23 +121,6 @@ extern NSString* const kRKStringBoundary; return kRKStringBoundary; } -- (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 @"application/octet-stream"; -} - #pragma mark NSStream methods - (void)open { diff --git a/Code/Support/NSString+RestKit.h b/Code/Support/NSString+RestKit.h index dc9ff398..6e7b5c18 100644 --- a/Code/Support/NSString+RestKit.h +++ b/Code/Support/NSString+RestKit.h @@ -106,4 +106,15 @@ */ - (NSString *)stringByReplacingURLEncoding; +/** + Interprets the receiver as a path and returns the MIME Type for the path extension + using Core Services. + + For example, given a string with the path /Users/blake/Documents/monkey.json we would get + @"application/json" as the MIME Type. + + @return The expected MIME Type of the resource identified by the path or nil if unknown + */ +- (NSString *)MIMETypeForPathExtension; + @end diff --git a/Code/Support/NSString+RestKit.m b/Code/Support/NSString+RestKit.m index 44dfe697..6f44dfb3 100644 --- a/Code/Support/NSString+RestKit.m +++ b/Code/Support/NSString+RestKit.m @@ -18,6 +18,11 @@ // limitations under the License. // +#if TARGET_OS_MAC +#import +#elif TARGET_OS_IPHONE +#import +#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 diff --git a/Code/Support/NSURL+RestKit.h b/Code/Support/NSURL+RestKit.h index 6f00d27b..3551849c 100644 --- a/Code/Support/NSURL+RestKit.h +++ b/Code/Support/NSURL+RestKit.h @@ -27,4 +27,15 @@ */ - (NSDictionary *)queryDictionary; +/** + Returns the MIME Type for the resource identified by the URL by interpretting the + path extension using Core Services. + + For example, given a URL to http://restkit.org/monkey.json we would get + @"application/json" as the MIME Type. + + @return The expected MIME Type of the resource identified by the URL or nil if unknown + */ +- (NSString *)MIMETypeForPathExtension; + @end diff --git a/Code/Support/NSURL+RestKit.m b/Code/Support/NSURL+RestKit.m index f28f883b..80ce2b38 100644 --- a/Code/Support/NSURL+RestKit.m +++ b/Code/Support/NSURL+RestKit.m @@ -21,6 +21,7 @@ #import "NSURL+RestKit.h" #import "NSDictionary+RKAdditions.h" #import "RKFixCategoryBug.h" +#import "NSString+RestKit.h" RK_FIX_CATEGORY_BUG(NSURL_RestKit) @@ -30,4 +31,8 @@ RK_FIX_CATEGORY_BUG(NSURL_RestKit) return [NSDictionary dictionaryWithURLEncodedString:self.query]; } +- (NSString *)MIMETypeForPathExtension { + return [[self path] MIMETypeForPathExtension]; +} + @end diff --git a/Specs/Network/RKURLSpec.m b/Specs/Network/RKURLSpec.m index 7c323998..54566d34 100644 --- a/Specs/Network/RKURLSpec.m +++ b/Specs/Network/RKURLSpec.m @@ -3,7 +3,7 @@ // RestKit // // Created by Blake Watters on 6/29/11. -// Copyright 2011 Two Toasters +// Copyright 2011 RestKit // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ #import "RKSpecEnvironment.h" #import "RKURL.h" +#import "NSURL+RestKit.h" @interface RKURLSpec : RKSpec @end @@ -80,5 +81,10 @@ assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test/"))); } +- (void)itShouldReturnTheMIMETypeForURL { + NSURL *URL = [NSURL URLWithString:@"http://restkit.org/path/to/resource.xml"]; + assertThat([URL MIMETypeForPathExtension], is(equalTo(@"application/xml"))); +} + @end \ No newline at end of file diff --git a/Specs/Support/NSStringRestKitSpec.m b/Specs/Support/NSStringRestKitSpec.m index a8c49c23..fca9e3c7 100755 --- a/Specs/Support/NSStringRestKitSpec.m +++ b/Specs/Support/NSStringRestKitSpec.m @@ -70,4 +70,9 @@ assertThat(queryParams, hasEntries(@"keyA", @"valA", @"keyB", @"valB", nil)); } +- (void)itShouldReturnTheMIMETypeForAPath { + NSString *MIMEType = [@"/path/to/file.xml" MIMETypeForPathExtension]; + assertThat(MIMEType, is(equalTo(@"application/xml"))); +} + @end