From 0e3ac727210c00c7a654895943f421bd949ad78f Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 12 Oct 2018 02:21:19 +0700 Subject: [PATCH] Fixed album pages Previously album pages would still use utils.getPrettyBytes(), but one of the previous commits got rid of it in favor of client-side solution. I forgot to update album pages to also use client-side solution, but this commit fixes it. On a side note, I also switched all instances of consts to var in the function. --- public/js/album.js | 28 +++++++++++++++++++++++++++- public/js/dashboard.js | 8 ++++---- routes/album.js | 1 - views/_globals.njk | 2 +- views/album.njk | 2 +- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/public/js/album.js b/public/js/album.js index 7db04b0..230bf27 100644 --- a/public/js/album.js +++ b/public/js/album.js @@ -1,7 +1,33 @@ /* global LazyLoad */ -var page = {} +var page = { + lazyLoad: null, + + // byte units for getPrettyBytes() + byteUnits: ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] +} + +page.getPrettyBytes = num => { + // MIT License + // Copyright (c) Sindre Sorhus (sindresorhus.com) + if (!Number.isFinite(num)) { return num } + + var neg = num < 0 + if (neg) { num = -num } + if (num < 1) { return (neg ? '-' : '') + num + ' B' } + + var exponent = Math.min(Math.floor(Math.log10(num) / 3), page.byteUnits.length - 1) + var numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)) + var unit = page.byteUnits[exponent] + + return (neg ? '-' : '') + numStr + ' ' + unit +} window.onload = function () { + var elements = document.getElementsByClassName('file-size') + for (var i = 0; i < elements.length; i++) { + elements[i].innerHTML = page.getPrettyBytes(parseInt(elements[i].innerHTML)) + } + page.lazyLoad = new LazyLoad() } diff --git a/public/js/dashboard.js b/public/js/dashboard.js index 0e64ea0..2d778ac 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -1418,13 +1418,13 @@ page.getPrettyBytes = num => { // Copyright (c) Sindre Sorhus (sindresorhus.com) if (!Number.isFinite(num)) { return num } - const neg = num < 0 + var neg = num < 0 if (neg) { num = -num } if (num < 1) { return (neg ? '-' : '') + num + ' B' } - const exponent = Math.min(Math.floor(Math.log10(num) / 3), page.byteUnits.length - 1) - const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)) - const unit = page.byteUnits[exponent] + var exponent = Math.min(Math.floor(Math.log10(num) / 3), page.byteUnits.length - 1) + var numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)) + var unit = page.byteUnits[exponent] return (neg ? '-' : '') + numStr + ' ' + unit } diff --git a/routes/album.js b/routes/album.js index a711a19..77c8e1a 100644 --- a/routes/album.js +++ b/routes/album.js @@ -41,7 +41,6 @@ routes.get('/a/:identifier', async (req, res, next) => { for (const file of files) { file.file = `${basedomain}/${file.name}` - file.size = utils.getPrettyBytes(parseInt(file.size)) file.extname = path.extname(file.name).toLowerCase() if (utils.mayGenerateThumb(file.extname)) { diff --git a/views/_globals.njk b/views/_globals.njk index e7ba384..150f34d 100644 --- a/views/_globals.njk +++ b/views/_globals.njk @@ -15,7 +15,7 @@ v2: Images and config files (manifest.json, browserconfig.xml, etc). v3: CSS and JS files (libs such as bulma, lazyload, etc). #} -{% set v1 = "Wd5CjV76Yz" %} +{% set v1 = "UW9mVRyFee" %} {% set v2 = "Ii3JYKIhb0" %} {% set v3 = "HrvcYD3KTh" %} diff --git a/views/album.njk b/views/album.njk index 37aae49..7a731c7 100644 --- a/views/album.njk +++ b/views/album.njk @@ -86,7 +86,7 @@

{{ file.name }}

-

{{ file.size }}

+

{{ file.size }}

{% endfor %}