From 36ea283f1b345fcba3d4d343c81fa95bf3e7877b Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 25 Jul 2018 10:26:19 +0200 Subject: [PATCH] Rename and update bytesToSizeString function and move it to heleprs.php with human_filesize name --- Blacklight/http/XML_Response.php | 2 +- Blacklight/utility/Utility.php | 32 ------------------------ Changelog | 1 + app/Extensions/helper/helpers.php | 20 +++++++++++++-- app/Http/Controllers/QueueController.php | 10 ++++---- 5 files changed, 25 insertions(+), 40 deletions(-) diff --git a/Blacklight/http/XML_Response.php b/Blacklight/http/XML_Response.php index 9a7824169..3bce55f85 100755 --- a/Blacklight/http/XML_Response.php +++ b/Blacklight/http/XML_Response.php @@ -532,7 +532,7 @@ class XML_Response "src=\"{$this->server['server']['url']}/covers/{$dir}/{$this->release->$column}{$dcov}.jpg\" ". "width=\"120\" alt=\"{$this->release->searchname}\" />\n"; } - $size = Utility::bytesToSizeString($this->release->size); + $size = human_filesize($this->release->size); $this->cdata .= "\t
  • ID: server['server']['url']}/details/{$this->release->guid}\">{$this->release->guid}
  • \n". "\t
  • Name: {$this->release->searchname}
  • \n". diff --git a/Blacklight/utility/Utility.php b/Blacklight/utility/Utility.php index 736ab8b25..22b621f96 100755 --- a/Blacklight/utility/Utility.php +++ b/Blacklight/utility/Utility.php @@ -258,17 +258,6 @@ class Utility return $prefix.implode(DS, $elements); } - /** - * @param $text - */ - public static function stripBOM(&$text): void - { - $bom = pack('CCC', 0xef, 0xbb, 0xbf); - if (0 === strncmp($text, $bom, 3)) { - $text = substr($text, 3); - } - } - /** * Strips non-printing characters from a string. * @@ -509,27 +498,6 @@ class Utility return $buffer; } - /** - * Get human readable size string from bytes. - * - * @param int $size Bytes number to convert. - * @param int $precision How many floating point units to add. - * - * @return string - */ - public static function bytesToSizeString($size, $precision = 0): string - { - static $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - $step = 1024; - $i = 0; - while (($size / $step) > 0.9) { - $size /= $step; - $i++; - } - - return round($size, $precision).$units[$i]; - } - /** * @param array $options * diff --git a/Changelog b/Changelog index ed6b57760..dab8a0e24 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-07-25 DariusIII + * Chg: Rename and update bytesToSizeString function and move it to heleprs.php with human_filesize name * Fix: Fix wrong namespace for Blacklight\utility\Utility class in XML_Response class 2018-07-24 DariusIII * Chg: Small updates/fixed for used regexes in Categorize class diff --git a/app/Extensions/helper/helpers.php b/app/Extensions/helper/helpers.php index 4b840b54b..01acaf8ea 100644 --- a/app/Extensions/helper/helpers.php +++ b/app/Extensions/helper/helpers.php @@ -165,9 +165,8 @@ if (! function_exists('makeFieldLinks')) { if (! function_exists('createGUID')) { /** - * Create a GUID for a release. - * * @return string + * @throws \Exception */ function createGUID(): string { @@ -202,4 +201,21 @@ if (! function_exists('makeFieldLinks')) { return new Color($string); } } + + if (! function_exists('human_filesize')) { + + /** + * @param $bytes + * @param int $decimals + * + * @return string + */ + function human_filesize($bytes, $decimals = 0): string + { + $size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; + $factor = floor((\strlen($bytes) - 1) / 3); + + return round(sprintf("%.{$decimals}f", $bytes / (1024 ** $factor))) . @$size[$factor]; + } + } } diff --git a/app/Http/Controllers/QueueController.php b/app/Http/Controllers/QueueController.php index 4ce8dfb59..f9aaefd57 100644 --- a/app/Http/Controllers/QueueController.php +++ b/app/Http/Controllers/QueueController.php @@ -126,11 +126,11 @@ class QueueController extends BasePageController if ($status !== false) { $output .= "
    -
    Avg Speed:
    ".Utility::bytesToSizeString($status['AverageDownloadRate'], 2)."/s
    -
    Speed:
    ".Utility::bytesToSizeString($status['DownloadRate'], 2)."/s
    -
    Limit:
    ".Utility::bytesToSizeString($status['DownloadLimit'], 2)."/s
    -
    Queue Left(no pars):
    ".Utility::bytesToSizeString($status['RemainingSizeLo'], 2)."
    -
    Free Space:
    ".Utility::bytesToSizeString($status['FreeDiskSpaceMB'] * 1024000, 2)."
    +
    Avg Speed:
    ".human_filesize($status['AverageDownloadRate'], 2)."/s
    +
    Speed:
    ".human_filesize($status['DownloadRate'], 2)."/s
    +
    Limit:
    ".human_filesize($status['DownloadLimit'], 2)."/s
    +
    Queue Left(no pars):
    ".human_filesize($status['RemainingSizeLo'], 2)."
    +
    Free Space:
    ".human_filesize($status['FreeDiskSpaceMB'] * 1024000, 2)."
    Status:
    ".($status['Download2Paused'] === 1 ? 'Paused' : 'Downloading').'
    '; }