From 63031fb6c1b3dc31d2d9cc1d0e7148c827656922 Mon Sep 17 00:00:00 2001 From: Paul 'flowerysong' Arthur Date: Fri, 15 Oct 2010 19:16:42 +0000 Subject: Use a function to format bytes -> human-readable sizes. Also bump catalog build memory profiling logging to level 5; fixes FS#141. --- lib/class/catalog.class.php | 23 ++++------------------- lib/class/song.class.php | 2 +- lib/class/user.class.php | 18 +----------------- lib/general.lib.php | 23 +++++++++++++++++++++++ templates/show_duplicates.inc.php | 2 +- templates/show_song.inc.php | 2 +- templates/show_stats.inc.php | 4 ++-- 7 files changed, 33 insertions(+), 41 deletions(-) diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index 8374bd36..cda423c5 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -202,22 +202,7 @@ class Catalog extends database_object { $hours = floor($results['time']/3600); - // Convert size to megabytes - $size = $results['size']/1048576; - - // Do pretty formatting and final unit conversion if needed - if ($size > 1048576) { - $results['total_size'] = sprintf("%.2f",($size/1048576)); - $results['size_unit'] = 'TB'; - } - else if ($size > 1024) { - $results['total_size'] = sprintf("%.2f",($size/1024)); - $results['size_unit'] = 'GB'; - } - else { - $results['total_size'] = sprintf("%.2f",$size); - $results['size_unit'] = 'MB'; - } + $results['formatted_size'] = format_bytes($results['size']); $days = floor($hours/24); $hours = $hours%24; @@ -450,7 +435,7 @@ class Catalog extends database_object { public function add_files($path,$options) { // Profile the memory a bit - debug_event('Memory',memory_get_usage(true)/1024/1024 . "MB",1); + debug_event('Memory', format_bytes(memory_get_usage(true)), 5); // See if we want a non-root path for the add if (isset($options['subdirectory'])) { @@ -485,7 +470,7 @@ class Catalog extends database_object { // Ensure that we've got our cache $this->_create_filecache(); - debug_event('Memory',memory_get_usage(true)/1024/1024 . "MB",1); + debug_event('Memory', format_bytes(memory_get_usage(true)), 5); // Set the base "ticker" we will only update ever 5+ seconds $ticker = time(); @@ -497,7 +482,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',memory_get_usage(true)/1024/1024 . "MB",1); + debug_event('Memory', 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 48ddd451..b9f257a7 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -707,7 +707,7 @@ class Song extends database_object implements media { $this->f_tags = Tag::get_display($tags,$this->id,'song'); // Format the size - $this->f_size = sprintf("%.2f",($this->size/1048576)); + $this->f_size = format_bytes($this->size); return true; diff --git a/lib/class/user.class.php b/lib/class/user.class.php index de759378..7649df31 100644 --- a/lib/class/user.class.php +++ b/lib/class/user.class.php @@ -699,23 +699,7 @@ class User extends database_object { $total = $total + $r['size']; } - $divided = 0; - - while (strlen(floor($total)) > 3) { - $total = ($total / 1024); - $divided++; - } - - switch ($divided) { - default: - case '1': $name = "KB"; break; - case '2': $name = "MB"; break; - case '3': $name = "GB"; break; - case '4': $name = "TB"; break; - case '5': $name = "PB"; break; - } // end switch - - $this->f_useage = round($total,2) . $name; + $this->f_useage = format_bytes($total); /* Get Users Last ip */ if (count($data = $this->get_ip_history(1))) { diff --git a/lib/general.lib.php b/lib/general.lib.php index 8033c927..c941bae9 100644 --- a/lib/general.lib.php +++ b/lib/general.lib.php @@ -110,6 +110,29 @@ function unhtmlentities($string) { } //unhtmlentities +/** + * format_bytes + * Turns a size in bytes into a human-readable value + */ +function format_bytes($value, $precision = 2) { + $divided = 0; + while (strlen(floor($value)) > 3) { + $value = ($value / 1024); + $divided++; + } + + switch ($divided) { + 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; + } // end switch + + return round($value, $precision) . ' ' . $unit; +} + /** * make_bool * This takes a value and returns what we consider to be the correct boolean diff --git a/templates/show_duplicates.inc.php b/templates/show_duplicates.inc.php index d22552a6..9c57a049 100644 --- a/templates/show_duplicates.inc.php +++ b/templates/show_duplicates.inc.php @@ -65,7 +65,7 @@ f_album_link; ?> f_time; ?> f_bitrate; ?> - f_size; ?>MB + f_size; ?> file); ?> id; $songprops[gettext_noop('Catalog Number')] = scrub_out($song->catalog_number); $songprops[gettext_noop('Bitrate')] = scrub_out($song->f_bitrate); if (Access::check('interface','75')) { - $songprops[gettext_noop('Filename')] = scrub_out($song->file) . " " . $song->f_size . "MB"; + $songprops[gettext_noop('Filename')] = scrub_out($song->file) . " " . $song->f_size; } if ($song->update_time) { $songprops[gettext_noop('Last Updated')] = date("d/m/Y H:i",$song->update_time); diff --git a/templates/show_stats.inc.php b/templates/show_stats.inc.php index 142ebcf6..98aac9c0 100644 --- a/templates/show_stats.inc.php +++ b/templates/show_stats.inc.php @@ -44,7 +44,7 @@ $catalogs = Catalog::get_catalogs(); - + @@ -83,7 +83,7 @@ $catalogs = Catalog::get_catalogs(); f_clean); ?> - + -- cgit