mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Merge pull request #3773 from Ragazzo/file_validator_adjusted
File validator adjusted
This commit is contained in:
@ -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.
|
||||||
@ -29,6 +30,15 @@ class FileValidator extends Validator
|
|||||||
* @see wrongType
|
* @see wrongType
|
||||||
*/
|
*/
|
||||||
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.
|
||||||
@ -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
|
|||||||
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
|
|||||||
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;
|
|||||||
|
|
||||||
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.
|
||||||
@ -51,15 +50,6 @@ class ImageValidator extends FileValidator
|
|||||||
* @see overWidth
|
* @see overWidth
|
||||||
*/
|
*/
|
||||||
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
|
|||||||
* - {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
|
|||||||
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
|
|||||||
*/
|
*/
|
||||||
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
|
|||||||
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
|
|||||||
{
|
{
|
||||||
$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'));
|
||||||
|
Reference in New Issue
Block a user