@ -1,83 +1,83 @@
<?php
<?php
/**
/**
* @file
* @file
*
* Replacement for the original islandora MimeClass.
* Replacement for the original islandora MimeClass.
* Does essentially the same thing, just using the built in drupal 7
* Does essentially the same thing, just using the built in drupal 7
* mime system.
* mime system.
*
*
* A nice added bonus is that this new class will put a note in watchdog when
* A nice added bonus is that this new class will put a note in watchdog when
* it encounters a filetype it doesnt know.
* it encounters a filetype it doesnt know.
*
*/
*/
class MimeDetect {
class MimeDetect {
private $mime_types;
const DEFAULT_MIMETYPE = 'application/octet-stream';
private $ext_types;
const DEFAULT_EXTENSION = 'bin';
protected $mimeTypes;
protected $extTypes;
/**
/**
* Construtor
* Construtor.
*/
*/
public function __construct() {
public function __construct() {
//Drupal returns the mimetype list as a double assoc array with two entries:
// Mimetype list returned as a double assoc array with two entries:
//mimetypes: Integer array of mimetypes
// Mimetypes: Integer array of mimetypes
//extensions: Assoc array of extensions
// Extensions: Assoc array of extensions
//
//The extension array can be used as a key to get the value in the mimetypes
//array.
//
//
//Example if $extensions['png'] == 5, then $mimetypes[5] = 'image/png'
// Extension array can be used as a key to get mimetypes
//we want to combine this into 1 array for easy access.
// Example if $extensions['png'] == 5, then
// $mimetypes[5] = 'image/png'
// We want to combine this into 1 array for easy access.
$temp = file_mimetype_mapping();
$temp = file_mimetype_mapping();
$temp_mime_types = $temp['mimetypes'];
$temp_mime_types = $temp['mimetypes'];
$temp_mime_ext = $temp['extensions'];
$temp_mime_ext = $temp['extensions'];
//f ree the memory in temp since we don't need it anymore
// F ree the memory in temp since we don't need it anymore.
unset($temp);
unset($temp);
//c ombine the two arrays into one.
// C ombine the two arrays into one.
foreach ($temp_mime_ext as $key => $val ) {
foreach ($temp_mime_ext as $key => $val) {
$this->mime_types[$key] = $temp_mime_types[$val];
$this->mime_types[$key] = $temp_mime_types[$val];
}
}
//flip the mime_type array to make the ext array
// Flip the mime_type array to make the ext array.
$this->ext_types = array_flip($this->mime_types);
$this->ext_types = array_flip($this->mime_types);
}
}
/**
/**
* function: getType
* For backwards-compatibility with our old mimetype class.
* description: An alias to get_mimetype,
*
* for backwards-compatibility with our old mimetype class.
* @param string $filename
* Filename to convert to mimetype.
*
*
* @param type $filename
* @return data
* @return type
* Returns a string with the mimetype for the file.
*/
*/
function getType($filename) {
public function getType($filename) {
return $this->getMimetype($filename);
return $this->getMimetype($filename);
}
}
/**
/**
* function: get_mimetype
* Returns a mimetype associated with the file extension of $filename.
* description: returns a mimetype associated with the
*
* file extension of $filename
* @param string $filename
* The filename we want the mime type of.
*
* @param bool $debug
* Enable or disable debug output mode.
*
*
* @param type $filename
* @param type $debug
* @return string
* @return string
* m imetype associated with the file extension of $filename
* Mimetype associated with the file extension of $filename.
*/
*/
public function getMimetype($filename, $debug = FALSE) {
public function getMimetype($filename, $debug = FALSE) {
//t he file extension will be the last index in the exploded string
// T he file extension will be the last index in the exploded string.
$ext = @end(explode('.' , $filename));
$ext = @end(explode('.', $filename));
$ext = strtolower($ext);
$ext = strtolower($ext);
//check the array for a predetermined mime type. If not found, add a warning
// Check the array for a predetermined mime type.
//to the watch dog and use the system command to get a value
// If type not found, add a warning to watchdog.
if (!empty($this->mime_types[$ext])) {
if (!empty($this->mime_types[$ext])) {
if (TRUE === $debug) {
if (TRUE === $debug) {
$return_type = $this->mime_types[$ext];
$return_type = $this->mime_types[$ext];
@ -87,13 +87,13 @@ class MimeDetect {
return $this->mime_types[$ext];
return $this->mime_types[$ext];
}
}
else {
else {
//t his file was not in the drupal mime system.
// T his file was not in the drupal mime system.
//a dd a line to the watchdog informing us of this incident
// A dd a line to the watchdog informing us of this incident.
$err_msg = 'Found a file extension not covered in the drupal mime func:';
$err_msg = 'Found a file extension not covered in the drupal mime func:';
watchdog('MimeClass', $err_msg . $ext );
watchdog('MimeClass', $err_msg . $ext);
}
}
//by default return application octet stream
// By default return application octet stream.
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');
}
}
@ -102,12 +102,16 @@ class MimeDetect {
}
}
/**
/**
* function: get_extension
* Returns *one* valid file extension for a given $mime_type.
* description: returns *one* valid file extension for a given $mime_type
*
* @param string $type
* String containing the mimetype
*
* @param bool $debug
* Toggle debug mode on/off
*
*
* @param type $mime_type
* @return string
* @param type $debug
* Return the file extension
* @return type
*/
*/
public function getExtension($type, $debug = FALSE) {
public function getExtension($type, $debug = FALSE) {
@ -117,10 +121,10 @@ class MimeDetect {
}
}
return $this->ext_types[$type];
return $this->ext_types[$type];
}
}
else{
else {
//return bin by default... this could cause errors if handled incorrectly
// Return bin by default. This could cause errors.
//and probably should return false or -1 or something to be handled
// Function should return false or -1 or something to be handled.
//c lient side.
// C lient side.
if (TRUE == $debug) {
if (TRUE == $debug) {
return array('extension' => 'bin', 'method' => 'last_resort');
return array('extension' => 'bin', 'method' => 'last_resort');
}
}
@ -129,4 +133,3 @@ class MimeDetect {
}
}
}
}