mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-05 09:39:09 +08:00
Added optional MIME Type auto-detection using MobileCoreServices
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// Copyright 2009 Two Toasters. All rights reserved.
|
||||
//
|
||||
|
||||
#import <MobileCoreServices/UTType.h>
|
||||
#import "RKParamsAttachment.h"
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,10 @@
|
||||
*/
|
||||
extern NSString* const kRKStringBoundary;
|
||||
|
||||
@interface RKParamsAttachment (Private)
|
||||
- (NSString *)mimeTypeForExtension:(NSString *)extension;
|
||||
@end
|
||||
|
||||
@implementation RKParamsAttachment
|
||||
|
||||
@synthesize fileName = _fileName, MIMEType = _MIMEType, name = _name;
|
||||
@@ -51,17 +56,16 @@ extern NSString* const kRKStringBoundary;
|
||||
}
|
||||
|
||||
- (id)initWithName:(NSString*)name file:(NSString*)filePath {
|
||||
if (self = [self initWithName:name]) {
|
||||
_fileName = [filePath lastPathComponent];
|
||||
_MIMEType = @"application/octet-stream"; // TODO: Add MIME type auto-detection!
|
||||
// TODO: [self mimeTypeForExtension:[path pathExtension] -> default the MIMEType
|
||||
_bodyStream = [[NSInputStream inputStreamWithFileAtPath:filePath] retain];
|
||||
|
||||
if (self = [self initWithName:name]) {
|
||||
NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:filePath], @"Expected file to exist at path: %@", filePath);
|
||||
_fileName = [filePath lastPathComponent];
|
||||
_MIMEType = [self mimeTypeForExtension:[filePath pathExtension]];
|
||||
_bodyStream = [[NSInputStream inputStreamWithFileAtPath:filePath] retain];
|
||||
|
||||
NSError* error = nil;
|
||||
_bodyLength = [[[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] objectForKey:NSFileSize] unsignedIntegerValue];
|
||||
if (error) {
|
||||
NSLog(@"Encountered an error while determining file size: %@");
|
||||
NSLog(@"Encountered an error while determining file size: %@", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +90,25 @@ 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 {
|
||||
// Generate the MIME header for this part
|
||||
if (self.fileName && self.MIMEType) {
|
||||
|
||||
@@ -58,7 +58,12 @@ To add RestKit to your project (you're using git, right?):
|
||||
|
||||
1. Add the submodule: `git submodule add git://github.com/twotoasters/RestKit.git RestKit`
|
||||
1. Open RestKit.xcodeproj and drag the RestKit project file into your XCode project.
|
||||
1. Click on the entry for RestKit.xcodeproj in your project's **Groups & Files** section. In the right hand pane, find the entries for **libRestKitSupport.a** **libRestKitObjectMapping.a** **libRestKitNetwork.a** and **libRestKitJSONParserYAJL.a** and click the checkboxes on the far right underneath the silver target icon. This will link your project against RestKit. If you wish to use the Core Data support, click the checkbox next to **libRestKitCoreData.a** also (this requires that you add Core Data to the Linked Libraries section at the bottom of the screen as well).
|
||||
1. Click on the entry for RestKit.xcodeproj in your project's **Groups & Files** section. In the right hand pane, find the entries for **libRestKitSupport.a** **libRestKitObjectMapping.a** **libRestKitNetwork.a** and **libRestKitJSONParserYAJL.a** and click the checkboxes on the far right underneath the silver target icon. This will link your project against RestKit. If you wish to use the Core Data support, click the checkbox next to **libRestKitCoreData.a** also.
|
||||
1. Look to the bottom of the **General** pane labeled **Linked Libraries**. You will need to add the following frameworks:
|
||||
* **CFNetwork.framework** - Required for networking support.
|
||||
* **SystemConfiguration.framework** - Required for detection of network availability.
|
||||
* **MobileCoreServices.framework** - Optional. Provides support for MIME type auto-detection for uploaded files.
|
||||
* **CoreData.framework** - Optional. Required for use of the Core Data backed persistent object store.
|
||||
1. Get Info on your target and you should be looking at the **General** tag. In the top **Direct Dependencies** section, click the plus button and add a direct dependency on the RestKit target.
|
||||
1. Switch to the 'Build' tab in your project inspector. Make sure that your **Configuration** pop-up menu reads **All Configurations** so that your changes will work for all build configurations.
|
||||
1. Find the **Header Search Paths** setting. Double click and add a new entry. When RestKit is compiled, it will copy all relevant headers to the appropriate location under the /Build directory within the RestKit checkout. You need to add a path to the /Build directory of RestKit, relative to your project file. For example, if you checked the submodule out to the 'Libraries' subdirectory of your project, your header path would be 'Libraries/RestKit/Build'.
|
||||
|
||||
Reference in New Issue
Block a user