diff options
author | Paul Arthur <paul.arthur@flowerysong.com> | 2013-01-26 02:36:58 -0500 |
---|---|---|
committer | Paul Arthur <paul.arthur@flowerysong.com> | 2013-01-26 02:36:58 -0500 |
commit | 8a750c3e875d590d351c3042570a134fcdf03e5d (patch) | |
tree | 846df051bf732bd588cd1571d1727123493cef5b /lib/class | |
parent | a12679b13d8d06c87308b1d26bc23c6b4fa5d92e (diff) | |
download | ampache-8a750c3e875d590d351c3042570a134fcdf03e5d.tar.gz ampache-8a750c3e875d590d351c3042570a134fcdf03e5d.tar.bz2 ampache-8a750c3e875d590d351c3042570a134fcdf03e5d.zip |
Move [un]format_bytes() from general.lib.php to UI
Diffstat (limited to 'lib/class')
-rw-r--r-- | lib/class/catalog.class.php | 8 | ||||
-rw-r--r-- | lib/class/song.class.php | 2 | ||||
-rw-r--r-- | lib/class/ui.class.php | 54 | ||||
-rw-r--r-- | lib/class/user.class.php | 2 |
4 files changed, 60 insertions, 6 deletions
diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index 9a75a5ea..3833d329 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -188,7 +188,7 @@ class Catalog extends database_object { $hours = floor($results['time'] / 3600); - $results['formatted_size'] = format_bytes($results['size']); + $results['formatted_size'] = UI::format_bytes($results['size']); $days = floor($hours / 24); $hours = $hours % 24; @@ -422,7 +422,7 @@ class Catalog extends database_object { public function add_files($path, $options) { // Profile the memory a bit - debug_event('Memory', format_bytes(memory_get_usage(true)), 5); + debug_event('Memory', UI::format_bytes(memory_get_usage(true)), 5); // See if we want a non-root path for the add if (isset($options['subdirectory'])) { @@ -457,7 +457,7 @@ class Catalog extends database_object { // Ensure that we've got our cache $this->_create_filecache(); - debug_event('Memory', format_bytes(memory_get_usage(true)), 5); + debug_event('Memory', UI::format_bytes(memory_get_usage(true)), 5); /* Recurse through this dir and create the files array */ while ( false !== ( $file = readdir($handle) ) ) { @@ -466,7 +466,7 @@ class Catalog extends database_object { if (substr($file,0,1) == '.') { continue; } debug_event('read',"Starting work on $file inside $path",'5','ampache-catalog'); - debug_event('Memory', format_bytes(memory_get_usage(true)), 5); + debug_event('Memory', UI::format_bytes(memory_get_usage(true)), 5); /* Create the new path */ $full_file = $path.$slash_type.$file; diff --git a/lib/class/song.class.php b/lib/class/song.class.php index b547f71a..ac25b1a0 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -795,7 +795,7 @@ class Song extends database_object implements media { $this->f_tags = Tag::get_display($tags,$this->id,'song'); // Format the size - $this->f_size = format_bytes($this->size); + $this->f_size = UI::format_bytes($this->size); return true; diff --git a/lib/class/ui.class.php b/lib/class/ui.class.php index f37c5a36..ae4ec091 100644 --- a/lib/class/ui.class.php +++ b/lib/class/ui.class.php @@ -105,6 +105,60 @@ class UI { } /** + * format_bytes + * + * Turns a size in bytes into the best human-readable value + */ + public static function format_bytes($value, $precision = 2) { + $pass = 0; + while (strlen(floor($value)) > 3) { + $value /= 1024; + $pass++; + } + + switch ($pass) { + case 1: $unit = 'kB'; break; + case 2: $unit = 'MB'; break; + case 3: $unit = 'GB'; break; + case 4: $unit = 'TB'; break; + case 5: $unit = 'PB'; break; + default: $unit = 'B'; break; + } + + return round($value, $precision) . ' ' . $unit; + } + + /** + * unformat_bytes + * + * Parses a human-readable size + */ + public static function unformat_bytes($value) { + if (preg_match('/^([0-9]+) *([[:alpha:]]+)$/', $value, $matches)) { + $value = $matches[1]; + $unit = strtolower(substr($matches[2], 0, 1)); + } + else { + return $value; + } + + switch($unit) { + case 'p': + $value *= 1024; + case 't': + $value *= 1024; + case 'g': + $value *= 1024; + case 'm': + $value *= 1024; + case 'k': + $value *= 1024; + } + + return $value; + } + + /** * get_icon * * Returns an <img> tag for the specified icon diff --git a/lib/class/user.class.php b/lib/class/user.class.php index 0ee517f2..01f709f2 100644 --- a/lib/class/user.class.php +++ b/lib/class/user.class.php @@ -697,7 +697,7 @@ class User extends database_object { $total = $total + $r['size']; } - $this->f_useage = format_bytes($total); + $this->f_useage = UI::format_bytes($total); /* Get Users Last ip */ if (count($data = $this->get_ip_history(1))) { |