Skip to content
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
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
/**
* @Annotation
@@ -23,7 +24,11 @@
*/
class File extends Constraint
{
const SIZE_FORMAT_BINARY = 2;
const SIZE_FORMAT_DECIMAL = 10;
public $maxSize = null;
public $maxSizeFormat = self::SIZE_FORMAT_BINARY;
public $mimeTypes = array();
public $notFoundMessage = 'The file could not be found.';
public $notReadableMessage = 'The file is not readable.';
@@ -38,4 +43,37 @@ class File extends Constraint
public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
public $uploadErrorMessage = 'The file could not be uploaded.';
public function __construct($options = null)
{
if (is_array($options) && array_key_exists('maxSizeFormat', $options)) {
throw new ConstraintDefinitionException(sprintf(
'The option "maxSizeFormat" is not supported by the constraint %s',

This comment has been minimized.

Copy link
@Tobion

Tobion Jun 1, 2014

Member

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.

This comment has been minimized.

Copy link
@Tobion

Tobion Jun 1, 2014

Member

Then I would also rename the property to $binaryFormat = null|false|true when null then based on maxSize.

This comment has been minimized.

Copy link
@jderusse

jderusse Jun 1, 2014

Author Contributor

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

This comment has been minimized.

Copy link
@Tobion

Tobion Jun 1, 2014

Member

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.

__CLASS__
));
}
parent::__construct($options);
if ($this->maxSize) {
if (ctype_digit((string) $this->maxSize)) {
$this->maxSize = (int) $this->maxSize;
$this->maxSizeFormat = self::SIZE_FORMAT_DECIMAL;
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000;
$this->maxSizeFormat = self::SIZE_FORMAT_DECIMAL;
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000000;
$this->maxSizeFormat = self::SIZE_FORMAT_DECIMAL;
} elseif (preg_match('/^\d++ki$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 10;
$this->maxSizeFormat = self::SIZE_FORMAT_BINARY;
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 20;
$this->maxSizeFormat = self::SIZE_FORMAT_BINARY;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
}
}
}
@@ -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));

This comment has been minimized.

Copy link
@Tobion

Tobion Jun 1, 2014

Member

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);
}
@@ -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'),
);
}
}
@@ -99,27 +99,36 @@ public function provideMaxSizeExceededTests()
// round(size) == 1.01kB, limit == 1kB
array(ceil(1.005*1000), 1000, '1.01', '1', 'kB'),
array(ceil(1.005*1000), '1k', '1.01', '1', 'kB'),
array(ceil(1.005*1024), '1ki', '1.01', '1', 'KiB'),
// round(size) == 1kB, limit == 1kB -> use bytes
array(ceil(1.004*1000), 1000, '1004', '1000', 'bytes'),
array(ceil(1.004*1000), '1k', '1004', '1000', 'bytes'),
array(ceil(1.004*1024), '1ki', '1029', '1024', 'bytes'),
array(1000 + 1, 1000, '1001', '1000', 'bytes'),
array(1000 + 1, '1k', '1001', '1000', 'bytes'),
array(1024 + 1, '1ki', '1025', '1024', 'bytes'),
// round(size) == 1.01MB, limit == 1MB
array(ceil(1.005*1000*1000), 1000*1000, '1.01', '1', 'MB'),
array(ceil(1.005*1000*1000), '1000k', '1.01', '1', 'MB'),
array(ceil(1.005*1000*1000), '1M', '1.01', '1', 'MB'),
array(ceil(1.005*1024*1024), '1024ki', '1.01', '1', 'MiB'),
array(ceil(1.005*1024*1024), '1Mi', '1.01', '1', 'MiB'),
// round(size) == 1MB, limit == 1MB -> use kB
array(ceil(1.004*1000*1000), 1000*1000, '1004', '1000', 'kB'),
array(ceil(1.004*1000*1000), '1000k', '1004', '1000', 'kB'),
array(ceil(1.004*1000*1000), '1M', '1004', '1000', 'kB'),
array(ceil(1.004*1024*1024), '1024ki', '1028.1', '1024', 'KiB'),
array(ceil(1.004*1024*1024), '1Mi', '1028.1', '1024', 'KiB'),
array(1000*1000 + 1, 1000*1000, '1000001', '1000000', 'bytes'),
array(1000*1000 + 1, '1000k', '1000001', '1000000', 'bytes'),
array(1000*1000 + 1, '1M', '1000001', '1000000', 'bytes'),
array(1024*1024 + 1, '1024ki', '1048577', '1048576', 'bytes'),
array(1024*1024 + 1, '1Mi', '1048577', '1048576', 'bytes'),
);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.