Browse Source

some code style fixes

pull/167/head
William Panting 12 years ago
parent
commit
2b20e2e632
  1. 78
      MimeClass.inc

78
MimeClass.inc

@ -21,10 +21,11 @@
* http://api.drupal.org/api/function/file_default_mimetype_mapping/7 * http://api.drupal.org/api/function/file_default_mimetype_mapping/7
* *
*/ */
class MimeClass { class MimeClass {
private $private_mime_types = array( private $private_mime_types = array(
/** /*
* This is a shortlist of mimetypes which should catch most * This is a shortlist of mimetypes which should catch most
* mimetype<-->extension lookups in the context of Islandora collections. * mimetype<-->extension lookups in the context of Islandora collections.
* *
@ -37,16 +38,16 @@ class MimeClass {
* project, as lookups against this list will be quicker and less * project, as lookups against this list will be quicker and less
* resource intensive than other methods. * resource intensive than other methods.
* *
* Lookups are first checked against this short list. If no results are found, * Lookups are first checked against this short list.
* then the lookup function may move on to check other sources, namely the * If no results are found, then the lookup function may move
* system's mime.types file. * on to check other sources, namely the system's mime.types file.
* *
* In most cases though, this short list should suffice. * In most cases though, this short list should suffice.
* *
* If modifying this list, please note that for promiscuous mimetypes * If modifying this list, please note that for promiscuous mimetypes
* (those which map to multiple extensions, such as text/plain) * (those which map to multiple extensions, such as text/plain)
* The function get_extension will always return the *LAST* extension in this list, * The function get_extension will always return the *LAST* extension
* so you should put your preferred extension *LAST*. * in this list, so you should put your preferred extension *LAST*.
* *
* e.g... * e.g...
* "jpeg" => "image/jpeg", * "jpeg" => "image/jpeg",
@ -89,7 +90,7 @@ class MimeClass {
'ksp' => 'application/x-kspread', 'ksp' => 'application/x-kspread',
'kwt' => 'application/x-kword', 'kwt' => 'application/x-kword',
'kwd' => 'application/x-kword', 'kwd' => 'application/x-kword',
// ms office 97: // MS office 97:
'doc' => 'application/msword', 'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint', 'ppt' => 'application/vnd.ms-powerpoint',
@ -113,9 +114,9 @@ class MimeClass {
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
// wordperfect (who cares?): // Wordperfect (who cares?):
'wpd' => 'application/wordperfect', 'wpd' => 'application/wordperfect',
// common and generic containers: // Common and generic containers:
'pdf' => 'application/pdf', 'pdf' => 'application/pdf',
'eps' => 'application/postscript', 'eps' => 'application/postscript',
'ps' => 'application/postscript', 'ps' => 'application/postscript',
@ -185,7 +186,8 @@ class MimeClass {
"ogg" => "audio/ogg", "ogg" => "audio/ogg",
"flac" => "audio/x-flac", "flac" => "audio/x-flac",
"wav" => "audio/vnd.wave", "wav" => "audio/vnd.wave",
// compressed formats: (note: http://svn.cleancode.org/svn/email/trunk/mime.types) /* Compressed formats:
(note: http://svn.cleancode.org/svn/email/trunk/mime.types).*/
"tgz" => "application/x-gzip", "tgz" => "application/x-gzip",
"gz" => "application/x-gzip", "gz" => "application/x-gzip",
"tar" => "application/x-tar", "tar" => "application/x-tar",
@ -204,10 +206,10 @@ class MimeClass {
*/ */
public function __construct() { public function __construct() {
// populate the reverse shortlist: // Populate the reverse shortlist:
$this->private_file_extensions = array_flip($this->private_mime_types); $this->private_file_extensions = array_flip($this->private_mime_types);
// pick up a local mime.types file if it is available // Pick up a local mime.types file if it is available.
if (is_readable('mime.types')) { if (is_readable('mime.types')) {
$this->etc_mime_types = 'mime.types'; $this->etc_mime_types = 'mime.types';
} }
@ -227,40 +229,47 @@ class MimeClass {
/** /**
* function: get_mimetype * function: get_mimetype
* description: returns a mimetype associated with the file extension of $filename * description: returns a mimetype associated with the
* file extension of $filename
* *
* @param type $filename * @param type $filename
* @param type $debug * @param type $debug
* @return type * @return string
* mimetype associated with the file extension of $filename
*/ */
public function get_mimetype($filename, $debug = FALSE) { public function get_mimetype($filename, $debug = FALSE) {
$ext = strtolower(substr($filename, strrpos($filename, '.') + 1)); $ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
if (!empty($this->private_mime_types[$ext])) { if (!empty($this->private_mime_types[$ext])) {
if (TRUE === $debug) if (TRUE === $debug) {
return array('mime_type' => $this->private_mime_types[$ext], 'method' => 'from_array'); return array('mime_type' => $this->private_mime_types[$ext], 'method' => 'from_array');
}
return $this->private_mime_types[$ext]; return $this->private_mime_types[$ext];
} }
if (function_exists('file_get_mimetype')) { if (function_exists('file_get_mimetype')) {
$drupal_mimetype = file_get_mimetype($filename); $drupal_mimetype = file_get_mimetype($filename);
if ('application/octet-stream' != $drupal_mimetype) { if ('application/octet-stream' != $drupal_mimetype) {
if (TRUE == $debug) if (TRUE == $debug) {
return array('mime_type' => $drupal_mimetype, 'method' => 'file_get_mimetype'); return array('mime_type' => $drupal_mimetype, 'method' => 'file_get_mimetype');
}
return $drupal_mimetype; return $drupal_mimetype;
} }
} }
if (!isset($this->system_types)) if (!isset($this->system_types)) {
$this->system_types = $this->system_extension_mime_types(); $this->system_types = $this->system_extension_mime_types();
}
if (isset($this->system_types[$ext])) { if (isset($this->system_types[$ext])) {
if (TRUE == $debug) if (TRUE == $debug) {
return array('mime_type' => $this->system_types[$ext], 'method' => 'mime.types'); return array('mime_type' => $this->system_types[$ext], 'method' => 'mime.types');
}
return $this->system_types[$ext]; return $this->system_types[$ext];
} }
if (TRUE === $debug) if (TRUE === $debug) {
return array('mime_type' => 'application/octet-stream', 'method' => 'last_resort'); return array('mime_type' => 'application/octet-stream', 'method' => 'last_resort');
}
return 'application/octet-stream'; return 'application/octet-stream';
} }
@ -275,21 +284,25 @@ class MimeClass {
public function get_extension($mime_type, $debug = FALSE) { public function get_extension($mime_type, $debug = FALSE) {
if (!empty($this->private_file_extensions[$mime_type])) { if (!empty($this->private_file_extensions[$mime_type])) {
if (TRUE == $debug) if (TRUE == $debug) {
return array('extension' => $this->private_file_extensions[$mime_type], 'method' => 'from_array'); return array('extension' => $this->private_file_extensions[$mime_type], 'method' => 'from_array');
}
return $this->private_file_extensions[$mime_type]; return $this->private_file_extensions[$mime_type];
} }
if (!isset($this->system_exts)) if (!isset($this->system_exts)) {
$this->system_exts = $this->system_mime_type_extensions(); $this->system_exts = $this->system_mime_type_extensions();
}
if (isset($this->system_exts[$mime_type])) { if (isset($this->system_exts[$mime_type])) {
if (TRUE == $debug) if (TRUE == $debug) {
return array('extension' => $this->system_exts[$mime_type], 'method' => 'mime.types'); return array('extension' => $this->system_exts[$mime_type], 'method' => 'mime.types');
}
return $this->system_exts[$mime_type]; return $this->system_exts[$mime_type];
} }
if (TRUE == $debug) if (TRUE == $debug) {
return array('extension' => 'bin', 'method' => 'last_resort'); return array('extension' => 'bin', 'method' => 'last_resort');
}
return 'bin'; return 'bin';
} }
@ -306,15 +319,18 @@ class MimeClass {
$file = fopen($this->etc_mime_types, 'r'); $file = fopen($this->etc_mime_types, 'r');
while (($line = fgets($file)) !== FALSE) { while (($line = fgets($file)) !== FALSE) {
$line = trim(preg_replace('/#.*/', '', $line)); $line = trim(preg_replace('/#.*/', '', $line));
if (!$line) if (!$line) {
continue; continue;
}
$parts = preg_split('/\s+/', $line); $parts = preg_split('/\s+/', $line);
if (count($parts) == 1) if (count($parts) == 1) {
continue; continue;
}
// A single part means a mimetype without extensions, which we ignore. // A single part means a mimetype without extensions, which we ignore.
$type = array_shift($parts); $type = array_shift($parts);
if (!isset($out[$type])) if (!isset($out[$type])) {
$out[$type] = array_shift($parts); $out[$type] = array_shift($parts);
}
// We take the first ext from the line if many are present. // We take the first ext from the line if many are present.
} }
fclose($file); fclose($file);
@ -335,15 +351,18 @@ class MimeClass {
$file = fopen($this->etc_mime_types, 'r'); $file = fopen($this->etc_mime_types, 'r');
while (($line = fgets($file)) !== FALSE) { while (($line = fgets($file)) !== FALSE) {
$line = trim(preg_replace('/#.*/', '', $line)); $line = trim(preg_replace('/#.*/', '', $line));
if (!$line) if (!$line) {
continue; continue;
}
$parts = preg_split('/\s+/', $line); $parts = preg_split('/\s+/', $line);
if (count($parts) == 1) if (count($parts) == 1) {
continue; continue;
}
// A single part means a mimetype without extensions, which we ignore. // A single part means a mimetype without extensions, which we ignore.
$type = array_shift($parts); $type = array_shift($parts);
foreach ($parts as $part) foreach ($parts as $part) {
$out[$part] = $type; $out[$part] = $type;
}
} }
fclose($file); fclose($file);
} }
@ -351,4 +370,3 @@ class MimeClass {
} }
} }

Loading…
Cancel
Save