Skip to content

Commit

Permalink
Properly handle files with 0 filesize
Browse files Browse the repository at this point in the history
Fixes #1079
  • Loading branch information
enyo committed Oct 11, 2015
1 parent a12f5ec commit 184aa89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/dropzone.coffee
Expand Up @@ -814,18 +814,21 @@ class Dropzone extends Emitter

# Returns a nicely formatted filesize
filesize: (size) ->
units = [ 'TB', 'GB', 'MB', 'KB', 'b' ]
selectedSize = selectedUnit = null
selectedSize = 0
selectedUnit = "b"

for unit, i in units
cutoff = Math.pow(@options.filesizeBase, 4 - i) / 10
if size > 0
units = [ 'TB', 'GB', 'MB', 'KB', 'b' ]

if size >= cutoff
selectedSize = size / Math.pow(@options.filesizeBase, 4 - i)
selectedUnit = unit
break
for unit, i in units
cutoff = Math.pow(@options.filesizeBase, 4 - i) / 10

if size >= cutoff
selectedSize = size / Math.pow(@options.filesizeBase, 4 - i)
selectedUnit = unit
break

selectedSize = Math.round(10 * selectedSize) / 10 # Cutting of digits
selectedSize = Math.round(10 * selectedSize) / 10 # Cutting of digits

"<strong>#{selectedSize}</strong> #{selectedUnit}"

Expand Down
3 changes: 3 additions & 0 deletions test/test.coffee
Expand Up @@ -871,6 +871,9 @@ describe "Dropzone", ->

describe ".filesize()", ->

it "should handle files with 0 size properly", ->
dropzone.filesize(0).should.eql "<strong>0</strong> b"

it "should convert to KiloBytes, etc..", ->

dropzone.options.filesizeBase.should.eql 1000 # Just making sure the default config is correct
Expand Down

0 comments on commit 184aa89

Please sign in to comment.