Ejemplo n.º 1
0
 /**
  * Test that getTypeFromExt() returns a mime type from the ext.
  */
 public function testGetTypeFromExt()
 {
     $this->assertEquals('image/png', MimeType::getTypeFromExt('png'));
     $this->assertEquals('application/x-7z-compressed', MimeType::getTypeFromExt('7z'));
     try {
         MimeType::getTypeFromExt('foobar');
         $this->assertTrue(false);
     } catch (Exception $e) {
         $this->assertTrue(true);
     }
 }
Ejemplo n.º 2
0
 /**
  * Return the mime type.
  *
  * @uses Transit\MimeType
  *
  * @return string
  */
 public function type()
 {
     return $this->_cache(__FUNCTION__, function ($file) {
         /** @type \Transit\File $file */
         $type = null;
         // We can't use the file command on windows
         if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
             $type = shell_exec(sprintf("file -b --mime %s", escapeshellarg($file->path())));
             if ($type && strpos($type, ';') !== false) {
                 $type = strstr($type, ';', true);
             }
         }
         // Fallback because of fileinfo bug: https://bugs.php.net/bug.php?id=53035
         if (!$type) {
             $info = finfo_open(FILEINFO_MIME_TYPE);
             $type = finfo_file($info, $file->path());
             finfo_close($info);
         }
         // Check the mimetype against the extension
         // If they are different, use the extension since fileinfo returns invalid mimetypes
         // This could be problematic in the future, but unknown better alternative
         if ($ext = $file->ext()) {
             try {
                 $extType = MimeType::getTypeFromExt($ext);
                 // Use $_FILES['type'] last since sometimes it returns application/octet-stream and other mimetypes
                 // When we should always have a true mimetype
             } catch (InvalidArgumentException $e) {
                 $extType = $file->data('type');
             }
             if ($type !== $extType) {
                 $type = $extType;
             }
         }
         return $type;
     });
 }