Commit c8b75c6a by Alexander Makarov

Merge pull request #3773 from Ragazzo/file_validator_adjusted

File validator adjusted
parents 83e6610c 0206e917
...@@ -9,6 +9,7 @@ namespace yii\validators; ...@@ -9,6 +9,7 @@ namespace yii\validators;
use Yii; use Yii;
use yii\web\UploadedFile; use yii\web\UploadedFile;
use yii\helpers\FileHelper;
/** /**
* FileValidator verifies if an attribute is receiving a valid uploaded file. * FileValidator verifies if an attribute is receiving a valid uploaded file.
...@@ -30,6 +31,15 @@ class FileValidator extends Validator ...@@ -30,6 +31,15 @@ class FileValidator extends Validator
*/ */
public $types; public $types;
/** /**
* @var array|string a list of file MIME types that are allowed to be uploaded.
* This can be either an array or a string consisting of file MIME types
* separated by space or comma (e.g. "text/plain, image/png").
* Mime type names are case-insensitive. Defaults to null, meaning all MIME types
* are allowed.
* @see wrongMimeType
*/
public $mimeTypes;
/**
* @var integer the minimum number of bytes required for the uploaded file. * @var integer the minimum number of bytes required for the uploaded file.
* Defaults to null, meaning no limit. * Defaults to null, meaning no limit.
* @see tooSmall * @see tooSmall
...@@ -93,6 +103,17 @@ class FileValidator extends Validator ...@@ -93,6 +103,17 @@ class FileValidator extends Validator
* - {limit}: the value of [[maxFiles]] * - {limit}: the value of [[maxFiles]]
*/ */
public $tooMany; public $tooMany;
/**
* @var string the error message used when the file has an mime type
* that is not listed in [[mimeTypes]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {mimeTypes}: the value of [[mimeTypes]]
*/
public $wrongMimeType;
/** /**
* @inheritdoc * @inheritdoc
...@@ -121,6 +142,12 @@ class FileValidator extends Validator ...@@ -121,6 +142,12 @@ class FileValidator extends Validator
if (!is_array($this->types)) { if (!is_array($this->types)) {
$this->types = preg_split('/[\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY); $this->types = preg_split('/[\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY);
} }
if ($this->wrongMimeType === null) {
$this->wrongMimeType = Yii::t('yii', 'Only files with these MIME types are allowed: {mimeTypes}.');
}
if (!is_array($this->mimeTypes)) {
$this->mimeTypes = preg_split('/[\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
}
} }
/** /**
...@@ -178,6 +205,8 @@ class FileValidator extends Validator ...@@ -178,6 +205,8 @@ class FileValidator extends Validator
return [$this->tooSmall, ['file' => $file->name, 'limit' => $this->minSize]]; return [$this->tooSmall, ['file' => $file->name, 'limit' => $this->minSize]];
} elseif (!empty($this->types) && !in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true)) { } elseif (!empty($this->types) && !in_array(strtolower(pathinfo($file->name, PATHINFO_EXTENSION)), $this->types, true)) {
return [$this->wrongType, ['file' => $file->name, 'extensions' => implode(', ', $this->types)]]; return [$this->wrongType, ['file' => $file->name, 'extensions' => implode(', ', $this->types)]];
} elseif (!empty($this->mimeTypes) && !in_array(FileHelper::getMimeType($file->tempName), $this->mimeTypes, true)) {
return [$this->wrongMimeType, ['file' => $file->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]];
} else { } else {
return null; return null;
} }
......
...@@ -9,7 +9,6 @@ namespace yii\validators; ...@@ -9,7 +9,6 @@ namespace yii\validators;
use Yii; use Yii;
use yii\web\UploadedFile; use yii\web\UploadedFile;
use yii\helpers\FileHelper;
/** /**
* ImageValidator verifies if an attribute is receiving a valid image. * ImageValidator verifies if an attribute is receiving a valid image.
...@@ -52,15 +51,6 @@ class ImageValidator extends FileValidator ...@@ -52,15 +51,6 @@ class ImageValidator extends FileValidator
*/ */
public $maxHeight; public $maxHeight;
/** /**
* @var array|string a list of file MIME types that are allowed to be uploaded.
* This can be either an array or a string consisting of file MIME types
* separated by space or comma (e.g. "image/jpeg, image/png").
* Mime type names are case-insensitive. Defaults to null, meaning all MIME types
* are allowed.
* @see wrongMimeType
*/
public $mimeTypes;
/**
* @var string the error message used when the image is under [[minWidth]]. * @var string the error message used when the image is under [[minWidth]].
* You may use the following tokens in the message: * You may use the following tokens in the message:
* *
...@@ -96,16 +86,7 @@ class ImageValidator extends FileValidator ...@@ -96,16 +86,7 @@ class ImageValidator extends FileValidator
* - {limit}: the value of [[maxHeight]] * - {limit}: the value of [[maxHeight]]
*/ */
public $overHeight; public $overHeight;
/**
* @var string the error message used when the file has an mime type
* that is not listed in [[mimeTypes]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {mimeTypes}: the value of [[mimeTypes]]
*/
public $wrongMimeType;
/** /**
* @inheritdoc * @inheritdoc
...@@ -129,12 +110,6 @@ class ImageValidator extends FileValidator ...@@ -129,12 +110,6 @@ class ImageValidator extends FileValidator
if ($this->overHeight === null) { if ($this->overHeight === null) {
$this->overHeight = Yii::t('yii', 'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.'); $this->overHeight = Yii::t('yii', 'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
} }
if ($this->wrongMimeType === null) {
$this->wrongMimeType = Yii::t('yii', 'Only files with these MIME types are allowed: {mimeTypes}.');
}
if (!is_array($this->mimeTypes)) {
$this->mimeTypes = preg_split('/[\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
}
} }
/** /**
...@@ -155,10 +130,6 @@ class ImageValidator extends FileValidator ...@@ -155,10 +130,6 @@ class ImageValidator extends FileValidator
*/ */
protected function validateImage($image) protected function validateImage($image)
{ {
if (!empty($this->mimeTypes) && !in_array(FileHelper::getMimeType($image->tempName), $this->mimeTypes, true)) {
return [$this->wrongMimeType, ['file' => $image->name, 'mimeTypes' => implode(', ', $this->mimeTypes)]];
}
if (false === ($imageInfo = getimagesize($image->tempName))) { if (false === ($imageInfo = getimagesize($image->tempName))) {
return [$this->notImage, ['file' => $image->name]]; return [$this->notImage, ['file' => $image->name]];
} }
......
...@@ -21,7 +21,7 @@ class FileValidatorTest extends TestCase ...@@ -21,7 +21,7 @@ class FileValidatorTest extends TestCase
public function testAssureMessagesSetOnInit() public function testAssureMessagesSetOnInit()
{ {
$val = new FileValidator(); $val = new FileValidator();
foreach (['message', 'uploadRequired', 'tooMany', 'wrongType', 'tooBig', 'tooSmall'] as $attr) { foreach (['message', 'uploadRequired', 'tooMany', 'wrongType', 'tooBig', 'tooSmall', 'wrongMimeType'] as $attr) {
$this->assertTrue(is_string($val->$attr)); $this->assertTrue(is_string($val->$attr));
} }
} }
...@@ -30,18 +30,44 @@ class FileValidatorTest extends TestCase ...@@ -30,18 +30,44 @@ class FileValidatorTest extends TestCase
{ {
$val = new FileValidator(['types' => 'jpeg, jpg, gif']); $val = new FileValidator(['types' => 'jpeg, jpg, gif']);
$this->assertEquals(['jpeg', 'jpg', 'gif'], $val->types); $this->assertEquals(['jpeg', 'jpg', 'gif'], $val->types);
$val = new FileValidator(['types' => 'jpeg']); $val = new FileValidator(['types' => 'jpeg']);
$this->assertEquals(['jpeg'], $val->types); $this->assertEquals(['jpeg'], $val->types);
$val = new FileValidator(['types' => '']); $val = new FileValidator(['types' => '']);
$this->assertEquals([], $val->types); $this->assertEquals([], $val->types);
$val = new FileValidator(['types' => []]); $val = new FileValidator(['types' => []]);
$this->assertEquals([], $val->types); $this->assertEquals([], $val->types);
$val = new FileValidator(); $val = new FileValidator();
$this->assertEquals([], $val->types); $this->assertEquals([], $val->types);
$val = new FileValidator(['types' => ['jpeg', 'exe']]); $val = new FileValidator(['types' => ['jpeg', 'exe']]);
$this->assertEquals(['jpeg', 'exe'], $val->types); $this->assertEquals(['jpeg', 'exe'], $val->types);
} }
public function testMimeTypeSplitOnInit()
{
$val = new FileValidator(['mimeTypes' => 'text/plain, image/png']);
$this->assertEquals(['text/plain', 'image/png'], $val->mimeTypes);
$val = new FileValidator(['mimeTypes' => 'text/plain']);
$this->assertEquals(['text/plain'], $val->mimeTypes);
$val = new FileValidator(['mimeTypes' => '']);
$this->assertEquals([], $val->mimeTypes);
$val = new FileValidator(['mimeTypes' => []]);
$this->assertEquals([], $val->mimeTypes);
$val = new FileValidator();
$this->assertEquals([], $val->mimeTypes);
$val = new FileValidator(['mimeTypes' => ['text/plain', 'image/png']]);
$this->assertEquals(['text/plain', 'image/png'], $val->mimeTypes);
}
public function testGetSizeLimit() public function testGetSizeLimit()
{ {
$size = $this->sizeToBytes(ini_get('upload_max_filesize')); $size = $this->sizeToBytes(ini_get('upload_max_filesize'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment