New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Validator] Support "maxSize" given in KiB #11027
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,13 +26,16 @@ | |
class FileValidator extends ConstraintValidator | ||
{ | ||
const KB_BYTES = 1000; | ||
|
||
const MB_BYTES = 1000000; | ||
const KIB_BYTES = 1024; | ||
const MIB_BYTES = 1048576; | ||
|
||
private static $suffices = array( | ||
1 => 'bytes', | ||
self::KB_BYTES => 'kB', | ||
self::MB_BYTES => 'MB', | ||
self::KIB_BYTES => 'KiB', | ||
self::MIB_BYTES => 'MiB', | ||
); | ||
|
||
/** | ||
|
@@ -52,16 +55,11 @@ public function validate($value, Constraint $constraint) | |
switch ($value->getError()) { | ||
case UPLOAD_ERR_INI_SIZE: | ||
if ($constraint->maxSize) { | ||
if (ctype_digit((string) $constraint->maxSize)) { | ||
$limitInBytes = (int) $constraint->maxSize; | ||
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) { | ||
$limitInBytes = $constraint->maxSize * self::KB_BYTES; | ||
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) { | ||
$limitInBytes = $constraint->maxSize * self::MB_BYTES; | ||
} else { | ||
if (!ctype_digit((string) $constraint->maxSize)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this can be removed now. The maxSize property could indeed be overridden manually but then, we could not rely on anything and would for example also need to check whether mimeTypes is an array at all. So I think we can just rely on the construct making it an integer. |
||
} | ||
$limitInBytes = min(UploadedFile::getMaxFilesize(), $limitInBytes); | ||
|
||
$limitInBytes = min(UploadedFile::getMaxFilesize(), (int) $constraint->maxSize); | ||
} else { | ||
$limitInBytes = UploadedFile::getMaxFilesize(); | ||
} | ||
|
@@ -125,24 +123,27 @@ public function validate($value, Constraint $constraint) | |
$sizeInBytes = filesize($path); | ||
$limitInBytes = (int) $constraint->maxSize; | ||
|
||
if (preg_match('/^\d++k$/', $constraint->maxSize)) { | ||
$limitInBytes *= self::KB_BYTES; | ||
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) { | ||
$limitInBytes *= self::MB_BYTES; | ||
} elseif (!ctype_digit((string) $constraint->maxSize)) { | ||
if (!ctype_digit((string) $constraint->maxSize)) { | ||
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize)); | ||
} | ||
|
||
if ($sizeInBytes > $limitInBytes) { | ||
// Convert the limit to the smallest possible number | ||
// (i.e. try "MB", then "kB", then "bytes") | ||
$coef = self::MB_BYTES; | ||
if (File::SIZE_FORMAT_DECIMAL === $constraint->maxSizeFormat) { | ||
$coef = self::MB_BYTES; | ||
$coefFactor = self::KB_BYTES; | ||
} else { | ||
$coef = self::MIB_BYTES; | ||
$coefFactor = self::KIB_BYTES; | ||
} | ||
|
||
$limitAsString = (string) ($limitInBytes / $coef); | ||
|
||
// Restrict the limit to 2 decimals (without rounding! we | ||
// need the precise value) | ||
while (self::moreDecimalsThan($limitAsString, 2)) { | ||
$coef /= 1000; | ||
$coef /= $coefFactor; | ||
$limitAsString = (string) ($limitInBytes / $coef); | ||
} | ||
|
||
|
@@ -152,7 +153,7 @@ public function validate($value, Constraint $constraint) | |
// If the size and limit produce the same string output | ||
// (due to rounding), reduce the coefficient | ||
while ($sizeAsString === $limitAsString) { | ||
$coef /= 1000; | ||
$coef /= $coefFactor; | ||
$limitAsString = (string) ($limitInBytes / $coef); | ||
$sizeAsString = (string) round($sizeInBytes / $coef, 2); | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Validator/Tests/Constraints/FileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraints\File; | ||
|
||
class FileTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @param mixed $maxSize | ||
* @param mixed $bytes | ||
* @dataProvider provideValidSizes | ||
*/ | ||
public function testMaxSize($maxSize, $bytes) | ||
{ | ||
$file = new File(array('maxSize' => $maxSize)); | ||
|
||
$this->assertSame($bytes, $file->maxSize); | ||
} | ||
|
||
/** | ||
* @param mixed $maxSize | ||
* @param mixed $bytes | ||
* @dataProvider provideInValidSizes | ||
* @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException | ||
*/ | ||
public function testInvalideMaxSize($maxSize) | ||
{ | ||
$file = new File(array('maxSize' => $maxSize)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provideValidSizes() | ||
{ | ||
return array( | ||
array('500', 500), | ||
array(12300, 12300), | ||
array('1ki', 1024), | ||
array('1KI', 1024), | ||
array('2k', 2000), | ||
array('2K', 2000), | ||
array('1mi', 1048576), | ||
array('1MI', 1048576), | ||
array('3m', 3000000), | ||
array('3M', 3000000), | ||
); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provideInvalidSizes() | ||
{ | ||
return array( | ||
array('+100'), | ||
array('foo'), | ||
array('1Ko'), | ||
array('1kio'), | ||
array('1G'), | ||
array('1Gi'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this makes sense to disallow. Since it's a public property it could be manually set after the constructor anyway. So I would make it so that one could specifiy the size in binary format but still set the formatting to decimal for the validation message. Only set it to binary when it's null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I would also rename the property to
$binaryFormat = null|false|true
when null then based on maxSize.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a previous PR, @fabpot says that he prefered to avoid adding another parameter (#10917 (comment)).
And I agree with him, the purpose of this new parameter will just be to let the user choise the format of the error message 😕
But you're right this trick is trying to avoid to expose a new paramater, but in the reality, there is a new parameter.
Should I leave maxSize has the user defined it. And expose 2 getter in Constraint\File: getMaxSizeBytes and getMaxSizeFormat ? But this is not what @webmozart expected in his ticket #10962
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see what is bad at having an option for binary/decimal validation format. For example I can image to have that configurable in a CMS which can be useful when you expect your users won't understand the binary suffixes.