/** * Test that is() methods return a bool dependent on the mime type. */ public function testIs() { $this->assertTrue(MimeType::isApplication('application/x-7z-compressed')); $this->assertFalse(MimeType::isApplication('image/png')); $this->assertTrue(MimeType::isAudio('audio/midi')); $this->assertFalse(MimeType::isAudio('text/xml')); $this->assertTrue(MimeType::isImage('image/gif')); $this->assertFalse(MimeType::isImage('text/plain')); $this->assertTrue(MimeType::isText('text/yaml')); $this->assertFalse(MimeType::isText('video/mp4')); $this->assertTrue(MimeType::isVideo('video/mp4')); $this->assertFalse(MimeType::isVideo('application/x-7z-compressed')); $this->assertTrue(MimeType::isSubType('archive', 'application/x-7z-compressed')); $this->assertFalse(MimeType::isSubType('archive', 'video/mp4')); }
/** * Validate the top-level type of file, e.g., image. * * @uses Transit\MimeType * * @param string|array $mimeTypes * @return bool */ public function type($mimeTypes) { $types = array(); foreach ((array) $mimeTypes as $mimeType) { switch ($mimeType) { case 'application': $types += MimeType::getApplicationList(); break; case 'audio': $types += MimeType::getAudioList(); break; case 'image': $types += MimeType::getImageList(); break; case 'text': $types += MimeType::getTextList(); break; case 'video': $types += MimeType::getVideoList(); break; default: $types += MimeType::getSubTypeList($mimeType); break; } } return in_array($this->getFile()->type(), $types); }
/** * 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; }); }
/** * Return true if the file is part of a sub-type. * * @param string $subType * @return bool */ public function isSubType($subType) { return MimeType::isSubType($subType, $this); }