Add CakeNumber::fromReadableSize() and Validation::filesize()

This commit is contained in:
Ceeram 2012-06-25 17:34:30 +02:00
parent ad5345887a
commit 6ec0afcf5d
5 changed files with 105 additions and 3 deletions

View file

@ -315,7 +315,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('23', 'y', '17', 'n'));
->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
@ -333,7 +333,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('999999', '23', 'n'));
->will($this->onConsecutiveCalls('999999', '24', 'n'));
$this->Task->expects($this->at(10))->method('out')
->with($this->stringContains('make a valid'));

View file

@ -523,4 +523,43 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* testFromReadableSize
*
* @dataProvider filesizes
* @return void
*/
public function testFromReadableSize($size, $expected) {
$result = $this->Number->fromReadableSize($size);
$this->assertEquals($expected, $result);
}
/**
* testFromReadableSize
*
* @expectedException CakeException
* @return void
*/
public function testFromReadableSizeException() {
$result = $this->Number->fromReadableSize('bogus');
}
/**
* filesizes dataprovider
*
* @return array
*/
public function filesizes() {
return array(
array('512B', 512),
array('1KB', 1024),
array('1.5KB', 1536),
array('1MB', 1048576),
array('1.5MB', 1572864),
array('1GB', 1073741824),
array('1.5GB', 1610612736),
array('512', 512),
);
}
}

View file

@ -2203,7 +2203,7 @@ class ValidationTest extends CakeTestCase {
}
/**
* testMimeType method
* testUploadError method
*
* @return void
*/
@ -2214,4 +2214,23 @@ class ValidationTest extends CakeTestCase {
$this->assertFalse(Validation::uploadError(2));
$this->assertFalse(Validation::uploadError(array('error' => 2)));
}
/**
* testFileSize method
*
* @return void
*/
public function testFileSize() {
$image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
$this->assertTrue(Validation::fileSize($image, '<', 1024));
$this->assertTrue(Validation::fileSize(array('tmp_name' => $image), 'isless', 1024));
$this->assertTrue(Validation::fileSize($image, '<', '1KB'));
$this->assertTrue(Validation::fileSize($image, '>=', 200));
$this->assertTrue(Validation::fileSize($image, '==', 201));
$this->assertTrue(Validation::fileSize($image, '==', '201B'));
$this->assertFalse(Validation::fileSize($image, 'isgreater', 1024));
$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
}
}

View file

@ -101,6 +101,28 @@ class CakeNumber {
}
}
/**
* Converts filesize from human readable string to bytes
*
* @param string $size Size in human readable string like '5MB'
* @return integer Bytes
*/
public static function fromReadableSize($size) {
if (ctype_digit($size)) {
return $size * 1;
}
$units = array('KB', 'MB', 'GB', 'TB', 'PB');
foreach ($units as $i => $unit) {
if ($unit == substr($size, -2)) {
return $size * pow(1024, $i + 1);
}
}
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
return $size * 1;
}
throw new CakeException(__d('cake_dev', 'No unit type.'));
}
/**
* Formats a number into a percentage string.
*

View file

@ -19,6 +19,7 @@
App::uses('Multibyte', 'I18n');
App::uses('File', 'Utility');
App::uses('CakeNumber', 'Utility');
// Load multibyte if the extension is missing.
if (!function_exists('mb_strlen')) {
class_exists('Multibyte');
@ -881,6 +882,27 @@ class Validation {
return in_array($mime, $mimeTypes);
}
/**
* Checks the filesize
*
* @param string|array $check
* @param integer|string $size Size in bytes or human readable string like '5MB'
* @param string $operator See `Validation::comparison()`
* @return boolean Success
*/
public static function fileSize($check, $operator = null, $size = null) {
if (is_array($check) && isset($check['tmp_name'])) {
$check = $check['tmp_name'];
}
if (is_string($size)) {
$size = CakeNumber::fromReadableSize($size);
}
$filesize = filesize($check);
return self::comparison($filesize, $operator, $size);
}
/**
* Checking for upload errors
*