function testSimplify() { MimeType::config('magic', array('engine' => false)); MimeType::config('glob', array('engine' => false)); $this->assertEqual(MimeType::simplify('application/x-pdf'), 'application/pdf'); $this->assertEqual(MimeType::simplify('x-inode/x-directory'), 'inode/directory'); $this->assertEqual(MimeType::simplify('application/octet-stream; encoding=compress'), 'application/octet-stream'); $this->assertEqual(MimeType::simplify('application/x-test; encoding=compress'), 'application/test'); $this->assertEqual(MimeType::simplify('text/plain; charset=iso-8859-1'), 'text/plain'); $this->assertEqual(MimeType::simplify('text/plain charset=us-ascii'), 'text/plain'); }
/** * Method for checking if the adapter is going to work with the provided $Media * * Called before the adapter is going to be initialized * May be overridden * * @param object $Media * @return boolean */ function compatible($Media) { $default = array('mimeTypes' => array(), 'extensions' => array(), 'functions' => array(), 'imports' => array(), 'commands' => array()); $require = array_merge($default, $this->require); if (!empty($require['mimeTypes'])) { if (!in_array(MimeType::simplify($Media->mimeType), $require['mimeTypes'])) { return false; } } foreach ($require['extensions'] as $check) { if (!extension_loaded($check)) { return false; } } foreach ($require['functions'] as $check) { if (!function_exists($check)) { return false; } } foreach ($require['commands'] as $check) { if (!$this->_which($check)) { return false; } } foreach ($require['imports'] as $import) { if (!App::import($import)) { return false; } } return true; }
/** * Guesses the MIME type of the file * * Empty results are currently not handled: * application/x-empty * application/x-not-regular-file * * @param string $file * @param options $options Valid options are: * - `'paranoid'` If set to true only then content for the file is used for detection * - `'properties'` Used for simplification, defaults to false * - `'experimental'` Used for simplification, defaults to false * @return mixed string with MIME type on success * @access public */ function guessType($file, $options = array()) { $_this =& MimeType::getInstance(); $defaults = array('paranoid' => false, 'properties' => false, 'experimental' => true); extract($options + $defaults); $magicMatch = $globMatch = array(); if (!$paranoid) { if (is_a($_this->__glob, 'MimeGlob')) { $globMatch = $_this->__glob->analyze($file); } if (count($globMatch) === 1) { return MimeType::simplify(array_shift($globMatch), $properties, $experimental); } } if (!is_readable($file)) { return null; } if (is_a($_this->__magic, 'finfo')) { $magicMatch = $_this->__magic->file($file); } elseif ($_this->__magic === 'mime_magic') { $magicMatch = mime_content_type($file); } elseif (is_a($_this->__magic, 'MimeMagic')) { $magicMatch = $_this->__magic->analyze($file); } $magicMatch = empty($magicMatch) ? array() : array($magicMatch); if (empty($magicMatch)) { $File =& new File($file); if (preg_match('/[\\t\\n\\r]+/', $File->read(32))) { return 'text/plain'; } return 'application/octet-stream'; } if (count($magicMatch) === 1) { return MimeType::simplify(array_shift($magicMatch), $properties, $experimental); } if ($globMatch && $magicMatch) { $combinedMatch = array_intersect($globMatch, $magicMatch); if (count($combinedMatch) === 1) { return MimeType::simplify(array_shift($combinedMatch), $properties, $experimental); } } return null; }