BobbyWibowo/lolisafe

[FEATURE REQUEST] MiB as default unit

Closed this issue · 7 comments

Hello,

I would like to know if it is possible to set the default size unit as MiB (mebibyte) instead of MB (megabyte) as it is what computers really use.

Having MB as default could lead Windows users (thanks, Microsoft) to think that they can upload eg. 512 "MB" (it's MiB in reality) when they can only upload 488 "MB" (MiB). Uploading a 500 "MB" file as shown from Windows Explorer would fail as it is bigger than 488 MiB.

Could I set config.js maxSize field to 512MiB? Or make the binary notation the default one for new users?

There's no easy way to switch the default unit to MiB as of yet.
Using the unit directly in the config file, for any options that require MB, won't work as you'd expect cause some of the internal math expect MB and thus multiplies by 1000 instead of 1024.
Example:

const urlMaxSizeBytes = parseInt(config.uploads.urlMaxSize) * 1e6

MiB being the unit generally used for display in PCs is a fair point, but I've come to notice that most web things generally uses MB instead of MiB, which I think perhaps due to it being the actual SI unit (Cloudflare for example).


You can perhaps play around with the behavior by editing the configs in src/js/home.js and src/js/misc/utils.js, but you'll have to rebuild production assets yourself (install dev dependencies), and pulling updates from this repo may become somewhat annoying due to serious merging issues.

Anyway, this will be a rather low priority thing in my todo list unfortunately.

Thanks for the help! I guess I can live with MB on config files and in the inner functioning of the script as I can always convert MiB to MB.

The last paragraph you wrote was about changing the default unit shown in the home, right? I think that would be a sufficient "patch" to meet my concerns.

The last paragraph you wrote was about changing the default unit shown in the home, right? I think that would be a sufficient "patch" to meet my concerns.

Yea. Primarily src/js/misc/utils.js where the logic to decide whether to use SI unit or not is.

Thanks! I will check it soon following the instructions you provided.
Feel free to close this issue if you feel like doing so. I will post my results if you decide to leave it open for a while.

So far I've discovered that selecting the MiB display unit as your preferred one, creates an entry called "siBytes" with 0 as value in the local storage of the browser for lolisafe.

If the value is set to anything else than 0, MB is shown. Otherwise MiB is used. A quick and (dirty?) approach for having MiB as the default value would be to create this entry automatically if the user has none when he/she visits the page. (Of course this would work if choosing MB created an entry when selected. Right now it deletes the siBytes entry)

Cleanest method would be to just reverse the logic in misc/utils.js (line 65):

const si = localStorage[lsKeys.siBytes] !== '0'
const neg = num < 0 ? '-' : ''
const scale = si ? 1000 : 1024

and option texts in home.js (line 602-603):

lolisafe/src/js/home.js

Lines 598 to 604 in e10ce78

const config = {
siBytes: {
label: 'File size display',
select: [
{ value: 'default', text: '1000 B = 1 kB = 1 Kilobyte' },
{ value: '0', text: '1024 B = 1 KiB = 1 Kibibyte' }
],

It'll then only use MB when siBytes is set to 0.

It'll look a bit weird when someone reads the code directly (cause how come when siBytes is false/0, it uses "SI bytes" instead, and all that stuff), but that would behave the closest to actually having MiB as default.


(Of course this would work if choosing MB created an entry when selected. Right now it deletes the siBytes entry)

Yea, I realized early on that this would be a barrier to the solution you thought of.
I sorta preferred deleting the entries when users choose to use default values, to sorta leave as few footprints as possible, and all that kinda stuff. I guess it was a needless concern? I dunno tbh, lmao

Thank you for your help! I finally managed to make MiB the default unit following your tips. These are the changes I've made:

misc/utils.js (Original):

const si = localStorage[lsKeys.siBytes] !== '0'
const neg = num < 0 ? '-' : ''
const scale = si ? 1000 : 1024
if (neg) num = -num
if (num < scale) return `${neg}${num} B`
const exponent = Math.min(Math.floor((Math.log(num) * Math.LOG10E) / 3), 8) // 8 is count of KMGTPEZY
const numStr = Number((num / Math.pow(scale, exponent)).toPrecision(3))
const pre = (si ? 'kMGTPEZY' : 'KMGTPEZY').charAt(exponent - 1) + (si ? '' : 'i')

misc/utils.js (Modified lines 65 and 71):

const si = localStorage[lsKeys.siBytes] !== '0' 
const neg = num < 0 ? '-' : '' 
const scale = si ? 1024 : 1000
if (neg) num = -num
if (num < scale) return `${neg}${num} B`

const exponent = Math.min(Math.floor((Math.log(num) * Math.LOG10E) / 3), 8) // 8 is count of KMGTPEZY
const numStr = Number((num / Math.pow(scale, exponent)).toPrecision(3))
const pre = (si ? 'kMGTPEZY' : 'KMGTPEZY').charAt(exponent - 1) + (si ? 'i' : '')

home.js (Original):

lolisafe/src/js/home.js

Lines 598 to 604 in e10ce78

const config = {
siBytes: {
label: 'File size display',
select: [
{ value: 'default', text: '1000 B = 1 kB = 1 Kilobyte' },
{ value: '0', text: '1024 B = 1 KiB = 1 Kibibyte' }
],

home.js (Modified lines 602 and 603):

const config = { 
   siBytes: { 
     label: 'File size display', 
     select: [ 
       { value: 'default', text: '1024 B = 1 KiB = 1 Kibibyte' }, 
       { value: '0', text: '1000 B = 1 kB = 1 Kilobyte' }
     ], 

And now 512MiB (my max upload size) is shown instead of 537MB when entering the website for the first time 😄