Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #260 from ramsnerm/master
Browse files Browse the repository at this point in the history
Add human readable filesize to book download button
  • Loading branch information
rvolz authored Nov 8, 2017
2 parents f435a8d + fd48205 commit a609209
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,11 +1123,25 @@ function titlesSlice($index = 0)
'sort' => $sort));
}

# Creates a human readable filesize string
function human_filesize($bytes, $decimals = 2) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

# Show a single title > /titles/:id. The ID ist the Calibre ID
function title($id)
{
global $app, $globalSettings;

// Add filter for huma nreadable filesize
$filter = new Twig_SimpleFilter('hfsize', function ($string) {
return human_filesize($string);
});
$tenv = $app->view->getEnvironment();
$tenv->addFilter($filter);

// parameter checking
if (!is_numeric($id)) {
$app->getLog()->warn('title: invalid title id ' . $id);
Expand Down
2 changes: 1 addition & 1 deletion templates/title_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h2>{{page.glob.l10n.download}}</h2>
{% if page.glob.glob_dl_toggle == true %} disabled="true" {% endif %}
data-ajax="false"
href="{{page.rot}}/titles/{{book.id}}/file/{{format.name|url_encode()}}.{{format.format|lower}}">
{{ format.format }}
{{ format.format }} | {{format.uncompressed_size|hfsize}}
</a>
{% endfor %}
{% if kindle_format %}
Expand Down

0 comments on commit a609209

Please sign in to comment.