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

Merged
merged 2 commits into from Sep 18, 2012

Projects

None yet

4 participants

@ceeram
Member
ceeram commented Sep 17, 2012

I still had this hanging around

@lorenzo lorenzo commented on an outdated diff Sep 17, 2012
lib/Cake/Utility/CakeNumber.php
+ * @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.'));
@lorenzo
lorenzo Sep 17, 2012 Member

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

@lorenzo
lorenzo Sep 17, 2012 Member

Default return would be a function param

@markstory
Member

I think this is a useful change and having additional basic validation methods for file uploads should make handling file uploads simpler for users.

@jrbasso jrbasso commented on the diff Sep 17, 2012
lib/Cake/Utility/CakeNumber.php
@@ -102,6 +102,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);
@jrbasso
jrbasso Sep 17, 2012 Member

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.

@ceeram
ceeram Sep 18, 2012 Member

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

thx for the pointer

@jrbasso
jrbasso Sep 18, 2012 Member

@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.

@ceeram
ceeram Sep 18, 2012 Member

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

@jrbasso
Member
jrbasso commented Sep 17, 2012

+1 for this change.

@markstory markstory merged commit 07c5102 into cakephp:2.3 Sep 18, 2012
@ceeram ceeram referenced this pull request Sep 18, 2012
Merged

2.3 cakenumber #856

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment