mimedetect.module in MimeDetect 7
Same filename and directory in other branches
Provide server side mime type detection.
File
mimedetect.moduleView source
<?php
/**
* @file
* Provide server side mime type detection.
*/
/**
* Implements hook_menu().
*/
function mimedetect_menu() {
$items = array();
// The admin settings form.
$items['admin/config/media/mimedetect'] = array(
'title' => 'Mime type detection',
'description' => 'Control how the mime type of uploaded files will be determined.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mimedetect_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'mimedetect.admin.inc',
);
return $items;
}
/**
* Detect File Mime Type.
*
* @param object $file
* A standard Drupal file object. The uri property is used to locate the file
* and if the mime detection fails, the mimetype property is returned.
*
* @return string
* Containing the file's MIME type.
*/
function mimedetect_mime($file) {
$file = (object) $file;
$mime = FALSE;
// Try to use the fileinfo extension first.
if (variable_get('mimedetect_enable_file_info', FALSE) && extension_loaded('fileinfo')) {
$magic_file = variable_get('mimedetect_magic', '');
static $finfo = FALSE;
if ($finfo || ($finfo = @finfo_open(FILEINFO_MIME, $magic_file))) {
$mime = finfo_file($finfo, drupal_realpath($file->uri));
}
}
// Try the 'file' binary.
if ((!$mime || $mime == 'application/octet-stream') && variable_get('mimedetect_enable_file_binary', FALSE) && ($filebin = variable_get('mimedetect_file_binary', '/usr/bin/file')) && basename($filebin) == 'file' && is_executable($filebin)) {
// On OSX the -i switch is -I, so if we use the long flags everyone is
// happy. I checked back to version 3.41 and it still supports the long
// names but if you run into problems you can use " -bi ".
$command = $filebin . ' --brief --mime' . (!empty($magic_file) ? ' --magic-file=' . escapeshellarg($magic_file) : '') . ' ' . escapeshellarg(drupal_realpath($file->uri));
$mime = trim(exec($command));
}
// With text we often get charset like 'text/plain; charset=us-ascii'.
$mime = explode(';', $mime);
$mime = trim($mime[0]);
// If it was unable to determine the file MIME type, fallback to the core
// MIME mapping based on file name extension.
if (!$mime || $mime == 'application/octet-stream') {
$mime = file_get_mimetype($file->filename);
}
return $mime;
}
Functions
Name | Description |
---|---|
mimedetect_menu | Implements hook_menu(). |
mimedetect_mime | Detect File Mime Type. |