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

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

Merged
merged 2 commits into from Sep 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -315,7 +315,7 @@ public function testInteractiveFieldValidation() {
$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');
Expand All @@ -333,7 +333,7 @@ public function testInteractiveFieldValidationWithBogusResponse() {
$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'));
Expand Down
40 changes: 40 additions & 0 deletions lib/Cake/Test/Case/Utility/CakeNumberTest.php
Expand Up @@ -523,4 +523,44 @@ public function testToPercentage() {
$this->assertEquals($expected, $result);
}

/**
* testFromReadableSize
*
* @dataProvider filesizes
* @return void
*/
public function testFromReadableSize($params, $expected) {
$result = $this->Number->fromReadableSize($params['size'], $params['default']);
$this->assertEquals($expected, $result);
}

/**
* testFromReadableSize
*
* @expectedException CakeException
* @return void
*/
public function testFromReadableSizeException() {
$result = $this->Number->fromReadableSize('bogus', false);
}

/**
* filesizes dataprovider
*
* @return array
*/
public function filesizes() {
return array(
array(array('size' => '512B', 'default' => false), 512),
array(array('size' => '1KB', 'default' => false), 1024),
array(array('size' => '1.5KB', 'default' => false), 1536),
array(array('size' => '1MB', 'default' => false), 1048576),
array(array('size' => '1.5MB', 'default' => false), 1572864),
array(array('size' => '1GB', 'default' => false), 1073741824),
array(array('size' => '1.5GB', 'default' => false), 1610612736),
array(array('size' => '512', 'default' => 'Unknown type'), 512),
array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
);
}

}
21 changes: 20 additions & 1 deletion lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -2203,7 +2203,7 @@ public function testMimeTypeFalse() {
}

/**
* testMimeType method
* testUploadError method
*
* @return void
*/
Expand All @@ -2214,4 +2214,23 @@ public function testUploadError() {
$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'));
}

}
26 changes: 26 additions & 0 deletions lib/Cake/Utility/CakeNumber.php
Expand Up @@ -101,6 +101,32 @@ public static function toReadableSize($size) {
}
}

/**
* Converts filesize from human readable string to bytes
*
* @param string $size Size in human readable string like '5MB'
* @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type'
* @return integer Bytes
*/
public static function fromReadableSize($size, $default = false) {
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't remove the unit from $size?

Btw, to simplify a bit, you could get the last 2 chars outside, once size never changes. Also, you could override the foreach using array_search + if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrbasso its merge din already like this, but this is what you had in mind?: 90c32ad

thx for the pointer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ceeram The array_search portion looks good. But the multiplication point still kind of weird to me. For example, if $size is 10MB, you will have '10MB' * pow(1024, 1 + 1). Seems it is working, but is kind of weird to do math with 'MB' in the variable.

Btw, to avoid a issue in the future, what you think in use strtoupper in the unit? It will allow '10mb' as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, seems odd to calcaluate with MB in the strings, pow() converts all to numeric, but i think its clearer if i take it out.
Also will make it strtoupper

}
}
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
return $size * 1;
}
if ($default !== false) {
return $default;
}
throw new CakeException(__d('cake_dev', 'No unit type.'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a default return? throwing an exception sounds a bit too much for view related code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default return would be a function param

}

/**
* Formats a number into a percentage string.
*
Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -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');
Expand Down Expand Up @@ -881,6 +882,27 @@ public static function mimeType($check, $mimeTypes = array()) {
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
*
Expand Down