Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-memory mime-type detection with Cocoa (OS X)?

My application is a viewer for a custom format, a zip file with a well defined XML manifest and resources, such as images and movies. I use zlib to open up the zip file in memory and then proceed to display said resources.

One problem I've ran into is that I'm unable to properly display videos, apparently because QTMovie cannot determine the mime-type. Movie loaded from file ([QTMovie movieWithFile]) works perfectly. Loaded from memory ([QTMovie movieWithData]) refuses to work.

This makes sense, because lacking the file extension, QTMovie cannot determine the mime-type information. After a bit of a search, I've resorted to using QTDataReference in the following mannner:

NSData *movieData = ...read from memory...;
QTDataReference *movieDataReference = [[QTDataReference alloc] initWithReferenceToData:movieData name:fileName MIMEType:@"video/x-m4v"];
QTMovie *mov = [QTMovie movieWithDataReference:movieDataReference error:&err];

This works nicely, however hardcoding MIMEType is far from ideal. I have access to the filename and the extension, so I've attempted to find the mime-type using UTI (thanks to the nice folks on #macdev):

- (NSString*)mimeTypeForExtension:(NSString*)ext {

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)ext,NULL);
    return NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)UTI,kUTTagClassMIMEType));
}

This however doesn't work. Clearly, there's an internal OS X database of extensions and corresponding mime-types, somewhere. Otherwise from-disk movies wouldn't work. How do I get access to it?

Thanks!

like image 329
EightyEight Avatar asked Dec 07 '22 08:12

EightyEight


1 Answers

The problem your having is that m4v and m4p dont have a mime types registered with Launch Services (probably because the mime type for m4v and m4p is not standard). In any event, what you should probably do is handle edge cases like this and then check for nil when the function returns (in case the extension is both not registered and not handled by you).

The other thing is that you're leaking memory with your current use. I'm assuming you're using garbage collection, but the first call creates a CFString that is never released. Here is a suggested implementation of your method:

-(NSString*)mimeTypeForExtension:(NSString*)ext
{
    NSAssert( ext, @"Extension cannot be nil" );
    NSString* mimeType = nil;

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
        (CFStringRef)ext, NULL);
    if( !UTI ) return nil;

    CFStringRef registeredType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
    if( !registeredType ) // check for edge case
    {
        if( [ext isEqualToString:@"m4v"] )
            mimeType = @"video/x-m4v";
        else if( [ext isEqualToString:@"m4p"] )
            mimeType = @"audio/x-m4p";
        // handle anything else here that you know is not registered
    } else {
        mimeType = NSMakeCollectable(registeredType);
    }

    CFRelease(UTI);
    return mimeType;
}
like image 73
Jason Coco Avatar answered Jan 11 '23 08:01

Jason Coco