From d366080489546209e3b296b8f867404d5c851701 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Fri, 6 Apr 2012 00:00:43 +0300 Subject: [PATCH] Fix bug and improve method of finding file content type. --- pithos-macos/PithosUtilities.m | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/pithos-macos/PithosUtilities.m b/pithos-macos/PithosUtilities.m index af97bb2..da1a2a9 100644 --- a/pithos-macos/PithosUtilities.m +++ b/pithos-macos/PithosUtilities.m @@ -881,13 +881,34 @@ // Content type of the file or nil if it cannot be determined + (NSString *)contentTypeOfFile:(NSString *)filePath error:(NSError **)error { - NSURLResponse *response = nil; - [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath] - cachePolicy:NSURLCacheStorageNotAllowed - timeoutInterval:.1] - returningResponse:&response - error:error]; - return [response MIMEType]; + // Based on http://www.ddeville.me/2011/12/mime-to-UTI-cocoa/ + // and Apple example ImageBrowserViewAppearance/ImageBrowserController.m + LSItemInfoRecord info; + CFStringRef uti = NULL; + CFURLRef url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)filePath, kCFURLPOSIXPathStyle, FALSE); + if (LSCopyItemInfoForURL(url, kLSRequestExtension | kLSRequestTypeCreator, &info) == noErr) { + // Obtain the UTI using the file information. + // If there is a file extension, get the UTI. + if (info.extension != NULL) { + uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, info.extension, kUTTypeData); + CFRelease(info.extension); + } + // No UTI yet + if (uti == NULL) { + // If there is an OSType, get the UTI. + CFStringRef typeString = UTCreateStringForOSType(info.filetype); + if ( typeString != NULL) { + uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassOSType, typeString, kUTTypeData); + CFRelease(typeString); + } + } + if (uti != NULL) { + CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType); + CFRelease(uti); + return (NSString *)MIMEType; + } + } + return nil; } // Creates a directory if it doesn't exist and returns if successful -- 1.7.10.4