Added regex support to Validation::mimeType().

This commit is contained in:
ADmad 2014-01-30 17:11:54 +05:30
parent 6eb5a38f22
commit 2d10707d18
2 changed files with 10 additions and 2 deletions

View file

@ -2356,9 +2356,12 @@ class ValidationTest extends CakeTestCase {
public function testMimeType() { public function testMimeType() {
$image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif'; $image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
$File = new File($image, false); $File = new File($image, false);
$this->skipIf(!$File->mime(), 'Cannot determine mimeType'); $this->skipIf(!$File->mime(), 'Cannot determine mimeType');
$this->assertTrue(Validation::mimeType($image, array('image/gif'))); $this->assertTrue(Validation::mimeType($image, array('image/gif')));
$this->assertTrue(Validation::mimeType(array('tmp_name' => $image), array('image/gif'))); $this->assertTrue(Validation::mimeType(array('tmp_name' => $image), array('image/gif')));
$this->assertTrue(Validation::mimeType(array('tmp_name' => $image), '#image/.+#'));
$this->assertFalse(Validation::mimeType($image, array('image/GIF'))); $this->assertFalse(Validation::mimeType($image, array('image/GIF')));
$this->assertFalse(Validation::mimeType($image, array('image/png'))); $this->assertFalse(Validation::mimeType($image, array('image/png')));

View file

@ -925,10 +925,10 @@ class Validation {
} }
/** /**
* Checks the mime type of a file. Comparison is case sensitive. * Checks the mime type of a file.
* *
* @param string|array $check * @param string|array $check
* @param array $mimeTypes to check for * @param array|string $mimeTypes Array of mime types or regex pattern to check.
* @return boolean Success * @return boolean Success
* @throws CakeException when mime type can not be determined. * @throws CakeException when mime type can not be determined.
*/ */
@ -943,6 +943,11 @@ class Validation {
if ($mime === false) { if ($mime === false) {
throw new CakeException(__d('cake_dev', 'Can not determine the mimetype.')); throw new CakeException(__d('cake_dev', 'Can not determine the mimetype.'));
} }
if (is_string($mimeTypes)) {
return self::_check($mime, $mimeTypes);
}
return in_array($mime, $mimeTypes); return in_array($mime, $mimeTypes);
} }