8000 [Validator] Support "maxSize" given in KiB by jderusse · Pull Request #11027 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Support "maxSize" given in KiB #11027

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Convert File maxSize into Bytes on construction
  • Loading branch information
Jérémy Derussé committed May 31, 2014
commit 35a070862e4ed0c8ee055e75b923cdffc6f5fc19
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Constraints/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @Annotation
Expand All @@ -38,4 +39,25 @@ 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)
{
parent::__construct($options);

if ($this->maxSize) {
if (ctype_digit((string) $this->maxSize)) {
$this->maxSize = (int) $this->maxSize;
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000;
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize * 1000000;
} elseif (preg_match('/^\d++ki$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 10;
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
$this->maxSize = $this->maxSize << 20;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
}
}
}
17 changes: 4 additions & 13 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
Expand Down Expand Up @@ -125,11 +120,7 @@ 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));
}

Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/FileTest.php
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'),
);
}
}
0