On 2011-10-01 16:06:31 +0000, Johannes Pfau <[email protected]> said:

The freedesktop.org part is complete, the windows part will be added
soon (the public API won't change) and it seems there's no API on OSX to
get a MIME type for a file, so OSX will have to use the freedesktop.org
backend.

Mac OS X uses primarily Uniform Type Identifiers and maps all other kinds of type identifiers (extensions, MIME, HFS+ type codes) to UTIs.

<http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html#//apple_ref/doc/uid/TP40001319-CH202-CHDHIJDE>

If
you want to get the MIME type for a file, first get its UTI:

        const char * path;

        FSRef fileRef;
        FSPathMakeRef(path, &fileRef, NULL);

        CFStringRef uti = NULL;
        LSCopyItemAttribute(&fileRef, kLSRolesAll, kLSItemContentType, &uti);

then ask for the preferred MIME type for this UTI:

        CFStringRef mime = UTTypeCopyPreferredTagWithClass(uti, 
kUTTagClassMIMEType);

You might want to create a C string from that:

        size_t mime_c_str_length = CFStringLength(mime); // assuming ASCII here
        char *mime_c_str = malloc(mime_c_str_length+1);
CFStringGetCString(mime, mime_c_str, mime_c_str_length+1, kCFStringEncodingUTF8);

And once you're done, don't forget to release all those strings to avoid leaks:

        CFRelease(uti);
        CFRelease(mime);
        free(mime_c_str);

--
Michel Fortin
[email protected]
http://michelf.com/

Reply via email to