X-Git-Url: http://git.draconx.ca/gitweb/homepage.git/blobdiff_plain/da38a0de6d2814c450ea356412faa03ee54cad68..4f4b3ddd920c95d380a2f25dabd4b68457bb1564:/lib/helpers.rb diff --git a/lib/helpers.rb b/lib/helpers.rb index d2f3a7c..7425075 100644 --- a/lib/helpers.rb +++ b/lib/helpers.rb @@ -63,3 +63,17 @@ def item_longdesc(item) p = xml.xpath('//xhtml:p', Xmlns) if p.empty? then nil else p[0].xpath('string(.)') end end + +def human_filesize(size) + units = ["B", "KiB", "MiB", "GiB"] + + for unit in units + if (size < 1024) + break + end + + size /= 1024.0 + end + + sprintf("%.1f %s", size + 0.05, unit) +end