summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul 'flowerysong' Arthur <flowerysong00@yahoo.com>2010-10-15 19:16:42 +0000
committerPaul 'flowerysong' Arthur <flowerysong00@yahoo.com>2010-10-15 19:16:42 +0000
commit63031fb6c1b3dc31d2d9cc1d0e7148c827656922 (patch)
tree5c3b60b2cffaff77b64461155fcaab9c345f4d1d
parent680e9f6206de1e17cdc63ebec498f6ebafa667b6 (diff)
downloadampache-63031fb6c1b3dc31d2d9cc1d0e7148c827656922.tar.gz
ampache-63031fb6c1b3dc31d2d9cc1d0e7148c827656922.tar.bz2
ampache-63031fb6c1b3dc31d2d9cc1d0e7148c827656922.zip
Use a function to format bytes -> human-readable sizes. Also bump catalog build
memory profiling logging to level 5; fixes FS#141.
-rw-r--r--lib/class/catalog.class.php23
-rw-r--r--lib/class/song.class.php2
-rw-r--r--lib/class/user.class.php18
-rw-r--r--lib/general.lib.php23
-rw-r--r--templates/show_duplicates.inc.php2
-rw-r--r--templates/show_song.inc.php2
-rw-r--r--templates/show_stats.inc.php4
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
@@ -111,6 +111,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
* value. We need a special function because PHP considers "false" to be true.
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 @@
<td class="cel_album"><?php echo $song->f_album_link; ?></td>
<td class="cel_length"><?php echo $song->f_time; ?></td>
<td class="cel_bitrate"><?php echo $song->f_bitrate; ?></td>
- <td class="cel_size"><?php echo $song->f_size; ?>MB</td>
+ <td class="cel_size"><?php echo $song->f_size; ?></td>
<td class="cel_filename"><?php echo scrub_out($song->file); ?></td>
</tr>
<?php
diff --git a/templates/show_song.inc.php b/templates/show_song.inc.php
index ab9dd9b7..48ee9471 100644
--- a/templates/show_song.inc.php
+++ b/templates/show_song.inc.php
@@ -53,7 +53,7 @@ $button_flip_state_id = 'button_flip_state_' . $song->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();
<td><?php echo $stats['songs']; ?></td>
<td><?php echo $stats['video']; ?></td>
<td><?php echo $stats['tags']; ?></td>
- <td><?php echo $stats['total_size']; ?> <?php echo $stats['size_unit']; ?></td>
+ <td><?php echo $stats['formatted_size']; ?></td>
<td><?php echo $stats['time_text']; ?></td>
</tr>
</table>
@@ -83,7 +83,7 @@ $catalogs = Catalog::get_catalogs();
<td class="cel_lastclean"><?php echo scrub_out($catalog->f_clean); ?></td>
<td class="cel_songs"><?php echo scrub_out($stats['songs']); ?></td>
<td class="cel_video"><?php echo scrub_out($stats['video']); ?></td>
- <td class="cel_total"><?php echo scrub_out($stats['total_size'] . ' ' . $stats['size_unit']); ?></td>
+ <td class="cel_total"><?php echo scrub_out($stats['formatted_size']); ?></td>
</tr>
<?php } ?>