merged branch Tobion/max-filesize-validator (PR #7362)
This PR was merged into the 2.1 branch.
Commits
-------
7216cb0
[Validator] fix showing wrong max file size for upload errors
Discussion
----------
[Validator] fix showing wrong max file size for upload errors
this was because the maxSize option wasn't parsed correctly and simple string comparision could lead to wrong results, e.g. 200 > 1000M
| Q | A
| ------------- | ---
| Bug fix? | [yes]
| New feature? | [no]
| BC breaks? | [no]
| Deprecations? | [no]
| Tests pass? | [yes|]
| Fixed tickets | [#6441,#5551]
| License | MIT
Will apply cleanly to 2.2
This commit is contained in:
commit
cefc8202e3
@ -37,8 +37,21 @@ class FileValidator extends ConstraintValidator
|
||||
if ($value instanceof UploadedFile && !$value->isValid()) {
|
||||
switch ($value->getError()) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
$maxSize = UploadedFile::getMaxFilesize();
|
||||
$maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
|
||||
if ($constraint->maxSize) {
|
||||
if (ctype_digit((string) $constraint->maxSize)) {
|
||||
$maxSize = (int) $constraint->maxSize;
|
||||
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
||||
$maxSize = $constraint->maxSize * 1024;
|
||||
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
||||
$maxSize = $constraint->maxSize * 1048576;
|
||||
} else {
|
||||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
||||
}
|
||||
$maxSize = min(UploadedFile::getMaxFilesize(), $maxSize);
|
||||
} else {
|
||||
$maxSize = UploadedFile::getMaxFilesize();
|
||||
}
|
||||
|
||||
$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
|
||||
'{{ limit }}' => $maxSize,
|
||||
'{{ suffix }}' => 'bytes',
|
||||
@ -97,15 +110,15 @@ class FileValidator extends ConstraintValidator
|
||||
if ($constraint->maxSize) {
|
||||
if (ctype_digit((string) $constraint->maxSize)) {
|
||||
$size = filesize($path);
|
||||
$limit = $constraint->maxSize;
|
||||
$limit = (int) $constraint->maxSize;
|
||||
$suffix = 'bytes';
|
||||
} elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
|
||||
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
||||
$size = round(filesize($path) / 1000, 2);
|
||||
$limit = $matches[1];
|
||||
$limit = (int) $constraint->maxSize;
|
||||
$suffix = 'kB';
|
||||
} elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
|
||||
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
||||
$size = round(filesize($path) / 1000000, 2);
|
||||
$limit = $matches[1];
|
||||
$limit = (int) $constraint->maxSize;
|
||||
$suffix = 'MB';
|
||||
} else {
|
||||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
||||
|
@ -288,12 +288,13 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @dataProvider uploadedFileErrorProvider
|
||||
*/
|
||||
public function testUploadedFileError($error, $message, array $params = array())
|
||||
public function testUploadedFileError($error, $message, array $params = array(), $maxSize = null)
|
||||
{
|
||||
$file = new UploadedFile('/path/to/file', 'originalName', 'mime', 0, $error);
|
||||
|
||||
$constraint = new File(array(
|
||||
$message => 'myMessage',
|
||||
'maxSize' => $maxSize
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
@ -316,10 +317,24 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
|
||||
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
|
||||
// when no maxSize is specified on constraint, it should use the ini value
|
||||
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
|
||||
'{{ limit }}' => UploadedFile::getMaxFilesize(),
|
||||
'{{ suffix }}' => 'bytes',
|
||||
));
|
||||
|
||||
// it should use the smaller limitation (maxSize option in this case)
|
||||
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
|
||||
'{{ limit }}' => 1,
|
||||
'{{ suffix }}' => 'bytes',
|
||||
), '1');
|
||||
|
||||
// it correctly parses the maxSize option and not only uses simple string comparison
|
||||
// 1000M should be bigger than the ini value
|
||||
$tests[] = array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array(
|
||||
'{{ limit }}' => UploadedFile::getMaxFilesize(),
|
||||
'{{ suffix }}' => 'bytes',
|
||||
), '1000M');
|
||||
}
|
||||
|
||||
return $tests;
|
||||
|
Reference in New Issue
Block a user