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

View File

@@ -18,13 +18,11 @@
// limitations under the License.
//
#if TARGET_OS_IPHONE
#import <MobileCoreServices/UTType.h>
#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 {

View File

@@ -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

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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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