From b176a68d4d06d0ce99523eedaa4cfbb6d4c29055 Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Wed, 29 May 2024 15:35:26 -0600 Subject: [PATCH] Replace TextSinglePageSizeNumber and TextSinglePageSizeUnit with TextSinglePageSizeFormatted --- lib/Ebook.php | 41 ++++++++++++++++++----------------------- www/ebooks/ebook.php | 2 +- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/Ebook.php b/lib/Ebook.php index 0ba4dd88..f3c124b6 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -41,8 +41,7 @@ use function Safe\shell_exec; * @property string $TitleWithCreditsHtml * @property string $TextUrl * @property string $TextSinglePageUrl - * @property string $TextSinglePageSizeNumber - * @property string $TextSinglePageSizeUnit + * @property string $TextSinglePageSizeFormatted * @property string $IndexableText */ class Ebook{ @@ -110,8 +109,7 @@ class Ebook{ protected ?string $_TitleWithCreditsHtml = null; protected ?string $_TextUrl = null; protected ?string $_TextSinglePageUrl = null; - protected ?string $_TextSinglePageSizeNumber = null; - protected ?string $_TextSinglePageSizeUnit = null; + protected ?string $_TextSinglePageSizeFormatted = null; protected ?string $_IndexableText = null; // ******* @@ -523,29 +521,26 @@ class Ebook{ return $this->_TextSinglePageUrl; } - protected function GetTextSinglePageSizeNumber(): string{ - if($this->_TextSinglePageSizeNumber === null){ - $sizes = 'BKMGTP'; - $factor = intval(floor((strlen((string)$this->TextSinglePageByteCount) - 1) / 3)); - try{ - $this->_TextSinglePageSizeNumber = sprintf('%.1f', $this->TextSinglePageByteCount / pow(1024, $factor)); + protected function GetTextSinglePageSizeFormatted(): string{ + if($this->_TextSinglePageSizeFormatted === null){ + $bytes = $this->TextSinglePageByteCount; + $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); + + $index = 0; + while($bytes >= 1024 && $index < count($sizes) - 1){ + $bytes /= 1024; + $index++; } - catch(\DivisionByZeroError){ - $this->_TextSinglePageSizeNumber = '0'; + + if($index == 0){ + // No decimal point for smaller than a KB. + $this->_TextSinglePageSizeFormatted = sprintf("%d %s", $bytes, $sizes[$index]); + }else{ + $this->_TextSinglePageSizeFormatted = sprintf("%.1f %s", $bytes, $sizes[$index]); } } - return $this->_TextSinglePageSizeNumber; - } - - protected function GetTextSinglePageSizeUnit(): string{ - if($this->_TextSinglePageSizeUnit === null){ - $sizes = 'BKMGTP'; - $factor = intval(floor((strlen((string)$this->TextSinglePageByteCount) - 1) / 3)); - $this->_TextSinglePageSizeUnit = $sizes[$factor] ?? ''; - } - - return $this->_TextSinglePageSizeUnit; + return $this->_TextSinglePageSizeFormatted; } protected function GetIndexableText(): string{ diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php index 2aeff858..f69e4e57 100644 --- a/www/ebooks/ebook.php +++ b/www/ebooks/ebook.php @@ -289,7 +289,7 @@ catch(Exceptions\EbookNotFoundException){ TextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?> class="has-size"> - Read on one pageTextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?>TextSinglePageSizeNumber ?>MB + Read on one pageTextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?>TextSinglePageSizeFormatted ?>