Index: filefield.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield/filefield.module,v
retrieving revision 1.50.2.6
diff -u -r1.50.2.6 filefield.module
--- filefield.module	6 Dec 2007 23:51:26 -0000	1.50.2.6
+++ filefield.module	29 Dec 2007 22:14:53 -0000
@@ -135,6 +135,7 @@
   if (filefield_check_directory($widget_file_path) && $file = file_save_upload((object)$file, $filepath)) {
     $file = (array)$file;
     $file['fid'] = db_next_id('{files}_fid');
+    $file['filemime'] = filefield_mime($file);
     db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
              VALUES (%d, %d, "%s","%s","%s",%d)',
             $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
@@ -821,3 +822,118 @@
   print drupal_to_js(array('status' => TRUE, 'data' => $output));
   exit();
 }
+
+
+/** 
+ *  Detect File Mime Type...
+ * 
+ */
+function filefield_mime($file) {
+  $file = (object)$file;
+  $mime = FALSE;
+  
+  // Lookup array, Just in case we can't figure out the mime type.
+  $extension_mimes = array(
+    // MS ASF derived multimedia filetypes
+    'asx' => 'video/x-ms-asf',
+    'wma' => 'audio/x-ms-wma',
+    'wax' => 'audio/x-ms-wax',
+    'wmv' => 'video/x-ms-wmv',
+    'wvx' => ' video/x-ms-wvx',
+    'wm' =>  'video/x-ms-wm',
+    'wmx' => 'video/x-ms-wmx',
+    'wmz' => 'application/x-ms-wmz',
+    'wmd' => 'application/x-ms-wmd',
+
+    // Stanard Audio Types
+    'au' => 'audio/basic',
+    'snd' => 'audio/basic',
+    'mid' => 'audio/mid',
+    'rmi' => 'audio/mid',
+    'mp3' => 'audio/mpeg',
+    'aif' => 'audio/x-aiff',
+    'aifc' => 'audio/x-aiff',
+    'aidff' => 'audio/x-aiff',
+    'm3u' => 'audio/x-mpegurl',
+    'ra' => 'audio/x-pn-realaudio',
+    'ram' => 'audio/x-pn-realaudio',
+    'wav' => 'audio/x-wav',
+
+    // Standard Image Types
+    'bmp' => 'image/bmp',
+    'cod' => 'image/cis-cod',
+    'gif' => 'image/gif',
+    'ief' => 'image/ief',
+    'jpe' => 'image/jpeg',
+    'jpg' => 'image/jpeg',
+    'jpeg' => 'image/jpeg',
+    'jfif' => 'image/pipeg',
+    'svg' => 'image/svg+xml',
+    'tif' => 'image/tiff',
+    'tiff' => 'image/tiff',
+    'ras' => 'image/x-cmu-raster',
+    'cmx' => 'image/x-cmx',
+    'ico' => 'image/x-icon',
+    'pnm' => 'image/x-portable-anymap',
+    'pbm' => 'image/x-portable-bitmap',
+    'pgm' => 'image/x-portable-graymap',
+    'ppm' => 'image/x-portable-pixmap',
+    'rbg' => 'image/x-rgb',
+    'xbm' => 'image/x-xbitmap',
+    'xpm' => 'image/x-xpixmap',
+    'xwd' => 'image/x-xwindowdump',
+
+    // Standard Video types
+    'mp2' => 'video/mpeg',
+    'mpa' => 'video/mpeg',
+    'mpe' => 'video/mpeg',
+    'mpeg' => 'video/mpeg',
+    'mpg' => 'video/mpeg',
+    'mpv2' => 'video/mpeg',
+    'mov' => 'video/quicktime',
+    'qt' => 'video/quicktime',
+    'lsf' => 'video/x-la-asf',
+    'lsx' => 'video/x-la-asf',
+    'asf' => 'video/x-ms-asf',
+    'asr' => 'video/x-ms-asf',
+    'asx' => 'video/x-ms-asf',
+    'avi' => 'video/x-msvideo',
+    'movie' => 'video/x-sgi-movie',
+
+  );
+
+  // Try to use the fileinfo extension first.
+  if (!$mime && extension_loaded('fileinfo')) {
+    static $finfo = FALSE;
+    if ($finfo || $finfo = @finfo_open(FILEINFO_MIME)) {
+      $mime = finfo_file($finfo, $file->filepath);
+    }
+  }
+
+  // Try the 'file' binary.
+  if (!$mime && $filebin = variable_get('filefield_file_binary', '/usr/bin/file') && is_executable($filebin)) {
+    $command = $filebin .' -bi '. escapeshellarg($file->filepath);
+    $mime = trim(exec($command, $out));
+  }
+
+  // Trust the browser...  ack!
+  if (!$mime) {
+    $mime = $file->filemime;
+  }
+
+  // ASF derived media formats are hard to detect with magic. They're
+  // typically all reported as video/x-ms-asf or application/octet-stream
+  // These aren't really informative about the media type, so we attempt to 
+  // figure it out by extension.
+  // I expect OGG to present similar difficulties in determining how it should be played...
+  
+  if (!$mime || $mime == 'application/octet-stream') {
+    $parts = explode('.', $file->filename);
+    $extension = array_pop($parts);
+    if (isset($extension_mimes[$extension])) {
+      $mime = $extension_mimes[$extension];
+    }
+  }
+
+  return $mime;
+}
