BobbyWibowo / lolisafe

Blazing fast file uploader and awesome bunker written in node! 🚀
MIT License
317 stars 56 forks source link

[FEATURE REQUEST] MiB as default unit #75

Closed rubenmdh closed 2 years ago

rubenmdh commented 4 years ago

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?

BobbyWibowo commented 4 years ago

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: https://github.com/BobbyWibowo/lolisafe/blob/36763c2a770122687ce784df88e5a643696340b4/controllers/uploadController.js#L23

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.

rubenmdh commented 4 years ago

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.

BobbyWibowo commented 4 years ago

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.

rubenmdh commented 4 years ago

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.

rubenmdh commented 4 years ago

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)

BobbyWibowo commented 4 years ago

Cleanest method would be to just reverse the logic in misc/utils.js (line 65): https://github.com/BobbyWibowo/lolisafe/blob/e10ce7807feaf96eaff47c8a3785e0937e2fbca0/src/js/misc/utils.js#L63-L65 and option texts in home.js (line 602-603): https://github.com/BobbyWibowo/lolisafe/blob/e10ce7807feaf96eaff47c8a3785e0937e2fbca0/src/js/home.js#L598-L604 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

rubenmdh commented 4 years ago

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):

https://github.com/BobbyWibowo/lolisafe/blob/e10ce7807feaf96eaff47c8a3785e0937e2fbca0/src/js/misc/utils.js#L63-L71

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): https://github.com/BobbyWibowo/lolisafe/blob/e10ce7807feaf96eaff47c8a3785e0937e2fbca0/src/js/home.js#L598-L604

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 😄