Products.cmfplone: File size unit of measurement

Created on 9 Jul 2020  路  6Comments  路  Source: plone/Products.CMFPlone

If you check the human_readable_size helper method code:
https://github.com/plone/Products.CMFPlone/blob/94f95a29173807c71bafde5bd8f2b9783d36e2e6/Products/CMFPlone/utils.py#L851-L876
you will see that it uses the units GB, MB, KB.
Given that the ratio between two consecutive units is 1024 we might want to switch to more appropriate units GiB, MiB, KiB.

What version of Plone I am using:

Plone 5.2.2

question

Most helpful comment

Note that I think also windows uses the incorrect sizes (not sure about mac), see https://devblogs.microsoft.com/oldnewthing/20090611-00/?p=17933 (old but I think it is still valid).
And also https://xkcd.com/394/:
https://imgs.xkcd.com/comics/kilobyte.png

All 6 comments

Yes, see https://en.wikipedia.org/wiki/Gibibyte

And if you modify this, please internationalize the unit so we can translate it differently.
In French this is Gio, Mio, Kio

defining
unit = _("KiB")
and doing translate(unit, context=request) before doing the concatenation. You will need the request, if you don't want to change the api too much, you can get it with:

from zope.globarequest import getRequest
request = getRequest()

If someone create a PR for this, please add me as reviewer so I will verify the code. :)

mockup is using dropzone filesize function to show "0.8 MB" or "0.9 GB" and it's using a power of 10, so on js side this is correct.

See mockup/node_modules/dropzone/dist/dropzone.js

            _ref1 = file.previewElement.querySelectorAll("[data-dz-size]");           
            for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {                
              node = _ref1[_j];                                                   
              node.innerHTML = this.filesize(file.size);                          
            }

and

      Dropzone.prototype.filesize = function(size) {                              
        var cutoff, i, selectedSize, selectedUnit, unit, units, _i, _len;         
        selectedSize = 0;                                                         
        selectedUnit = "b";                                                       
        if (size > 0) {                                                           
          units = ['TB', 'GB', 'MB', 'KB', 'b'];                                  
          for (i = _i = 0, _len = units.length; _i < _len; i = ++_i) {            
            unit = units[i];                                                      
            cutoff = Math.pow(this.options.filesizeBase, 4 - i) / 10;             
            if (size >= cutoff) {                                                 
              selectedSize = size / Math.pow(this.options.filesizeBase, 4 - i);   
              selectedUnit = unit;                                                
              break;                                                              
            }                                                                     
          }                                                                       
          selectedSize = Math.round(10 * selectedSize) / 10;                      
        }                                                                         
        return "<strong>" + selectedSize + "</strong> " + selectedUnit;           
      };

Note that I think also windows uses the incorrect sizes (not sure about mac), see https://devblogs.microsoft.com/oldnewthing/20090611-00/?p=17933 (old but I think it is still valid).
And also https://xkcd.com/394/:
https://imgs.xkcd.com/comics/kilobyte.png

I think we should just use the 1000-based KB and change the code according to that.

The kibibyte re-standardization was an unfortunate failure IMO. If it wasn't such a tongue-twister it might have had better chances for more widespread use.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davisagli picture davisagli  路  4Comments

mauritsvanrees picture mauritsvanrees  路  5Comments

zopyx picture zopyx  路  4Comments

mauritsvanrees picture mauritsvanrees  路  4Comments

zopyx picture zopyx  路  5Comments