plone / Products.CMFPlone

The core of the Plone content management system
https://plone.org
GNU General Public License v2.0
250 stars 188 forks source link

File size unit of measurement #3147

Open ale-rt opened 4 years ago

ale-rt commented 4 years ago

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

vincentfretin commented 4 years ago

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

vincentfretin commented 4 years ago

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()
vincentfretin commented 4 years ago

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

vincentfretin commented 4 years ago

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;           
      };
ale-rt commented 4 years ago

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

thet commented 4 years ago

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.