Skip to content

Commit 6ec0afc

Browse files
committed
Add CakeNumber::fromReadableSize() and Validation::filesize()
1 parent ad53458 commit 6ec0afc

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function testInteractiveFieldValidation() {
315315
$this->Task->initValidations();
316316
$this->Task->interactive = true;
317317
$this->Task->expects($this->any())->method('in')
318-
->will($this->onConsecutiveCalls('23', 'y', '17', 'n'));
318+
->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
319319

320320
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
321321
$expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
@@ -333,7 +333,7 @@ public function testInteractiveFieldValidationWithBogusResponse() {
333333
$this->Task->interactive = true;
334334

335335
$this->Task->expects($this->any())->method('in')
336-
->will($this->onConsecutiveCalls('999999', '23', 'n'));
336+
->will($this->onConsecutiveCalls('999999', '24', 'n'));
337337

338338
$this->Task->expects($this->at(10))->method('out')
339339
->with($this->stringContains('make a valid'));

lib/Cake/Test/Case/Utility/CakeNumberTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,43 @@ public function testToPercentage() {
523523
$this->assertEquals($expected, $result);
524524
}
525525

526+
/**
527+
* testFromReadableSize
528+
*
529+
* @dataProvider filesizes
530+
* @return void
531+
*/
532+
public function testFromReadableSize($size, $expected) {
533+
$result = $this->Number->fromReadableSize($size);
534+
$this->assertEquals($expected, $result);
535+
}
536+
537+
/**
538+
* testFromReadableSize
539+
*
540+
* @expectedException CakeException
541+
* @return void
542+
*/
543+
public function testFromReadableSizeException() {
544+
$result = $this->Number->fromReadableSize('bogus');
545+
}
546+
547+
/**
548+
* filesizes dataprovider
549+
*
550+
* @return array
551+
*/
552+
public function filesizes() {
553+
return array(
554+
array('512B', 512),
555+
array('1KB', 1024),
556+
array('1.5KB', 1536),
557+
array('1MB', 1048576),
558+
array('1.5MB', 1572864),
559+
array('1GB', 1073741824),
560+
array('1.5GB', 1610612736),
561+
array('512', 512),
562+
);
563+
}
564+
526565
}

lib/Cake/Test/Case/Utility/ValidationTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ public function testMimeTypeFalse() {
22032203
}
22042204

22052205
/**
2206-
* testMimeType method
2206+
* testUploadError method
22072207
*
22082208
* @return void
22092209
*/
@@ -2214,4 +2214,23 @@ public function testUploadError() {
22142214
$this->assertFalse(Validation::uploadError(2));
22152215
$this->assertFalse(Validation::uploadError(array('error' => 2)));
22162216
}
2217+
2218+
/**
2219+
* testFileSize method
2220+
*
2221+
* @return void
2222+
*/
2223+
public function testFileSize() {
2224+
$image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
2225+
$this->assertTrue(Validation::fileSize($image, '<', 1024));
2226+
$this->assertTrue(Validation::fileSize(array('tmp_name' => $image), 'isless', 1024));
2227+
$this->assertTrue(Validation::fileSize($image, '<', '1KB'));
2228+
$this->assertTrue(Validation::fileSize($image, '>=', 200));
2229+
$this->assertTrue(Validation::fileSize($image, '==', 201));
2230+
$this->assertTrue(Validation::fileSize($image, '==', '201B'));
2231+
2232+
$this->assertFalse(Validation::fileSize($image, 'isgreater', 1024));
2233+
$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
2234+
}
2235+
22172236
}

lib/Cake/Utility/CakeNumber.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ public static function toReadableSize($size) {
101101
}
102102
}
103103

104+
/**
105+
* Converts filesize from human readable string to bytes
106+
*
107+
* @param string $size Size in human readable string like '5MB'
108+
* @return integer Bytes
109+
*/
110+
public static function fromReadableSize($size) {
111+
if (ctype_digit($size)) {
112+
return $size * 1;
113+
}
114+
$units = array('KB', 'MB', 'GB', 'TB', 'PB');
115+
foreach ($units as $i => $unit) {
116+
if ($unit == substr($size, -2)) {
117+
return $size * pow(1024, $i + 1);
118+
}
119+
}
120+
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
121+
return $size * 1;
122+
}
123+
throw new CakeException(__d('cake_dev', 'No unit type.'));
124+
}
125+
104126
/**
105127
* Formats a number into a percentage string.
106128
*

lib/Cake/Utility/Validation.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
App::uses('Multibyte', 'I18n');
2121
App::uses('File', 'Utility');
22+
App::uses('CakeNumber', 'Utility');
2223
// Load multibyte if the extension is missing.
2324
if (!function_exists('mb_strlen')) {
2425
class_exists('Multibyte');
@@ -881,6 +882,27 @@ public static function mimeType($check, $mimeTypes = array()) {
881882
return in_array($mime, $mimeTypes);
882883
}
883884

885+
/**
886+
* Checks the filesize
887+
*
888+
* @param string|array $check
889+
* @param integer|string $size Size in bytes or human readable string like '5MB'
890+
* @param string $operator See `Validation::comparison()`
891+
* @return boolean Success
892+
*/
893+
public static function fileSize($check, $operator = null, $size = null) {
894+
if (is_array($check) && isset($check['tmp_name'])) {
895+
$check = $check['tmp_name'];
896+
}
897+
898+
if (is_string($size)) {
899+
$size = CakeNumber::fromReadableSize($size);
900+
}
901+
$filesize = filesize($check);
902+
903+
return self::comparison($filesize, $operator, $size);
904+
}
905+
884906
/**
885907
* Checking for upload errors
886908
*

0 commit comments

Comments
 (0)