From b3690a585df8bf65942022b7dd6f7e9269f8cfbb Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 17 Jan 2011 21:47:00 +0000 Subject: [PATCH] Using OS services to determine MIME types svn path=/trunk/netsurf/; revision=11358 --- cocoa/fetch.m | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/cocoa/fetch.m b/cocoa/fetch.m index 7ad862b8b..385826f1d 100644 --- a/cocoa/fetch.m +++ b/cocoa/fetch.m @@ -1,5 +1,5 @@ /* - * Copyright 2003 James Bursa + * Copyright 2011 Sven Weidauer * * This file is part of NetSurf, http://www.netsurf-browser.org/ * @@ -16,42 +16,33 @@ * along with this program. If not, see . */ -#include -#include -#include "content/fetch.h" -#include "utils/log.h" -#include "utils/utils.h" +#import -/** - * filetype -- determine the MIME type of a local file - */ +#import "utils/log.h" const char *fetch_filetype(const char *unix_path) { - int l; - LOG(("unix path %s", unix_path)); - l = strlen(unix_path); - if (2 < l && strcasecmp(unix_path + l - 3, "css") == 0) - return "text/css"; - if (2 < l && strcasecmp(unix_path + l - 3, "jpg") == 0) - return "image/jpeg"; - if (3 < l && strcasecmp(unix_path + l - 4, "jpeg") == 0) - return "image/jpeg"; - if (2 < l && strcasecmp(unix_path + l - 3, "gif") == 0) - return "image/gif"; - if (2 < l && strcasecmp(unix_path + l - 3, "png") == 0) - return "image/png"; - if (2 < l && strcasecmp(unix_path + l - 3, "jng") == 0) - return "image/jng"; - if (2 < l && strcasecmp(unix_path + l - 3, "svg") == 0) - return "image/svg"; - if (2 < l && strcasecmp(unix_path + l - 3, "bmp") == 0) - return "image/x-ms-bmp"; - return "text/html"; + NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile: [NSString stringWithUTF8String: unix_path] + error: NULL]; + + NSString *mimeType = nil; + if (nil != uti) { + mimeType = (NSString *)UTTypeCopyPreferredTagWithClass( (CFStringRef)uti, kUTTagClassMIMEType ); + } + + const char *result = "text/html"; + if (nil != mimeType) { + result = [mimeType UTF8String]; + [mimeType release]; + } + + LOG(( "\tMIME type for '%s' is '%s'", unix_path, result )); + + return result; } char *fetch_mimetype(const char *ro_path) { - return strdup("text/plain"); + return strdup( fetch_filetype( ro_path ) ); }