From dbc2fd5fcd80bc580282cfaada7967193b2a824a Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 9 Jul 2022 10:28:13 -0500 Subject: [PATCH] Improve bulk downloads page --- README.md | 2 +- lib/Formatter.php | 26 +++++++++++++++++ scripts/generate-monthly-downloads | 4 +++ www/css/core.css | 39 +++++++++++++++++++++++--- www/patrons-circle/downloads/index.php | 33 ++++++++++++++++------ 5 files changed, 91 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index efd6aede..59b26b2c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ PHP 7+ is required. ```shell # Install Apache, PHP, PHP-FPM, and various other dependencies. -sudo apt install -y git composer php-fpm php-cli php-gd php-xml php-apcu php-mbstring php-intl php-curl php-zip apache2 apache2-utils libfcgi0ldbl task-spooler ipv6calc mariadb-server libaprutil1-dbd-mysql +sudo apt install -y git composer php-fpm php-cli php-gd php-xml php-apcu php-mbstring php-intl php-curl php-zip apache2 apache2-utils libfcgi0ldbl task-spooler ipv6calc mariadb-server libaprutil1-dbd-mysql attr # Create the site root and logs root and clone this repo into it. sudo mkdir /standardebooks.org/ diff --git a/lib/Formatter.php b/lib/Formatter.php index a932258a..eb63c367 100644 --- a/lib/Formatter.php +++ b/lib/Formatter.php @@ -39,4 +39,30 @@ class Formatter{ public static function ToPlainXmlText(?string $text): string{ return htmlspecialchars(trim($text ?? ''), ENT_QUOTES|ENT_XML1, 'utf-8'); } + + public static function ToFileSize(?int $bytes): string{ + // See https://stackoverflow.com/a/5501447 + $output = ''; + + if($bytes >= 1073741824){ + $output = number_format(round($bytes / 1073741824, 1), 1) . 'G'; + } + elseif($bytes >= 1048576){ + $output = number_format(round($bytes / 1048576, 1), 1) . 'M'; + } + elseif($bytes >= 1024){ + $output = number_format($bytes / 1024, 2) . 'KB'; + } + elseif($bytes > 1){ + $output = $bytes . ' bytes'; + } + elseif($bytes == 1){ + $output = $bytes . ' byte'; + } + else{ + $output = '0 bytes'; + } + + return $output; + } } diff --git a/scripts/generate-monthly-downloads b/scripts/generate-monthly-downloads index 72f176a8..36abec49 100755 --- a/scripts/generate-monthly-downloads +++ b/scripts/generate-monthly-downloads @@ -93,5 +93,9 @@ foreach($ebooksByMonth as $month => $ebooks){ $zip->close(); rename($tempFilename, $filePath); + + // Set a filesystem attribute for the number of ebooks in the file. This will be used + // to display that number on the downloads page. + exec('attr -q -s ebook-count -V ' . escapeshellarg(sizeof($ebooks)) . ' ' . escapeshellarg($filePath)); } } diff --git a/www/css/core.css b/www/css/core.css index 2f802b9c..5ba6b2fb 100644 --- a/www/css/core.css +++ b/www/css/core.css @@ -679,18 +679,40 @@ ul.message.error li:only-child{ margin-right: auto; } -ul.download-list i{ +ul.download-list thead{ + font-style: italic; +} + +ul.download-list table thead td{ + padding-top: 0; + padding-bottom: 0; +} + +ul.download-list table td{ + padding: .25rem; +} + +ul.download-list table td + td{ + text-align: right; + white-space: nowrap; + +} + +ul.download-list tbody td + td{ color: var(--sub-text); } +main > section.narrow > ul.download-list{ + width: auto; + max-width: none; +} + ul.download-list{ display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4rem; list-style: none; margin-top: 4rem; - margin-left: auto; - margin-right: auto; } ul.download-list > li{ @@ -706,8 +728,17 @@ ul.download-list > li li:first-of-type{ margin-top: 1rem; } -.bulk-downloads h2{ +ul.download-list p.header{ + font-size: 1.4rem; + font-family: "League Spartan", Arial, sans-serif; + margin-top: 4rem; + line-height: 1.2; + letter-spacing: 1px; + text-transform: uppercase; + color: var(--header); margin-top: 0; + margin-bottom: 1rem; + text-align: center; } .ebooks nav li.highlighted a:focus, diff --git a/www/patrons-circle/downloads/index.php b/www/patrons-circle/downloads/index.php index f909ef19..1fbb8433 100644 --- a/www/patrons-circle/downloads/index.php +++ b/www/patrons-circle/downloads/index.php @@ -18,7 +18,13 @@ foreach($files as $file){ $updated = new DateTime('@' . filemtime($file)); $obj->Month = $date->format('F'); $obj->Url = '/patrons-circle/downloads/' . basename($file); + $obj->Size = Formatter::ToFileSize(filesize($file)); $obj->Updated = $updated->format('M i'); + // The count of ebooks in each file is stored as a filesystem attribute + $obj->Count = exec('attr -g ebook-count ' . escapeshellarg($file)) ?: null; + if($obj->Count !== null){ + $obj->Count = intval($obj->Count); + } if($updated->format('Y') != gmdate('Y')){ $obj->Updated = $obj->Updated . $updated->format(', Y'); @@ -33,10 +39,10 @@ foreach($files as $file){ $years[$year][] = $obj; } -?> 'Bulk Ebook Download', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?> +?> 'Bulk Ebook Downloads', 'highlight' => '', 'description' => 'Download zip files containing all of the Standard Ebooks released in a given month.']) ?>
-

Bulk Ebook Download

+

Bulk Ebook Downloads

@@ -55,14 +61,25 @@ foreach($files as $file){
    $items){ ?>
  • -
    -

    -
      +

      + + + + + + + + -
    • Month) ?> (Updated Updated) ?>)

    • + + + + + + - - + +
      EbooksSizeUpdated
      Month) ?>Count)) ?>Size) ?>Updated) ?>